1 <?php rcs_id('$Id: PageList.php,v 1.47 2004-01-25 07:58:29 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).
35 * The 'sortby' option is handled here correctly, but at the backends at
36 * the page iterator not yet.
38 * TODO: order, sortby, limit, offset, rows arguments for multiple pages/multiple rows.
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 // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
69 // asc or desc: +pagename, -pagename
70 $sortby = '+' . $this->_field;
71 if ($sorted = $GLOBALS['request']->getArg('sortby')) {
73 if ($sorted == '+' . $this->_field)
74 $sortby = '-' . $this->_field;
75 elseif ($sorted == '-' . $this->_field)
76 $sortby = '+' . $this->_field;
78 $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(' '));
80 $s = HTML(HTML::raw(' '), HTML::u($this->_heading), HTML::raw(' '));
82 return HTML::td(array('align' => 'center'),$s);
86 class _PageList_Column extends _PageList_Column_base {
87 function _PageList_Column ($field, $default_heading, $align = false) {
88 $this->_PageList_Column_base($default_heading, $align);
90 $this->_need_rev = substr($field, 0, 4) == 'rev:';
92 $this->_field = substr($field, 4);
94 $this->_field = $field;
97 function _getValue ($page_handle, &$revision_handle) {
98 if ($this->_need_rev) {
99 if (!$revision_handle)
100 $revision_handle = $page_handle->getCurrentRevision();
101 return $revision_handle->get($this->_field);
104 return $page_handle->get($this->_field);
109 class _PageList_Column_size extends _PageList_Column {
110 function _getValue ($page_handle, &$revision_handle) {
111 if (!$revision_handle)
112 $revision_handle = $page_handle->getCurrentRevision();
113 return $this->_getSize($revision_handle);
116 function _getSize($revision_handle) {
117 $bytes = strlen($revision_handle->_data['%content']);
118 return ByteFormatter($bytes);
123 class _PageList_Column_bool extends _PageList_Column {
124 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
125 $this->_PageList_Column($field, $default_heading, 'center');
126 $this->_textIfTrue = $text;
127 $this->_textIfFalse = new RawXml('—'); //mdash
130 function _getValue ($page_handle, &$revision_handle) {
131 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
132 return $val ? $this->_textIfTrue : $this->_textIfFalse;
136 class _PageList_Column_checkbox extends _PageList_Column {
137 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
138 $this->_name = $name;
139 $this->_PageList_Column($field, $default_heading, 'center');
141 function _getValue ($pagelist, $page_handle, &$revision_handle) {
142 $pagename = $page_handle->getName();
143 if (!empty($pagelist->_selected[$pagename])) {
144 return HTML::input(array('type' => 'checkbox',
145 'name' => $this->_name . "[$pagename]",
146 'value' => $pagename,
149 return HTML::input(array('type' => 'checkbox',
150 'name' => $this->_name . "[$pagename]",
151 'value' => $pagename));
154 function format ($pagelist, $page_handle, &$revision_handle) {
155 return HTML::td($this->_tdattr,
157 $this->_getValue($pagelist, $page_handle, $revision_handle),
158 HTML::raw(' '));
162 class _PageList_Column_time extends _PageList_Column {
163 function _PageList_Column_time ($field, $default_heading) {
164 $this->_PageList_Column($field, $default_heading, 'right');
166 $this->Theme = &$Theme;
169 function _getValue ($page_handle, &$revision_handle) {
170 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
171 return $this->Theme->formatDateTime($time);
175 class _PageList_Column_version extends _PageList_Column {
176 function _getValue ($page_handle, &$revision_handle) {
177 if (!$revision_handle)
178 $revision_handle = $page_handle->getCurrentRevision();
179 return $revision_handle->getVersion();
183 // If needed this could eventually become a subclass
184 // of a new _PageList_Column_action class for other actions.
185 class _PageList_Column_remove extends _PageList_Column {
186 function _getValue ($page_handle, &$revision_handle) {
187 return Button(array('action' => 'remove'), _("Remove"),
188 $page_handle->getName());
192 // Output is hardcoded to limit of first 50 bytes. Otherwise
193 // on very large Wikis this will fail if used with AllPages
194 // (PHP memory limit exceeded)
195 class _PageList_Column_content extends _PageList_Column {
196 function _PageList_Column_content ($field, $default_heading, $align = false) {
197 _PageList_Column::_PageList_Column($field, $default_heading, $align);
199 $this->_heading .= sprintf(_(" ... first %d bytes"),
202 function _getValue ($page_handle, &$revision_handle) {
203 if (!$revision_handle)
204 $revision_handle = $page_handle->getCurrentRevision();
205 // Not sure why implode is needed here, I thought
206 // getContent() already did this, but it seems necessary.
207 $c = implode("\n", $revision_handle->getContent());
208 if (($len = strlen($c)) > $this->bytes) {
209 $c = substr($c, 0, $this->bytes);
211 include_once('lib/BlockParser.php');
212 // false --> don't bother processing hrefs for embedded WikiLinks
213 $ct = TransformText($c, $revision_handle->get('markup'), false);
214 return HTML::div(array('style' => 'font-size:xx-small'),
215 HTML::div(array('class' => 'transclusion'), $ct),
216 // TODO: Don't show bytes here if size column present too
217 /* Howto??? $this->parent->_columns['size'] ? "" :*/
218 ByteFormatter($len, /*$longformat = */true));
222 class _PageList_Column_author extends _PageList_Column {
223 function _PageList_Column_author ($field, $default_heading, $align = false) {
224 _PageList_Column::_PageList_Column($field, $default_heading, $align);
225 global $WikiNameRegexp, $request;
226 $this->WikiNameRegexp = $WikiNameRegexp;
227 $this->dbi = &$request->getDbh();
230 function _getValue ($page_handle, &$revision_handle) {
231 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
232 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
233 return WikiLink($author);
239 class _PageList_Column_pagename extends _PageList_Column_base {
240 var $_field = 'pagename';
242 function _PageList_Column_pagename () {
243 $this->_PageList_Column_base(_("Page Name"));
245 $this->dbi = &$request->getDbh();
248 function _getValue ($page_handle, &$revision_handle) {
249 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
250 return WikiLink($page_handle);
252 return WikiLink($page_handle, 'unknown');
259 var $_group_rows = 3;
260 var $_columns = array();
261 var $_excluded_pages = array();
262 var $_rows = array();
264 var $_pagename_seen = false;
265 var $_types = array();
266 var $_options = array();
267 var $_selected = array();
269 function PageList ($columns = false, $exclude = false, $options = false) {
270 if ($columns == 'all') {
271 $this->_initAvailableColumns();
272 $columns = array_keys($this->_types);
273 // FIXME: Probably a good idea to NOT include the
274 // columns 'content' and 'remove' when 'all' is
279 if (!is_array($columns))
280 $columns = explode(',', $columns);
281 if (in_array('all',$columns)) { // e.g. 'checkbox,all'
282 $this->_initAvailableColumns();
283 $columns = array_merge($columns,array_keys($this->_types));
284 $columns = array_diff($columns,array('all'));
286 foreach ($columns as $col) {
287 $this->_addColumn($col);
290 $this->_addColumn('pagename');
293 if (!is_array($exclude))
294 $exclude = explode(',', $exclude);
295 $this->_excluded_pages = $exclude;
298 $this->_options = $options;
299 $this->_messageIfEmpty = _("<no matches>");
302 function setCaption ($caption_string) {
303 $this->_caption = $caption_string;
306 function getCaption () {
307 // put the total into the caption if needed
308 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
309 return sprintf($this->_caption, $this->getTotal());
310 return $this->_caption;
313 function setMessageIfEmpty ($msg) {
314 $this->_messageIfEmpty = $msg;
318 function getTotal () {
319 return count($this->_rows);
322 function isEmpty () {
323 return empty($this->_rows);
326 // $action = flip_order, db
327 function sortby ($string, $action) {
329 if (substr($string,0,1) == '+') {
330 $order = '+'; $string = substr($string,1);
331 } elseif (substr($string,0,1) == '-') {
332 $order = '-'; $string = substr($string,1);
334 if (in_array($string,array('pagename','mtime','hits'))) {
335 // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
336 // asc or desc: +pagename, -pagename
337 if ($action == 'flip_order') {
338 return ($order == '+' ? '-' : '+') . $string;
339 } elseif ($action == 'db') {
340 return $string . ($order == '+' ? ' ASC' : ' DESC');
346 function addPage ($page_handle) {
347 if (is_string($page_handle)) {
348 if (in_array($page_handle, $this->_excluded_pages))
349 return; // exclude page.
350 $dbi = $GLOBALS['request']->getDbh();
351 $page_handle = $dbi->getPage($page_handle);
353 if (in_array($page_handle->getName(), $this->_excluded_pages))
354 return; // exclude page.
357 $group = (int)(count($this->_rows) / $this->_group_rows);
358 $class = ($group % 2) ? 'oddrow' : 'evenrow';
359 $revision_handle = false;
361 if (count($this->_columns) > 1) {
362 $row = HTML::tr(array('class' => $class));
363 foreach ($this->_columns as $col)
364 $row->pushContent($col->format($this, $page_handle, $revision_handle));
367 $col = $this->_columns[0];
368 $row = HTML::li(array('class' => $class),
369 $col->_getValue($page_handle, $revision_handle));
372 $this->_rows[] = $row;
375 function addPages ($page_iter) {
376 while ($page = $page_iter->next())
377 $this->addPage($page);
380 function addPageList (&$list) {
382 while ($page = next($list))
383 $this->addPage($page);
386 function getContent() {
387 // Note that the <caption> element wants inline content.
388 $caption = $this->getCaption();
390 if ($this->isEmpty())
391 return $this->_emptyList($caption);
392 elseif (count($this->_columns) == 1)
393 return $this->_generateList($caption);
395 return $this->_generateTable($caption);
398 function printXML() {
399 PrintXML($this->getContent());
403 return AsXML($this->getContent());
410 function _initAvailableColumns() {
411 if (!empty($this->_types))
417 => new _PageList_Column_content('content', _("Content")),
420 => new _PageList_Column_remove('remove', _("Remove")),
423 => new _PageList_Column_checkbox('p', _("Selected")),
426 => new _PageList_Column_pagename,
429 => new _PageList_Column_time('rev:mtime',
432 => new _PageList_Column('hits', _("Hits"), 'right'),
435 => new _PageList_Column_size('size', _("Size"), 'right'),
436 /*array('align' => 'char', 'char' => ' ')*/
439 => new _PageList_Column('rev:summary', _("Last Summary")),
442 => new _PageList_Column_version('rev:version', _("Version"),
445 => new _PageList_Column_author('rev:author',
448 => new _PageList_Column_bool('locked', _("Locked"),
451 => new _PageList_Column_bool('rev:is_minor_edit',
452 _("Minor Edit"), _("minor")),
454 => new _PageList_Column('rev:markup', _("Markup"))
458 function _addColumn ($column) {
460 $this->_initAvailableColumns();
462 if (isset($this->_columns_seen[$column]))
463 return false; // Already have this one.
464 $this->_columns_seen[$column] = true;
466 if (strstr($column, ':'))
467 list ($column, $heading) = explode(':', $column, 2);
469 if (!isset($this->_types[$column])) {
470 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
474 $col = $this->_types[$column];
475 if (!empty($heading))
476 $col->setHeading($heading);
478 $this->_columns[] = $col;
483 // make a table given the caption
484 function _generateTable($caption) {
485 $table = HTML::table(array('cellpadding' => 0,
488 'class' => 'pagelist'));
490 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
493 foreach ($this->_columns as $col) {
494 // Todo: add links to resort the table
495 $row->pushContent($col->heading());
496 $table_summary[] = $col->_heading;
498 // Table summary for non-visual browsers.
499 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
501 $table->pushContent(HTML::thead($row),
502 HTML::tbody(false, $this->_rows));
506 function _generateList($caption) {
507 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
508 return $caption ? HTML(HTML::p($caption), $list) : $list;
511 function _emptyList($caption) {
514 $html->pushContent(HTML::p($caption));
515 if ($this->_messageIfEmpty)
516 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
521 /* List pages with checkboxes to select from.
522 * Todo: All, None jscript buttons.
525 class PageList_Selectable
528 function PageList_Selectable ($columns=false, $exclude=false) {
529 PageList::PageList($columns,$exclude);
532 function addPageList ($array) {
533 while (list($pagename,$selected) = each($array)) {
534 if ($selected) $this->addPageSelected($pagename);
535 $this->addPage($pagename);
539 function addPageSelected ($pagename) {
540 $this->_selected[$pagename] = 1;
544 // (c-file-style: "gnu")
549 // c-hanging-comment-ender-p: nil
550 // indent-tabs-mode: nil