3 /* Copyright (C) 2004-2009 $ThePhpWikiProgrammingTeam
4 * Copyright (C) 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * List a number of pagenames, optionally as table with various columns.
25 * This library relieves some work for these plugins:
27 * AllPages, BackLinks, LikePages, MostPopular, TitleSearch, WikiAdmin* and more
29 * It also allows dynamic expansion of those plugins to include more
30 * columns in their output.
32 * Column 'info=' arguments:
34 * 'pagename' _("Page Name")
35 * 'mtime' _("Last Modified")
37 * 'summary' _("Last Summary")
38 * 'version' _("Version")),
39 * 'author' _("Last Author")),
40 * 'locked' _("Locked"), _("locked")
41 * 'minor' _("Minor Edit"), _("minor")
42 * 'markup' _("Markup")
44 * 'creator' _("Creator")
46 * 'checkbox' selectable checkbox at the left.
49 * Special, custom columns: Either theme or plugin (WikiAdmin*) specific.
50 * 'remove' _("Remove")
51 * 'perm' _("Permission Mask")
53 * 'renamed_pagename' _("Rename to")
54 * 'ratingwidget', ... wikilens theme specific.
55 * 'custom' See plugin/_WikiTranslation
57 * Symbolic 'info=' arguments:
58 * 'all' All columns except the special columns
59 * 'most' pagename, mtime, author, size, hits, ...
60 * 'some' pagename, mtime, author
62 * FIXME: In this refactoring I (Jeff) have un-implemented _ctime, _cauthor, and
63 * number-of-revision. Note the _ctime and _cauthor as they were implemented
64 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
65 * database. If lots of revisions have been made to a page, it's more than likely
66 * that some older revisions (include revision 1) have been cleaned (deleted).
69 * paging support: limit, offset args
70 * check PagePerm "list" access-type,
71 * all columns are sortable. Thanks to the wikilens team.
72 * cols > 1, comma, azhead, ordered (OL lists)
73 * ->supportedArgs() which arguments are supported, so that the plugin
74 * doesn't explictly need to declare it
76 * fix sortby logic, fix multiple sortby and other paging args per page.
77 * info=relation,linkto nopage=1
78 * use custom format method (RecentChanges, rss, ...)
81 * fix memory exhaustion on large pagelists with old --memory-limit php's only.
82 * Status: improved 2004-06-25 16:19:36 rurban
84 class _PageList_Column_base {
85 var $_tdattr = array();
87 function _PageList_Column_base ($default_heading, $align = false) {
88 $this->_heading = $default_heading;
91 // align="char" isn't supported by any browsers yet :(
92 //if (is_array($align))
93 // $this->_tdattr = $align;
95 $this->_tdattr['align'] = $align;
99 function format ($pagelist, $page_handle, &$revision_handle) {
100 $nbsp = HTML::raw(' ');
101 return HTML::td($this->_tdattr,
103 $this->_getValue($page_handle, $revision_handle),
107 function getHeading () {
108 return $this->_heading;
111 function setHeading ($heading) {
112 $this->_heading = $heading;
116 function heading () {
118 $nbsp = HTML::raw(' ');
120 if (1 /* or in_array($this->_field, PageList::sortable_columns())*/) {
121 // multiple comma-delimited sortby args: "+hits,+pagename"
122 // asc or desc: +pagename, -pagename
123 $sortby = PageList::sortby($this->_field, 'flip_order');
124 //Fixme: pass all also other GET args along. (limit, p[])
125 //TODO: support GET and POST
126 $s = HTML::a(array('href' =>
127 $request->GetURLtoSelf(array('sortby' => $sortby)),
128 'class' => 'pagetitle',
129 'title' => sprintf(_("Sort by %s"), $this->_field)),
130 $nbsp, HTML::u($this->_heading), $nbsp);
132 $s = HTML($nbsp, HTML::u($this->_heading), $nbsp);
134 return HTML::th(array('align' => 'center'),$s);
137 // new grid-style sortable heading
138 // TODO: via activeui.js ? (fast dhtml sorting)
139 function button_heading (&$pagelist, $colNum) {
140 global $WikiTheme, $request;
142 $nbsp = HTML::raw(' ');
143 if (!$WikiTheme->DUMP_MODE /* or in_array($this->_field, PageList::sortable_columns()) */) {
144 // TODO: add to multiple comma-delimited sortby args: "+hits,+pagename"
146 $noimg_src = $WikiTheme->getButtonURL('no_order');
148 $noimg = HTML::img(array('src' => $noimg_src,
153 if ($pagelist->sortby($colNum, 'check')) { // show icon? request or plugin arg
154 $sortby = $pagelist->sortby($colNum, 'flip_order');
155 $desc = (substr($sortby,0,1) == '-'); // +pagename or -pagename
156 $src = $WikiTheme->getButtonURL($desc ? 'asc_order' : 'desc_order');
157 $reverse = $desc ? _("reverse")." " : "";
159 // initially unsorted
160 $sortby = $pagelist->sortby($colNum, 'get');
165 $img->setAttr('alt', ".");
167 $img = HTML::img(array('src' => $src,
169 'alt' => _("Click to reverse sort order")));
171 $s = HTML::a(array('href' =>
172 //Fixme: pass all also other GET args along. (limit is ok, p[])
173 $request->GetURLtoSelf(array('sortby' => $sortby,
174 'id' => $pagelist->id)),
175 'class' => 'gridbutton',
176 'title' => sprintf(_("Click to sort by %s"), $reverse . $this->_field)),
177 $nbsp, $this->_heading,
181 $s = HTML($nbsp, $this->_heading, $nbsp);
183 return HTML::th(array('align' => 'center', 'valign' => 'middle',
184 'class' => 'gridbutton'), $s);
188 * Take two columns of this type and compare them.
189 * An undefined value is defined to be < than the smallest defined value.
190 * This base class _compare only works if the value is simple (e.g., a number).
192 * @param $colvala $this->_getValue() of column a
193 * @param $colvalb $this->_getValue() of column b
195 * @return -1 if $a < $b, 1 if $a > $b, 0 otherwise.
197 function _compare($colvala, $colvalb) {
198 if (is_string($colvala))
199 return strcmp($colvala,$colvalb);
201 if (($colvala === $colvalb) || (!isset($colvala) && !isset($colvalb))) {
204 $ret = (!isset($colvala) || ($colvala < $colvalb)) ? -1 : 1;
210 class _PageList_Column extends _PageList_Column_base {
211 function _PageList_Column ($field, $default_heading, $align = false) {
212 $this->_PageList_Column_base($default_heading, $align);
214 $this->_need_rev = substr($field, 0, 4) == 'rev:';
215 $this->_iscustom = substr($field, 0, 7) == 'custom:';
216 if ($this->_iscustom) {
217 $this->_field = substr($field, 7);
219 elseif ($this->_need_rev)
220 $this->_field = substr($field, 4);
222 $this->_field = $field;
225 function _getValue ($page_handle, &$revision_handle) {
226 if ($this->_need_rev) {
227 if (!$revision_handle)
228 // columns which need the %content should override this. (size, hi_content)
229 $revision_handle = $page_handle->getCurrentRevision(false);
230 return $revision_handle->get($this->_field);
233 return $page_handle->get($this->_field);
237 function _getSortableValue ($page_handle, &$revision_handle) {
238 $val = $this->_getValue($page_handle, $revision_handle);
239 if ($this->_field == 'hits')
241 elseif (is_object($val) && method_exists($val, 'asString'))
242 return $val->asString();
244 return (string) $val;
248 /* overcome a call_user_func limitation by not being able to do:
249 * call_user_func_array(array(&$class, $class_name), $params);
250 * So we need $class = new $classname($params);
251 * And we add a 4th param to get at the parent $pagelist object
253 class _PageList_Column_custom extends _PageList_Column {
254 function _PageList_Column_custom($params) {
255 $this->_pagelist =& $params[3];
256 $this->_PageList_Column($params[0], $params[1], $params[2]);
260 class _PageList_Column_size extends _PageList_Column {
261 function format (&$pagelist, $page_handle, &$revision_handle) {
262 return HTML::td($this->_tdattr,
264 $this->_getValue($pagelist, $page_handle, $revision_handle),
265 HTML::raw(' '));
268 function _getValue (&$pagelist, $page_handle, &$revision_handle) {
269 if (!$revision_handle or (!$revision_handle->_data['%content']
270 or $revision_handle->_data['%content'] === true)) {
271 $revision_handle = $page_handle->getCurrentRevision(true);
272 unset($revision_handle->_data['%pagedata']['_cached_html']);
274 $size = $this->_getSize($revision_handle);
275 // we can safely purge the content when it is not sortable
276 if (empty($pagelist->_sortby[$this->_field]))
277 unset($revision_handle->_data['%content']);
281 function _getSortableValue ($page_handle, &$revision_handle) {
282 if (!$revision_handle)
283 $revision_handle = $page_handle->getCurrentRevision(true);
284 return (empty($revision_handle->_data['%content']))
285 ? 0 : strlen($revision_handle->_data['%content']);
288 function _getSize($revision_handle) {
289 $bytes = @strlen($revision_handle->_data['%content']);
290 return ByteFormatter($bytes);
295 class _PageList_Column_bool extends _PageList_Column {
296 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
297 $this->_PageList_Column($field, $default_heading, 'center');
298 $this->_textIfTrue = $text;
299 $this->_textIfFalse = new RawXml('—'); //mdash
302 function _getValue ($page_handle, &$revision_handle) {
303 //FIXME: check if $this is available in the parent (->need_rev)
304 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
305 return $val ? $this->_textIfTrue : $this->_textIfFalse;
309 class _PageList_Column_checkbox extends _PageList_Column {
310 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
311 $this->_name = $name;
312 $heading = HTML::input(array('type' => 'button',
313 'title' => _("Click to de-/select all pages"),
314 'name' => $default_heading,
315 'value' => $default_heading,
316 'onclick' => "flipAll(this.form)"
318 $this->_PageList_Column($field, $heading, 'center');
320 function _getValue ($pagelist, $page_handle, &$revision_handle) {
321 $pagename = $page_handle->getName();
322 $selected = !empty($pagelist->_selected[$pagename]);
323 if (strstr($pagename,'[') or strstr($pagename,']')) {
324 $pagename = str_replace(array('[',']'),array('%5B','%5D'),$pagename);
327 return HTML::input(array('type' => 'checkbox',
328 'name' => $this->_name . "[$pagename]",
330 'checked' => 'checked'));
332 return HTML::input(array('type' => 'checkbox',
333 'name' => $this->_name . "[$pagename]",
337 function format ($pagelist, $page_handle, &$revision_handle) {
338 return HTML::td($this->_tdattr,
340 $this->_getValue($pagelist, $page_handle, $revision_handle),
341 HTML::raw(' '));
343 // don't sort this javascript button
344 function button_heading ($pagelist, $colNum) {
345 $s = HTML(HTML::raw(' '), $this->_heading, HTML::raw(' '));
346 return HTML::th(array('align' => 'center', 'valign' => 'middle',
347 'class' => 'gridbutton'), $s);
351 class _PageList_Column_time extends _PageList_Column {
352 function _PageList_Column_time ($field, $default_heading) {
353 $this->_PageList_Column($field, $default_heading, 'right');
355 $this->WikiTheme = &$WikiTheme;
358 function _getValue ($page_handle, &$revision_handle) {
359 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
360 return $this->WikiTheme->formatDateTime($time);
363 function _getSortableValue ($page_handle, &$revision_handle) {
364 return _PageList_Column::_getValue($page_handle, $revision_handle);
368 class _PageList_Column_version extends _PageList_Column {
369 function _getValue ($page_handle, &$revision_handle) {
370 if (!$revision_handle)
371 $revision_handle = $page_handle->getCurrentRevision();
372 return $revision_handle->getVersion();
376 // Output is hardcoded to limit of first 50 bytes. Otherwise
377 // on very large Wikis this will fail if used with AllPages
378 // (PHP memory limit exceeded)
379 class _PageList_Column_content extends _PageList_Column {
380 function _PageList_Column_content ($field, $default_heading, $align = false, $search = false) {
381 $this->_PageList_Column($field, $default_heading, $align);
383 $this->search = $search;
384 if ($field == 'content') {
385 $this->_heading .= sprintf(_(" ... first %d bytes"),
387 } elseif ($field == 'rev:hi_content') {
388 global $HTTP_POST_VARS;
389 if (!$this->search and !empty($HTTP_POST_VARS['admin_replace'])) {
390 $this->search = $HTTP_POST_VARS['admin_replace']['from'];
392 $this->_heading .= sprintf(_(" ... around %s"),
393 '»'.$this->search.'«');
397 function _getValue ($page_handle, &$revision_handle) {
398 if (!$revision_handle or (!$revision_handle->_data['%content']
399 or $revision_handle->_data['%content'] === true)) {
400 $revision_handle = $page_handle->getCurrentRevision(true);
402 // Not sure why implode is needed here, I thought
403 // getContent() already did this, but it seems necessary.
404 $c = implode("\n", $revision_handle->getContent());
405 if (empty($pagelist->_sortby[$this->_field]))
406 unset($revision_handle->_data['%content']);
407 if ($this->_field == 'hi_content') {
408 if (!empty($revision_handle->_data['%pagedata'])) {
409 $revision_handle->_data['%pagedata']['_cached_html'] = '';
410 // PHP Fatal error: Cannot unset string offsets
411 //unset($revision_handle->_data['%pagedata']['_cached_html']);
413 $search = $this->search;
415 if (is_object($page_handle) and !empty($page_handle->score))
416 $score = $page_handle->score;
417 elseif (is_array($page_handle) and !empty($page_handle['score']))
418 $score = $page_handle['score'];
419 // Remove special characters so that highlighting works
420 $search = preg_replace('/^[\^\*]/', '', $search);
421 $search = preg_replace('/[\^\*]$/', '', $search);
422 if ($search and ($i = strpos(strtolower($c), strtolower($search))) !== false) {
423 $l = strlen($search);
424 $j = max(0, $i - ($this->bytes / 2));
425 return HTML::div(array('style' => 'font-size:x-small'),
426 HTML::div(array('class' => 'transclusion'),
427 HTML::span(($j ? '...' : '').substr($c, $j, ($j ? $this->bytes / 2 : $i))),
428 HTML::span(array("style"=>"background:yellow"),substr($c, $i, $l)),
429 HTML::span(substr($c, $i+$l, ($this->bytes / 2))."..."." ".($score ? sprintf("[%0.1f]",$score):""))));
431 if (strpos($c," ") !== false)
434 $c = sprintf(_("%s not found"), '»'.$search.'«');
435 return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
436 $c." ".($score ? sprintf("[%0.1f]",$score):""));
438 } elseif (($len = strlen($c)) > $this->bytes) {
439 $c = substr($c, 0, $this->bytes);
441 include_once('lib/BlockParser.php');
442 // false --> don't bother processing hrefs for embedded WikiLinks
443 $ct = TransformText($c, $revision_handle->get('markup'), false);
444 if (empty($pagelist->_sortby[$this->_field]))
445 unset($revision_handle->_data['%pagedata']['_cached_html']);
446 return HTML::div(array('style' => 'font-size:x-small'),
447 HTML::div(array('class' => 'transclusion'), $ct),
448 // Don't show bytes here if size column present too
449 ($this->parent->_columns_seen['size'] or !$len) ? "" :
450 ByteFormatter($len, /*$longformat = */true));
452 function _getSortableValue ($page_handle, &$revision_handle) {
453 if (is_object($page_handle) and !empty($page_handle->score))
454 return $page_handle->score;
455 elseif (is_array($page_handle) and !empty($page_handle['score']))
456 return $page_handle['score'];
458 return substr(_PageList_Column::_getValue($page_handle, $revision_handle),0,50);
462 class _PageList_Column_author extends _PageList_Column {
463 function _PageList_Column_author ($field, $default_heading, $align = false) {
464 _PageList_Column::_PageList_Column($field, $default_heading, $align);
465 $this->dbi =& $GLOBALS['request']->getDbh();
468 function _getValue ($page_handle, &$revision_handle) {
469 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
470 if ($this->dbi->isWikiPage($author))
471 return WikiLink($author);
476 function _getSortableValue ($page_handle, &$revision_handle) {
477 return _PageList_Column::_getValue($page_handle, $revision_handle);
481 class _PageList_Column_owner extends _PageList_Column_author {
482 function _getValue ($page_handle, &$revision_handle) {
483 $author = $page_handle->getOwner();
484 if ($this->dbi->isWikiPage($author))
485 return WikiLink($author);
489 function _getSortableValue ($page_handle, &$revision_handle) {
490 return _PageList_Column::_getValue($page_handle, $revision_handle);
494 class _PageList_Column_creator extends _PageList_Column_author {
495 function _getValue ($page_handle, &$revision_handle) {
496 $author = $page_handle->getCreator();
497 if ($this->dbi->isWikiPage($author))
498 return WikiLink($author);
502 function _getSortableValue ($page_handle, &$revision_handle) {
503 return _PageList_Column::_getValue($page_handle, $revision_handle);
507 class _PageList_Column_pagename extends _PageList_Column_base {
508 var $_field = 'pagename';
510 function _PageList_Column_pagename () {
511 $this->_PageList_Column_base(_("Page Name"));
513 $this->dbi = &$request->getDbh();
516 function _getValue ($page_handle, &$revision_handle) {
517 if ($this->dbi->isWikiPage($page_handle->getName()))
518 return WikiLink($page_handle, 'known');
520 return WikiLink($page_handle, 'unknown');
523 function _getSortableValue ($page_handle, &$revision_handle) {
524 return $page_handle->getName();
528 * Compare two pagenames for sorting. See _PageList_Column::_compare.
530 function _compare($colvala, $colvalb) {
531 return strcmp($colvala, $colvalb);
536 var $_group_rows = 3;
537 var $_columns = array();
538 var $_columnsMap = array(); // Maps column name to column number.
539 var $_excluded_pages = array();
540 var $_pages = array();
542 var $_pagename_seen = false;
543 var $_types = array();
544 var $_options = array();
545 var $_selected = array();
546 var $_sortby = array();
549 function PageList ($columns = false, $exclude = false, $options = false) {
550 // unique id per pagelist on each page.
551 if (!isset($GLOBALS['request']->_pagelist))
552 $GLOBALS['request']->_pagelist = 0;
554 $GLOBALS['request']->_pagelist++;
555 $this->id = $GLOBALS['request']->_pagelist;
556 if ($GLOBALS['request']->getArg('count'))
557 $options['count'] = $GLOBALS['request']->getArg('count');
559 $this->_options = $options;
561 $this->_initAvailableColumns();
562 // let plugins predefine only certain objects, such its own custom pagelist columns
565 'all' => array_diff(array_keys($this->_types), // all but...
566 array('checkbox','remove','renamed_pagename',
567 'content','hi_content','perm','acl')),
568 'most' => array('pagename','mtime','author','hits'),
569 'some' => array('pagename','mtime','author')
571 if (isset($this->_options['listtype'])
572 and $this->_options['listtype'] == 'dl')
573 $this->_options['nopage'] = 1;
575 if (!is_array($columns))
576 $columns = explode(',', $columns);
577 // expand symbolic columns:
578 foreach ($symbolic_columns as $symbol => $cols) {
579 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
580 $columns = array_diff(array_merge($columns,$cols),array($symbol));
584 if (empty($this->_options['nopage']) and !in_array('pagename',$columns))
585 $this->_addColumn('pagename');
586 foreach ($columns as $col) {
588 $this->_addColumn($col);
592 // If 'pagename' is already present, _addColumn() will not add it again
593 if (empty($this->_options['nopage']))
594 $this->_addColumn('pagename');
596 if (!empty($this->_options['types'])) {
597 foreach ($this->_options['types'] as $type) {
598 $this->_types[$type->_field] = $type;
599 $this->_addColumn($type->_field);
601 unset($this->_options['types']);
605 // explicit header options: ?id=x&sortby=... override options[]
606 // support multiple sorts. check multiple, no nested elseif
607 if (($this->id == $request->getArg("id"))
608 and $request->getArg('sortby'))
610 // add it to the front of the sortby array
611 $this->sortby($request->getArg('sortby'), 'init');
612 $this->_options['sortby'] = $request->getArg('sortby');
614 if (!empty($options['sortby'])) {
615 if (empty($this->_options['sortby']))
616 $this->_options['sortby'] = $options['sortby'];
617 $this->sortby($options['sortby'], 'init');
619 if (!isset($request->args["id"]) and $request->getArg('sortby')
620 and empty($this->_options['sortby']))
622 $this->_options['sortby'] = $request->getArg('sortby');
623 $this->sortby($this->_options['sortby'], 'init');
625 // same as above but without the special sortby push, and mutually exclusive (elseif)
626 foreach ($this->pagingArgs() as $key) {
627 if ($key == 'sortby') continue;
628 if (($this->id == $request->getArg("id"))
629 and $request->getArg($key))
631 $this->_options[$key] = $request->getArg($key);
633 elseif (!empty($options) and !empty($options[$key])) {
634 $this->_options[$key] = $options[$key];
636 elseif (!isset($request->args["id"]) and $request->getArg($key)) {
637 $this->_options[$key] = $request->getArg($key);
640 $this->_options[$key] = false;
643 if (is_string($exclude) and !is_array($exclude))
644 $exclude = $this->explodePageList($exclude, false,
645 $this->_options['sortby'],
646 $this->_options['limit']);
647 $this->_excluded_pages = $exclude;
649 $this->_messageIfEmpty = _("<no matches>");
652 // Currently PageList takes these arguments:
653 // 1: info, 2: exclude, 3: hash of options
654 // Here we declare which options are supported, so that
655 // the calling plugin may simply merge this with its own default arguments
656 function supportedArgs () {
657 // Todo: add all supported Columns, like locked, minor, ...
658 return array(// Currently supported options:
659 /* what columns, what pages */
660 'info' => 'pagename',
661 'exclude' => '', // also wildcards, comma-seperated lists
662 // and <!plugin-list !> arrays
663 /* select pages by meta-data: */
664 'author' => false, // current user by []
665 'owner' => false, // current user by []
666 'creator' => false, // current user by []
668 /* for the sort buttons in <th> */
669 'sortby' => '', // same as for WikiDB::getAllPages
670 // (unsorted is faster)
672 /* PageList pager options:
673 * These options may also be given to _generate(List|Table) later
674 * But limit and offset might help the query WikiDB::getAllPages()
676 'limit' => 50, // number of rows (pagesize)
677 'paging' => 'auto', // 'auto' top + bottom rows if applicable
678 // // 'top' top only if applicable
679 // // 'bottom' bottom only if applicable
680 // // 'none' don't page at all
681 // (TODO: clarify what if $paging==false ?)
683 /* list-style options (with single pagename column only so far) */
684 'cols' => 1, // side-by-side display of list (1-3)
685 'azhead' => 0, // 1: group by initials
686 // 2: provide shortcut links to initials also
687 'comma' => 0, // condensed comma-seperated list,
688 // 1 if without links, 2 if with
689 'commasep' => false, // Default: ', '
690 'listtype' => '', // ul (default), ol, dl, comma
691 'ordered' => false, // OL or just UL lists (ignored for comma)
692 'linkmore' => '', // If count>0 and limit>0 display a link with
693 // the number of all results, linked to the given pagename.
695 'nopage' => false, // for info=col omit the pagename column
696 // array_keys($this->_types). filter by columns: e.g. locked=1
697 'pagename' => null, // string regex
709 function pagingArgs() {
710 return array('sortby','limit','paging','count','dosort');
713 function setCaption ($caption_string) {
714 $this->_caption = $caption_string;
717 function addCaption ($caption_string) {
718 $this->_caption = HTML($this->_caption," ",$caption_string);
721 function getCaption () {
722 // put the total into the caption if needed
723 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
724 return sprintf($this->_caption, $this->getTotal());
725 return $this->_caption;
728 function setMessageIfEmpty ($msg) {
729 $this->_messageIfEmpty = $msg;
733 function getTotal () {
734 return !empty($this->_options['count'])
735 ? (integer) $this->_options['count'] : count($this->_pages);
738 function isEmpty () {
739 return empty($this->_pages);
742 function addPage($page_handle) {
743 if (!empty($this->_excluded_pages)) {
744 if (!in_array((is_string($page_handle) ? $page_handle : $page_handle->getName()),
745 $this->_excluded_pages))
746 $this->_pages[] = $page_handle;
748 $this->_pages[] = $page_handle;
752 function pageNames() {
754 $limit = @$this->_options['limit'];
755 foreach ($this->_pages as $page_handle) {
756 $pages[] = $page_handle->getName();
757 if ($limit and count($pages) > $limit)
763 function _getPageFromHandle($page_handle) {
764 if (is_string($page_handle)) {
765 if (empty($page_handle)) return $page_handle;
766 //$dbi = $GLOBALS['request']->getDbh(); // no, safe some memory!
767 $page_handle = $GLOBALS['request']->_dbi->getPage($page_handle);
773 * Take a PageList_Page object, and return an HTML object to display
774 * it in a table or list row.
776 function _renderPageRow (&$page_handle, $i = 0) {
777 $page_handle = $this->_getPageFromHandle($page_handle);
778 //FIXME. only on sf.net
779 if (!is_object($page_handle)) {
780 trigger_error("PageList: Invalid page_handle $page_handle", E_USER_WARNING);
783 if (!isset($page_handle)
784 or empty($page_handle)
785 or (!empty($this->_excluded_pages)
786 and in_array($page_handle->getName(), $this->_excluded_pages)))
787 return; // exclude page.
789 // enforce view permission
790 if (!mayAccessPage('view', $page_handle->getName()))
793 $group = (int)($i / $this->_group_rows);
794 $class = ($group % 2) ? 'oddrow' : 'evenrow';
795 $revision_handle = false;
796 $this->_maxlen = max($this->_maxlen, strlen($page_handle->getName()));
798 if (count($this->_columns) > 1) {
799 $row = HTML::tr(array('class' => $class));
801 foreach ($this->_columns as $col) {
802 $col->current_row = $i;
803 $col->current_column = $j;
804 $row->pushContent($col->format($this, $page_handle, $revision_handle));
808 $col = $this->_columns[0];
809 $col->current_row = $i;
810 $col->current_column = 0;
811 $row = $col->_getValue($page_handle, $revision_handle);
817 /* ignore from, but honor limit */
818 function addPages ($page_iter) {
819 // TODO: if limit check max(strlen(pagename))
821 if (isa($page_iter->_iter, "WikiDB_backend_dbaBase_pageiter")) {
824 elseif (isset($this->_options['limit'])) { // extract from,count from limit
825 list($from, $limit) = WikiDB_backend::limit($this->_options['limit']);
830 while ($page = $page_iter->next()) {
832 if ($from and $i < $from)
834 if (!$limit or ($limit and $i < $limit))
835 $this->addPage($page);
837 if (empty($this->_options['count']))
838 $this->_options['count'] = $i;
841 function addPageList (&$list) {
842 if (empty($list)) return; // Protect reset from a null arg
843 if (isset($this->_options['limit'])) { // extract from,count from limit
844 list($from, $limit) = WikiDB_backend::limit($this->_options['limit']);
850 foreach ($list as $page) {
852 if ($from and $i < $from)
854 if (!$limit or ($limit and $i < $limit)) {
855 if (is_object($page)) $page = $page->_pagename;
856 $this->addPage((string)$page);
863 $dbi =& $request->getDbh();
864 if (isa($dbi,'WikiDB_SQL')) {
865 extract($dbi->_backend->_table_names);
866 $res = $dbi->_backend->_dbh->getOne("SELECT max(length(pagename)) FROM $page_tbl");
867 if (DB::isError($res) || empty($res)) return false;
869 } elseif (isa($dbi,'WikiDB_ADODB')) {
870 extract($dbi->_backend->_table_names);
871 $row = $dbi->_backend->_dbh->getRow("SELECT max(length(pagename)) FROM $page_tbl");
872 return $row ? $row[0] : false;
877 function getContent() {
878 // Note that the <caption> element wants inline content.
879 $caption = $this->getCaption();
881 if ($this->isEmpty())
882 return $this->_emptyList($caption);
883 elseif (isset($this->_options['listtype'])
884 and in_array($this->_options['listtype'],
885 array('ol','ul','comma','dl')))
886 return $this->_generateList($caption);
887 elseif (count($this->_columns) == 1)
888 return $this->_generateList($caption);
890 return $this->_generateTable($caption);
893 function printXML() {
894 PrintXML($this->getContent());
898 return AsXML($this->getContent());
902 * Handle sortby requests for the DB iterator and table header links.
903 * Prefix the column with + or - like "+pagename","-mtime", ...
906 * 'init' : unify with predefined order. "pagename" => "+pagename"
907 * 'flip_order' : "mtime" => "+mtime" => "-mtime" ...
908 * 'db' : "-pagename" => "pagename DESC"
911 * Now all columns are sortable. (patch by DanFr)
912 * Some columns have native DB backend methods, some not.
914 function sortby ($column, $action, $valid_fields=false) {
917 if (empty($column)) return '';
918 if (is_int($column)) {
919 $column = $this->_columns[$column - 1]->_field;
920 //$column = $col->_field;
922 //if (!is_string($column)) return '';
923 // support multiple comma-delimited sortby args: "+hits,+pagename"
925 if (strstr($column, ',')) {
926 $result = ($action == 'check') ? true : array();
927 foreach (explode(',', $column) as $col) {
928 if ($action == 'check')
929 $result = $result && $this->sortby($col, $action, $valid_fields);
931 $result[] = $this->sortby($col, $action, $valid_fields);
933 // 'check' returns true/false for every col. return true if all are true.
934 // i.e. the unsupported 'every' operator in functional languages.
935 if ($action == 'check')
938 return join(",", $result);
940 if (substr($column,0,1) == '+') {
941 $order = '+'; $column = substr($column,1);
942 } elseif (substr($column,0,1) == '-') {
943 $order = '-'; $column = substr($column,1);
945 // default initial order: +pagename, -mtime, -hits
947 if (!empty($this->_sortby[$column]))
948 $order = $this->_sortby[$column];
950 if (in_array($column, array('mtime','hits')))
956 if ($action == 'get') {
957 return $order . $column;
958 } elseif ($action == 'flip_order') {
960 trigger_error("flip $order $column ".$this->id, E_USER_NOTICE);
961 return ($order == '+' ? '-' : '+') . $column;
962 } elseif ($action == 'init') { // only allowed from PageList::PageList
964 if ($this->sortby($column, 'clicked')) {
965 trigger_error("clicked $order $column $this->id", E_USER_NOTICE);
966 //$order = ($order == '+' ? '-' : '+'); // $this->sortby($sortby, 'flip_order');
969 $this->_sortby[$column] = $order; // forces show icon
970 return $order . $column;
971 } elseif ($action == 'check') { // show icon?
972 //if specified via arg or if clicked
973 $show = (!empty($this->_sortby[$column]) or $this->sortby($column, 'clicked'));
974 if (0 and $show and DEBUG) {
975 trigger_error("show $order $column ".$this->id, E_USER_NOTICE);
978 } elseif ($action == 'clicked') { // flip sort order?
980 $arg = $request->getArg('sortby');
982 and strstr($arg, $column)
983 and (!isset($request->args['id'])
984 or $this->id == $request->getArg('id')));
985 } elseif ($action == 'db') {
986 // Performance enhancement: use native DB sort if possible.
987 if (($valid_fields and in_array($column, $valid_fields))
988 or (method_exists($request->_dbi->_backend, 'sortable_columns')
989 and (in_array($column, $request->_dbi->_backend->sortable_columns())))) {
990 // omit this sort method from the _sortPages call at rendering
991 // asc or desc: +pagename, -pagename
992 return $column . ($order == '+' ? ' ASC' : ' DESC');
1000 /* Splits pagelist string into array.
1001 * Test* or Test1,Test2
1002 * Limitation: Doesn't split into comma-sep and then expand wildcards.
1003 * "Test1*,Test2*" is expanded into TextSearch "Test1* Test2*"
1005 * echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
1007 function explodePageList($input, $include_empty=false, $sortby='',
1008 $limit='', $exclude='')
1010 if (empty($input)) return array();
1011 if (is_array($input)) return $input;
1012 // expand wildcards from list of all pages
1013 if (preg_match('/[\?\*]/', $input) or substr($input,0,1) == "^") {
1014 include_once("lib/TextSearchQuery.php");
1015 $search = new TextSearchQuery(str_replace(",", " ", $input), true,
1016 (substr($input,0,1) == "^") ? 'posix' : 'glob');
1017 $dbi = $GLOBALS['request']->getDbh();
1018 $iter = $dbi->titleSearch($search, $sortby, $limit, $exclude);
1020 while ($pagehandle = $iter->next()) {
1021 $pages[] = trim($pagehandle->getName());
1025 //TODO: need an SQL optimization here
1026 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit,
1028 while ($pagehandle = $allPagehandles->next()) {
1029 $allPages[] = $pagehandle->getName();
1031 return explodeList($input, $allPages);
1034 //TODO: do the sorting, normally not needed if used for exclude only
1035 return array_map("trim", explode(',', $input));
1039 // TODO: optimize getTotal => store in count
1040 function allPagesByAuthor($wildcard, $include_empty=false, $sortby='',
1041 $limit='', $exclude='')
1043 $dbi = $GLOBALS['request']->getDbh();
1044 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1045 $allPages = array();
1046 if ($wildcard === '[]') {
1047 $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
1048 if (!$wildcard) return $allPages;
1050 $do_glob = preg_match('/[\?\*]/', $wildcard);
1051 while ($pagehandle = $allPagehandles->next()) {
1052 $name = $pagehandle->getName();
1053 $author = $pagehandle->getAuthor();
1056 if (glob_match($wildcard, $author))
1057 $allPages[] = $name;
1058 } elseif ($wildcard == $author) {
1059 $allPages[] = $name;
1062 // TODO: purge versiondata_cache
1067 function allPagesByOwner($wildcard, $include_empty=false, $sortby='',
1068 $limit='', $exclude='') {
1069 $dbi = $GLOBALS['request']->getDbh();
1070 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1071 $allPages = array();
1072 if ($wildcard === '[]') {
1073 $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
1074 if (!$wildcard) return $allPages;
1076 $do_glob = preg_match('/[\?\*]/', $wildcard);
1077 while ($pagehandle = $allPagehandles->next()) {
1078 $name = $pagehandle->getName();
1079 $owner = $pagehandle->getOwner();
1082 if (glob_match($wildcard, $owner))
1083 $allPages[] = $name;
1084 } elseif ($wildcard == $owner) {
1085 $allPages[] = $name;
1092 function allPagesByCreator($wildcard, $include_empty=false, $sortby='',
1093 $limit='', $exclude='') {
1094 $dbi = $GLOBALS['request']->getDbh();
1095 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1096 $allPages = array();
1097 if ($wildcard === '[]') {
1098 $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
1099 if (!$wildcard) return $allPages;
1101 $do_glob = preg_match('/[\?\*]/', $wildcard);
1102 while ($pagehandle = $allPagehandles->next()) {
1103 $name = $pagehandle->getName();
1104 $creator = $pagehandle->getCreator();
1107 if (glob_match($wildcard, $creator))
1108 $allPages[] = $name;
1109 } elseif ($wildcard == $creator) {
1110 $allPages[] = $name;
1117 // UserPages are pages NOT owned by ADMIN_USER
1118 function allUserPages($include_empty=false, $sortby='',
1119 $limit='', $exclude='') {
1120 $dbi = $GLOBALS['request']->getDbh();
1121 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1122 $allPages = array();
1123 while ($pagehandle = $allPagehandles->next()) {
1124 $name = $pagehandle->getName();
1125 $owner = $pagehandle->getOwner();
1126 if ($owner !== ADMIN_USER) {
1127 $allPages[] = $name;
1133 ////////////////////
1135 ////////////////////
1136 /** Plugin and theme hooks:
1137 * If the pageList is initialized with $options['types'] these types are also initialized,
1138 * overriding the standard types.
1140 function _initAvailableColumns() {
1141 global $customPageListColumns;
1145 => new _PageList_Column_content('rev:content', _("Content")),
1146 // new: plugin specific column types initialised by the relevant plugins
1148 'hi_content' // with highlighted search for SearchReplace
1149 => new _PageList_Column_content('rev:hi_content', _("Content")),
1151 => new _PageList_Column_remove('remove', _("Remove")),
1152 // initialised by the plugin
1154 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
1156 => new _PageList_Column_perm('perm', _("Permission")),
1158 => new _PageList_Column_acl('acl', _("ACL")),
1161 => new _PageList_Column_checkbox('p', _("All")),
1163 => new _PageList_Column_pagename,
1165 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
1167 => new _PageList_Column('hits', _("Hits"), 'right'),
1169 => new _PageList_Column_size('rev:size', _("Size"), 'right'),
1170 /*array('align' => 'char', 'char' => ' ')*/
1172 => new _PageList_Column('rev:summary', _("Last Summary")),
1174 => new _PageList_Column_version('rev:version', _("Version"),
1177 => new _PageList_Column_author('rev:author', _("Last Author")),
1179 => new _PageList_Column_owner('author_id', _("Owner")),
1181 => new _PageList_Column_creator('author_id', _("Creator")),
1184 => new _PageList_Column_author('group', _("Group")),
1187 => new _PageList_Column_bool('locked', _("Locked"),
1190 => new _PageList_Column_bool('external', _("External"),
1193 => new _PageList_Column_bool('rev:is_minor_edit',
1194 _("Minor Edit"), _("minor")),
1196 => new _PageList_Column('rev:markup', _("Markup")),
1197 // 'rating' initialised by the wikilens theme hook: addPageListColumn
1200 => new _PageList_Column_rating('rating', _("Rate")),
1203 if (empty($this->_types))
1204 $this->_types = array();
1205 // add plugin specific pageList columns, initialized by $options['types']
1206 $this->_types = array_merge($standard_types, $this->_types);
1207 // add theme custom specific pageList columns:
1208 // set the 4th param as the current pagelist object.
1209 if (!empty($customPageListColumns)) {
1210 foreach ($customPageListColumns as $column => $params) {
1211 $class_name = array_shift($params);
1212 $params[3] =& $this;
1213 // ref to a class does not work with php-4
1214 $this->_types[$column] = new $class_name($params);
1219 function getOption($option) {
1220 if (array_key_exists($option, $this->_options)) {
1221 return $this->_options[$option];
1229 * Add a column to this PageList, given a column name.
1230 * The name is a type, and optionally has a : and a label. Examples:
1233 * pagename:This page
1235 * mtime:Last modified
1237 * If this function is called multiple times for the same type, the
1238 * column will only be added the first time, and ignored the succeeding times.
1239 * If you wish to add multiple columns of the same type, use addColumnObject().
1241 * @param column name
1242 * @return true if column is added, false otherwise
1244 function _addColumn ($column) {
1246 if (isset($this->_columns_seen[$column]))
1247 return false; // Already have this one.
1248 if (!isset($this->_types[$column]))
1249 $this->_initAvailableColumns();
1250 $this->_columns_seen[$column] = true;
1252 if (strstr($column, ':'))
1253 list ($column, $heading) = explode(':', $column, 2);
1255 // FIXME: these column types have hooks (objects) elsewhere
1256 // Omitting this warning should be overridable by the extension
1257 if (!isset($this->_types[$column])) {
1258 $silently_ignore = array('numbacklinks',
1259 'rating','ratingvalue',
1260 'coagreement', 'minmisery',
1262 'averagerating', 'top3recs',
1263 'relation', 'linkto');
1264 if (!in_array($column, $silently_ignore))
1265 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
1268 // FIXME: anon users might rate and see ratings also.
1269 // Defer this logic to the plugin.
1270 if ($column == 'rating' and !$GLOBALS['request']->_user->isSignedIn())
1273 $this->addColumnObject($this->_types[$column]);
1279 * Add a column to this PageList, given a column object.
1281 * @param $col object An object derived from _PageList_Column.
1283 function addColumnObject($col) {
1284 if (is_array($col)) {// custom column object
1286 $class_name = array_shift($params);
1287 $params[3] =& $this;
1288 $col = new $class_name($params);
1290 $heading = $col->getHeading();
1291 if (!empty($heading))
1292 $col->setHeading($heading);
1294 $this->_columns[] = $col;
1295 $this->_columnsMap[$col->_field] = count($this->_columns); // start with 1
1299 * Compare _PageList_Page objects.
1301 function _pageCompare(&$a, &$b) {
1302 if (empty($this->_sortby) or count($this->_sortby) == 0) {
1303 // No columns to sort by
1307 $pagea = $this->_getPageFromHandle($a); // If a string, convert to page
1308 assert(isa($pagea, 'WikiDB_Page'));
1309 $pageb = $this->_getPageFromHandle($b); // If a string, convert to page
1310 assert(isa($pageb, 'WikiDB_Page'));
1311 foreach ($this->_sortby as $colNum => $direction) {
1312 // get column type object
1313 if (!is_int($colNum)) { // or column fieldname
1314 if (isset($this->_columnsMap[$colNum]))
1315 $col = $this->_columns[$this->_columnsMap[$colNum] - 1];
1316 elseif (isset($this->_types[$colNum]))
1317 $col = $this->_types[$colNum];
1322 assert(isset($col));
1323 $revision_handle = false;
1324 $aval = $col->_getSortableValue($pagea, $revision_handle);
1325 $revision_handle = false;
1326 $bval = $col->_getSortableValue($pageb, $revision_handle);
1328 $cmp = $col->_compare($aval, $bval);
1329 if ($direction === "-") // Reverse the sense of the comparison
1333 // This is the first comparison that is not equal-- go with it
1341 * Put pages in order according to the sortby arg, if given
1342 * If the sortby cols are already sorted by the DB call, don't do usort.
1343 * TODO: optimize for multiple sortable cols
1345 function _sortPages() {
1346 if (count($this->_sortby) > 0) {
1347 $need_sort = $this->_options['dosort'];
1349 foreach ($this->_sortby as $col => $dir) {
1350 if (! $this->sortby($col, 'db'))
1353 if ($need_sort) { // There are some columns to sort by
1354 // TODO: consider nopage
1355 usort($this->_pages, array($this, '_pageCompare'));
1358 //unset($GLOBALS['PhpWiki_pagelist']);
1361 function limit($limit) {
1362 if (is_array($limit)) return $limit;
1363 if (strstr($limit, ','))
1364 return split(',', $limit);
1366 return array(0, $limit);
1369 function pagingTokens($numrows = false, $ncolumns = false, $limit = false) {
1370 if ($numrows === false)
1371 $numrows = $this->getTotal();
1372 if ($limit === false)
1373 $limit = $this->_options['limit'];
1374 if ($ncolumns === false)
1375 $ncolumns = count($this->_columns);
1377 list($offset, $pagesize) = $this->limit($limit);
1379 (!$offset and $numrows < $pagesize) or
1380 (($offset + $pagesize) < 0))
1383 $request = &$GLOBALS['request'];
1384 $pagename = $request->getArg('pagename');
1385 $defargs = array_merge(array('id' => $this->id), $request->args);
1386 if (USE_PATH_INFO) unset($defargs['pagename']);
1387 if (isset($defargs['action']) and ($defargs['action'] == 'browse')) {
1388 unset($defargs['action']);
1393 $tokens['PREV'] = false; $tokens['PREV_LINK'] = "";
1394 $tokens['COLS'] = $ncolumns;
1395 $tokens['COUNT'] = $numrows;
1396 $tokens['OFFSET'] = $offset;
1397 $tokens['SIZE'] = $pagesize;
1398 $tokens['NUMPAGES'] = (int) ceil($numrows / $pagesize);
1399 $tokens['ACTPAGE'] = (int) ceil(($offset / $pagesize)+1);
1401 $prev['limit'] = max(0, $offset - $pagesize) . ",$pagesize";
1402 $prev['count'] = $numrows;
1403 $tokens['LIMIT'] = $prev['limit'];
1404 $tokens['PREV'] = true;
1405 $tokens['PREV_LINK'] = WikiURL($pagename, $prev);
1406 $prev['limit'] = "0,$pagesize";
1407 $tokens['FIRST_LINK'] = WikiURL($pagename, $prev);
1410 $tokens['NEXT'] = false; $tokens['NEXT_LINK'] = "";
1411 if (($offset + $pagesize) < $numrows) {
1412 $next['limit'] = min($offset + $pagesize, $numrows - $pagesize) . ",$pagesize";
1413 $next['count'] = $numrows;
1414 $tokens['LIMIT'] = $next['limit'];
1415 $tokens['NEXT'] = true;
1416 $tokens['NEXT_LINK'] = WikiURL($pagename, $next);
1417 $next['limit'] = $numrows - $pagesize . ",$pagesize";
1418 $tokens['LAST_LINK'] = WikiURL($pagename, $next);
1423 // make a table given the caption
1424 function _generateTable($caption) {
1425 if (count($this->_sortby) > 0) $this->_sortPages();
1427 // wikiadminutils hack. that's a way to pagelist non-pages
1428 $rows = isset($this->_rows) ? $this->_rows : array();
1430 $count = $this->getTotal();
1431 $do_paging = ( isset($this->_options['paging'])
1432 and !empty($this->_options['limit'])
1434 and $this->_options['paging'] != 'none' );
1436 $tokens = $this->pagingTokens($count,
1437 count($this->_columns),
1438 $this->_options['limit']);
1440 $this->_pages = array_slice($this->_pages, $tokens['OFFSET'], $tokens['COUNT']);
1443 foreach ($this->_pages as $pagenum => $page) {
1444 $one_row = $this->_renderPageRow($page, $i++);
1446 if ($one_row) $nb_row++;
1448 $table = HTML::table(array('cellpadding' => 0,
1452 'class' => 'pagelist',
1455 $caption = preg_replace('/{total}/', $nb_row, asString($caption));
1456 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
1460 $table_summary = array();
1461 $i = 1; // start with 1!
1462 foreach ($this->_columns as $col) {
1463 $heading = $col->button_heading($this, $i);
1465 and isset($col->_field)
1466 and $col->_field == 'pagename'
1467 and ($maxlen = $this->maxLen())) {
1468 // $heading->setAttr('width', $maxlen * 7);
1470 $row->pushContent($heading);
1471 if (is_string($col->getHeading()))
1472 $table_summary[] = $col->getHeading();
1475 // Table summary for non-visual browsers.
1476 $table->setAttr('summary', sprintf(_("Columns: %s."),
1477 join(", ", $table_summary)));
1478 $table->pushContent(HTML::colgroup(array('span' => count($this->_columns))));
1480 if ($tokens === false) {
1481 $table->pushContent(HTML::thead($row),
1482 HTML::tbody(false, $rows));
1486 $paging = Template("pagelink", $tokens);
1487 if ($this->_options['paging'] != 'bottom')
1488 $table->pushContent(HTML::thead($paging));
1489 if ($this->_options['paging'] != 'top')
1490 $table->pushContent(HTML::tfoot($paging));
1491 $table->pushContent(HTML::tbody(false, HTML($row, $rows)));
1494 $table->pushContent(HTML::thead($row),
1495 HTML::tbody(false, $rows));
1500 /* recursive stack for private sublist options (azhead, cols) */
1501 function _saveOptions($opts) {
1502 $stack = array('pages' => $this->_pages);
1503 foreach ($opts as $k => $v) {
1504 $stack[$k] = $this->_options[$k];
1505 $this->_options[$k] = $v;
1507 if (empty($this->_stack))
1508 $this->_stack = new Stack();
1509 $this->_stack->push($stack);
1511 function _restoreOptions() {
1512 assert($this->_stack);
1513 $stack = $this->_stack->pop();
1514 $this->_pages = $stack['pages'];
1515 unset($stack['pages']);
1516 foreach ($stack as $k => $v) {
1517 $this->_options[$k] = $v;
1521 // 'cols' - split into several columns
1522 // 'azhead' - support <h3> grouping into initials
1523 // 'ordered' - OL or UL list (not yet inherited to all plugins)
1524 // 'comma' - condensed comma-list only, 1: no links, >1: with links
1525 // FIXME: only unique list entries, esp. with nopage
1526 function _generateList($caption='') {
1527 if (empty($this->_pages)) return; // stop recursion
1528 if (!isset($this->_options['listtype']))
1529 $this->_options['listtype'] = '';
1531 foreach ($this->_pages as $pagenum => $page) {
1532 $one_row = $this->_renderPageRow($page);
1533 $rows[] = array('header' => WikiLink($page), 'render' => $one_row);
1534 if ($one_row) $nb_row++;
1538 $caption = preg_replace('/{total}/', $nb_row, asString($caption));
1539 $out->pushContent(HTML::p($caption));
1541 // Semantic Search et al: only unique list entries, esp. with nopage
1542 if (!is_array($this->_pages[0]) and is_string($this->_pages[0])) {
1543 $this->_pages = array_unique($this->_pages);
1545 if (count($this->_sortby) > 0) $this->_sortPages();
1546 $count = $this->getTotal();
1547 $do_paging = ( isset($this->_options['paging'])
1548 and !empty($this->_options['limit'])
1550 and $this->_options['paging'] != 'none' );
1552 $tokens = $this->pagingTokens($count,
1553 count($this->_columns),
1554 $this->_options['limit']);
1556 $paging = Template("pagelink", $tokens);
1557 $out->pushContent(HTML::table(array('width'=>'50%'), $paging));
1561 // need a recursive switch here for the azhead and cols grouping.
1562 if (!empty($this->_options['cols']) and $this->_options['cols'] > 1) {
1563 $count = count($this->_pages);
1564 $length = intval($count / ($this->_options['cols']));
1565 // If division does not give an integer, we need one more line
1566 // E.g. 13 pages to display in 3 columns.
1567 if (($length * ($this->_options['cols'])) != $count) {
1570 $width = sprintf("%d", 100 / $this->_options['cols']).'%';
1571 $cols = HTML::tr(array('valign' => 'top'));
1572 for ($i=0; $i < $count; $i += $length) {
1573 $this->_saveOptions(array('cols' => 0, 'paging' => 'none'));
1574 $this->_pages = array_slice($this->_pages, $i, $length);
1575 $cols->pushContent(HTML::td(/*array('width' => $width),*/
1576 $this->_generateList()));
1577 $this->_restoreOptions();
1579 // speed up table rendering by defining colgroups
1580 $out->pushContent(HTML::table(HTML::colgroup(array('span' => $this->_options['cols'],
1581 'width' => $width)),
1586 // Ignore azhead if not sorted by pagename
1587 if (!empty($this->_options['azhead'])
1588 and strstr($this->sortby($this->_options['sortby'], 'init'), "pagename")
1591 $cur_h = substr($this->_pages[0]->getName(), 0, 1);
1592 $out->pushContent(HTML::h3($cur_h));
1593 // group those pages together with same $h
1595 for ($i=0; $i < count($this->_pages); $i++) {
1596 $page =& $this->_pages[$i];
1597 $h = substr($page->getName(), 0, 1);
1598 if ($h != $cur_h and $i > $j) {
1599 $this->_saveOptions(array('cols' => 0, 'azhead' => 0));
1600 $this->_pages = array_slice($this->_pages, $j, $i - $j);
1601 $out->pushContent($this->_generateList());
1602 $this->_restoreOptions();
1604 $out->pushContent(HTML::h3($h));
1608 if ($i > $j) { // flush the rest
1609 $this->_saveOptions(array('cols' => 0, 'azhead' => 0));
1610 $this->_pages = array_slice($this->_pages, $j, $i - $j);
1611 $out->pushContent($this->_generateList());
1612 $this->_restoreOptions();
1617 if ($this->_options['listtype'] == 'comma')
1618 $this->_options['comma'] = 2;
1619 if (!empty($this->_options['comma'])) {
1620 if ($this->_options['comma'] == 1)
1621 $out->pushContent($this->_generateCommaListAsString());
1623 $out->pushContent($this->_generateCommaList($this->_options['comma']));
1627 if ($this->_options['listtype'] == 'ol')
1628 $this->_options['ordered'] = 1;
1629 elseif ($this->_options['listtype'] == 'ul')
1630 $this->_options['ordered'] = 0;
1631 if (!empty($this->_options['ordered']))
1632 $list = HTML::ol(array('class' => 'pagelist'));
1633 elseif ($this->_options['listtype'] == 'dl') {
1634 $list = HTML::dl(array('class' => 'pagelist'));
1636 $list = HTML::ul(array('class' => 'pagelist'));
1639 //TODO: currently we ignore limit here and hope that the backend didn't ignore it. (BackLinks)
1640 if (!empty($this->_options['limit']))
1641 list($offset, $pagesize) = $this->limit($this->_options['limit']);
1644 foreach (array_reverse($rows) as $one_row) {
1645 $pagehtml = $one_row['render'];
1646 if (!$pagehtml) continue;
1647 $group = ($i++ / $this->_group_rows);
1648 //TODO: here we switch every row, in tables every third.
1649 // unification or parametrized?
1650 $class = ($group % 2) ? 'oddrow' : 'evenrow';
1651 if ($this->_options['listtype'] == 'dl') {
1652 $header = $one_row['header'];
1653 //if ($this->_sortby['hi_content'])
1654 $list->pushContent(HTML::dt(array('class' => $class), $header),
1655 HTML::dd(array('class' => $class), $pagehtml));
1657 $list->pushContent(HTML::li(array('class' => $class), $pagehtml));
1658 if ($pagesize and $i > $pagesize) break;
1660 $out->pushContent($list);
1661 if ( $do_paging and $tokens ) {
1662 $out->pushContent(HTML::table($paging));
1668 // Condense list without a href links: "Page1, Page2, ..."
1669 // Alternative $seperator = HTML::Raw(' · ')
1670 // FIXME: only unique list entries, esp. with nopage
1671 function _generateCommaListAsString() {
1672 if (defined($this->_options['commasep']))
1673 $seperator = $this->_options['commasep'];
1677 foreach ($this->_pages as $pagenum => $page) {
1678 if ($s = $this->_renderPageRow($page)) // some pages are not viewable
1679 $pages[] = is_string($s) ? $s : $s->asString();
1681 return HTML(join($seperator, $pages));
1685 // Normal WikiLink list.
1686 // Future: 1 = reserved for plain string (see above)
1687 // 2 and more => HTML link specialization?
1688 // FIXME: only unique list entries, esp. with nopage
1689 function _generateCommaList($style = false) {
1690 if (defined($this->_options['commasep']))
1691 $seperator = HTLM::Raw($this->_options['commasep']);
1695 $html->pushContent($this->_renderPageRow($this->_pages[0]));
1696 next($this->_pages);
1697 foreach ($this->_pages as $pagenum => $page) {
1698 if ($s = $this->_renderPageRow($page)) // some pages are not viewable
1699 $html->pushContent($seperator, $s);
1704 function _emptyList($caption) {
1707 $caption = preg_replace('/{total}/', '0', asString($caption));
1708 $html->pushContent(HTML::p($caption));
1710 if ($this->_messageIfEmpty)
1711 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
1717 /* List pages with checkboxes to select from.
1718 * The [Select] button toggles via Javascript flipAll
1721 class PageList_Selectable
1724 function PageList_Selectable ($columns=false, $exclude='', $options = false) {
1726 if (!is_array($columns))
1727 $columns = explode(',', $columns);
1728 if (!in_array('checkbox',$columns))
1729 array_unshift($columns,'checkbox');
1731 $columns = array('checkbox','pagename');
1733 $this->PageList($columns, $exclude, $options);
1736 function addPageList ($array) {
1737 while (list($pagename,$selected) = each($array)) {
1738 if ($selected) $this->addPageSelected((string)$pagename);
1739 $this->addPage((string)$pagename);
1743 function addPageSelected ($pagename) {
1744 $this->_selected[$pagename] = 1;
1748 class PageList_Unselectable
1751 function PageList_Unselectable ($columns=false, $exclude='', $options = false) {
1753 if (!is_array($columns))
1754 $columns = explode(',', $columns);
1756 $columns = array('pagename');
1758 $this->PageList($columns, $exclude, $options);
1761 function addPageList ($array) {
1762 while (list($pagename,$selected) = each($array)) {
1763 if ($selected) $this->addPageSelected((string)$pagename);
1764 $this->addPage((string)$pagename);
1768 function addPageSelected ($pagename) {
1769 $this->_selected[$pagename] = 1;
1773 // (c-file-style: "gnu")
1777 // c-basic-offset: 4
1778 // c-hanging-comment-ender-p: nil
1779 // indent-tabs-mode: nil