1 <?php rcs_id('$Id: PageList.php,v 1.58 2004-02-24 15:20:05 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,
89 'nopurge' => 'cache')),
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);
100 function button_heading () {
101 global $Theme, $request;
103 if (in_array($this->_field,PageList::sortable_columns())) {
104 // multiple comma-delimited sortby args: "+hits,+pagename"
106 if ($request->getArg('sortby')) {
107 if (PageList::sortby($this->_field,'check')) { // show icon?
108 $sortby = PageList::sortby($request->getArg('sortby'),'flip_order');
109 $request->setArg('sortby',$sortby);
110 $desc = (substr($sortby,0,1) == '-'); // asc or desc? (+pagename, -pagename)
111 $src = $Theme->getButtonURL($desc ? 'asc_order' : 'desc_order');
113 $sortby = PageList::sortby($this->_field,'init');
116 $sortby = PageList::sortby($this->_field,'init');
118 $s = HTML::a(array('href' =>
119 //Fixme: pass all also other GET args along. (limit, p[])
120 $request->GetURLtoSelf(array('sortby' => $sortby,
121 'nopurge' => 'cache')),
122 'class' => 'gridbutton',
123 'title' => sprintf(_("Sort by %s"),$this->_field)),
125 $src ? HTML(HTML::raw(' '),
126 HTML::img(array('src' => $src,
129 'alt' => _("Click to reverse sort order"))))
132 $s = $this->_heading;
134 return HTML::th(array('align' => 'center', 'class' => 'gridbutton'),
135 HTML::raw(' '), $s, HTML::raw(' '));
139 class _PageList_Column extends _PageList_Column_base {
140 function _PageList_Column ($field, $default_heading, $align = false) {
141 $this->_PageList_Column_base($default_heading, $align);
143 $this->_need_rev = substr($field, 0, 4) == 'rev:';
144 if ($this->_need_rev)
145 $this->_field = substr($field, 4);
147 $this->_field = $field;
150 function _getValue ($page_handle, &$revision_handle) {
151 if ($this->_need_rev) {
152 if (!$revision_handle)
153 $revision_handle = $page_handle->getCurrentRevision();
154 return $revision_handle->get($this->_field);
157 return $page_handle->get($this->_field);
162 class _PageList_Column_size extends _PageList_Column {
163 function _getValue ($page_handle, &$revision_handle) {
164 if (!$revision_handle)
165 $revision_handle = $page_handle->getCurrentRevision();
166 return $this->_getSize($revision_handle);
169 function _getSize($revision_handle) {
170 $bytes = strlen($revision_handle->_data['%content']);
171 return ByteFormatter($bytes);
176 class _PageList_Column_bool extends _PageList_Column {
177 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
178 $this->_PageList_Column($field, $default_heading, 'center');
179 $this->_textIfTrue = $text;
180 $this->_textIfFalse = new RawXml('—'); //mdash
183 function _getValue ($page_handle, &$revision_handle) {
184 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
185 return $val ? $this->_textIfTrue : $this->_textIfFalse;
189 class _PageList_Column_checkbox extends _PageList_Column {
190 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
191 $this->_name = $name;
192 $heading = HTML::input(array('type' => 'button',
193 'title' => _("Click to de-/select all pages"),
195 'name' => $default_heading,
196 'value' => $default_heading,
197 'onclick' => "flipAll(this.form)"
199 $this->_PageList_Column($field, $heading, 'center');
201 function _getValue ($pagelist, $page_handle, &$revision_handle) {
202 $pagename = $page_handle->getName();
203 if (!empty($pagelist->_selected[$pagename])) {
204 return HTML::input(array('type' => 'checkbox',
205 'name' => $this->_name . "[$pagename]",
206 'value' => $pagename,
207 'checked' => 'CHECKED'));
209 return HTML::input(array('type' => 'checkbox',
210 'name' => $this->_name . "[$pagename]",
211 'value' => $pagename));
214 function format ($pagelist, $page_handle, &$revision_handle) {
215 return HTML::td($this->_tdattr,
217 $this->_getValue($pagelist, $page_handle, $revision_handle),
218 HTML::raw(' '));
222 class _PageList_Column_time extends _PageList_Column {
223 function _PageList_Column_time ($field, $default_heading) {
224 $this->_PageList_Column($field, $default_heading, 'right');
226 $this->Theme = &$Theme;
229 function _getValue ($page_handle, &$revision_handle) {
230 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
231 return $this->Theme->formatDateTime($time);
235 class _PageList_Column_version extends _PageList_Column {
236 function _getValue ($page_handle, &$revision_handle) {
237 if (!$revision_handle)
238 $revision_handle = $page_handle->getCurrentRevision();
239 return $revision_handle->getVersion();
243 // If needed this could eventually become a subclass
244 // of a new _PageList_Column_action class for other actions.
245 // only for WikiAdminRemove or WikiAdminSelect
246 class _PageList_Column_remove extends _PageList_Column {
247 function _getValue ($page_handle, &$revision_handle) {
248 return Button(array('action' => 'remove'), _("Remove"),
249 $page_handle->getName());
253 // only for WikiAdminRename
254 class _PageList_Column_renamed_pagename extends _PageList_Column {
255 function _getValue ($page_handle, &$revision_handle) {
256 $post_args = $GLOBALS['request']->getArg('admin_rename');
257 $value = str_replace($post_args['from'], $post_args['to'],$page_handle->getName());
258 return HTML::div(" => ",HTML::input(array('type' => 'text',
259 'name' => 'rename[]',
260 'value' => $value)));
264 // Output is hardcoded to limit of first 50 bytes. Otherwise
265 // on very large Wikis this will fail if used with AllPages
266 // (PHP memory limit exceeded)
267 class _PageList_Column_content extends _PageList_Column {
268 function _PageList_Column_content ($field, $default_heading, $align = false) {
269 _PageList_Column::_PageList_Column($field, $default_heading, $align);
271 if ($field == 'content') {
272 $this->_heading .= sprintf(_(" ... first %d bytes"),
274 } elseif ($field == 'hi_content') {
275 if (!empty($_POST['admin_replace'])) {
276 $search = $_POST['admin_replace']['from'];
277 $this->_heading .= sprintf(_(" ... around %s"),
282 function _getValue ($page_handle, &$revision_handle) {
283 if (!$revision_handle)
284 $revision_handle = $page_handle->getCurrentRevision();
285 // Not sure why implode is needed here, I thought
286 // getContent() already did this, but it seems necessary.
287 $c = implode("\n", $revision_handle->getContent());
288 if ($this->_field == 'hi_content') {
289 $search = $_POST['admin_replace']['from'];
290 if ($search and ($i = strpos($c,$search))) {
291 $l = strlen($search);
292 $j = max(0,$i - ($this->bytes / 2));
293 return HTML::div(array('style' => 'font-size:x-small'),
294 HTML::div(array('class' => 'transclusion'),
295 HTML::span(substr($c, $j, ($this->bytes / 2))),
296 HTML::span(array("style"=>"background:yellow"),$search),
297 HTML::span(substr($c, $i+$l, ($this->bytes / 2))))
300 $c = sprintf(_("%s not found"),
302 return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
305 } elseif (($len = strlen($c)) > $this->bytes) {
306 $c = substr($c, 0, $this->bytes);
308 include_once('lib/BlockParser.php');
309 // false --> don't bother processing hrefs for embedded WikiLinks
310 $ct = TransformText($c, $revision_handle->get('markup'), false);
311 return HTML::div(array('style' => 'font-size:x-small'),
312 HTML::div(array('class' => 'transclusion'), $ct),
313 // Don't show bytes here if size column present too
314 ($this->parent->_columns_seen['size'] or !$len) ? "" :
315 ByteFormatter($len, /*$longformat = */true));
319 class _PageList_Column_author extends _PageList_Column {
320 function _PageList_Column_author ($field, $default_heading, $align = false) {
321 _PageList_Column::_PageList_Column($field, $default_heading, $align);
322 global $WikiNameRegexp, $request;
323 $this->WikiNameRegexp = $WikiNameRegexp;
324 $this->dbi = &$request->getDbh();
327 function _getValue ($page_handle, &$revision_handle) {
328 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
329 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
330 return WikiLink($author);
336 class _PageList_Column_perm extends _PageList_Column {
337 function _getValue ($page_handle, &$revision_handle) {
338 $perm_array = pagePermissions($page_handle->_pagename);
339 return pagePermissionsSimpleFormat($perm_array,
340 $page_handle->get('author'),$page_handle->get('group'));
343 var_dump($perm_array);
344 $xml = ob_get_contents();
351 class _PageList_Column_acl extends _PageList_Column {
352 function _getValue ($page_handle, &$revision_handle) {
353 $perm_tree = pagePermissions($page_handle->_pagename);
354 return pagePermissionsAclFormat($perm_tree);
357 var_dump($perm_array);
358 $xml = ob_get_contents();
365 class _PageList_Column_pagename extends _PageList_Column_base {
366 var $_field = 'pagename';
368 function _PageList_Column_pagename () {
369 $this->_PageList_Column_base(_("Page Name"));
371 $this->dbi = &$request->getDbh();
374 function _getValue ($page_handle, &$revision_handle) {
375 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
376 return WikiLink($page_handle);
378 return WikiLink($page_handle, 'unknown');
385 var $_group_rows = 3;
386 var $_columns = array();
387 var $_excluded_pages = array();
388 var $_rows = array();
390 var $_pagename_seen = false;
391 var $_types = array();
392 var $_options = array();
393 var $_selected = array();
394 var $_sortby = array();
396 function PageList ($columns = false, $exclude = false, $options = false) {
397 $this->_initAvailableColumns();
400 'all' => array_diff(array_keys($this->_types), // all but...
401 array('checkbox','remove','renamed_pagename',
402 'content','hi_content','perm','acl')),
403 'most' => array('pagename','mtime','author','size','hits'),
404 'some' => array('pagename','mtime','author')
407 if (!is_array($columns))
408 $columns = explode(',', $columns);
409 // expand symbolic columns:
410 foreach ($symbolic_columns as $symbol => $cols) {
411 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
412 $columns = array_diff(array_merge($columns,$cols),array($symbol));
415 if (!in_array('pagename',$columns))
416 $this->_addColumn('pagename');
417 foreach ($columns as $col) {
418 $this->_addColumn($col);
421 $this->_addColumn('pagename');
424 if (!is_array($exclude))
425 $exclude = explode(',', $exclude);
426 $this->_excluded_pages = $exclude;
429 $this->_options = $options;
430 if (!empty($options) and !empty($options['sortby'])) {
431 $this->sortby($options['sortby'],'init');
433 $this->sortby($GLOBALS['request']->getArg('sortby'),'init');
436 $this->_messageIfEmpty = _("<no matches>");
439 // Currently PageList takes these arguments:
440 // 1: info, 2: exclude, 3: hash of options
441 // Here we declare which options are supported, so that
442 // the calling plugin may simply merge this with its own default arguments
443 function supportedArgs () {
444 return array(//Currently supported options:
445 'info' => 'pagename',
446 'exclude' => '', // also wildcards and comma-seperated lists
448 // for the sort buttons in <th>
449 'sortby' => '', // same as for WikiDB::getAllPages
451 //PageList pager options:
452 // These options may also be given to _generate(List|Table) later
453 // But limit and offset might help the query WikiDB::getAllPages()
454 //cols => 1, // side-by-side display of list (1-3)
455 //limit => 50, // length of one column
456 //offset => 0, // needed internally for the pager
457 //paging => 'auto', // '': normal paging mode
458 // // 'auto': drop 'info' columns and enhance rows
459 // // when the list becomes large
460 // // 'none': don't page at all
464 function setCaption ($caption_string) {
465 $this->_caption = $caption_string;
468 function getCaption () {
469 // put the total into the caption if needed
470 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
471 return sprintf($this->_caption, $this->getTotal());
472 return $this->_caption;
475 function setMessageIfEmpty ($msg) {
476 $this->_messageIfEmpty = $msg;
480 function getTotal () {
481 return count($this->_rows);
484 function isEmpty () {
485 return empty($this->_rows);
488 function addPage ($page_handle) {
489 if (is_string($page_handle)) {
490 if (in_array($page_handle, $this->_excluded_pages))
491 return; // exclude page.
492 $dbi = $GLOBALS['request']->getDbh();
493 $page_handle = $dbi->getPage($page_handle);
494 } elseif (is_object($page_handle)) {
495 if (in_array($page_handle->getName(), $this->_excluded_pages))
496 return; // exclude page.
499 $group = (int)(count($this->_rows) / $this->_group_rows);
500 $class = ($group % 2) ? 'oddrow' : 'evenrow';
501 $revision_handle = false;
503 if (count($this->_columns) > 1) {
504 $row = HTML::tr(array('class' => $class));
505 foreach ($this->_columns as $col)
506 $row->pushContent($col->format($this, $page_handle, $revision_handle));
509 $col = $this->_columns[0];
510 $row = HTML::li(array('class' => $class),
511 $col->_getValue($page_handle, $revision_handle));
514 $this->_rows[] = $row;
517 function addPages ($page_iter) {
518 while ($page = $page_iter->next())
519 $this->addPage($page);
522 function addPageList (&$list) {
524 while ($page = next($list))
525 $this->addPage($page);
528 function getContent() {
529 // Note that the <caption> element wants inline content.
530 $caption = $this->getCaption();
532 if ($this->isEmpty())
533 return $this->_emptyList($caption);
534 elseif (count($this->_columns) == 1)
535 return $this->_generateList($caption);
537 return $this->_generateTable($caption);
540 function printXML() {
541 PrintXML($this->getContent());
545 return AsXML($this->getContent());
548 function sortable_columns() {
549 return array('pagename','mtime','hits');
553 * Handles sortby requests for the DB iterator and table header links
554 * Prefix the column with + or - like "+pagename","-mtime", ...
555 * supported columns: 'pagename','mtime','hits'
556 * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
557 * 'db' "-pagename" => "pagename DESC"
559 function sortby ($column, $action) {
560 if (empty($column)) return;
561 //support multiple comma-delimited sortby args: "+hits,+pagename"
562 if (strstr($column,',')) {
564 foreach (explode(',',$column) as $col) {
565 $result[] = $this->sortby($col,$action);
567 return join(",",$result);
569 if (substr($column,0,1) == '+') {
570 $order = '+'; $column = substr($column,1);
571 } elseif (substr($column,0,1) == '-') {
572 $order = '-'; $column = substr($column,1);
574 if (in_array($column,PageList::sortable_columns())) {
575 // default order: +pagename, -mtime, -hits
577 if (in_array($column,array('mtime','hits')))
581 if ($action == 'flip_order') {
582 return ($order == '+' ? '-' : '+') . $column;
583 } elseif ($action == 'init') {
584 $this->_sortby[$column] = $order;
585 return $order . $column;
586 } elseif ($action == 'check') {
587 return (!empty($this->_sortby[$column]) or
588 ($GLOBALS['request']->getArg('sortby') and
589 strstr($GLOBALS['request']->getArg('sortby'),$column)));
590 } elseif ($action == 'db') {
591 // asc or desc: +pagename, -pagename
592 return $column . ($order == '+' ? ' ASC' : ' DESC');
598 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
599 function explodePageList($input, $perm = false, $sortby=false, $limit=false) {
600 // expand wildcards from list of all pages
601 if (preg_match('/[\?\*]/',$input)) {
602 $dbi = $GLOBALS['request']->getDbh();
603 $allPagehandles = $dbi->getAllPages($perm,$sortby,$limit);
604 while ($pagehandle = $allPagehandles->next()) {
605 $allPages[] = $pagehandle->getName();
607 return explodeList($input, $allPages);
609 //TODO: do the sorting
610 return explode(',',$input);
618 //Performance Fixme: Initialize only the requested objects
619 function _initAvailableColumns() {
620 if (!empty($this->_types))
626 => new _PageList_Column_content('rev:content', _("Content")),
627 'hi_content' // with highlighted search for SearchReplace
628 => new _PageList_Column_content('rev:hi_content', _("Content")),
630 => new _PageList_Column_remove('remove', _("Remove")),
632 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
634 => new _PageList_Column_perm('perm', _("Permission")),
636 => new _PageList_Column_acl('acl', _("ACL")),
638 => new _PageList_Column_checkbox('p', _("Select")),
640 => new _PageList_Column_pagename,
642 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
644 => new _PageList_Column('hits', _("Hits"), 'right'),
646 => new _PageList_Column_size('rev:size', _("Size"), 'right'),
647 /*array('align' => 'char', 'char' => ' ')*/
649 => new _PageList_Column('rev:summary', _("Last Summary")),
651 => new _PageList_Column_version('rev:version', _("Version"),
654 => new _PageList_Column_author('rev:author', _("Last Author")),
656 => new _PageList_Column_author('owner', _("Owner")),
658 => new _PageList_Column_author('group', _("Group")),
660 => new _PageList_Column_bool('locked', _("Locked"),
663 => new _PageList_Column_bool('rev:is_minor_edit',
664 _("Minor Edit"), _("minor")),
666 => new _PageList_Column('rev:markup', _("Markup"))
670 function _addColumn ($column) {
672 $this->_initAvailableColumns();
674 if (isset($this->_columns_seen[$column]))
675 return false; // Already have this one.
676 $this->_columns_seen[$column] = true;
678 if (strstr($column, ':'))
679 list ($column, $heading) = explode(':', $column, 2);
681 if (!isset($this->_types[$column])) {
682 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
686 $col = $this->_types[$column];
687 if (!empty($heading))
688 $col->setHeading($heading);
690 $this->_columns[] = $col;
695 // make a table given the caption
696 function _generateTable($caption) {
697 $table = HTML::table(array('cellpadding' => 0,
700 'class' => 'pagelist'));
702 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
704 //Warning: This is quite fragile. It depends solely on a private variable
706 if (in_array('checkbox',$this->_columns_seen)) {
707 $table->pushContent($this->_jsFlipAll());
710 $table_summary = array();
711 foreach ($this->_columns as $col) {
712 $row->pushContent($col->button_heading());
713 if (is_string($col->_heading))
714 $table_summary[] = $col->_heading;
716 // Table summary for non-visual browsers.
717 $table->setAttr('summary', sprintf(_("Columns: %s."),
718 implode(", ", $table_summary)));
720 $table->pushContent(HTML::thead($row),
721 HTML::tbody(false, $this->_rows));
725 function _jsFlipAll() {
727 function flipAll(formObj) {
729 for (var i=0;i < formObj.length;i++) {
730 fldObj = formObj.elements[i];
731 if (fldObj.type == 'checkbox') {
732 if (isFirstSet == -1)
733 isFirstSet = (fldObj.checked) ? true : false;
734 fldObj.checked = (isFirstSet) ? false : true;
740 function _generateList($caption) {
741 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
743 //Warning: This is quite fragile. It depends solely on a private variable
745 if (in_array('checkbox',$this->_columns_seen)) {
746 $out->pushContent($this->_jsFlipAll());
749 $out->pushContent(HTML::p($caption));
750 $out->pushContent($list);
754 function _emptyList($caption) {
757 $html->pushContent(HTML::p($caption));
758 if ($this->_messageIfEmpty)
759 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
765 /* List pages with checkboxes to select from.
766 * The [Select] button toggles via _jsFlipAll
769 class PageList_Selectable
772 function PageList_Selectable ($columns=false, $exclude=false) {
774 if (!is_array($columns))
775 $columns = explode(',', $columns);
776 if (!in_array('checkbox',$columns))
777 array_unshift($columns,'checkbox');
779 $columns = array('checkbox','pagename');
781 PageList::PageList($columns,$exclude);
784 function addPageList ($array) {
785 while (list($pagename,$selected) = each($array)) {
786 if ($selected) $this->addPageSelected($pagename);
787 $this->addPage($pagename);
791 function addPageSelected ($pagename) {
792 $this->_selected[$pagename] = 1;
795 //insert javascript when clicked on Selected Select/Deselect all
798 // (c-file-style: "gnu")
803 // c-hanging-comment-ender-p: nil
804 // indent-tabs-mode: nil