1 <?php rcs_id('$Id: PageList.php,v 1.62 2004-03-08 19:30:01 rurban Exp $');
4 * List a number of pagenames, optionally as table with various columns.
5 * This library relieves some work for these plugins:
7 * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch and more
9 * It also allows dynamic expansion of those plugins to include more
10 * columns in their output.
12 * Column 'info=' arguments:
14 * 'pagename' _("Page Name")
15 * 'mtime' _("Last Modified")
17 * 'summary' _("Last Summary")
18 * 'version' _("Version")),
19 * 'author' _("Last Author")),
20 * 'locked' _("Locked"), _("locked")
21 * 'minor' _("Minor Edit"), _("minor")
22 * 'markup' _("Markup")
24 * 'owner' _("Owner"), //todo: implement this again for PagePerm
25 * 'group' _("Group"), //todo: implement this for PagePerm
27 * More columns for special plugins:
28 * Todo: move this admin action away, not really an info column
29 * 'checkbox' A selectable checkbox appears at the left.
30 * 'remove' _("Remove")
31 * 'perm' _("Permission Mask")
33 * 'renamed_pagename' _("Rename to")
35 * Symbolic 'info=' arguments:
36 * 'all' All columns except remove, content and renamed_pagename
37 * 'most' pagename, mtime, author, size, hits, ...
38 * 'some' pagename, mtime, author
40 * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
41 * number-of-revision. Note the _ctime and _cauthor as they were implemented
42 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
43 * database. If lots of revisions have been made to a page, it's more than likely
44 * that some older revisions (include revision 1) have been cleaned (deleted).
47 * limit, offset, rows arguments for multiple pages/multiple rows.
49 * check PagePerm "list" access-type
51 * ->supportedArgs() which arguments are supported, so that the plugin
52 * doesn't explictly need to declare it
54 class _PageList_Column_base {
55 var $_tdattr = array();
57 function _PageList_Column_base ($default_heading, $align = false) {
58 $this->_heading = $default_heading;
61 // align="char" isn't supported by any browsers yet :(
62 //if (is_array($align))
63 // $this->_tdattr = $align;
65 $this->_tdattr['align'] = $align;
69 function format ($pagelist, $page_handle, &$revision_handle) {
70 return HTML::td($this->_tdattr,
72 $this->_getValue($page_handle, $revision_handle),
76 function setHeading ($heading) {
77 $this->_heading = $heading;
82 if (in_array($this->_field,PageList::sortable_columns())) {
83 // multiple comma-delimited sortby args: "+hits,+pagename"
84 // asc or desc: +pagename, -pagename
85 $sortby = PageList::sortby($this->_field,'flip_order');
86 //Fixme: pass all also other GET args along. (limit, p[])
87 $s = HTML::a(array('href' =>
88 $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby,
90 'class' => 'pagetitle',
91 'title' => sprintf(_("Sort by %s"),$this->_field)),
92 HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
94 $s = HTML(HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
96 return HTML::th(array('align' => 'center'),$s);
101 function button_heading () {
102 global $Theme, $request;
104 if (in_array($this->_field,PageList::sortable_columns())) {
105 // multiple comma-delimited sortby args: "+hits,+pagename"
107 if ($request->getArg('sortby')) {
108 if (PageList::sortby($this->_field,'check')) { // show icon?
109 $sortby = PageList::sortby($request->getArg('sortby'),'flip_order');
110 $request->setArg('sortby',$sortby);
111 $desc = (substr($sortby,0,1) == '-'); // asc or desc? (+pagename, -pagename)
112 $src = $Theme->getButtonURL($desc ? 'asc_order' : 'desc_order');
114 $sortby = PageList::sortby($this->_field,'init');
117 $sortby = PageList::sortby($this->_field,'init');
119 $s = HTML::a(array('href' =>
120 //Fixme: pass all also other GET args along. (limit, p[])
121 //Fixme: convert to POST submit[sortby]
122 $request->GetURLtoSelf(array('sortby' => $sortby,
124 'class' => 'gridbutton',
125 'title' => sprintf(_("Click to sort by %s"),$this->_field)),
128 $src ? HTML::img(array('src' => $src,
131 'alt' => _("Click to reverse sort order")))
132 : HTML::img(array('src' => $Theme->getButtonURL('no_order'),
135 'alt' => _("Click to sort"))));
137 $s = $this->_heading;
139 return HTML::th(array('align' => 'center', 'class' => 'gridbutton'),
140 HTML::raw(' '), $s, HTML::raw(' '));
144 class _PageList_Column extends _PageList_Column_base {
145 function _PageList_Column ($field, $default_heading, $align = false) {
146 $this->_PageList_Column_base($default_heading, $align);
148 $this->_need_rev = substr($field, 0, 4) == 'rev:';
149 if ($this->_need_rev)
150 $this->_field = substr($field, 4);
152 $this->_field = $field;
155 function _getValue ($page_handle, &$revision_handle) {
156 if ($this->_need_rev) {
157 if (!$revision_handle)
158 $revision_handle = $page_handle->getCurrentRevision();
159 return $revision_handle->get($this->_field);
162 return $page_handle->get($this->_field);
167 class _PageList_Column_size extends _PageList_Column {
168 function _getValue ($page_handle, &$revision_handle) {
169 if (!$revision_handle)
170 $revision_handle = $page_handle->getCurrentRevision();
171 return $this->_getSize($revision_handle);
174 function _getSize($revision_handle) {
175 $bytes = strlen($revision_handle->_data['%content']);
176 return ByteFormatter($bytes);
181 class _PageList_Column_bool extends _PageList_Column {
182 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
183 $this->_PageList_Column($field, $default_heading, 'center');
184 $this->_textIfTrue = $text;
185 $this->_textIfFalse = new RawXml('—'); //mdash
188 function _getValue ($page_handle, &$revision_handle) {
189 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
190 return $val ? $this->_textIfTrue : $this->_textIfFalse;
194 class _PageList_Column_checkbox extends _PageList_Column {
195 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
196 $this->_name = $name;
197 $heading = HTML::input(array('type' => 'button',
198 'title' => _("Click to de-/select all pages"),
200 'name' => $default_heading,
201 'value' => $default_heading,
202 'onclick' => "flipAll(this.form)"
204 $this->_PageList_Column($field, $heading, 'center');
206 function _getValue ($pagelist, $page_handle, &$revision_handle) {
207 $pagename = $page_handle->getName();
208 if (!empty($pagelist->_selected[$pagename])) {
209 return HTML::input(array('type' => 'checkbox',
210 'name' => $this->_name . "[$pagename]",
212 'checked' => 'CHECKED'));
214 return HTML::input(array('type' => 'checkbox',
215 'name' => $this->_name . "[$pagename]",
219 function format ($pagelist, $page_handle, &$revision_handle) {
220 return HTML::td($this->_tdattr,
222 $this->_getValue($pagelist, $page_handle, $revision_handle),
223 HTML::raw(' '));
227 class _PageList_Column_time extends _PageList_Column {
228 function _PageList_Column_time ($field, $default_heading) {
229 $this->_PageList_Column($field, $default_heading, 'right');
231 $this->Theme = &$Theme;
234 function _getValue ($page_handle, &$revision_handle) {
235 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
236 return $this->Theme->formatDateTime($time);
240 class _PageList_Column_version extends _PageList_Column {
241 function _getValue ($page_handle, &$revision_handle) {
242 if (!$revision_handle)
243 $revision_handle = $page_handle->getCurrentRevision();
244 return $revision_handle->getVersion();
248 // If needed this could eventually become a subclass
249 // of a new _PageList_Column_action class for other actions.
250 // only for WikiAdminRemove or WikiAdminSelect
251 class _PageList_Column_remove extends _PageList_Column {
252 function _getValue ($page_handle, &$revision_handle) {
253 return Button(array('action' => 'remove'), _("Remove"),
254 $page_handle->getName());
258 // only for WikiAdminRename
259 class _PageList_Column_renamed_pagename extends _PageList_Column {
260 function _getValue ($page_handle, &$revision_handle) {
261 $post_args = $GLOBALS['request']->getArg('admin_rename');
262 $value = str_replace($post_args['from'], $post_args['to'],$page_handle->getName());
263 $div = HTML::div(" => ",HTML::input(array('type' => 'text',
264 'name' => 'rename[]',
265 'value' => $value)));
266 $new_page = $GLOBALS['request']->getPage($value);
267 if ($new_page->exists()) {
268 $div->setAttr('class','error');
269 $div->setAttr('title',_("This page already exists"));
275 // Output is hardcoded to limit of first 50 bytes. Otherwise
276 // on very large Wikis this will fail if used with AllPages
277 // (PHP memory limit exceeded)
278 class _PageList_Column_content extends _PageList_Column {
279 function _PageList_Column_content ($field, $default_heading, $align = false) {
280 _PageList_Column::_PageList_Column($field, $default_heading, $align);
282 if ($field == 'content') {
283 $this->_heading .= sprintf(_(" ... first %d bytes"),
285 } elseif ($field == 'hi_content') {
286 if (!empty($_POST['admin_replace'])) {
287 $search = $_POST['admin_replace']['from'];
288 $this->_heading .= sprintf(_(" ... around %s"),
293 function _getValue ($page_handle, &$revision_handle) {
294 if (!$revision_handle)
295 $revision_handle = $page_handle->getCurrentRevision();
296 // Not sure why implode is needed here, I thought
297 // getContent() already did this, but it seems necessary.
298 $c = implode("\n", $revision_handle->getContent());
299 if ($this->_field == 'hi_content') {
300 $search = $_POST['admin_replace']['from'];
301 if ($search and ($i = strpos($c,$search))) {
302 $l = strlen($search);
303 $j = max(0,$i - ($this->bytes / 2));
304 return HTML::div(array('style' => 'font-size:x-small'),
305 HTML::div(array('class' => 'transclusion'),
306 HTML::span(substr($c, $j, ($this->bytes / 2))),
307 HTML::span(array("style"=>"background:yellow"),$search),
308 HTML::span(substr($c, $i+$l, ($this->bytes / 2))))
311 $c = sprintf(_("%s not found"),
313 return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
316 } elseif (($len = strlen($c)) > $this->bytes) {
317 $c = substr($c, 0, $this->bytes);
319 include_once('lib/BlockParser.php');
320 // false --> don't bother processing hrefs for embedded WikiLinks
321 $ct = TransformText($c, $revision_handle->get('markup'), false);
322 return HTML::div(array('style' => 'font-size:x-small'),
323 HTML::div(array('class' => 'transclusion'), $ct),
324 // Don't show bytes here if size column present too
325 ($this->parent->_columns_seen['size'] or !$len) ? "" :
326 ByteFormatter($len, /*$longformat = */true));
330 class _PageList_Column_author extends _PageList_Column {
331 function _PageList_Column_author ($field, $default_heading, $align = false) {
332 _PageList_Column::_PageList_Column($field, $default_heading, $align);
333 global $WikiNameRegexp, $request;
334 $this->WikiNameRegexp = $WikiNameRegexp;
335 $this->dbi = &$request->getDbh();
338 function _getValue ($page_handle, &$revision_handle) {
339 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
340 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
341 return WikiLink($author);
347 class _PageList_Column_perm extends _PageList_Column {
348 function _getValue ($page_handle, &$revision_handle) {
349 $perm_array = pagePermissions($page_handle->_pagename);
350 return pagePermissionsSimpleFormat($perm_array,
351 $page_handle->get('author'),$page_handle->get('group'));
354 var_dump($perm_array);
355 $xml = ob_get_contents();
362 class _PageList_Column_acl extends _PageList_Column {
363 function _getValue ($page_handle, &$revision_handle) {
364 $perm_tree = pagePermissions($page_handle->_pagename);
365 return pagePermissionsAclFormat($perm_tree);
368 var_dump($perm_array);
369 $xml = ob_get_contents();
376 class _PageList_Column_pagename extends _PageList_Column_base {
377 var $_field = 'pagename';
379 function _PageList_Column_pagename () {
380 $this->_PageList_Column_base(_("Page Name"));
382 $this->dbi = &$request->getDbh();
385 function _getValue ($page_handle, &$revision_handle) {
386 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
387 return WikiLink($page_handle);
389 return WikiLink($page_handle, 'unknown');
396 var $_group_rows = 3;
397 var $_columns = array();
398 var $_excluded_pages = array();
399 var $_rows = array();
401 var $_pagename_seen = false;
402 var $_types = array();
403 var $_options = array();
404 var $_selected = array();
405 var $_sortby = array();
407 function PageList ($columns = false, $exclude = false, $options = false) {
408 $this->_initAvailableColumns();
411 'all' => array_diff(array_keys($this->_types), // all but...
412 array('checkbox','remove','renamed_pagename',
413 'content','hi_content','perm','acl')),
414 'most' => array('pagename','mtime','author','size','hits'),
415 'some' => array('pagename','mtime','author')
418 if (!is_array($columns))
419 $columns = explode(',', $columns);
420 // expand symbolic columns:
421 foreach ($symbolic_columns as $symbol => $cols) {
422 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
423 $columns = array_diff(array_merge($columns,$cols),array($symbol));
426 if (!in_array('pagename',$columns))
427 $this->_addColumn('pagename');
428 foreach ($columns as $col) {
429 $this->_addColumn($col);
432 $this->_addColumn('pagename');
435 if (!is_array($exclude))
436 $exclude = $this->explodePageList($exclude);
437 $this->_excluded_pages = $exclude;
440 $this->_options = $options;
441 if (!empty($options) and !empty($options['sortby'])) {
442 $this->sortby($options['sortby'],'init');
444 $this->sortby($GLOBALS['request']->getArg('sortby'),'init');
447 $this->_messageIfEmpty = _("<no matches>");
450 // Currently PageList takes these arguments:
451 // 1: info, 2: exclude, 3: hash of options
452 // Here we declare which options are supported, so that
453 // the calling plugin may simply merge this with its own default arguments
454 function supportedArgs () {
455 return array(//Currently supported options:
456 'info' => 'pagename',
457 'exclude' => '', // also wildcards and comma-seperated lists
459 // for the sort buttons in <th>
460 'sortby' => '', // same as for WikiDB::getAllPages
462 //PageList pager options:
463 // These options may also be given to _generate(List|Table) later
464 // But limit and offset might help the query WikiDB::getAllPages()
465 //cols => 1, // side-by-side display of list (1-3)
466 //limit => 50, // length of one column
467 //offset => 0, // needed internally for the pager
468 //paging => 'auto', // '': normal paging mode
469 // // 'auto': drop 'info' columns and enhance rows
470 // // when the list becomes large
471 // // 'none': don't page at all
475 function setCaption ($caption_string) {
476 $this->_caption = $caption_string;
479 function getCaption () {
480 // put the total into the caption if needed
481 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
482 return sprintf($this->_caption, $this->getTotal());
483 return $this->_caption;
486 function setMessageIfEmpty ($msg) {
487 $this->_messageIfEmpty = $msg;
491 function getTotal () {
492 return count($this->_rows);
495 function isEmpty () {
496 return empty($this->_rows);
499 function addPage ($page_handle) {
500 if (is_string($page_handle)) {
501 if (empty($page_handle)) return;
502 if (in_array($page_handle, $this->_excluded_pages))
503 return; // exclude page.
504 $dbi = $GLOBALS['request']->getDbh();
505 $page_handle = $dbi->getPage($page_handle);
506 } elseif (is_object($page_handle)) {
507 if (in_array($page_handle->getName(), $this->_excluded_pages))
508 return; // exclude page.
511 $group = (int)(count($this->_rows) / $this->_group_rows);
512 $class = ($group % 2) ? 'oddrow' : 'evenrow';
513 $revision_handle = false;
515 if (count($this->_columns) > 1) {
516 $row = HTML::tr(array('class' => $class));
517 foreach ($this->_columns as $col)
518 $row->pushContent($col->format($this, $page_handle, $revision_handle));
521 $col = $this->_columns[0];
522 $row = HTML::li(array('class' => $class),
523 $col->_getValue($page_handle, $revision_handle));
526 $this->_rows[] = $row;
529 function addPages ($page_iter) {
530 while ($page = $page_iter->next())
531 $this->addPage($page);
534 function addPageList (&$list) {
536 while ($page = next($list))
537 $this->addPage($page);
540 function getContent() {
541 // Note that the <caption> element wants inline content.
542 $caption = $this->getCaption();
544 if ($this->isEmpty())
545 return $this->_emptyList($caption);
546 elseif (count($this->_columns) == 1)
547 return $this->_generateList($caption);
549 return $this->_generateTable($caption);
552 function printXML() {
553 PrintXML($this->getContent());
557 return AsXML($this->getContent());
560 function sortable_columns() {
561 return array('pagename','mtime','hits');
565 * Handles sortby requests for the DB iterator and table header links
566 * Prefix the column with + or - like "+pagename","-mtime", ...
567 * supported columns: 'pagename','mtime','hits'
568 * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
569 * 'db' "-pagename" => "pagename DESC"
571 function sortby ($column, $action) {
572 if (empty($column)) return;
573 //support multiple comma-delimited sortby args: "+hits,+pagename"
574 if (strstr($column,',')) {
576 foreach (explode(',',$column) as $col) {
577 $result[] = $this->sortby($col,$action);
579 return join(",",$result);
581 if (substr($column,0,1) == '+') {
582 $order = '+'; $column = substr($column,1);
583 } elseif (substr($column,0,1) == '-') {
584 $order = '-'; $column = substr($column,1);
586 if (in_array($column,PageList::sortable_columns())) {
587 // default order: +pagename, -mtime, -hits
589 if (in_array($column,array('mtime','hits')))
593 if ($action == 'flip_order') {
594 return ($order == '+' ? '-' : '+') . $column;
595 } elseif ($action == 'init') {
596 $this->_sortby[$column] = $order;
597 return $order . $column;
598 } elseif ($action == 'check') {
599 return (!empty($this->_sortby[$column]) or
600 ($GLOBALS['request']->getArg('sortby') and
601 strstr($GLOBALS['request']->getArg('sortby'),$column)));
602 } elseif ($action == 'db') {
603 // asc or desc: +pagename, -pagename
604 return $column . ($order == '+' ? ' ASC' : ' DESC');
610 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
611 function explodePageList($input, $perm = false, $sortby=false, $limit=false) {
612 // expand wildcards from list of all pages
613 if (preg_match('/[\?\*]/',$input)) {
614 $dbi = $GLOBALS['request']->getDbh();
615 $allPagehandles = $dbi->getAllPages($perm,$sortby,$limit);
616 while ($pagehandle = $allPagehandles->next()) {
617 $allPages[] = $pagehandle->getName();
619 return explodeList($input, $allPages);
621 //TODO: do the sorting, normally not needed if used for exclude only
622 return explode(',',$input);
630 //Performance Fixme: Initialize only the requested objects
631 function _initAvailableColumns() {
632 if (!empty($this->_types))
638 => new _PageList_Column_content('rev:content', _("Content")),
639 'hi_content' // with highlighted search for SearchReplace
640 => new _PageList_Column_content('rev:hi_content', _("Content")),
642 => new _PageList_Column_remove('remove', _("Remove")),
644 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
646 => new _PageList_Column_perm('perm', _("Permission")),
648 => new _PageList_Column_acl('acl', _("ACL")),
650 => new _PageList_Column_checkbox('p', _("Select")),
652 => new _PageList_Column_pagename,
654 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
656 => new _PageList_Column('hits', _("Hits"), 'right'),
658 => new _PageList_Column_size('rev:size', _("Size"), 'right'),
659 /*array('align' => 'char', 'char' => ' ')*/
661 => new _PageList_Column('rev:summary', _("Last Summary")),
663 => new _PageList_Column_version('rev:version', _("Version"),
666 => new _PageList_Column_author('rev:author', _("Last Author")),
668 => new _PageList_Column_author('owner', _("Owner")),
670 => new _PageList_Column_author('group', _("Group")),
672 => new _PageList_Column_bool('locked', _("Locked"),
675 => new _PageList_Column_bool('rev:is_minor_edit',
676 _("Minor Edit"), _("minor")),
678 => new _PageList_Column('rev:markup', _("Markup"))
682 function _addColumn ($column) {
684 $this->_initAvailableColumns();
686 if (isset($this->_columns_seen[$column]))
687 return false; // Already have this one.
688 $this->_columns_seen[$column] = true;
690 if (strstr($column, ':'))
691 list ($column, $heading) = explode(':', $column, 2);
693 if (!isset($this->_types[$column])) {
694 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
698 $col = $this->_types[$column];
699 if (!empty($heading))
700 $col->setHeading($heading);
702 $this->_columns[] = $col;
707 // make a table given the caption
708 function _generateTable($caption) {
709 $table = HTML::table(array('cellpadding' => 0,
712 'class' => 'pagelist'));
714 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
716 //Warning: This is quite fragile. It depends solely on a private variable
718 if (in_array('checkbox',$this->_columns_seen)) {
719 $table->pushContent($this->_jsFlipAll());
722 $table_summary = array();
723 foreach ($this->_columns as $col) {
724 $row->pushContent($col->button_heading());
725 if (is_string($col->_heading))
726 $table_summary[] = $col->_heading;
728 // Table summary for non-visual browsers.
729 $table->setAttr('summary', sprintf(_("Columns: %s."),
730 implode(", ", $table_summary)));
732 $table->pushContent(HTML::thead($row),
733 HTML::tbody(false, $this->_rows));
737 function _jsFlipAll() {
739 function flipAll(formObj) {
741 for (var i=0;i < formObj.length;i++) {
742 fldObj = formObj.elements[i];
743 if (fldObj.type == 'checkbox') {
744 if (isFirstSet == -1)
745 isFirstSet = (fldObj.checked) ? true : false;
746 fldObj.checked = (isFirstSet) ? false : true;
752 function _generateList($caption) {
753 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
755 //Warning: This is quite fragile. It depends solely on a private variable
757 // questionable if its of use here anyway. this is a one-col list only.
758 if (in_array('checkbox',$this->_columns_seen)) {
759 $out->pushContent($this->_jsFlipAll());
762 $out->pushContent(HTML::p($caption));
763 $out->pushContent($list);
767 function _emptyList($caption) {
770 $html->pushContent(HTML::p($caption));
771 if ($this->_messageIfEmpty)
772 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
778 /* List pages with checkboxes to select from.
779 * The [Select] button toggles via _jsFlipAll
782 class PageList_Selectable
785 function PageList_Selectable ($columns=false, $exclude=false) {
787 if (!is_array($columns))
788 $columns = explode(',', $columns);
789 if (!in_array('checkbox',$columns))
790 array_unshift($columns,'checkbox');
792 $columns = array('checkbox','pagename');
794 PageList::PageList($columns,$exclude);
797 function addPageList ($array) {
798 while (list($pagename,$selected) = each($array)) {
799 if ($selected) $this->addPageSelected($pagename);
800 $this->addPage($pagename);
804 function addPageSelected ($pagename) {
805 $this->_selected[$pagename] = 1;
808 //insert javascript when clicked on Selected Select/Deselect all
811 // (c-file-style: "gnu")
816 // c-hanging-comment-ender-p: nil
817 // indent-tabs-mode: nil