1 <?php rcs_id('$Id: PageList.php,v 1.48 2004-01-25 10:52:18 rurban Exp $');
4 * This library relieves some work for these plugins:
6 * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch and more
8 * It also allows dynamic expansion of those plugins to include more
9 * columns in their output.
11 * Column 'info=' arguments:
13 * 'pagename' _("Page Name")
14 * 'mtime' _("Last Modified")
16 * 'summary' _("Last Summary")
17 * 'version' _("Version")),
18 * 'author' _("Last Author")),
19 * 'locked' _("Locked"), _("locked")
20 * 'minor' _("Minor Edit"), _("minor")
21 * 'markup' _("Markup")
23 * 'remove' _("Remove") //admin action, not really an info column
25 * 'all' All columns will be displayed. This argument must appear alone.
26 * 'checkbox' A selectable checkbox appears at the left.
28 * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
29 * number-of-revision. Note the _ctime and _cauthor as they were implemented
30 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
31 * database. If lots of revisions have been made to a page, it's more than likely
32 * that some older revisions (include revision 1) have been cleaned (deleted).
34 * TODO: limit, offset, rows arguments for multiple pages/multiple rows.
36 class _PageList_Column_base {
37 var $_tdattr = array();
39 function _PageList_Column_base ($default_heading, $align = false) {
40 $this->_heading = $default_heading;
43 // align="char" isn't supported by any browsers yet :(
44 //if (is_array($align))
45 // $this->_tdattr = $align;
47 $this->_tdattr['align'] = $align;
51 function format ($pagelist, $page_handle, &$revision_handle) {
52 return HTML::td($this->_tdattr,
54 $this->_getValue($page_handle, $revision_handle),
58 function setHeading ($heading) {
59 $this->_heading = $heading;
63 if (in_array($this->_field,array('pagename','mtime','hits'))) {
64 // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
65 // asc or desc: +pagename, -pagename
66 $sortby = '+' . $this->_field;
67 if ($sorted = $GLOBALS['request']->getArg('sortby')) {
69 if ($sorted == '+' . $this->_field)
70 $sortby = '-' . $this->_field;
71 elseif ($sorted == '-' . $this->_field)
72 $sortby = '+' . $this->_field;
74 $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(' '));
76 $s = HTML(HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
78 return HTML::td(array('align' => 'center'),$s);
82 class _PageList_Column extends _PageList_Column_base {
83 function _PageList_Column ($field, $default_heading, $align = false) {
84 $this->_PageList_Column_base($default_heading, $align);
86 $this->_need_rev = substr($field, 0, 4) == 'rev:';
88 $this->_field = substr($field, 4);
90 $this->_field = $field;
93 function _getValue ($page_handle, &$revision_handle) {
94 if ($this->_need_rev) {
95 if (!$revision_handle)
96 $revision_handle = $page_handle->getCurrentRevision();
97 return $revision_handle->get($this->_field);
100 return $page_handle->get($this->_field);
105 class _PageList_Column_size extends _PageList_Column {
106 function _getValue ($page_handle, &$revision_handle) {
107 if (!$revision_handle)
108 $revision_handle = $page_handle->getCurrentRevision();
109 return $this->_getSize($revision_handle);
112 function _getSize($revision_handle) {
113 $bytes = strlen($revision_handle->_data['%content']);
114 return ByteFormatter($bytes);
119 class _PageList_Column_bool extends _PageList_Column {
120 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
121 $this->_PageList_Column($field, $default_heading, 'center');
122 $this->_textIfTrue = $text;
123 $this->_textIfFalse = new RawXml('—'); //mdash
126 function _getValue ($page_handle, &$revision_handle) {
127 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
128 return $val ? $this->_textIfTrue : $this->_textIfFalse;
132 class _PageList_Column_checkbox extends _PageList_Column {
133 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
134 $this->_name = $name;
135 $this->_PageList_Column($field, $default_heading, 'center');
137 function _getValue ($pagelist, $page_handle, &$revision_handle) {
138 $pagename = $page_handle->getName();
139 if (!empty($pagelist->_selected[$pagename])) {
140 return HTML::input(array('type' => 'checkbox',
141 'name' => $this->_name . "[$pagename]",
142 'value' => $pagename,
145 return HTML::input(array('type' => 'checkbox',
146 'name' => $this->_name . "[$pagename]",
147 'value' => $pagename));
150 function format ($pagelist, $page_handle, &$revision_handle) {
151 return HTML::td($this->_tdattr,
153 $this->_getValue($pagelist, $page_handle, $revision_handle),
154 HTML::raw(' '));
158 class _PageList_Column_time extends _PageList_Column {
159 function _PageList_Column_time ($field, $default_heading) {
160 $this->_PageList_Column($field, $default_heading, 'right');
162 $this->Theme = &$Theme;
165 function _getValue ($page_handle, &$revision_handle) {
166 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
167 return $this->Theme->formatDateTime($time);
171 class _PageList_Column_version extends _PageList_Column {
172 function _getValue ($page_handle, &$revision_handle) {
173 if (!$revision_handle)
174 $revision_handle = $page_handle->getCurrentRevision();
175 return $revision_handle->getVersion();
179 // If needed this could eventually become a subclass
180 // of a new _PageList_Column_action class for other actions.
181 class _PageList_Column_remove extends _PageList_Column {
182 function _getValue ($page_handle, &$revision_handle) {
183 return Button(array('action' => 'remove'), _("Remove"),
184 $page_handle->getName());
188 // Output is hardcoded to limit of first 50 bytes. Otherwise
189 // on very large Wikis this will fail if used with AllPages
190 // (PHP memory limit exceeded)
191 class _PageList_Column_content extends _PageList_Column {
192 function _PageList_Column_content ($field, $default_heading, $align = false) {
193 _PageList_Column::_PageList_Column($field, $default_heading, $align);
195 $this->_heading .= sprintf(_(" ... first %d bytes"),
198 function _getValue ($page_handle, &$revision_handle) {
199 if (!$revision_handle)
200 $revision_handle = $page_handle->getCurrentRevision();
201 // Not sure why implode is needed here, I thought
202 // getContent() already did this, but it seems necessary.
203 $c = implode("\n", $revision_handle->getContent());
204 if (($len = strlen($c)) > $this->bytes) {
205 $c = substr($c, 0, $this->bytes);
207 include_once('lib/BlockParser.php');
208 // false --> don't bother processing hrefs for embedded WikiLinks
209 $ct = TransformText($c, $revision_handle->get('markup'), false);
210 return HTML::div(array('style' => 'font-size:xx-small'),
211 HTML::div(array('class' => 'transclusion'), $ct),
212 // TODO: Don't show bytes here if size column present too
213 /* Howto??? $this->parent->_columns['size'] ? "" :*/
214 ByteFormatter($len, /*$longformat = */true));
218 class _PageList_Column_author extends _PageList_Column {
219 function _PageList_Column_author ($field, $default_heading, $align = false) {
220 _PageList_Column::_PageList_Column($field, $default_heading, $align);
221 global $WikiNameRegexp, $request;
222 $this->WikiNameRegexp = $WikiNameRegexp;
223 $this->dbi = &$request->getDbh();
226 function _getValue ($page_handle, &$revision_handle) {
227 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
228 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
229 return WikiLink($author);
235 class _PageList_Column_pagename extends _PageList_Column_base {
236 var $_field = 'pagename';
238 function _PageList_Column_pagename () {
239 $this->_PageList_Column_base(_("Page Name"));
241 $this->dbi = &$request->getDbh();
244 function _getValue ($page_handle, &$revision_handle) {
245 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
246 return WikiLink($page_handle);
248 return WikiLink($page_handle, 'unknown');
255 var $_group_rows = 3;
256 var $_columns = array();
257 var $_excluded_pages = array();
258 var $_rows = array();
260 var $_pagename_seen = false;
261 var $_types = array();
262 var $_options = array();
263 var $_selected = array();
265 function PageList ($columns = false, $exclude = false, $options = false) {
266 if ($columns == 'all') {
267 $this->_initAvailableColumns();
268 $columns = array_keys($this->_types);
269 // FIXME: Probably a good idea to NOT include the
270 // columns 'content' and 'remove' when 'all' is
275 if (!is_array($columns))
276 $columns = explode(',', $columns);
277 if (in_array('all',$columns)) { // e.g. 'checkbox,all'
278 $this->_initAvailableColumns();
279 $columns = array_merge($columns,array_keys($this->_types));
280 $columns = array_diff($columns,array('all'));
282 foreach ($columns as $col) {
283 $this->_addColumn($col);
286 $this->_addColumn('pagename');
289 if (!is_array($exclude))
290 $exclude = explode(',', $exclude);
291 $this->_excluded_pages = $exclude;
294 $this->_options = $options;
295 $this->_messageIfEmpty = _("<no matches>");
298 function setCaption ($caption_string) {
299 $this->_caption = $caption_string;
302 function getCaption () {
303 // put the total into the caption if needed
304 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
305 return sprintf($this->_caption, $this->getTotal());
306 return $this->_caption;
309 function setMessageIfEmpty ($msg) {
310 $this->_messageIfEmpty = $msg;
314 function getTotal () {
315 return count($this->_rows);
318 function isEmpty () {
319 return empty($this->_rows);
322 // $action = flip_order, db
323 function sortby ($string, $action) {
325 if (substr($string,0,1) == '+') {
326 $order = '+'; $string = substr($string,1);
327 } elseif (substr($string,0,1) == '-') {
328 $order = '-'; $string = substr($string,1);
330 if (in_array($string,array('pagename','mtime','hits'))) {
331 // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
332 // asc or desc: +pagename, -pagename
333 if ($action == 'flip_order') {
334 return ($order == '+' ? '-' : '+') . $string;
335 } elseif ($action == 'db') {
336 return $string . ($order == '+' ? ' ASC' : ' DESC');
342 function addPage ($page_handle) {
343 if (is_string($page_handle)) {
344 if (in_array($page_handle, $this->_excluded_pages))
345 return; // exclude page.
346 $dbi = $GLOBALS['request']->getDbh();
347 $page_handle = $dbi->getPage($page_handle);
349 if (in_array($page_handle->getName(), $this->_excluded_pages))
350 return; // exclude page.
353 $group = (int)(count($this->_rows) / $this->_group_rows);
354 $class = ($group % 2) ? 'oddrow' : 'evenrow';
355 $revision_handle = false;
357 if (count($this->_columns) > 1) {
358 $row = HTML::tr(array('class' => $class));
359 foreach ($this->_columns as $col)
360 $row->pushContent($col->format($this, $page_handle, $revision_handle));
363 $col = $this->_columns[0];
364 $row = HTML::li(array('class' => $class),
365 $col->_getValue($page_handle, $revision_handle));
368 $this->_rows[] = $row;
371 function addPages ($page_iter) {
372 while ($page = $page_iter->next())
373 $this->addPage($page);
376 function addPageList (&$list) {
378 while ($page = next($list))
379 $this->addPage($page);
382 function getContent() {
383 // Note that the <caption> element wants inline content.
384 $caption = $this->getCaption();
386 if ($this->isEmpty())
387 return $this->_emptyList($caption);
388 elseif (count($this->_columns) == 1)
389 return $this->_generateList($caption);
391 return $this->_generateTable($caption);
394 function printXML() {
395 PrintXML($this->getContent());
399 return AsXML($this->getContent());
406 function _initAvailableColumns() {
407 if (!empty($this->_types))
413 => new _PageList_Column_content('content', _("Content")),
416 => new _PageList_Column_remove('remove', _("Remove")),
419 => new _PageList_Column_checkbox('p', _("Selected")),
422 => new _PageList_Column_pagename,
425 => new _PageList_Column_time('rev:mtime',
428 => new _PageList_Column('hits', _("Hits"), 'right'),
431 => new _PageList_Column_size('size', _("Size"), 'right'),
432 /*array('align' => 'char', 'char' => ' ')*/
435 => new _PageList_Column('rev:summary', _("Last Summary")),
438 => new _PageList_Column_version('rev:version', _("Version"),
441 => new _PageList_Column_author('rev:author',
444 => new _PageList_Column_bool('locked', _("Locked"),
447 => new _PageList_Column_bool('rev:is_minor_edit',
448 _("Minor Edit"), _("minor")),
450 => new _PageList_Column('rev:markup', _("Markup"))
454 function _addColumn ($column) {
456 $this->_initAvailableColumns();
458 if (isset($this->_columns_seen[$column]))
459 return false; // Already have this one.
460 $this->_columns_seen[$column] = true;
462 if (strstr($column, ':'))
463 list ($column, $heading) = explode(':', $column, 2);
465 if (!isset($this->_types[$column])) {
466 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
470 $col = $this->_types[$column];
471 if (!empty($heading))
472 $col->setHeading($heading);
474 $this->_columns[] = $col;
479 // make a table given the caption
480 function _generateTable($caption) {
481 $table = HTML::table(array('cellpadding' => 0,
484 'class' => 'pagelist'));
486 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
489 foreach ($this->_columns as $col) {
490 // Todo: add links to resort the table
491 $row->pushContent($col->heading());
492 $table_summary[] = $col->_heading;
494 // Table summary for non-visual browsers.
495 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
497 $table->pushContent(HTML::thead($row),
498 HTML::tbody(false, $this->_rows));
502 function _generateList($caption) {
503 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
504 return $caption ? HTML(HTML::p($caption), $list) : $list;
507 function _emptyList($caption) {
510 $html->pushContent(HTML::p($caption));
511 if ($this->_messageIfEmpty)
512 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
517 /* List pages with checkboxes to select from.
518 * Todo: All, None jscript buttons.
521 class PageList_Selectable
524 function PageList_Selectable ($columns=false, $exclude=false) {
525 PageList::PageList($columns,$exclude);
528 function addPageList ($array) {
529 while (list($pagename,$selected) = each($array)) {
530 if ($selected) $this->addPageSelected($pagename);
531 $this->addPage($pagename);
535 function addPageSelected ($pagename) {
536 $this->_selected[$pagename] = 1;
540 // (c-file-style: "gnu")
545 // c-hanging-comment-ender-p: nil
546 // indent-tabs-mode: nil