1 <?php rcs_id('$Id: PageList.php,v 1.44 2003-11-29 20:06:43 carstenklapp 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 $data = &$revision_handle->_data;
118 $bytes = strlen(&$data['%content']);
119 return ByteFormatter($bytes);
124 class _PageList_Column_bool extends _PageList_Column {
125 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
126 $this->_PageList_Column($field, $default_heading, 'center');
127 $this->_textIfTrue = $text;
128 $this->_textIfFalse = new RawXml('—'); //mdash
131 function _getValue ($page_handle, &$revision_handle) {
132 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
133 return $val ? $this->_textIfTrue : $this->_textIfFalse;
137 class _PageList_Column_checkbox extends _PageList_Column {
138 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
139 $this->_name = $name;
140 $this->_PageList_Column($field, $default_heading, 'center');
142 function _getValue ($pagelist, $page_handle, &$revision_handle) {
143 $pagename = $page_handle->getName();
144 if (!empty($pagelist->_selected[$pagename])) {
145 return HTML::input(array('type' => 'checkbox',
146 'name' => $this->_name . "[$pagename]",
147 'value' => $pagename,
150 return HTML::input(array('type' => 'checkbox',
151 'name' => $this->_name . "[$pagename]",
152 'value' => $pagename));
155 function format ($pagelist, $page_handle, &$revision_handle) {
156 return HTML::td($this->_tdattr,
158 $this->_getValue($pagelist, $page_handle, $revision_handle),
159 HTML::raw(' '));
163 class _PageList_Column_time extends _PageList_Column {
164 function _PageList_Column_time ($field, $default_heading) {
165 $this->_PageList_Column($field, $default_heading, 'right');
167 $this->Theme = &$Theme;
170 function _getValue ($page_handle, &$revision_handle) {
171 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
172 return $this->Theme->formatDateTime($time);
176 class _PageList_Column_version extends _PageList_Column {
177 function _getValue ($page_handle, &$revision_handle) {
178 if (!$revision_handle)
179 $revision_handle = $page_handle->getCurrentRevision();
180 return $revision_handle->getVersion();
184 // If needed this could eventually become a subclass
185 // of a new _PageList_Column_action class for other actions.
186 class _PageList_Column_remove extends _PageList_Column {
187 function _getValue ($page_handle, &$revision_handle) {
188 return Button(array('action' => 'remove'), _("Remove"),
189 $page_handle->getName());
193 // Output is hardcoded to limit of first 50 bytes. Otherwise
194 // on very large Wikis this will fail if used with AllPages
195 // (PHP memory limit exceeded)
196 class _PageList_Column_content extends _PageList_Column {
197 function _PageList_Column_content ($field, $default_heading, $align = false) {
198 _PageList_Column::_PageList_Column($field, $default_heading, $align);
200 $this->_heading .= sprintf(_(" ... first %d bytes"),
203 function _getValue ($page_handle, &$revision_handle) {
204 if (!$revision_handle)
205 $revision_handle = $page_handle->getCurrentRevision();
206 // Not sure why implode is needed here, I thought
207 // getContent() already did this, but it seems necessary.
208 $c = implode("\n", $revision_handle->getContent());
209 if (($len = strlen($c)) > $this->bytes) {
210 $c = substr($c, 0, $this->bytes);
212 require_once('lib/BlockParser.php');
213 // false --> don't bother processing hrefs for embedded WikiLinks
214 $ct = TransformText($c, $revision_handle->get('markup'), false);
215 return HTML::div(array('style' => 'font-size:xx-small'),
216 HTML::div(array('class' => 'transclusion'), $ct),
217 // TODO: Don't show bytes here if size column present too
218 /* Howto??? $this->parent->_columns['size'] ? "" :*/
219 ByteFormatter($len, /*$longformat = */true));
223 class _PageList_Column_author extends _PageList_Column {
224 function _PageList_Column_author ($field, $default_heading, $align = false) {
225 _PageList_Column::_PageList_Column($field, $default_heading, $align);
226 global $WikiNameRegexp, $request;
227 $this->WikiNameRegexp = $WikiNameRegexp;
228 $this->dbi = &$request->getDbh();
231 function _getValue ($page_handle, &$revision_handle) {
232 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
233 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
234 return WikiLink($author);
240 class _PageList_Column_pagename extends _PageList_Column_base {
241 var $_field = 'pagename';
243 function _PageList_Column_pagename () {
244 $this->_PageList_Column_base(_("Page Name"));
246 $this->dbi = &$request->getDbh();
249 function _getValue ($page_handle, &$revision_handle) {
250 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
251 return WikiLink($page_handle);
253 return WikiLink($page_handle, 'unknown');
260 var $_group_rows = 3;
261 var $_columns = array();
262 var $_excluded_pages = array();
263 var $_rows = array();
265 var $_pagename_seen = false;
266 var $_types = array();
267 var $_options = array();
268 var $_selected = array();
270 function PageList ($columns = false, $exclude = false, $options = false) {
271 if ($columns == 'all') {
272 $this->_initAvailableColumns();
273 $columns = array_keys($this->_types);
274 // FIXME: Probably a good idea to NOT include the
275 // columns 'content' and 'remove' when 'all' is
280 if (!is_array($columns))
281 $columns = explode(',', $columns);
282 if (in_array('all',$columns)) { // e.g. 'checkbox,all'
283 $this->_initAvailableColumns();
284 $columns = array_merge($columns,array_keys($this->_types));
285 $columns = array_diff($columns,array('all'));
287 foreach ($columns as $col) {
288 $this->_addColumn($col);
291 $this->_addColumn('pagename');
294 if (!is_array($exclude))
295 $exclude = explode(',', $exclude);
296 $this->_excluded_pages = $exclude;
299 $this->_options = $options;
300 $this->_messageIfEmpty = _("<no matches>");
303 function setCaption ($caption_string) {
304 $this->_caption = $caption_string;
307 function getCaption () {
308 // put the total into the caption if needed
309 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
310 return sprintf($this->_caption, $this->getTotal());
311 return $this->_caption;
314 function setMessageIfEmpty ($msg) {
315 $this->_messageIfEmpty = $msg;
319 function getTotal () {
320 return count($this->_rows);
323 function isEmpty () {
324 return empty($this->_rows);
327 function addPage ($page_handle) {
328 if (is_string($page_handle)) {
329 if (in_array($page_handle, $this->_excluded_pages))
330 return; // exclude page.
331 $dbi = $GLOBALS['request']->getDbh();
332 $page_handle = $dbi->getPage($page_handle);
334 if (in_array($page_handle->getName(), $this->_excluded_pages))
335 return; // exclude page.
338 $group = (int)(count($this->_rows) / $this->_group_rows);
339 $class = ($group % 2) ? 'oddrow' : 'evenrow';
340 $revision_handle = false;
342 if (count($this->_columns) > 1) {
343 $row = HTML::tr(array('class' => $class));
344 foreach ($this->_columns as $col)
345 $row->pushContent($col->format($this, $page_handle, $revision_handle));
348 $col = $this->_columns[0];
349 $row = HTML::li(array('class' => $class),
350 $col->_getValue($page_handle, $revision_handle));
353 $this->_rows[] = $row;
356 function addPages ($page_iter) {
357 while ($page = $page_iter->next())
358 $this->addPage($page);
361 function addPageList (&$list) {
363 while ($page = next($list))
364 $this->addPage($page);
367 function getContent() {
368 // Note that the <caption> element wants inline content.
369 $caption = $this->getCaption();
371 if ($this->isEmpty())
372 return $this->_emptyList($caption);
373 elseif (count($this->_columns) == 1)
374 return $this->_generateList($caption);
376 return $this->_generateTable($caption);
379 function printXML() {
380 PrintXML($this->getContent());
384 return AsXML($this->getContent());
391 function _initAvailableColumns() {
392 if (!empty($this->_types))
398 => new _PageList_Column_content('content', _("Content")),
401 => new _PageList_Column_remove('remove', _("Remove")),
404 => new _PageList_Column_checkbox('p', _("Selected")),
407 => new _PageList_Column_pagename,
410 => new _PageList_Column_time('rev:mtime',
413 => new _PageList_Column('hits', _("Hits"), 'right'),
416 => new _PageList_Column_size('size', _("Size"), 'right'),
417 /*array('align' => 'char', 'char' => ' ')*/
420 => new _PageList_Column('rev:summary', _("Last Summary")),
423 => new _PageList_Column_version('rev:version', _("Version"),
426 => new _PageList_Column_author('rev:author',
429 => new _PageList_Column_bool('locked', _("Locked"),
432 => new _PageList_Column_bool('rev:is_minor_edit',
433 _("Minor Edit"), _("minor")),
435 => new _PageList_Column('rev:markup', _("Markup"))
439 function _addColumn ($column) {
441 $this->_initAvailableColumns();
443 if (isset($this->_columns_seen[$column]))
444 return false; // Already have this one.
445 $this->_columns_seen[$column] = true;
447 if (strstr($column, ':'))
448 list ($column, $heading) = explode(':', $column, 2);
450 if (!isset($this->_types[$column])) {
451 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
455 $col = $this->_types[$column];
456 if (!empty($heading))
457 $col->setHeading($heading);
459 $this->_columns[] = $col;
464 // make a table given the caption
465 function _generateTable($caption) {
466 $table = HTML::table(array('cellpadding' => 0,
469 'class' => 'pagelist'));
471 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
474 foreach ($this->_columns as $col) {
475 // Todo: add links to resort the table
476 $row->pushContent($col->heading());
477 $table_summary[] = $col->_heading;
479 // Table summary for non-visual browsers.
480 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
482 $table->pushContent(HTML::thead($row),
483 HTML::tbody(false, $this->_rows));
487 function _generateList($caption) {
488 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
489 return $caption ? HTML(HTML::p($caption), $list) : $list;
492 function _emptyList($caption) {
495 $html->pushContent(HTML::p($caption));
496 if ($this->_messageIfEmpty)
497 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
502 /* List pages with checkboxes to select from.
503 * Todo: All, None jscript buttons.
506 class PageList_Selectable
509 function PageList_Selectable ($columns=false, $exclude=false) {
510 PageList::PageList($columns,$exclude);
513 function addPageList ($array) {
514 while (list($pagename,$selected) = each($array)) {
515 if ($selected) $this->addPageSelected($pagename);
516 $this->addPage($pagename);
520 function addPageSelected ($pagename) {
521 $this->_selected[$pagename] = 1;
525 // (c-file-style: "gnu")
530 // c-hanging-comment-ender-p: nil
531 // indent-tabs-mode: nil