2 /* Copyright (C) 2004-2010 $ThePhpWikiProgrammingTeam
3 * Copyright (C) 2008-2010 Marc-Etienne Vargenau, Alcatel-Lucent
5 * This file is part of PhpWiki.
7 * PhpWiki is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * PhpWiki is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * List a number of pagenames, optionally as table with various columns.
25 * See pgsrc/Help%2FPageList for arguments and details
27 * FIXME: In this refactoring I (Jeff) have un-implemented _ctime, _cauthor, and
28 * number-of-revision. Note the _ctime and _cauthor as they were implemented
29 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
30 * database. If lots of revisions have been made to a page, it's more than likely
31 * that some older revisions (include revision 1) have been cleaned (deleted).
34 * paging support: limit, offset args
35 * check PagePerm "list" access-type,
36 * all columns are sortable. Thanks to the wikilens team.
37 * cols > 1, comma, azhead, ordered (OL lists)
38 * ->supportedArgs() which arguments are supported, so that the plugin
39 * doesn't explictly need to declare it
40 * added slice option when the page_iter (e.g. ->_pages) is already sliced.
43 * fix sortby logic, fix multiple sortby and other paging args per page.
44 * info=relation,linkto nopage=1
45 * use custom format method (RecentChanges, rss, ...)
48 * fix memory exhaustion on large pagelists with old --memory-limit php's only.
49 * Status: improved 2004-06-25 16:19:36 rurban
51 class _PageList_Column_base
53 public $_tdattr = array();
55 function _PageList_Column_base($default_heading, $align = false)
57 $this->_heading = $default_heading;
60 $this->_tdattr['class'] = "align-".$align;
64 function format($pagelist, $page_handle, &$revision_handle)
66 return HTML::td($this->_tdattr,
67 $this->_getValue($page_handle, $revision_handle));
72 return $this->_heading;
75 function setHeading($heading)
77 $this->_heading = $heading;
85 if (1 /* or in_array($this->_field, PageList::sortable_columns())*/) {
86 // multiple comma-delimited sortby args: "+hits,+pagename"
87 // asc or desc: +pagename, -pagename
88 $sortby = PageList::sortby($this->_field, 'flip_order');
89 //Fixme: pass all also other GET args along. (limit, p[])
90 //TODO: support GET and POST
91 $s = HTML::a(array('href' =>
92 $request->GetURLtoSelf(array('sortby' => $sortby)),
93 'class' => 'pagetitle',
94 'title' => sprintf(_("Sort by %s"), $this->_field)),
95 HTML::u($this->_heading));
97 $s = HTML::u($this->_heading);
102 // new grid-style sortable heading
103 // TODO: via activeui.js ? (fast dhtml sorting)
104 function button_heading($pagelist, $colNum)
106 global $WikiTheme, $request;
108 if (!$WikiTheme->DUMP_MODE /* or in_array($this->_field, PageList::sortable_columns()) */) {
109 // TODO: add to multiple comma-delimited sortby args: "+hits,+pagename"
111 $noimg_src = $WikiTheme->getButtonURL('no_order');
113 $noimg = HTML::img(array('src' => $noimg_src,
116 $noimg = HTML::raw('');
117 if ($pagelist->sortby($colNum, 'check')) { // show icon? request or plugin arg
118 $sortby = $pagelist->sortby($colNum, 'flip_order');
119 $desc = (substr($sortby, 0, 1) == '-'); // +pagename or -pagename
120 $src = $WikiTheme->getButtonURL($desc ? 'asc_order' : 'desc_order');
121 $reverse = $desc ? _("reverse") . " " : "";
123 // initially unsorted
124 $sortby = $pagelist->sortby($colNum, 'get');
129 $img->setAttr('alt', ".");
131 $img = HTML::img(array('src' => $src,
132 'alt' => _("Click to reverse sort order")));
134 $s = HTML::a(array('href' =>
135 //Fixme: pass all also other GET args along. (limit is ok, p[])
136 $request->GetURLtoSelf(array('sortby' => $sortby,
137 'id' => $pagelist->id)),
138 'class' => 'gridbutton',
139 'title' => sprintf(_("Click to sort by %s"), $reverse . $this->_field)),
143 $s = HTML($this->_heading);
145 return HTML::th(array('class' => 'gridbutton'), $s);
149 * Take two columns of this type and compare them.
150 * An undefined value is defined to be < than the smallest defined value.
151 * This base class _compare only works if the value is simple (e.g., a number).
153 * @param mixed $colvala $this->_getValue() of column a
154 * @param mixed $colvalb $this->_getValue() of column b
156 * @return int -1 if $a < $b, 1 if $a > $b, 0 otherwise.
158 function _compare($colvala, $colvalb)
160 if (is_string($colvala))
161 return strcmp($colvala, $colvalb);
163 if (($colvala === $colvalb) || (!isset($colvala) && !isset($colvalb))) {
166 $ret = (!isset($colvala) || ($colvala < $colvalb)) ? -1 : 1;
172 class _PageList_Column extends _PageList_Column_base
174 function _PageList_Column($field, $default_heading, $align = false)
176 $this->_PageList_Column_base($default_heading, $align);
178 $this->_need_rev = substr($field, 0, 4) == 'rev:';
179 $this->_iscustom = substr($field, 0, 7) == 'custom:';
180 if ($this->_iscustom) {
181 $this->_field = substr($field, 7);
182 } elseif ($this->_need_rev)
183 $this->_field = substr($field, 4); else
184 $this->_field = $field;
187 function _getValue($page_handle, &$revision_handle)
189 if ($this->_need_rev) {
190 if (!$revision_handle)
191 // columns which need the %content should override this. (size, hi_content)
192 $revision_handle = $page_handle->getCurrentRevision(false);
193 return $revision_handle->get($this->_field);
195 return $page_handle->get($this->_field);
199 function _getSortableValue($page_handle, &$revision_handle)
201 $val = $this->_getValue($page_handle, $revision_handle);
202 if ($this->_field == 'hits')
204 elseif (is_object($val) && method_exists($val, 'asString'))
205 return $val->asString(); else
210 /* overcome a call_user_func limitation by not being able to do:
211 * call_user_func_array(array(&$class, $class_name), $params);
212 * So we need $class = new $classname($params);
213 * And we add a 4th param to get at the parent $pagelist object
215 class _PageList_Column_custom extends _PageList_Column
217 function _PageList_Column_custom($params)
219 $this->_pagelist =& $params[3];
220 $this->_PageList_Column($params[0], $params[1], $params[2]);
224 class _PageList_Column_size extends _PageList_Column
226 function format($pagelist, $page_handle, &$revision_handle)
228 return HTML::td($this->_tdattr,
229 $this->_getValuePageList($pagelist, $page_handle, $revision_handle));
232 function _getValuePageList($pagelist, $page_handle, &$revision_handle)
234 if (!$revision_handle or (!$revision_handle->_data['%content']
235 or $revision_handle->_data['%content'] === true)
237 $revision_handle = $page_handle->getCurrentRevision(true);
238 unset($revision_handle->_data['%pagedata']['_cached_html']);
240 $size = $this->_getSize($revision_handle);
241 // we can safely purge the content when it is not sortable
242 if (empty($pagelist->_sortby[$this->_field]))
243 unset($revision_handle->_data['%content']);
247 function _getSortableValue($page_handle, &$revision_handle)
249 if (!$revision_handle)
250 $revision_handle = $page_handle->getCurrentRevision(true);
251 return (empty($revision_handle->_data['%content']))
252 ? 0 : strlen($revision_handle->_data['%content']);
255 function _getSize($revision_handle)
257 $bytes = @strlen($revision_handle->_data['%content']);
258 return ByteFormatter($bytes);
262 class _PageList_Column_bool extends _PageList_Column
264 function _PageList_Column_bool($field, $default_heading, $text = 'yes')
266 $this->_PageList_Column($field, $default_heading, 'center');
267 $this->_textIfTrue = $text;
268 $this->_textIfFalse = new RawXml('—'); //mdash
271 function _getValue($page_handle, &$revision_handle)
273 //FIXME: check if $this is available in the parent (->need_rev)
274 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
275 return $val ? $this->_textIfTrue : $this->_textIfFalse;
279 class _PageList_Column_checkbox extends _PageList_Column
281 function _PageList_Column_checkbox($field, $default_heading, $name = 'p')
283 $this->_name = $name;
284 $heading = HTML::input(array('type' => 'button',
285 'title' => _("Click to de-/select all pages"),
286 'name' => $default_heading,
287 'value' => $default_heading,
288 'onclick' => "flipAll(this.form)"
290 $this->_PageList_Column($field, $heading, 'center');
293 function _getValuePageList($pagelist, $page_handle, &$revision_handle)
295 $pagename = $page_handle->getName();
296 $selected = !empty($pagelist->_selected[$pagename]);
297 if (strstr($pagename, '[') or strstr($pagename, ']')) {
298 $pagename = str_replace(array('[', ']'), array('%5B', '%5D'), $pagename);
301 return HTML::input(array('type' => 'checkbox',
302 'name' => $this->_name . "[$pagename]",
304 'checked' => 'checked'));
306 return HTML::input(array('type' => 'checkbox',
307 'name' => $this->_name . "[$pagename]",
312 function format($pagelist, $page_handle, &$revision_handle)
314 return HTML::td($this->_tdattr,
315 $this->_getValuePageList($pagelist, $page_handle, $revision_handle));
318 // don't sort this javascript button
319 function button_heading($pagelist, $colNum)
321 $s = HTML($this->_heading);
322 return HTML::th(array('class' => 'gridbutton'), $s);
326 class _PageList_Column_time extends _PageList_Column
328 function _PageList_Column_time($field, $default_heading)
330 $this->_PageList_Column($field, $default_heading, 'right');
332 $this->WikiTheme = &$WikiTheme;
335 function _getValue($page_handle, &$revision_handle)
337 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
338 return $this->WikiTheme->formatDateTime($time);
341 function _getSortableValue($page_handle, &$revision_handle)
343 return _PageList_Column::_getValue($page_handle, $revision_handle);
347 class _PageList_Column_version extends _PageList_Column
349 function _getValue($page_handle, &$revision_handle)
351 if (!$revision_handle)
352 $revision_handle = $page_handle->getCurrentRevision();
353 return $revision_handle->getVersion();
357 // Output is hardcoded to limit of first 50 bytes. Otherwise
358 // on very large Wikis this will fail if used with AllPages
359 // (PHP memory limit exceeded)
360 class _PageList_Column_content extends _PageList_Column
362 function _PageList_Column_content($field, $default_heading, $align = false,
363 $search = false, $hilight_re = false)
365 $this->_PageList_Column($field, $default_heading, $align);
367 $this->search = $search;
368 $this->hilight_re = $hilight_re;
369 if ($field == 'content') {
370 $this->_heading .= sprintf(_(" ... first %d bytes"),
372 } elseif ($field == 'rev:hi_content') {
373 global $HTTP_POST_VARS;
374 if (!$this->search and !empty($HTTP_POST_VARS['admin_replace'])) {
375 $this->search = $HTTP_POST_VARS['admin_replace']['from'];
377 $this->_heading .= sprintf(_(" ... around “%s”"), $this->search);
381 function _getValue($page_handle, &$revision_handle)
383 if (!$revision_handle or (!$revision_handle->_data['%content']
384 or $revision_handle->_data['%content'] === true)
386 $revision_handle = $page_handle->getCurrentRevision(true);
389 if ($this->_field == 'hi_content') {
390 if (!empty($revision_handle->_data['%pagedata'])) {
391 $revision_handle->_data['%pagedata']['_cached_html'] = '';
393 $search = $this->search;
395 if (is_object($page_handle) and !empty($page_handle->score))
396 $score = $page_handle->score;
397 elseif (is_array($page_handle) and !empty($page_handle['score']))
398 $score = $page_handle['score'];
400 $hilight_re = $this->hilight_re;
401 // use the TextSearchQuery highlighter
402 if ($search and $hilight_re) {
403 $matches = preg_grep("/$hilight_re/i", $revision_handle->getContent());
405 foreach (array_slice($matches, 0, 5) as $line) {
406 $line = WikiPlugin_FullTextSearch::highlight_line($line, $hilight_re);
407 $html[] = HTML::p(HTML::small(array('class' => 'search-context'), $line));
410 $html[] = HTML::small(sprintf("... [%0.1f]", $score));
413 // Remove special characters so that highlighting works
414 $search = preg_replace('/^[\^\*]/', '', $search);
415 $search = preg_replace('/[\^\*]$/', '', $search);
416 $c =& $revision_handle->getPackedContent();
417 if ($search and ($i = strpos(strtolower($c), strtolower($search))) !== false) {
418 $l = strlen($search);
419 $j = max(0, $i - ($this->bytes / 2));
420 return HTML::div(array('style' => 'font-size:x-small'),
421 HTML::div(array('class' => 'transclusion'),
422 HTML::span(($j ? '...' : '')
423 . substr($c, $j, ($j ? $this->bytes / 2 : $i))),
424 HTML::span(array("style" => "background:yellow"),
426 HTML::span(substr($c, $i + $l, ($this->bytes / 2))
428 . ($score ? sprintf("[%0.1f]", $score) : ""))));
430 $c = sprintf(_("“%s” not found"), $search);
431 return HTML::div(array('class' => 'align-center'),
432 $c . " " . ($score ? sprintf("[%0.1f]", $score) : ""));
434 } elseif (($len = strlen($c)) > $this->bytes) {
435 $c = substr($c, 0, $this->bytes);
437 include_once 'lib/BlockParser.php';
438 // false --> don't bother processing hrefs for embedded WikiLinks
439 $ct = TransformText($c, false);
440 if (empty($pagelist->_sortby[$this->_field]))
441 unset($revision_handle->_data['%pagedata']['_cached_html']);
442 return HTML::div(array('style' => 'font-size:x-small'),
443 HTML::div(array('class' => 'transclusion'), $ct),
444 // Don't show bytes here if size column present too
445 ($this->parent->_columns_seen['size'] or !$len) ? "" :
446 ByteFormatter($len, /*$longformat = */
450 function _getSortableValue($page_handle, &$revision_handle)
452 if (is_object($page_handle) and !empty($page_handle->score))
453 return $page_handle->score;
454 elseif (is_array($page_handle) and !empty($page_handle['score']))
455 return $page_handle['score']; else
456 return substr(_PageList_Column::_getValue($page_handle, $revision_handle), 0, 50);
460 class _PageList_Column_author extends _PageList_Column
462 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)
470 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
471 if ($this->dbi->isWikiPage($author))
472 return WikiLink($author);
477 function _getSortableValue($page_handle, &$revision_handle)
479 return _PageList_Column::_getValue($page_handle, $revision_handle);
483 class _PageList_Column_owner extends _PageList_Column_author
485 function _getValue($page_handle, &$revision_handle)
487 $author = $page_handle->getOwner();
488 if ($this->dbi->isWikiPage($author))
489 return WikiLink($author);
494 function _getSortableValue($page_handle, &$revision_handle)
496 return _PageList_Column::_getValue($page_handle, $revision_handle);
500 class _PageList_Column_creator extends _PageList_Column_author
502 function _getValue($page_handle, &$revision_handle)
504 $author = $page_handle->getCreator();
505 if ($this->dbi->isWikiPage($author))
506 return WikiLink($author);
511 function _getSortableValue($page_handle, &$revision_handle)
513 return _PageList_Column::_getValue($page_handle, $revision_handle);
517 class _PageList_Column_pagename extends _PageList_Column_base
519 public $_field = 'pagename';
521 function _PageList_Column_pagename()
523 $this->_PageList_Column_base(_("Page Name"));
525 $this->dbi = &$request->getDbh();
528 function _getValue($page_handle, &$revision_handle)
530 if ($this->dbi->isWikiPage($page_handle->getName()))
531 return WikiLink($page_handle, 'known');
533 return WikiLink($page_handle, 'unknown');
536 function _getSortableValue($page_handle, &$revision_handle)
538 return $page_handle->getName();
542 * Compare two pagenames for sorting. See _PageList_Column::_compare.
544 function _compare($colvala, $colvalb)
546 return strcmp($colvala, $colvalb);
550 class _PageList_Column_perm extends _PageList_Column
552 function _getValue($page_handle, &$revision_handle)
554 $perm_array = pagePermissions($page_handle->_pagename);
555 return pagePermissionsSimpleFormat($perm_array,
556 $page_handle->get('author'),
557 $page_handle->get('group'));
561 class _PageList_Column_acl extends _PageList_Column
563 function _getValue($page_handle, &$revision_handle)
565 $perm_tree = pagePermissions($page_handle->_pagename);
567 list($type, $perm) = pagePermissionsAcl($perm_tree[0], $perm_tree);
568 if ($type == 'inherited') {
569 $type = sprintf(_("page permission inherited from %s"), $perm_tree[1][0]);
570 } elseif ($type == 'page') {
571 $type = _("individual page permission");
572 } elseif ($type == 'default') {
573 $type = _("default page permission");
575 $result = HTML::span();
576 $result->pushContent($type);
577 $result->pushContent(HTML::br());
578 $result->pushContent($perm->asAclLines());
585 public $_group_rows = 3;
586 public $_columns = array();
587 public $_columnsMap = array(); // Maps column name to column number.
588 public $_excluded_pages = array();
589 public $_pages = array();
590 public $_caption = "";
591 public $_pagename_seen = false;
592 public $_types = array();
593 public $_options = array();
594 public $_selected = array();
595 public $_sortby = array();
598 function PageList($columns = false, $exclude = false, $options = false)
600 // unique id per pagelist on each page.
601 if (!isset($GLOBALS['request']->_pagelist))
602 $GLOBALS['request']->_pagelist = 0;
604 $GLOBALS['request']->_pagelist++;
605 $this->id = $GLOBALS['request']->_pagelist;
606 if ($GLOBALS['request']->getArg('count'))
607 $options['count'] = $GLOBALS['request']->getArg('count');
609 $this->_options = $options;
611 $this->_initAvailableColumns();
612 // let plugins predefine only certain objects, such its own custom pagelist columns
615 'all' => array_diff(array_keys($this->_types), // all but...
616 array('checkbox', 'remove', 'renamed_pagename',
617 'content', 'hi_content', 'perm', 'acl')),
618 'most' => array('pagename', 'mtime', 'author', 'hits'),
619 'some' => array('pagename', 'mtime', 'author')
621 if (isset($this->_options['listtype'])
622 and $this->_options['listtype'] == 'dl'
624 $this->_options['nopage'] = 1;
626 if (!is_array($columns))
627 $columns = explode(',', $columns);
628 // expand symbolic columns:
629 foreach ($symbolic_columns as $symbol => $cols) {
630 if (in_array($symbol, $columns)) { // e.g. 'checkbox,all'
631 $columns = array_diff(array_merge($columns, $cols), array($symbol));
635 if (empty($this->_options['nopage']) and !in_array('pagename', $columns))
636 $this->_addColumn('pagename');
637 foreach ($columns as $col) {
639 $this->_addColumn($col);
643 // If 'pagename' is already present, _addColumn() will not add it again
644 if (empty($this->_options['nopage']))
645 $this->_addColumn('pagename');
647 if (!empty($this->_options['types'])) {
648 foreach ($this->_options['types'] as $type) {
649 $this->_types[$type->_field] = $type;
650 $this->_addColumn($type->_field);
652 unset($this->_options['types']);
656 // explicit header options: ?id=x&sortby=... override options[]
657 // support multiple sorts. check multiple, no nested elseif
658 if (($this->id == $request->getArg("id"))
659 and $request->getArg('sortby')
661 // add it to the front of the sortby array
662 $this->sortby($request->getArg('sortby'), 'init');
663 $this->_options['sortby'] = $request->getArg('sortby');
665 if (!empty($options['sortby'])) {
666 if (empty($this->_options['sortby']))
667 $this->_options['sortby'] = $options['sortby'];
668 $this->sortby($options['sortby'], 'init');
670 if (!isset($request->args["id"]) and $request->getArg('sortby')
671 and empty($this->_options['sortby'])
673 $this->_options['sortby'] = $request->getArg('sortby');
674 $this->sortby($this->_options['sortby'], 'init');
676 // same as above but without the special sortby push, and mutually exclusive (elseif)
677 foreach ($this->pagingArgs() as $key) {
678 if ($key == 'sortby') continue;
679 if (($this->id == $request->getArg("id"))
680 and $request->getArg($key)
682 $this->_options[$key] = $request->getArg($key);
684 elseif (!empty($options) and !empty($options[$key])) {
685 $this->_options[$key] = $options[$key];
687 elseif (!isset($request->args["id"]) and $request->getArg($key)) {
688 $this->_options[$key] = $request->getArg($key);
690 $this->_options[$key] = false;
693 if (is_string($exclude) and !is_array($exclude))
694 $exclude = $this->explodePageList($exclude, false,
695 $this->_options['sortby'],
696 $this->_options['limit']);
697 $this->_excluded_pages = $exclude;
699 $this->_messageIfEmpty = _("<no matches>");
702 // Currently PageList takes these arguments:
703 // 1: info, 2: exclude, 3: hash of options
704 // Here we declare which options are supported, so that
705 // the calling plugin may simply merge this with its own default arguments
706 function supportedArgs()
708 // Todo: add all supported Columns, like locked, minor, ...
709 return array( // Currently supported options:
710 /* what columns, what pages */
711 'info' => 'pagename',
712 'exclude' => '', // also wildcards, comma-separated lists
713 // and <!plugin-list !> arrays
714 /* select pages by meta-data: */
715 'author' => false, // current user by []
716 'owner' => false, // current user by []
717 'creator' => false, // current user by []
719 /* for the sort buttons in <th> */
720 'sortby' => '', // same as for WikiDB::getAllPages
721 // (unsorted is faster)
723 /* PageList pager options:
724 * These options may also be given to _generate(List|Table) later
725 * But limit and offset might help the query WikiDB::getAllPages()
727 'limit' => 50, // number of rows (pagesize)
728 'paging' => 'auto', // 'auto' top + bottom rows if applicable
729 // // 'top' top only if applicable
730 // // 'bottom' bottom only if applicable
731 // // 'none' don't page at all
732 // (TODO: clarify what if $paging==false ?)
734 /* list-style options (with single pagename column only so far) */
735 'cols' => 1, // side-by-side display of list (1-3)
736 'azhead' => 0, // 1: group by initials
737 // 2: provide shortcut links to initials also
738 'comma' => 0, // condensed comma-separated list,
739 // 1 if without links, 2 if with
740 'commasep' => false, // Default: ', '
741 'listtype' => '', // ul (default), ol, dl, comma
742 'ordered' => false, // OL or just UL lists (ignored for comma)
743 'linkmore' => '', // If count>0 and limit>0 display a link with
744 // the number of all results, linked to the given pagename.
746 'nopage' => false, // for info=col omit the pagename column
747 // array_keys($this->_types). filter by columns: e.g. locked=1
748 'pagename' => null, // string regex
759 function pagingArgs()
761 return array('sortby', 'limit', 'paging', 'count', 'dosort');
764 function clearArg($arg_name)
766 if (isset($this->_options[$arg_name]))
767 unset($this->_options[$arg_name]);
771 * @param mixed $caption string or HTML
773 function setCaption($caption)
775 $this->_caption = $caption;
779 * @param mixed $caption string or HTML
781 function addCaption($caption)
783 $this->_caption = HTML($this->_caption, " ", $caption);
786 function getCaption()
788 // put the total into the caption if needed
789 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
790 return sprintf($this->_caption, $this->getTotal());
791 return $this->_caption;
794 function setMessageIfEmpty($msg)
796 $this->_messageIfEmpty = $msg;
801 return !empty($this->_options['count'])
802 ? (integer)$this->_options['count'] : count($this->_pages);
807 return empty($this->_pages);
810 function addPage($page_handle)
812 if (!empty($this->_excluded_pages)) {
813 if (!in_array((is_string($page_handle) ? $page_handle : $page_handle->getName()),
814 $this->_excluded_pages)
816 $this->_pages[] = $page_handle;
818 $this->_pages[] = $page_handle;
825 $limit = @$this->_options['limit'];
826 foreach ($this->_pages as $page_handle) {
827 $pages[] = $page_handle->getName();
828 if ($limit and count($pages) > $limit)
834 function _getPageFromHandle($page_handle)
836 if (is_string($page_handle)) {
837 if (empty($page_handle)) return $page_handle;
838 $page_handle = $GLOBALS['request']->_dbi->getPage($page_handle);
844 * Take a PageList_Page object, and return an HTML object to display
845 * it in a table or list row.
847 function _renderPageRow(&$page_handle, $i = 0)
849 $page_handle = $this->_getPageFromHandle($page_handle);
850 //FIXME. only on sf.net
851 if (!is_object($page_handle)) {
852 trigger_error("PageList: Invalid page_handle $page_handle", E_USER_WARNING);
855 if (!isset($page_handle)
856 or empty($page_handle)
857 or (!empty($this->_excluded_pages)
858 and in_array($page_handle->getName(), $this->_excluded_pages))
860 return; // exclude page.
862 // enforce view permission
863 if (!mayAccessPage('view', $page_handle->getName()))
866 $group = (int)($i / $this->_group_rows);
867 $class = ($group % 2) ? 'oddrow' : 'evenrow';
868 $revision_handle = false;
869 $this->_maxlen = max($this->_maxlen, strlen($page_handle->getName()));
871 if (count($this->_columns) > 1) {
872 $row = HTML::tr(array('class' => $class));
874 foreach ($this->_columns as $col) {
875 $col->current_row = $i;
876 $col->current_column = $j;
877 $row->pushContent($col->format($this, $page_handle, $revision_handle));
881 $col = $this->_columns[0];
882 $col->current_row = $i;
883 $col->current_column = 0;
884 $row = $col->_getValue($page_handle, $revision_handle);
890 /* ignore from, but honor limit */
891 function addPages($page_iter)
893 // TODO: if limit check max(strlen(pagename))
894 $limit = $page_iter->limit();
897 list($from, $limit) = $this->limit($limit);
898 $this->_options['slice'] = 0;
900 while ($page = $page_iter->next()) {
902 if ($from and $i < $from)
904 if (!$limit or ($limit and $i < $limit))
905 $this->addPage($page);
908 $this->_options['slice'] = 0;
909 while ($page = $page_iter->next()) {
910 $this->addPage($page);
913 if (!is_array($page_iter->_options) || !array_key_exists('limit_by_db', $page_iter->_options) || !$page_iter->_options['limit_by_db'])
914 $this->_options['slice'] = 1;
915 if ($i and empty($this->_options['count']))
916 $this->_options['count'] = $i;
919 function addPageList($list)
921 if (empty($list)) return; // Protect reset from a null arg
922 if (isset($this->_options['limit'])) { // extract from,count from limit
923 list($from, $limit) = WikiDB_backend::limit($this->_options['limit']);
928 $this->_options['slice'] = 0;
930 foreach ($list as $page) {
932 if ($from and $i < $from)
934 if (!$limit or ($limit and $i < $limit)) {
935 if (is_object($page)) $page = $page->_pagename;
936 $this->addPage((string)$page);
944 $dbi =& $request->getDbh();
945 if (isa($dbi, 'WikiDB_SQL')) {
946 extract($dbi->_backend->_table_names);
947 $res = $dbi->_backend->_dbh->getOne("SELECT max(length(pagename)) FROM $page_tbl");
948 if (DB::isError($res) || empty($res)) return false;
950 } elseif (isa($dbi, 'WikiDB_ADODB')) {
951 extract($dbi->_backend->_table_names);
952 $row = $dbi->_backend->_dbh->getRow("SELECT max(length(pagename)) FROM $page_tbl");
953 return $row ? $row[0] : false;
960 if (count($this->_pages) > 0) {
961 return $this->_pages[0];
966 function getContent()
968 // Note that the <caption> element wants inline content.
969 $caption = $this->getCaption();
971 if ($this->isEmpty())
972 return $this->_emptyList($caption);
973 elseif (isset($this->_options['listtype'])
974 and in_array($this->_options['listtype'], array('ol', 'ul', 'comma', 'dl'))
976 return $this->_generateList($caption); elseif (count($this->_columns) == 1)
977 return $this->_generateList($caption); else
978 return $this->_generateTable($caption);
983 PrintXML($this->getContent());
988 return AsXML($this->getContent());
992 * Handle sortby requests for the DB iterator and table header links.
993 * Prefix the column with + or - like "+pagename","-mtime", ...
996 * 'init' : unify with predefined order. "pagename" => "+pagename"
997 * 'flip_order' : "mtime" => "+mtime" => "-mtime" ...
998 * 'db' : "-pagename" => "pagename DESC"
1001 * Now all columns are sortable. (patch by DanFr)
1002 * Some columns have native DB backend methods, some not.
1004 function sortby($column, $action, $valid_fields = false)
1008 if (empty($column)) return '';
1009 if (is_int($column)) {
1010 $column = $this->_columns[$column - 1]->_field;
1013 // support multiple comma-delimited sortby args: "+hits,+pagename"
1015 if (strstr($column, ',')) {
1016 $result = ($action == 'check') ? true : array();
1017 foreach (explode(',', $column) as $col) {
1018 if ($action == 'check')
1019 $result = $result && $this->sortby($col, $action, $valid_fields);
1021 $result[] = $this->sortby($col, $action, $valid_fields);
1023 // 'check' returns true/false for every col. return true if all are true.
1024 // i.e. the unsupported 'every' operator in functional languages.
1025 if ($action == 'check')
1028 return join(",", $result);
1030 if (substr($column, 0, 1) == '+') {
1032 $column = substr($column, 1);
1033 } elseif (substr($column, 0, 1) == '-') {
1035 $column = substr($column, 1);
1037 // default initial order: +pagename, -mtime, -hits
1038 if (empty($order)) {
1039 if (!empty($this->_sortby[$column]))
1040 $order = $this->_sortby[$column];
1042 if (in_array($column, array('mtime', 'hits')))
1048 if ($action == 'get') {
1049 return $order . $column;
1050 } elseif ($action == 'flip_order') {
1052 trigger_error("flip $order $column " . $this->id, E_USER_NOTICE);
1053 return ($order == '+' ? '-' : '+') . $column;
1054 } elseif ($action == 'init') { // only allowed from PageList::PageList
1056 if ($this->sortby($column, 'clicked')) {
1057 trigger_error("clicked $order $column $this->id", E_USER_NOTICE);
1060 $this->_sortby[$column] = $order; // forces show icon
1061 return $order . $column;
1062 } elseif ($action == 'check') { // show icon?
1063 //if specified via arg or if clicked
1064 $show = (!empty($this->_sortby[$column]) or $this->sortby($column, 'clicked'));
1065 if (0 and $show and DEBUG) {
1066 trigger_error("show $order $column " . $this->id, E_USER_NOTICE);
1069 } elseif ($action == 'clicked') { // flip sort order?
1071 $arg = $request->getArg('sortby');
1073 and strstr($arg, $column)
1074 and (!isset($request->args['id'])
1075 or $this->id == $request->getArg('id')));
1076 } elseif ($action == 'db') {
1077 // Performance enhancement: use native DB sort if possible.
1078 if (($valid_fields and in_array($column, $valid_fields))
1079 or (method_exists($request->_dbi->_backend, 'sortable_columns')
1080 and (in_array($column, $request->_dbi->_backend->sortable_columns())))
1082 // omit this sort method from the _sortPages call at rendering
1083 // asc or desc: +pagename, -pagename
1084 return $column . ($order == '+' ? ' ASC' : ' DESC');
1092 /* Splits pagelist string into array.
1093 * Test* or Test1,Test2
1094 * Limitation: Doesn't split into comma-sep and then expand wildcards.
1095 * "Test1*,Test2*" is expanded into TextSearch "Test1* Test2*"
1097 * echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
1099 function explodePageList($input, $include_empty = false, $sortby = '',
1100 $limit = '', $exclude = '')
1102 if (empty($input)) return array();
1103 if (is_array($input)) return $input;
1104 // expand wildcards from list of all pages
1105 if (preg_match('/[\?\*]/', $input) or substr($input, 0, 1) == "^") {
1106 include_once 'lib/TextSearchQuery.php';
1107 $search = new TextSearchQuery(str_replace(",", " or ", $input), true,
1108 (substr($input, 0, 1) == "^") ? 'posix' : 'glob');
1109 $dbi = $GLOBALS['request']->getDbh();
1110 $iter = $dbi->titleSearch($search, $sortby, $limit, $exclude);
1112 while ($pagehandle = $iter->next()) {
1113 $pages[] = trim($pagehandle->getName());
1117 //TODO: do the sorting, normally not needed if used for exclude only
1118 return array_map("trim", explode(',', $input));
1122 // TODO: optimize getTotal => store in count
1123 function allPagesByAuthor($wildcard, $include_empty = false, $sortby = '',
1124 $limit = '', $exclude = '')
1126 $dbi = $GLOBALS['request']->getDbh();
1127 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1128 $allPages = array();
1129 if ($wildcard === '[]') {
1130 $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
1131 if (!$wildcard) return $allPages;
1133 $do_glob = preg_match('/[\?\*]/', $wildcard);
1134 while ($pagehandle = $allPagehandles->next()) {
1135 $name = $pagehandle->getName();
1136 $author = $pagehandle->getAuthor();
1139 if (glob_match($wildcard, $author))
1140 $allPages[] = $name;
1141 } elseif ($wildcard == $author) {
1142 $allPages[] = $name;
1145 // TODO: purge versiondata_cache
1150 function allPagesByOwner($wildcard, $include_empty = false, $sortby = '',
1151 $limit = '', $exclude = '')
1153 $dbi = $GLOBALS['request']->getDbh();
1154 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1155 $allPages = array();
1156 if ($wildcard === '[]') {
1157 $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
1158 if (!$wildcard) return $allPages;
1160 $do_glob = preg_match('/[\?\*]/', $wildcard);
1161 while ($pagehandle = $allPagehandles->next()) {
1162 $name = $pagehandle->getName();
1163 $owner = $pagehandle->getOwner();
1166 if (glob_match($wildcard, $owner))
1167 $allPages[] = $name;
1168 } elseif ($wildcard == $owner) {
1169 $allPages[] = $name;
1176 function allPagesByCreator($wildcard, $include_empty = false, $sortby = '',
1177 $limit = '', $exclude = '')
1179 $dbi = $GLOBALS['request']->getDbh();
1180 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1181 $allPages = array();
1182 if ($wildcard === '[]') {
1183 $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
1184 if (!$wildcard) return $allPages;
1186 $do_glob = preg_match('/[\?\*]/', $wildcard);
1187 while ($pagehandle = $allPagehandles->next()) {
1188 $name = $pagehandle->getName();
1189 $creator = $pagehandle->getCreator();
1192 if (glob_match($wildcard, $creator))
1193 $allPages[] = $name;
1194 } elseif ($wildcard == $creator) {
1195 $allPages[] = $name;
1202 // UserPages are pages NOT owned by ADMIN_USER
1203 function allUserPages($include_empty = false, $sortby = '',
1204 $limit = '', $exclude = '')
1206 $dbi = $GLOBALS['request']->getDbh();
1207 $allPagehandles = $dbi->getAllPages($include_empty, $sortby, $limit, $exclude);
1208 $allPages = array();
1209 while ($pagehandle = $allPagehandles->next()) {
1210 $name = $pagehandle->getName();
1211 $owner = $pagehandle->getOwner();
1212 if ($owner !== ADMIN_USER) {
1213 $allPages[] = $name;
1219 ////////////////////
1221 ////////////////////
1222 /** Plugin and theme hooks:
1223 * If the pageList is initialized with $options['types'] these types are also initialized,
1224 * overriding the standard types.
1226 function _initAvailableColumns()
1228 global $customPageListColumns;
1232 => new _PageList_Column_content('rev:content', _("Content")),
1233 // new: plugin specific column types initialised by the relevant plugins
1235 'hi_content' // with highlighted search for SearchReplace
1236 => new _PageList_Column_content('rev:hi_content', _("Content")),
1238 => new _PageList_Column_remove('remove', _("Remove")),
1239 // initialised by the plugin
1241 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
1244 => new _PageList_Column_perm('perm', _("Permission")),
1246 => new _PageList_Column_acl('acl', _("ACL")),
1248 => new _PageList_Column_checkbox('p', _("All")),
1250 => new _PageList_Column_pagename,
1252 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
1254 => new _PageList_Column('hits', _("Hits"), 'right'),
1256 => new _PageList_Column_size('rev:size', _("Size"), 'right'),
1258 => new _PageList_Column('rev:summary', _("Last Summary")),
1260 => new _PageList_Column_version('rev:version', _("Version"),
1263 => new _PageList_Column_author('rev:author', _("Last Author")),
1265 => new _PageList_Column_owner('author_id', _("Owner")),
1267 => new _PageList_Column_creator('author_id', _("Creator")),
1270 => new _PageList_Column_author('group', _("Group")),
1273 => new _PageList_Column_bool('locked', _("Locked"),
1276 => new _PageList_Column_bool('external', _("External"),
1279 => new _PageList_Column_bool('rev:is_minor_edit',
1280 _("Minor Edit"), _("minor")),
1281 // 'rating' initialised by the wikilens theme hook: addPageListColumn
1284 => new _PageList_Column_rating('rating', _("Rate")),
1287 if (empty($this->_types))
1288 $this->_types = array();
1289 // add plugin specific pageList columns, initialized by $options['types']
1290 $this->_types = array_merge($standard_types, $this->_types);
1291 // add theme custom specific pageList columns:
1292 // set the 4th param as the current pagelist object.
1293 if (!empty($customPageListColumns)) {
1294 foreach ($customPageListColumns as $column => $params) {
1295 $class_name = array_shift($params);
1296 $params[3] =& $this;
1297 // ref to a class does not work with php-4
1298 $this->_types[$column] = new $class_name($params);
1303 function getOption($option)
1305 if (array_key_exists($option, $this->_options)) {
1306 return $this->_options[$option];
1313 * Add a column to this PageList, given a column name.
1314 * The name is a type, and optionally has a : and a label. Examples:
1317 * pagename:This page
1319 * mtime:Last modified
1321 * If this function is called multiple times for the same type, the
1322 * column will only be added the first time, and ignored the succeeding times.
1323 * If you wish to add multiple columns of the same type, use addColumnObject().
1325 * @param string column name
1326 * @return bool true if column is added, false otherwise
1328 function _addColumn($column)
1330 if (isset($this->_columns_seen[$column]))
1331 return false; // Already have this one.
1332 if (!isset($this->_types[$column]))
1333 $this->_initAvailableColumns();
1334 $this->_columns_seen[$column] = true;
1336 if (strstr($column, ':'))
1337 list ($column, $heading) = explode(':', $column, 2);
1339 // FIXME: these column types have hooks (objects) elsewhere
1340 // Omitting this warning should be overridable by the extension
1341 if (!isset($this->_types[$column])) {
1342 $silently_ignore = array('numbacklinks',
1343 'rating', 'ratingvalue',
1344 'coagreement', 'minmisery',
1345 'averagerating', 'top3recs',
1346 'relation', 'linkto');
1347 if (!in_array($column, $silently_ignore))
1348 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
1351 if (!(defined('FUSIONFORGE') and FUSIONFORGE)) {
1352 // FIXME: anon users might rate and see ratings also.
1353 // Defer this logic to the plugin.
1354 if ($column == 'rating' and !$GLOBALS['request']->_user->isSignedIn()) {
1359 $this->addColumnObject($this->_types[$column]);
1365 * Add a column to this PageList, given a column object.
1367 * @param $col object An object derived from _PageList_Column.
1369 function addColumnObject($col)
1371 if (is_array($col)) { // custom column object
1373 $class_name = array_shift($params);
1374 $params[3] =& $this;
1375 $col = new $class_name($params);
1377 $heading = $col->getHeading();
1378 if (!empty($heading))
1379 $col->setHeading($heading);
1381 $this->_columns[] = $col;
1382 $this->_columnsMap[$col->_field] = count($this->_columns); // start with 1
1386 * Compare _PageList_Page objects.
1388 function _pageCompare(&$a, &$b)
1390 if (empty($this->_sortby) or count($this->_sortby) == 0) {
1391 // No columns to sort by
1394 $pagea = $this->_getPageFromHandle($a); // If a string, convert to page
1395 assert(isa($pagea, 'WikiDB_Page'));
1396 $pageb = $this->_getPageFromHandle($b); // If a string, convert to page
1397 assert(isa($pageb, 'WikiDB_Page'));
1398 foreach ($this->_sortby as $colNum => $direction) {
1399 // get column type object
1400 if (!is_int($colNum)) { // or column fieldname
1401 if (isset($this->_columnsMap[$colNum]))
1402 $col = $this->_columns[$this->_columnsMap[$colNum] - 1];
1403 elseif (isset($this->_types[$colNum]))
1404 $col = $this->_types[$colNum];
1409 assert(isset($col));
1410 $revision_handle = false;
1411 $aval = $col->_getSortableValue($pagea, $revision_handle);
1412 $revision_handle = false;
1413 $bval = $col->_getSortableValue($pageb, $revision_handle);
1415 $cmp = $col->_compare($aval, $bval);
1416 if ($direction === "-") // Reverse the sense of the comparison
1420 // This is the first comparison that is not equal-- go with it
1428 * Put pages in order according to the sortby arg, if given
1429 * If the sortby cols are already sorted by the DB call, don't do usort.
1430 * TODO: optimize for multiple sortable cols
1432 function _sortPages()
1434 if (count($this->_sortby) > 0) {
1435 $need_sort = $this->_options['dosort'];
1437 foreach ($this->_sortby as $col => $dir) {
1438 if (!$this->sortby($col, 'db'))
1441 if ($need_sort) { // There are some columns to sort by
1442 // TODO: consider nopage
1443 usort($this->_pages, array($this, '_pageCompare'));
1448 function limit($limit)
1450 if (is_array($limit)) {
1451 list($from, $count) = $limit;
1452 if ((!empty($from) && !is_numeric($from)) or (!empty($count) && !is_numeric($count))) {
1453 return $this->error(_("Illegal 'limit' argument: must be numeric"));
1457 if (strstr($limit, ',')) {
1458 list($from, $limit) = explode(',', $limit);
1459 if ((!empty($from) && !is_numeric($from)) or (!empty($limit) && !is_numeric($limit))) {
1460 return $this->error(_("Illegal 'limit' argument: must be numeric"));
1462 return array($from, $limit);
1464 if (!empty($limit) && !is_numeric($limit)) {
1465 return $this->error(_("Illegal 'limit' argument: must be numeric"));
1467 return array(0, $limit);
1471 function pagingTokens($numrows = false, $ncolumns = false, $limit = false)
1473 if ($numrows === false)
1474 $numrows = $this->getTotal();
1475 if ($limit === false)
1476 $limit = $this->_options['limit'];
1477 if ($ncolumns === false)
1478 $ncolumns = count($this->_columns);
1480 list($offset, $pagesize) = $this->limit($limit);
1482 (!$offset and $numrows < $pagesize) or
1483 (($offset + $pagesize) < 0)
1487 $request = &$GLOBALS['request'];
1488 $pagename = $request->getArg('pagename');
1489 $defargs = array_merge(array('id' => $this->id), $request->args);
1490 if (USE_PATH_INFO) unset($defargs['pagename']);
1491 if (isset($defargs['action']) and ($defargs['action'] == 'browse')) {
1492 unset($defargs['action']);
1497 $tokens['PREV'] = false;
1498 $tokens['PREV_LINK'] = "";
1499 $tokens['COLS'] = $ncolumns;
1500 $tokens['COUNT'] = $numrows;
1501 $tokens['OFFSET'] = $offset;
1502 $tokens['SIZE'] = $pagesize;
1503 $tokens['NUMPAGES'] = (int)ceil($numrows / $pagesize);
1504 $tokens['ACTPAGE'] = (int)ceil(($offset / $pagesize) + 1);
1506 $prev['limit'] = max(0, $offset - $pagesize) . ",$pagesize";
1507 $prev['count'] = $numrows;
1508 $tokens['LIMIT'] = $prev['limit'];
1509 $tokens['PREV'] = true;
1510 $tokens['PREV_LINK'] = WikiURL($pagename, $prev);
1511 $prev['limit'] = "0,$pagesize"; // FIRST_LINK
1512 $tokens['FIRST_LINK'] = WikiURL($pagename, $prev);
1515 $tokens['NEXT'] = false;
1516 $tokens['NEXT_LINK'] = "";
1517 if (($offset + $pagesize) < $numrows) {
1518 $next['limit'] = min($offset + $pagesize, $numrows - $pagesize)
1520 $next['count'] = $numrows;
1521 $tokens['LIMIT'] = $next['limit'];
1522 $tokens['NEXT'] = true;
1523 $tokens['NEXT_LINK'] = WikiURL($pagename, $next);
1524 $next['limit'] = $numrows - $pagesize . ",$pagesize"; // LAST_LINK
1525 $tokens['LAST_LINK'] = WikiURL($pagename, $next);
1530 // make a table given the caption
1531 function _generateTable($caption)
1533 if (count($this->_sortby) > 0) $this->_sortPages();
1535 // wikiadminutils hack. that's a way to pagelist non-pages
1536 $rows = isset($this->_rows) ? $this->_rows : array();
1538 $count = $this->getTotal();
1539 $do_paging = (isset($this->_options['paging'])
1540 and !empty($this->_options['limit'])
1542 and $this->_options['paging'] != 'none');
1544 $tokens = $this->pagingTokens($count,
1545 count($this->_columns),
1546 $this->_options['limit']);
1547 if ($tokens and !empty($this->_options['slice']))
1548 $this->_pages = array_slice($this->_pages, $tokens['OFFSET'], $tokens['SIZE']);
1550 foreach ($this->_pages as $pagenum => $page) {
1551 $one_row = $this->_renderPageRow($page, $i++);
1554 $table = HTML::table(array('class' => 'fullwidth pagelist'));
1556 $table->pushContent(HTML::caption(array('align' => 'top'), $caption));
1560 $i = 1; // start with 1!
1561 foreach ($this->_columns as $col) {
1562 $heading = $col->button_heading($this, $i);
1564 and isset($col->_field)
1565 and $col->_field == 'pagename'
1566 and ($maxlen = $this->maxLen())
1569 $row->pushContent($heading);
1572 $table->pushContent(HTML::colgroup(array('span' => count($this->_columns))));
1574 if ($tokens === false) {
1575 $table->pushContent(HTML::thead($row),
1576 HTML::tbody(false, $rows));
1580 $paging = Template("pagelink", $tokens);
1581 if ($this->_options['paging'] != 'bottom')
1582 $table->pushContent(HTML::thead($paging));
1583 if ($this->_options['paging'] != 'top')
1584 $table->pushContent(HTML::tfoot($paging));
1585 $table->pushContent(HTML::tbody(false, HTML($row, $rows)));
1588 $table->pushContent(HTML::thead($row),
1589 HTML::tbody(false, $rows));
1594 /* recursive stack for private sublist options (azhead, cols) */
1595 function _saveOptions($opts)
1597 $stack = array('pages' => $this->_pages);
1598 foreach ($opts as $k => $v) {
1599 $stack[$k] = $this->_options[$k];
1600 $this->_options[$k] = $v;
1602 if (empty($this->_stack))
1603 $this->_stack = new Stack();
1604 $this->_stack->push($stack);
1607 function _restoreOptions()
1609 assert($this->_stack);
1610 $stack = $this->_stack->pop();
1611 $this->_pages = $stack['pages'];
1612 unset($stack['pages']);
1613 foreach ($stack as $k => $v) {
1614 $this->_options[$k] = $v;
1618 // 'cols' - split into several columns
1619 // 'azhead' - support <h3> grouping into initials
1620 // 'ordered' - OL or UL list (not yet inherited to all plugins)
1621 // 'comma' - condensed comma-list only, 1: no links, >1: with links
1622 // FIXME: only unique list entries, esp. with nopage
1623 function _generateList($caption = '')
1625 if (empty($this->_pages)) return; // stop recursion
1626 if (!isset($this->_options['listtype']))
1627 $this->_options['listtype'] = '';
1628 foreach ($this->_pages as $pagenum => $page) {
1629 $one_row = $this->_renderPageRow($page);
1630 $rows[] = array('header' => WikiLink($page), 'render' => $one_row);
1634 $out->pushContent(HTML::p($caption));
1637 // Semantic Search et al: only unique list entries, esp. with nopage
1638 if (!is_array($this->_pages[0]) and is_string($this->_pages[0])) {
1639 $this->_pages = array_unique($this->_pages);
1641 if (count($this->_sortby) > 0) $this->_sortPages();
1642 $count = $this->getTotal();
1643 $do_paging = (isset($this->_options['paging'])
1644 and !empty($this->_options['limit'])
1646 and $this->_options['paging'] != 'none');
1648 $tokens = $this->pagingTokens($count,
1649 count($this->_columns),
1650 $this->_options['limit']);
1652 $paging = Template("pagelink", $tokens);
1653 $out->pushContent(HTML::table(array('class' => 'fullwidth'), $paging));
1657 if (!empty($this->_options['limit']) and !empty($this->_options['slice'])) {
1658 list($offset, $count) = $this->limit($this->_options['limit']);
1661 $count = count($this->_pages);
1663 // need a recursive switch here for the azhead and cols grouping.
1664 if (!empty($this->_options['cols']) and $this->_options['cols'] > 1) {
1665 $length = intval($count / ($this->_options['cols']));
1666 // If division does not give an integer, we need one more line
1667 // E.g. 13 pages to display in 3 columns.
1668 if (($length * ($this->_options['cols'])) != $count) {
1671 $width = sprintf("%d", 100 / $this->_options['cols']) . '%';
1672 $cols = HTML::tr(array('class' => 'top'));
1673 for ($i = $offset; $i < $offset + $count; $i += $length) {
1674 $this->_saveOptions(array('cols' => 0, 'paging' => 'none'));
1675 $this->_pages = array_slice($this->_pages, $i, $length);
1676 $cols->pushContent(HTML::td( /*array('width' => $width),*/
1677 $this->_generateList()));
1678 $this->_restoreOptions();
1680 // speed up table rendering by defining colgroups
1681 $out->pushContent(HTML::table(HTML::colgroup
1682 (array('span' => $this->_options['cols'], 'width' => $width)),
1687 // Ignore azhead if not sorted by pagename
1688 if (!empty($this->_options['azhead'])
1689 and strstr($this->sortby($this->_options['sortby'], 'init'), "pagename")
1691 $cur_h = substr($this->_pages[0]->getName(), 0, 1);
1692 $out->pushContent(HTML::h3($cur_h));
1693 // group those pages together with same $h
1695 for ($i = 0; $i < count($this->_pages); $i++) {
1696 $page =& $this->_pages[$i];
1697 $h = substr($page->getName(), 0, 1);
1698 if ($h != $cur_h and $i > $j) {
1699 $this->_saveOptions(array('cols' => 0, 'azhead' => 0, 'ordered' => $j + 1));
1700 $this->_pages = array_slice($this->_pages, $j, $i - $j);
1701 $out->pushContent($this->_generateList());
1702 $this->_restoreOptions();
1704 $out->pushContent(HTML::h3($h));
1708 if ($i > $j) { // flush the rest
1709 $this->_saveOptions(array('cols' => 0, 'azhead' => 0, 'ordered' => $j + 1));
1710 $this->_pages = array_slice($this->_pages, $j, $i - $j);
1711 $out->pushContent($this->_generateList());
1712 $this->_restoreOptions();
1717 if ($this->_options['listtype'] == 'comma')
1718 $this->_options['comma'] = 2;
1719 if (!empty($this->_options['comma'])) {
1720 if ($this->_options['comma'] == 1)
1721 $out->pushContent($this->_generateCommaListAsString());
1723 $out->pushContent($this->_generateCommaList($this->_options['comma']));
1727 if ($this->_options['listtype'] == 'ol') {
1728 if (empty($this->_options['ordered'])) {
1729 $this->_options['ordered'] = $offset + 1;
1731 } elseif ($this->_options['listtype'] == 'ul')
1732 $this->_options['ordered'] = 0;
1733 if ($this->_options['listtype'] == 'ol' and !empty($this->_options['ordered'])) {
1734 $list = HTML::ol(array('class' => 'pagelist',
1735 'start' => $this->_options['ordered']));
1736 } elseif ($this->_options['listtype'] == 'dl') {
1737 $list = HTML::dl(array('class' => 'pagelist'));
1739 $list = HTML::ul(array('class' => 'pagelist'));
1742 //TODO: currently we ignore limit here and hope that the backend didn't ignore it. (BackLinks)
1743 if (!empty($this->_options['limit']))
1744 list($offset, $pagesize) = $this->limit($this->_options['limit']);
1747 foreach (array_reverse($rows) as $one_row) {
1748 $pagehtml = $one_row['render'];
1749 if (!$pagehtml) continue;
1750 $group = ($i++ / $this->_group_rows);
1751 //TODO: here we switch every row, in tables every third.
1752 // unification or parametrized?
1753 $class = ($group % 2) ? 'oddrow' : 'evenrow';
1754 if ($this->_options['listtype'] == 'dl') {
1755 $header = $one_row['header'];
1756 $list->pushContent(HTML::dt(array('class' => $class), $header),
1757 HTML::dd(array('class' => $class), $pagehtml));
1759 $list->pushContent(HTML::li(array('class' => $class), $pagehtml));
1760 if ($pagesize and $i > $pagesize) break;
1762 $out->pushContent($list);
1763 if ($do_paging and $tokens) {
1764 $out->pushContent(HTML::table(array('class' => 'fullwidth'), $paging));
1770 // Condense list without a href links: "Page1, Page2, ..."
1771 // Alternative $seperator = HTML::Raw(' · ')
1772 // FIXME: only unique list entries, esp. with nopage
1773 function _generateCommaListAsString()
1775 if (defined($this->_options['commasep']))
1776 $seperator = $this->_options['commasep'];
1780 foreach ($this->_pages as $pagenum => $page) {
1781 if ($s = $this->_renderPageRow($page)) // some pages are not viewable
1782 $pages[] = is_string($s) ? $s : $s->asString();
1784 return HTML(join($seperator, $pages));
1788 // Normal WikiLink list.
1789 // Future: 1 = reserved for plain string (see above)
1790 // 2 and more => HTML link specialization?
1791 // FIXME: only unique list entries, esp. with nopage
1792 function _generateCommaList($style = false)
1794 if (defined($this->_options['commasep']))
1795 $seperator = HTML::Raw($this->_options['commasep']);
1799 $html->pushContent($this->_renderPageRow($this->_pages[0]));
1800 next($this->_pages);
1801 foreach ($this->_pages as $pagenum => $page) {
1802 if ($s = $this->_renderPageRow($page)) // some pages are not viewable
1803 $html->pushContent($seperator, $s);
1808 function _emptyList($caption)
1812 $html->pushContent(HTML::p($caption));
1814 if ($this->_messageIfEmpty)
1815 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
1821 /* List pages with checkboxes to select from.
1822 * The [Select] button toggles via Javascript flipAll
1825 class PageList_Selectable
1829 function PageList_Selectable($columns = false, $exclude = '', $options = false)
1832 if (!is_array($columns))
1833 $columns = explode(',', $columns);
1834 if (!in_array('checkbox', $columns))
1835 array_unshift($columns, 'checkbox');
1837 $columns = array('checkbox', 'pagename');
1839 $this->PageList($columns, $exclude, $options);
1842 function addPageList($array)
1844 while (list($pagename, $selected) = each($array)) {
1845 if ($selected) $this->addPageSelected((string)$pagename);
1846 $this->addPage((string)$pagename);
1850 function addPageSelected($pagename)
1852 $this->_selected[$pagename] = 1;
1856 class PageList_Unselectable
1860 function PageList_Unselectable($columns = false, $exclude = '', $options = false)
1863 if (!is_array($columns))
1864 $columns = explode(',', $columns);
1866 $columns = array('pagename');
1868 $this->PageList($columns, $exclude, $options);
1871 function addPageList($array)
1873 while (list($pagename, $selected) = each($array)) {
1874 if ($selected) $this->addPageSelected((string)$pagename);
1875 $this->addPage((string)$pagename);
1879 function addPageSelected($pagename)
1881 $this->_selected[$pagename] = 1;
1888 // c-basic-offset: 4
1889 // c-hanging-comment-ender-p: nil
1890 // indent-tabs-mode: nil