/[drupal]/contributions/modules/liquid/liquid.module
ViewVC logotype

Contents of /contributions/modules/liquid/liquid.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Thu Feb 8 23:14:33 2007 UTC (2 years, 9 months ago) by sorenp
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +4 -0 lines
File MIME type: text/x-php
Problems with the arg function not defined in hook_init when cachin is enabled has been fixed
1 <?php
2
3 //
4 // Copyright (c) 2006 by Sören Petersen
5 //
6 // $Id$
7 //
8
9 require_once('wikiid.inc');
10
11 // Permissions
12 define('PERM_CREATE_WIKI_CONTENT', 'create wiki content');
13 define('PERM_MANAGE_WIKI', 'manage wiki');
14
15 // Settings
16 define('PREF_LIQUID_USE_PATH_ALIAS', 'liquid_use_path_alias');
17 define('PREF_LIQUID_SET_BREADCRUMB', 'liquid_set_breadcrumb');
18 define('PREF_LIQUID_DEFAULT_TYPE', 'liquid_default_type');
19 define('PREF_LIQUID_DEFAULT_PAGE', 'liquid_default_page');
20 define('PREF_LIQUID_DEFAULT_EMPTY_PAGE', 'liquid_default_empty_page');
21 define('PREF_LIQUID_BIND_TITLE', '_bind_title');
22
23 // Default settings
24 define('PREF_LIQUID_USE_PATH_ALIAS_DEFAULT', true);
25 define('PREF_LIQUID_SET_BREADCRUMB_DEFAULT', true);
26 define('PREF_LIQUID_DEFAULT_TYPE_DEFAULT', 'page');
27 define('PREF_LIQUID_DEFAULT_PAGE_DEFAULT', 'Main_Page');
28 define('PREF_LIQUID_DEFAULT_EMPTY_PAGE_DEFAULT', '(There is currently no text in this page)');
29 define('PREF_LIQUID_BIND_TITLE_DEFAULT', false);
30
31
32 /**
33 * Implementation of hook_help.
34 */
35 function liquid_help($section ='') {
36 $output = '';
37
38 switch($section) {
39 case 'admin/modules#description':
40 $output = t('The base module of the Liquid Wiki Engine.');
41 break;
42 case 'admin/help#liquid':
43 $output = t('The base module of the Liquid Wiki Engine.');
44 break;
45 }
46
47 return $output;
48 }
49
50
51 /*
52 * Implementation of hook_perm.
53 */
54 function liquid_perm() {
55 return array(PERM_CREATE_WIKI_CONTENT, PERM_MANAGE_WIKI);
56 }
57
58 /*
59 * Implementation of hook_init.
60 */
61 function liquid_init() {
62 // avoid problems with caching
63 if (!function_exists('arg'))
64 return;
65
66 // set breadcrumb, if appropirate
67 if (variable_get(PREF_LIQUID_SET_BREADCRUMB, PREF_LIQUID_SET_BREADCRUMB_DEFAULT))
68 if (arg(0) == 'node' && (is_numeric(arg(1)) || is_numeric(arg(2)))) {
69 $nid = is_numeric(arg(1))?arg(1):arg(2);
70 if ($wid = liquid_lookup_wid($nid))
71 liquid_set_breadcrumb($wid);
72 }
73 }
74
75 /*
76 * Set breadcrumbs for wiki content
77 */
78 function liquid_set_breadcrumb($wid) {
79 $crumb = array();
80
81 while (!$wid->isNull()) {
82 $crumb[] = l($wid->displayName(), 'wiki/' . $wid->toURLString());
83 $wid = $wid->parent();
84 }
85
86 $crumb[] = l('Wiki', 'wiki');
87 $crumb[] = l('Home', '');
88
89 drupal_set_breadcrumb(array_reverse($crumb));
90 }
91
92 /*
93 * Implementation of hook_menu.
94 */
95 function liquid_menu($may_cache) {
96 $items = array();
97
98 if ($may_cache) {
99 // the default wiki page and empty page
100 $items[] = array('path' => 'wiki',
101 'title' => 'Wiki',
102 'callback' => 'liquid_page',
103 'access' => true,
104 'type' => MENU_NORMAL_ITEM);
105
106 // settings page
107 $items[] = array('path' => 'admin/settings/liquid',
108 'title' => t('Liquid'),
109 'description' => 'Liquid Settings',
110 'callback' => 'drupal_get_form',
111 'callback arguments' => array('liquid_admin_settings'),
112 'access' => user_access('administer site configuration'),
113 'type' => MENU_NORMAL_ITEM
114 );
115
116 } else {
117 if (arg(0) == 'node' && is_numeric(arg(1))) {
118 $nid = arg(1);
119 $node = node_load($nid);
120
121 // "wiki" tab in node display
122 $items[] = array('path' => 'node/'.arg(1).'/wiki/manage',
123 'title' => t('Wiki Settings'),
124 'callback' => 'drupal_get_form',
125 'callback arguments' => array('liquid_manage'),
126 'access' => liquid_access('wiki_manage', $node),
127 'type' => MENU_LOCAL_TASK,
128 'weight' => 100);
129
130 // are we viewing a node in the wiki?
131 if (liquid_in_wiki(arg(1))) {
132 // "move" tab in node display
133 $items[] = array('path' => 'node/'.arg(1).'/wiki/move',
134 'title' => t('Move'),
135 'callback' => 'liquid_move_page',
136 'access' => liquid_access('wiki_move', $node),
137 'type' => MENU_LOCAL_TASK,
138 'weight' => 40);
139
140 // "delete" tab in node display
141 $items[] = array('path' => 'node/'.arg(1).'/wiki/delete',
142 'title' => t('Delete'),
143 'callback' => 'drupal_goto',
144 'callback arguments' => array("node/$nid/delete"),
145 'access' => node_access('delete', $node),
146 'type' => MENU_LOCAL_TASK,
147 'weight' => 50);
148 }
149 }
150
151 // tabs on wiki page
152 if (arg(0) == 'wiki') {
153 $wid = liquid_requested_wid();
154 $handle = liquid_page_handle($wid);
155
156 if ($handle && function_exists($handle['menu_callback']))
157 $items = array_merge($items, call_user_func_array($handle['menu_callback'], $handle['params']));
158 }
159 }
160
161 return $items;
162 }
163
164 /*
165 * deduces the requested wiki id from the url
166 */
167 function liquid_requested_wid_string() {
168 if (arg(0) != 'wiki')
169 return null;
170
171 return substr($_GET['q'], 5);
172 }
173
174 function liquid_requested_wid() {
175 return new WikiId(liquid_requested_wid_string());
176 }
177
178 /*
179 * Request handler for /wiki
180 */
181 function liquid_page() {
182 // obtain wiki id
183 $wid = liquid_requested_wid();
184
185 // No wiki id specified?
186 if ($wid->isNull())
187 $wid = new WikiId(variable_get(PREF_LIQUID_DEFAULT_PAGE, PREF_LIQUID_DEFAULT_PAGE_DEFAULT));
188
189 // redirect to correct wiki id if nessecary
190 if (liquid_requested_wid_string() != $wid->toURLString())
191 drupal_goto("wiki/".$wid->toURLString());
192
193 // obtain page handle
194 $handle = liquid_page_handle($wid);
195
196 // no page handle?
197 if (!$handle || !function_exists($handle['callback'])) {
198 drupal_set_title(t('Invalid Wiki ID'));
199 if (variable_get(PREF_LIQUID_SET_BREADCRUMB, PREF_LIQUID_SET_BREADCRUMB_DEFAULT))
200 liquid_set_breadcrumb('');
201 return t("You have specified an invalid wiki ID");
202 }
203
204 $output = call_user_func_array($handle['callback'], $handle['params']);
205
206 return $output;
207 }
208
209 /*
210 * Implementation of hook_wikipage
211 */
212 function liquid_wikihook($wid) {
213 if (substr($wid->name(), 0, 8) == 'Special:') {
214 $op = substr($wid->name(), 8);
215 $wid = $wid->parent();
216 } else {
217 $op = 'view';
218 }
219
220 // We are not handling any other special pages
221 $data = $wid->data;
222 foreach ($data as $name) {
223 if (substr($name, 0, 8) == 'Special:')
224 return null;
225 }
226
227 switch ($op) {
228 case 'edit':
229 $res = array('default' => array('weight' => 100,
230 'callback' => 'liquid_wikihook_edit',
231 'params' => array($wid),
232 )
233 );
234 break;
235 case 'view':
236 default:
237 if (liquid_wid_exists($wid)) {
238 $res = array('default' => array('weight' => 100,
239 'callback' => 'liquid_wikihook_view',
240 'params' => array($wid),
241 )
242 );
243 } else {
244 $res = array('default' => array('weight' => 0,
245 'callback' => 'liquid_wikihook_empty',
246 'params' => array($wid),
247 'menu_callback' => 'liquid_emptymenu',
248 )
249 );
250 }
251 break;
252 }
253
254 return $res;
255 }
256
257 function liquid_wikihook_empty($wid) {
258 drupal_set_title($wid->toTitleString());
259
260 if (variable_get(PREF_LIQUID_SET_BREADCRUMB, PREF_LIQUID_SET_BREADCRUMB_DEFAULT))
261 liquid_set_breadcrumb($wid);
262
263 return variable_get(PREF_LIQUID_DEFAULT_EMPTY_PAGE, PREF_LIQUID_DEFAULT_EMPTY_PAGE_DEFAULT);
264 }
265
266 function liquid_wikihook_view($wid) {
267 $nid = liquid_lookup_nid($wid);
268 drupal_goto("node/$nid");
269 }
270
271 function liquid_wikihook_edit($wid) {
272 $nid = liquid_lookup_nid($wid);
273 if ($nid) {
274 drupal_goto("node/$nid/edit");
275 } else {
276 drupal_goto("node/add/".variable_get(PREF_LIQUID_DEFAULT_TYPE, PREF_LIQUID_DEFAULT_TYPE_DEFAULT),
277 "wid=".$wid->toURLString());
278 }
279 }
280
281 function liquid_emptymenu($wid) {
282 $items = array();
283 $items[] = array('path' => 'wiki/'.$wid->toURLString(),
284 'title' => t('view'),
285 'callback' => 'liquid_page',
286 'access' => true,
287 'type' => MENU_CALLBACK);
288
289 $items[] = array('path' => 'wiki/'.$wid->toURLString().'/Special:view',
290 'title' => t('view'),
291 'callback' => 'liquid_page',
292 'access' => true,
293 'type' => MENU_DEFAULT_LOCAL_TASK,
294 'weight' => -10);
295
296 $mayCreate = node_access('create', variable_get(PREF_LIQUID_DEFAULT_TYPE, PREF_LIQUID_DEFAULT_TYPE_DEFAULT))
297 && (user_access(PERM_CREATE_WIKI_CONTENT) || user_access(PERM_MANAGE_WIKI));
298
299 $items[] = array('path' => 'wiki/'.$wid->toURLString().'/Special:edit',
300 'title' => t('edit'),
301 'callback' => 'liquid_page',
302 'access' => $mayCreate,
303 'type' => MENU_LOCAL_TASK);
304 return $items;
305 }
306
307
308 function liquid_page_handle($wid) {
309 static $cache = array();
310 if (isset($cache[$wid->identifier()]))
311 return $cache[$wid->identifier()];
312
313 // don't search if wid is invalid
314 if (!$wid->isValid())
315 return $cache[$wid] = null;
316
317 $handles = module_invoke_all('wikihook', $wid);
318
319 $highest = -1;
320 $match = null;
321
322 foreach ($handles as $handle) {
323 if ($handle['weight'] > $highest) {
324 $highest = $handle['weight'];
325 $match = $handle;
326 }
327 }
328
329 $cache[$wid->identifier()] = $match;
330
331 return $match;
332 }
333
334 /*
335 * Admin Settings Form
336 */
337 function liquid_admin_settings() {
338 $form['General'] = array('#type' => 'fieldset', '#title' => 'General Settings');
339 $form['General'][PREF_LIQUID_USE_PATH_ALIAS] = array('#type' => 'checkbox',
340 '#title' => t('Use path aliasing for wiki pages'),
341 '#default_value' => variable_get(PREF_LIQUID_USE_PATH_ALIAS, PREF_LIQUID_USE_PATH_ALIAS_DEFAULT),
342 '#description' => t("Check this box to enable path aliasing of wiki pages. You must also enable the path module for this to work."));
343
344 $form['General'][PREF_LIQUID_SET_BREADCRUMB] = array('#type' => 'checkbox',
345 '#title' => t('Set breadcrumbs'),
346 '#default_value' => variable_get(PREF_LIQUID_SET_BREADCRUMB, PREF_LIQUID_SET_BREADCRUMB_DEFAULT),
347 '#description' => t("Check this box to make liquid set breadcrumbs for".
348 " your wiki pages"));
349
350 $form['General'][PREF_LIQUID_DEFAULT_PAGE] = array('#type' => 'textfield',
351 '#title' => t('Default page'),
352 '#description' => t('The wiki ID of the page that is shown when navigating to /wiki.'),
353 '#default_value' => variable_get(PREF_LIQUID_DEFAULT_PAGE, PREF_LIQUID_DEFAULT_PAGE_DEFAULT),
354 '#size' => 64,
355 '#maxlength' => 128);
356
357 $form['General'][PREF_LIQUID_DEFAULT_EMPTY_PAGE] = array('#type' => 'textarea',
358 '#title' => t('Default empty page'),
359 '#default_value' => variable_get(PREF_LIQUID_DEFAULT_EMPTY_PAGE, PREF_LIQUID_DEFAULT_EMPTY_PAGE_DEFAULT),
360 '#description' => t('This is the page that will be shown when a nonexisting wiki node is requested.')
361 );
362
363 $form['ContentType'] = array('#type' => 'fieldset', '#title' => 'ContentType Settings');
364
365 $form['ContentType'][PREF_LIQUID_DEFAULT_TYPE] = array('#type' => 'select', '#default_value' => variable_get(PREF_LIQUID_DEFAULT_TYPE, PREF_LIQUID_DEFAULT_TYPE_DEFAULT),
366 '#title' => t('Default content type'),
367 '#description' => t('This is the content type that will be created when'.
368 'somebody tries to change a nonexisting wiki page.'),
369 '#options' => node_get_types('names')
370 );
371
372 foreach (node_get_types('names') as $type => $name) {
373 $form['ContentType'][$type . PREF_LIQUID_BIND_TITLE] = array('#type' => 'checkbox',
374 '#title' => t("Force \"$name\"-node titles to be the same as their Wiki ID."),
375 '#description' => t('Bind the title of nodes in the wiki to the wiki ID'),
376 '#default_value' => variable_get($type . PREF_LIQUID_BIND_TITLE, PREF_LIQUID_BIND_TITLE_DEFAULT));
377
378 }
379
380
381 return system_settings_form($form);
382 }
383
384 function liquid_manage() {
385 $nid = arg(1);
386 $node = node_load($nid);
387
388
389 // check if the node has been inserted into the wiki
390 if (!liquid_in_wiki($nid)) {
391 $form['#showWikiOptions'] = false;
392
393 $status = t('The node is not inserted into the wiki.');
394 $instructions = t('Use the form below to insert the node.');
395
396
397 $form['insert'] = array('#type' => 'fieldset',
398 '#title' => t("Insert into Wiki"),
399 '#collapsible' => true,
400 '#collapsed' => false,
401 '#weight' => 10,
402 '#prefix' => "<p>$status</p><p>$instructions</p>");
403 $form['insert']['wikiID'] = array('#type' => 'textfield',
404 '#title' => t("Wiki ID"),
405 '#description' => t("The wiki ID is the identifier for a node in the wiki. The title "
406 ."of the node may be updated if the 'tie to title' setting has "
407 ."been enabled for this content type."),
408 '#default_value' => $node->title,
409 '#size' => 64,
410 '#maxlength' => 128,
411 '#weight' => 10);
412
413 $form['insert']['insert'] = array('#type' => 'submit',
414 '#value' => t("Insert"),
415 '#weight' => 20);
416
417 } else {
418
419 $wid = liquid_lookup_wid($nid);
420
421 $form['#showWikiOptions'] = true;
422
423 $status = t('The node is inserted in the wiki with wiki ID: <i>%wid</i>.', array('%wid'=>$wid->identifier()));
424 $instructions =t('Use the form below to manage wiki settings for the node or to remove it from the wiki.');
425
426 $form['nid'] = array('#type' => 'value', '#value' => $nid,
427 );
428
429 $form['update'] = array('#type' => 'submit',
430 '#value' => t("Update"),
431 '#weight' => 100,
432 '#prefix' => "<p>$status</p><p>$instructions</p>");
433
434 $form['remove'] = array('#type' => 'submit',
435 '#value' => t("Remove"),
436 '#weight' => 101);
437
438
439 }
440 drupal_set_title($node->title);
441 return $form;
442 }
443
444 function liquid_manage_validate($form, $form_values) {
445 $nid = arg(1);
446 $node = node_load($nid);
447 $wid = liquid_lookup_wid($nid);
448
449 switch ($form_values['op']) {
450 case t("Insert"):
451 $wid = new WikiId($form_values['wikiID']);
452
453 if (!$wid->isValid())
454 form_set_error('wikiID', t("The wiki ID '%wikiID' is invalid.", array('%wikiID'=>$wikiID)));
455
456 else if (liquid_wid_exists($wid))
457 form_set_error('wikiID', t("Wiki ID '%wikiID' allready taken.", array('%wikiID'=>$wikiID)));
458
459 break;
460
461 case t("Update"):
462 module_invoke_all('wikiapi', 'validate', $nid);
463 break;
464 }
465 }
466
467 function liquid_manage_submit($form, $form_values) {
468 $nid = arg(1);
469 $node = node_load($nid);
470 $wid = liquid_lookup_wid($nid);
471
472
473 switch ($form_values['op']) {
474 case t("Insert"):
475 $wid = new WikiId($form_values['wikiID']);
476 if (liquid_insert($wid, $node->nid)) {
477
478 // modify title, if required
479 if (variable_get($node->type . "_bind_title", false)) {
480 $node = node_load($nid, NULL, true);
481 $node->title = $wid->toTitleString();
482 node_save($node);
483 }
484
485 drupal_set_message(t("The node has been inserted into the wiki."));
486 unset($_POST['op']);
487 drupal_goto("node/$nid/wiki/manage");
488 }
489 break;
490 case t("Remove"):
491 if (liquid_remove(liquid_lookup_wid($node->nid))) {
492 drupal_set_message(t("The node has been removed from the wiki."));
493 return "node/$nid";
494 }
495 break;
496 case t("Update"):
497 module_invoke_all('wikiapi', 'update', $nid);
498
499 // update access grants
500 $node = node_load($nid);
501 node_access_acquire_grants($node);
502 liquid_access_acquire_grants($node);
503 break;
504
505 }
506 }
507
508 function liquid_move_page() {
509 $nid = arg(1);
510 $node = node_load($nid);
511 $error_message = '';
512
513 if (isset($_POST['op']) && $_POST['op'] == t("Move")) {
514 $edit = $_POST;
515 $new_wid = new WikiId($edit['new_wid']);
516 if (!$new_wid->isValid()) {
517 $error_message = t("The wiki ID '%wikiID' is invalid.", array('%wikiID'=>$new_wid->identifier()));
518 } else if (liquid_in_wiki($new_wid)) {
519 $error_message = t("Wiki ID '%wikiID' allready taken.", array('%wikiID'=>$new_wid->identifier()));
520 } else if (liquid_move(liquid_lookup_wid($node->nid), $new_wid)) {
521
522 // update title, if nessecary
523 if (variable_get($node->type . "_bind_title", false)) {
524 $node = node_load($nid, NULL, true);
525 $node->title = $new_wid->toTitleString();
526 node_save($node);
527 }
528
529 drupal_set_message(t("The node has been moved to %newname.", array('%newname' => $new_wid->identifier())));
530 drupal_goto("node/$nid");
531 }
532 }
533
534 $form['move'] = array('#type' => 'fieldset',
535 '#title' => t("Move"),
536 '#collapsible' => true,
537 '#collapsed' => false,
538 '#weight' => 10);
539
540 $form['move']['new_wid'] = array('#type' => 'textfield',
541 '#title' => t("Move to"),
542 '#description' => t("The name to move this node to."),
543 '#default_value' => $node->title,
544 '#size' => 64,
545 '#maxlength' => 128,
546 '#weight' => 10);
547
548 $form['move']['move'] = array('#type' => 'submit',
549 '#value' => t("Move"),
550 '#weight' => 20);
551
552 if ($error_message)
553 drupal_set_message($error_message, 'error');
554
555 drupal_set_title($node->title);
556 drupal_prepare_form('liquid_move_page', $form);
557 return drupal_render_form('liquid_move_page', $form);
558 }
559
560
561 function liquid_nodeapi(&$node, $op, $arg) {
562 if (true) {
563 switch ($op) {
564 case 'validate':
565 if ($node->wiki_insert && (user_access(PERM_CREATE_WIKI_CONTENT) || user_access(PERM_MANAGE_WIKI))) {
566 if (variable_get($node->type . PREF_LIQUID_BIND_TITLE, PREF_LIQUID_BIND_TITLE_DEFAULT)) {
567 $idField = 'title';
568 $wid = new WikiId($node->title);
569 } else {
570 $idField = 'wid';
571 $wid = new WikiId($node->wid);
572 }
573
574 if (!$wid->isValid()) {
575 form_set_error($idField, t("The wiki ID '%wikiID' is invalid.", array('%wikiID'=>$wid->identifier())));
576
577 } else if (liquid_lookup_nid($wid)) {
578 form_set_error($idField, t("Wiki ID '%wikiID' allready taken.", array('%wikiID'=>$wid->identifier())));
579 }
580 }
581
582 // Hack to reset title if bound to wiki name
583 $name = liquid_lookup_wid($node->nid);
584 if ($name && variable_get($node->type . "_bind_title", false))
585 $node->title = $name->toTitleString();
586
587 break;
588
589
590 case 'load':
591 break;
592
593 case 'insert':
594 if ($node->wiki_insert && (user_access(PERM_CREATE_WIKI_CONTENT) || user_access(PERM_MANAGE_WIKI))) {
595 if (variable_get($node->type . PREF_LIQUID_BIND_TITLE, PREF_LIQUID_BIND_TITLE_DEFAULT))
596 $wid = new WikiId($node->title);
597 else
598 $wid = new WikiId($node->wid);
599
600 liquid_insert($wid, $node->nid);
601 }
602 break;
603
604 case 'update':
605 break;
606
607
608 case 'delete':
609 // If the node is in the wiki, remove it
610 $wid = liquid_lookup_wid($node->nid);
611 if ($wid)
612 liquid_remove($wid);
613 break;
614
615 }
616 }
617 }
618
619 /*
620 * Implementation of hook_form_alter
621 *
622 * Updates the content type settings form as wel as the create/edit form for noedes.
623 */
624 function liquid_form_alter($form_id, &$form) {
625 if (isset($form['type'])) {
626 //
627 // Node create/edit form
628 //
629 if ($form['type']['#value'] . '_node_form' == $form_id) {
630 $type = $form['type']['#value'];
631 $node = $form['#node'];
632
633 // Are we creating a new node?
634 if (!$form['nid']['#value']) {
635 if (user_access(PERM_CREATE_WIKI_CONTENT) || user_access(PERM_MANAGE_WIKI)) {
636 // Has a wikiId been provided?
637 if (isset($_GET['wid'])) {
638 $wid = new WikiId($_GET['wid']);
639 $form['title']['#value'] = $wid->toTitleString();
640 $form['wiki_insert'] = array('#type'=>'value', '#value'=>'true');
641 $form['wid'] = array('#type'=>'value', '#value'=>$wid->identifier());
642
643 if (variable_get($type . PREF_LIQUID_BIND_TITLE, PREF_LIQUID_BIND_TITLE_DEFAULT))
644 $form['title']['#attributes']['disabled'] = 'true';
645 } else {
646 $form['wiki'] = array('#type' => 'fieldset', '#title' => t('Wiki'),
647 '#collapsible' => true, '#collapsed' => !isset($_POST['edit']['wiki_insert']));
648 $form['wiki']['wiki_insert'] = array('#type'=>'checkbox',
649 '#title'=>t("Insert into wiki"),
650 '#description'=>t("Check this box to insert the node uppon creation"),
651 '#default_value' => false);
652 if (!variable_get($type . PREF_LIQUID_BIND_TITLE, PREF_LIQUID_BIND_TITLE_DEFAULT))
653 $form['wiki']['wid'] = array('#type' => 'textfield',
654 '#title' => t("Wiki ID"));
655 }
656 }
657 } else {
658 // This is an edit form
659 if (variable_get($type . PREF_LIQUID_BIND_TITLE, PREF_LIQUID_BIND_TITLE_DEFAULT) && liquid_in_wiki($node->nid)) {
660 $form['title']['#attributes']['disabled'] = 'true';
661 }
662 }
663 }
664
665 }
666 }
667
668
669 /*
670 * Liquid Access functions
671 *
672 *****************************************************************************
673 * These functions mimics the the workings of the ordinare access system from
674 * the node module. Currently two operations are suported "wiki_manage" and
675 * "wiki_move".
676 */
677 function liquid_access_grants($op, $uid = NULL) {
678 global $user;
679
680 if (isset($uid)) {
681 $user_object = user_load(array('uid' => $uid));
682 }
683 else {
684 $user_object = $user;
685 }
686
687 return array_merge(array('all' => array(0)), module_invoke_all('wiki_grants', $user_object, $op));
688 }
689
690 function liquid_access($op, $node = NULL, $uid = NULL) {
691 $node = (object)$node;
692
693 if (user_access(PERM_MANAGE_WIKI)) {
694 return TRUE;
695 }
696
697 if ($node->nid && $node->status) {
698 $grants = array();
699 // again, it might cause problems to use hook_access_grants, but might be better than implementing
700 // an auxillary solution.
701 foreach (liquid_access_grants($op, $uid) as $realm => $gids) {
702 foreach ($gids as $gid) {
703 $grants[] = "(gid = $gid AND realm = '$realm')";
704 }
705 }
706
707 $grants_sql = '';
708 if (count($grants)) {
709 $grants_sql = 'AND ('. implode(' OR ', $grants) .')';
710 }
711
712 $sql = "SELECT COUNT(*) FROM {wiki_access} WHERE (nid = 0 OR nid = %d) $grants_sql AND grant_$op >= 1";
713 $result = db_query($sql, $node->nid);
714 return (db_result($result));
715 }
716 return FALSE;
717 }
718
719 function liquid_access_acquire_grants($node) {
720 $grants = module_invoke_all('wiki_access_records', $node);
721
722 if (!$grants) {
723 $grants[] = array('realm' => 'all', 'gid' => 0, 'grant_wiki_manage' => 0, 'grant_wiki_move' => 0);
724 }
725 else {
726 // retain grants by highest priority
727 $grant_by_priority = array();
728 foreach ($grants as $g) {
729 $grant_by_priority[intval($g['priority'])][] = $g;
730 }
731 krsort($grant_by_priority);
732 $grants = array_shift($grant_by_priority);
733 }
734
735 liquid_access_write_grants($node, $grants);
736 }
737
738
739 function liquid_access_write_grants($node, $grants, $realm = NULL, $delete = TRUE) {
740 if ($delete) {
741 $query = 'DELETE FROM {wiki_access} WHERE nid = %d';
742 if ($realm) {
743 $query .= " AND realm in ('%s', 'all')";
744 }
745 db_query($query, $node->nid, $realm);
746 }
747
748 // Only perform work when node_access modules are active.
749 if (count(module_implements('wiki_grants'))) {
750 foreach ($grants as $grant) {
751 if ($realm && $realm != $grant['realm']) {
752 continue;
753 }
754 // Only write grants; denies are implicit.
755 if ($grant['grant_wiki_move'] || $grant['grant_wiki_manage']) {
756 db_query("INSERT INTO {wiki_access} (nid, realm, gid, grant_wiki_manage, grant_wiki_move) VALUES (%d, '%s', %d, %d, %d)", $node->nid, $grant['realm'], $grant['gid'], $grant['grant_wiki_manage'], $grant['grant_wiki_move']);
757 }
758 }
759 }
760 }
761
762 function liquid_access_rebuild() {
763 db_query("DELETE FROM {wiki_access}");
764 // only recalculate if site is using a node_access module
765 if (count(module_implements('wiki_grants'))) {
766 // If not in 'safe mode', increase the maximum execution time:
767 if (!ini_get('safe_mode')) {
768 set_time_limit(240);
769 }
770 $result = db_query("SELECT nid FROM {wiki_name}");
771 while ($node = db_fetch_object($result)) {
772 liquid_access_acquire_grants(node_load($node->nid));
773 }
774 }
775 else {
776 // not using any wiki_access modules. add the default grant.
777 db_query("INSERT INTO {wiki_access} VALUES (0, 0, 'all', 0, 0)");
778 }
779 cache_clear_all();
780 }
781
782
783 /*
784 * Wiki database logic
785 *****************************************************************/
786
787 /*
788 * Returns the wid of the node with the given nid.
789 * Returns null if the node is not inserted into the wiki.
790 */
791
792 function liquid_lookup_wid($nid) {
793 // to aviod trouble...
794 if (!is_numeric($nid))
795 return null;
796
797 $query = "SELECT wid, nid FROM {wiki_name} WHERE nid = %d";
798 $result = db_query($query, $nid);
799
800 if (db_num_rows($result) == 0)
801 return NULL;
802
803 $resobj = db_fetch_object($result);
804
805 return new WikiId($resobj->wid);
806 }
807
808 /*
809 * Returns the nid of the node that has been bound to the given wid.
810 * Returns null if the wid does not exist.
811 */
812 function liquid_lookup_nid($wid) {
813 $query = "SELECT wid, nid FROM {wiki_name} WHERE wid = '%s'";
814 $result = db_query($query, $wid->identifier());
815
816 if (db_num_rows($result) == 0)
817 return NULL;
818
819 $resobj = db_fetch_object($result);
820
821 return $resobj->nid;
822 }
823
824 /*
825 * Returns true if the node with nid has been inserted into the wiki.
826 */
827 function liquid_in_wiki($nid) {
828 return !is_null(liquid_lookup_wid($nid));
829 }
830
831 /*
832 * Returns true if the wid is in use
833 */
834 function liquid_wid_exists($wid) {
835 return !is_null(liquid_lookup_nid($wid));
836 }
837
838 /*
839 * Insert a node into the wiki and alert modules over hook_wikiapi
840 */
841 function liquid_insert($wid, $nid) {
842 // insert the name binding into the database
843 db_query("INSERT INTO {wiki_name} VALUES ('%s', %d)", $wid->identifier(), $nid);
844
845 // create the path alias, if required
846 if (module_exists('path') && variable_get(PREF_LIQUID_USE_PATH_ALIAS, PREF_LIQUID_USE_PATH_ALIAS_DEFAULT)) {
847 // to avoid trouble with buggy path aliasing, only add path if its clean
848 // if (drupal_urlencode($wid) == $wid)
849 path_set_alias("node/$nid", drupal_urlencode("wiki/".$wid->toURLString()));
850 }
851
852 // call module hooks
853 module_invoke_all('wikiapi', 'insert', $nid, $wid);
854
855 // update access grants
856 $node = node_load($nid);
857 node_access_acquire_grants($node);
858 liquid_access_acquire_grants($node);
859
860 // report success
861 return true;
862 }
863
864 /*
865 * Move node in the wiki and alert modules over hook_wikiapi
866 */
867 function liquid_move($wid, $new_wid) {
868 // check that we are moving to a "free spot"
869 $new_wid_count = db_result(db_query("SELECT COUNT(wid) FROM {wiki_name} WHERE wid = '%s'", $new_wid->identifier()));
870 if ($new_wid_count != 0) {
871 drupal_set_message(t("Wiki move failed: Wiki ID already taken"), 'error');
872 return false;
873 }
874
875 // move binding from database
876 db_query("UPDATE {wiki_name} SET wid ='%s' WHERE wid = '%s'", $new_wid->identifier(), $wid->identifier());
877
878 $nid = liquid_lookup_nid($new_wid);
879 // move path alias, if required
880 if (module_exists('path') && variable_get(PREF_LIQUID_USE_PATH_ALIAS, PREF_LIQUID_USE_PATH_ALIAS_DEFAULT)) {
881 path_set_alias(NULL, drupal_urlencode("wiki/".$wid->toURLString()));
882 // to avoid trouble with buggy path aliasing, only add path if its clean
883 // if (drupal_urlencode($new_wid) == $new_wid)
884 path_set_alias("node/$nid", drupal_urlencode("wiki/".$new_wid->toURLString()));
885 }
886
887 // call module hooks
888 module_invoke_all('wikiapi', 'move', $nid, $new_wid, $wid);
889
890 // update access grants
891 $node = node_load($nid);
892 node_access_acquire_grants($node);
893 liquid_access_acquire_grants($node);
894
895 // report success
896 return true;
897 }
898
899 /*
900 * Remove a node from the wiki and alert modules over hook_wikiapi
901 */
902 function liquid_remove($wid) {
903 $nid = liquid_lookup_nid($wid);
904
905 // remove binding from database
906 db_query("DELETE FROM {wiki_name} WHERE wid='%s'", $wid->identifier());
907
908 // remove path alias, if required
909 if (module_exists('path') && variable_get(PREF_LIQUID_USE_PATH_ALIAS, PREF_LIQUID_USE_PATH_ALIAS_DEFAULT))
910 path_set_alias(NULL, drupal_urlencode("wiki/$wid"));
911
912 // call module hooks
913 module_invoke_all('wikiapi', 'remove', $nid, $wid);
914
915 // update access grants
916 $node = node_load($nid);
917 node_access_acquire_grants($node);
918 liquid_access_acquire_grants($node);
919
920 // report success
921 return true;
922 }

  ViewVC Help
Powered by ViewVC 1.1.2