1 <?php rcs_id('$Id: PageList.php,v 1.50 2004-02-11 19:46:54 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 * 'remove' _("Remove") //todo: move this admin action away, not really an info column
26 * 'checkbox' A selectable checkbox appears at the left.
27 * 'all' All columns except remove, content and renamed_pagename
28 * 'most' pagename, mtime, author, size, hits, ...
29 * 'some' pagename, mtime, author
31 * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
32 * number-of-revision. Note the _ctime and _cauthor as they were implemented
33 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
34 * database. If lots of revisions have been made to a page, it's more than likely
35 * that some older revisions (include revision 1) have been cleaned (deleted).
37 * TODO: limit, offset, rows arguments for multiple pages/multiple rows.
38 * check PagePerm "list" access-type
40 class _PageList_Column_base {
41 var $_tdattr = array();
43 function _PageList_Column_base ($default_heading, $align = false) {
44 $this->_heading = $default_heading;
47 // align="char" isn't supported by any browsers yet :(
48 //if (is_array($align))
49 // $this->_tdattr = $align;
51 $this->_tdattr['align'] = $align;
55 function format ($pagelist, $page_handle, &$revision_handle) {
56 return HTML::td($this->_tdattr,
58 $this->_getValue($page_handle, $revision_handle),
62 function setHeading ($heading) {
63 $this->_heading = $heading;
67 if (in_array($this->_field,array('pagename','mtime','hits'))) {
68 // multiple comma-delimited sortby args: "+hits,+pagename"
69 // asc or desc: +pagename, -pagename
70 $sortby = PageList::sortby($this->_field,'flip_order');
71 $s = HTML::a(array('href' => $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby)),'class' => 'pagetitle', 'title' => sprintf(_("Sort by %s"),$this->_field)), HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
73 $s = HTML(HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
75 return HTML::td(array('align' => 'center'),$s);
79 class _PageList_Column extends _PageList_Column_base {
80 function _PageList_Column ($field, $default_heading, $align = false) {
81 $this->_PageList_Column_base($default_heading, $align);
83 $this->_need_rev = substr($field, 0, 4) == 'rev:';
85 $this->_field = substr($field, 4);
87 $this->_field = $field;
90 function _getValue ($page_handle, &$revision_handle) {
91 if ($this->_need_rev) {
92 if (!$revision_handle)
93 $revision_handle = $page_handle->getCurrentRevision();
94 return $revision_handle->get($this->_field);
97 return $page_handle->get($this->_field);
102 class _PageList_Column_size extends _PageList_Column {
103 function _getValue ($page_handle, &$revision_handle) {
104 if (!$revision_handle)
105 $revision_handle = $page_handle->getCurrentRevision();
106 return $this->_getSize($revision_handle);
109 function _getSize($revision_handle) {
110 $bytes = strlen($revision_handle->_data['%content']);
111 return ByteFormatter($bytes);
116 class _PageList_Column_bool extends _PageList_Column {
117 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
118 $this->_PageList_Column($field, $default_heading, 'center');
119 $this->_textIfTrue = $text;
120 $this->_textIfFalse = new RawXml('—'); //mdash
123 function _getValue ($page_handle, &$revision_handle) {
124 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
125 return $val ? $this->_textIfTrue : $this->_textIfFalse;
129 class _PageList_Column_checkbox extends _PageList_Column {
130 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
131 $this->_name = $name;
132 $this->_PageList_Column($field, $default_heading, 'center');
134 function _getValue ($pagelist, $page_handle, &$revision_handle) {
135 $pagename = $page_handle->getName();
136 if (!empty($pagelist->_selected[$pagename])) {
137 return HTML::input(array('type' => 'checkbox',
138 'name' => $this->_name . "[$pagename]",
139 'value' => $pagename,
142 return HTML::input(array('type' => 'checkbox',
143 'name' => $this->_name . "[$pagename]",
144 'value' => $pagename));
147 function format ($pagelist, $page_handle, &$revision_handle) {
148 return HTML::td($this->_tdattr,
150 $this->_getValue($pagelist, $page_handle, $revision_handle),
151 HTML::raw(' '));
155 class _PageList_Column_time extends _PageList_Column {
156 function _PageList_Column_time ($field, $default_heading) {
157 $this->_PageList_Column($field, $default_heading, 'right');
159 $this->Theme = &$Theme;
162 function _getValue ($page_handle, &$revision_handle) {
163 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
164 return $this->Theme->formatDateTime($time);
168 class _PageList_Column_version extends _PageList_Column {
169 function _getValue ($page_handle, &$revision_handle) {
170 if (!$revision_handle)
171 $revision_handle = $page_handle->getCurrentRevision();
172 return $revision_handle->getVersion();
176 // If needed this could eventually become a subclass
177 // of a new _PageList_Column_action class for other actions.
178 // only for WikiAdminRemove or WikiAdminSelect
179 class _PageList_Column_remove extends _PageList_Column {
180 function _getValue ($page_handle, &$revision_handle) {
181 return Button(array('action' => 'remove'), _("Remove"),
182 $page_handle->getName());
186 // only for WikiAdminRename
187 class _PageList_Column_renamed_pagename extends _PageList_Column {
188 function _getValue ($page_handle, &$revision_handle) {
189 $post_args = $GLOBALS['request']->getArg('admin_rename');
190 $value = str_replace($post_args['from'], $post_args['to'],$page_handle->getName());
191 return HTML::div(" => ",HTML::input(array('type' => 'text',
192 'name' => 'rename[]',
193 'value' => $value)));
197 // Output is hardcoded to limit of first 50 bytes. Otherwise
198 // on very large Wikis this will fail if used with AllPages
199 // (PHP memory limit exceeded)
200 class _PageList_Column_content extends _PageList_Column {
201 function _PageList_Column_content ($field, $default_heading, $align = false) {
202 _PageList_Column::_PageList_Column($field, $default_heading, $align);
204 $this->_heading .= sprintf(_(" ... first %d bytes"),
207 function _getValue ($page_handle, &$revision_handle) {
208 if (!$revision_handle)
209 $revision_handle = $page_handle->getCurrentRevision();
210 // Not sure why implode is needed here, I thought
211 // getContent() already did this, but it seems necessary.
212 $c = implode("\n", $revision_handle->getContent());
213 if (($len = strlen($c)) > $this->bytes) {
214 $c = substr($c, 0, $this->bytes);
216 include_once('lib/BlockParser.php');
217 // false --> don't bother processing hrefs for embedded WikiLinks
218 $ct = TransformText($c, $revision_handle->get('markup'), false);
219 return HTML::div(array('style' => 'font-size:xx-small'),
220 HTML::div(array('class' => 'transclusion'), $ct),
221 // TODO: Don't show bytes here if size column present too
222 /* Howto??? $this->parent->_columns['size'] ? "" :*/
223 ByteFormatter($len, /*$longformat = */true));
227 class _PageList_Column_author extends _PageList_Column {
228 function _PageList_Column_author ($field, $default_heading, $align = false) {
229 _PageList_Column::_PageList_Column($field, $default_heading, $align);
230 global $WikiNameRegexp, $request;
231 $this->WikiNameRegexp = $WikiNameRegexp;
232 $this->dbi = &$request->getDbh();
235 function _getValue ($page_handle, &$revision_handle) {
236 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
237 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
238 return WikiLink($author);
244 class _PageList_Column_pagename extends _PageList_Column_base {
245 var $_field = 'pagename';
247 function _PageList_Column_pagename () {
248 $this->_PageList_Column_base(_("Page Name"));
250 $this->dbi = &$request->getDbh();
253 function _getValue ($page_handle, &$revision_handle) {
254 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
255 return WikiLink($page_handle);
257 return WikiLink($page_handle, 'unknown');
264 var $_group_rows = 3;
265 var $_columns = array();
266 var $_excluded_pages = array();
267 var $_rows = array();
269 var $_pagename_seen = false;
270 var $_types = array();
271 var $_options = array();
272 var $_selected = array();
274 function PageList ($columns = false, $exclude = false, $options = false) {
275 $this->_initAvailableColumns();
278 'all' => array_diff(array_keys($this->_types),
279 array('checkbox','remove','renamed_pagename','content')),
280 'most' => array('pagename','mtime','author','size','hits'),
281 'some' => array('pagename','mtime','author')
284 if (!is_array($columns))
285 $columns = explode(',', $columns);
286 // expand symbolic columns:
287 foreach ($symbolic_columns as $symbol => $cols) {
288 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
289 $columns = array_diff(array_merge($columns,$cols),array($symbol));
292 foreach ($columns as $col) {
293 $this->_addColumn($col);
296 $this->_addColumn('pagename');
299 if (!is_array($exclude))
300 $exclude = explode(',', $exclude);
301 $this->_excluded_pages = $exclude;
304 $this->_options = $options;
305 $this->_messageIfEmpty = _("<no matches>");
308 function setCaption ($caption_string) {
309 $this->_caption = $caption_string;
312 function getCaption () {
313 // put the total into the caption if needed
314 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
315 return sprintf($this->_caption, $this->getTotal());
316 return $this->_caption;
319 function setMessageIfEmpty ($msg) {
320 $this->_messageIfEmpty = $msg;
324 function getTotal () {
325 return count($this->_rows);
328 function isEmpty () {
329 return empty($this->_rows);
332 function addPage ($page_handle) {
333 if (is_string($page_handle)) {
334 if (in_array($page_handle, $this->_excluded_pages))
335 return; // exclude page.
336 $dbi = $GLOBALS['request']->getDbh();
337 $page_handle = $dbi->getPage($page_handle);
339 if (in_array($page_handle->getName(), $this->_excluded_pages))
340 return; // exclude page.
343 $group = (int)(count($this->_rows) / $this->_group_rows);
344 $class = ($group % 2) ? 'oddrow' : 'evenrow';
345 $revision_handle = false;
347 if (count($this->_columns) > 1) {
348 $row = HTML::tr(array('class' => $class));
349 foreach ($this->_columns as $col)
350 $row->pushContent($col->format($this, $page_handle, $revision_handle));
353 $col = $this->_columns[0];
354 $row = HTML::li(array('class' => $class),
355 $col->_getValue($page_handle, $revision_handle));
358 $this->_rows[] = $row;
361 function addPages ($page_iter) {
362 while ($page = $page_iter->next())
363 $this->addPage($page);
366 function addPageList (&$list) {
368 while ($page = next($list))
369 $this->addPage($page);
372 function getContent() {
373 // Note that the <caption> element wants inline content.
374 $caption = $this->getCaption();
376 if ($this->isEmpty())
377 return $this->_emptyList($caption);
378 elseif (count($this->_columns) == 1)
379 return $this->_generateList($caption);
381 return $this->_generateTable($caption);
384 function printXML() {
385 PrintXML($this->getContent());
389 return AsXML($this->getContent());
393 * handles sortby requests for the DB iterator and table header links
394 * prefix the column with + or - like "+pagename","-mtime", ...
395 * supported column: 'pagename','mtime','hits'
396 * supported action: 'flip_order', 'db'
398 function sortby ($column, $action) {
399 if (substr($column,0,1) == '+') {
400 $order = '+'; $column = substr($column,1);
401 } elseif (substr($column,0,1) == '-') {
402 $order = '-'; $column = substr($column,1);
404 if (in_array($column,array('pagename','mtime','hits'))) {
405 // default order: +pagename, -mtime, -hits
407 if (in_array($column,array('mtime','hits')))
411 //TODO: multiple comma-delimited sortby args: "+hits,+pagename"
412 if ($action == 'flip_order') {
413 return ($order == '+' ? '-' : '+') . $column;
414 } elseif ($action == 'db') {
415 // asc or desc: +pagename, -pagename
416 return $column . ($order == '+' ? ' ASC' : ' DESC');
422 // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
423 function explodePageList($input, $perm = false, $sortby = '') {
424 // expand wildcards from list of all pages
425 if (preg_match('/[\?\*]/',$input)) {
426 $dbi = $GLOBALS['request']->getDbh();
427 $allPagehandles = $dbi->getAllPages($perm,$sortby);
428 while ($pagehandle = $allPagehandles->next()) {
429 $allPages[] = $pagehandle->getName();
431 return explodeList($input, $allPages);
433 //TODO: do the sorting
434 return explode(',',$input);
442 function _initAvailableColumns() {
443 if (!empty($this->_types))
449 => new _PageList_Column_content('content', _("Content")),
452 => new _PageList_Column_remove('remove', _("Remove")),
455 => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
458 => new _PageList_Column_checkbox('p', _("Selected")),
461 => new _PageList_Column_pagename,
464 => new _PageList_Column_time('rev:mtime',
467 => new _PageList_Column('hits', _("Hits"), 'right'),
470 => new _PageList_Column_size('size', _("Size"), 'right'),
471 /*array('align' => 'char', 'char' => ' ')*/
474 => new _PageList_Column('rev:summary', _("Last Summary")),
477 => new _PageList_Column_version('rev:version', _("Version"),
480 => new _PageList_Column_author('rev:author',
483 => new _PageList_Column_bool('locked', _("Locked"),
486 => new _PageList_Column_bool('rev:is_minor_edit',
487 _("Minor Edit"), _("minor")),
489 => new _PageList_Column('rev:markup', _("Markup"))
493 function _addColumn ($column) {
495 $this->_initAvailableColumns();
497 if (isset($this->_columns_seen[$column]))
498 return false; // Already have this one.
499 $this->_columns_seen[$column] = true;
501 if (strstr($column, ':'))
502 list ($column, $heading) = explode(':', $column, 2);
504 if (!isset($this->_types[$column])) {
505 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
509 $col = $this->_types[$column];
510 if (!empty($heading))
511 $col->setHeading($heading);
513 $this->_columns[] = $col;
518 // make a table given the caption
519 function _generateTable($caption) {
520 $table = HTML::table(array('cellpadding' => 0,
523 'class' => 'pagelist'));
525 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
528 foreach ($this->_columns as $col) {
529 //TODO: add links to resort the table
530 $row->pushContent($col->heading());
531 $table_summary[] = $col->_heading;
533 // Table summary for non-visual browsers.
534 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
536 $table->pushContent(HTML::thead($row),
537 HTML::tbody(false, $this->_rows));
541 function _generateList($caption) {
542 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
543 return $caption ? HTML(HTML::p($caption), $list) : $list;
546 function _emptyList($caption) {
549 $html->pushContent(HTML::p($caption));
550 if ($this->_messageIfEmpty)
551 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
556 /* List pages with checkboxes to select from.
557 * Todo: All, None jscript buttons.
560 class PageList_Selectable
563 function PageList_Selectable ($columns=false, $exclude=false) {
564 PageList::PageList($columns,$exclude);
567 function addPageList ($array) {
568 while (list($pagename,$selected) = each($array)) {
569 if ($selected) $this->addPageSelected($pagename);
570 $this->addPage($pagename);
574 function addPageSelected ($pagename) {
575 $this->_selected[$pagename] = 1;
578 //insert javascript when clicked on Selected Select/Deselect all
581 // (c-file-style: "gnu")
586 // c-hanging-comment-ender-p: nil
587 // indent-tabs-mode: nil