1 <?php rcs_id('$Id: PageList.php,v 1.41 2002-10-29 01:12:24 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 * 'all' All columns will be displayed. This argument must appear alone.
24 * 'checkbox' A selectable checkbox appears at the left.
26 * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
27 * number-of-revision. Note the _ctime and _cauthor as they were implemented
28 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
29 * database. If lots of revisions have been made to a page, it's more than likely
30 * that some older revisions (include revision 1) have been cleaned (deleted).
33 * The 'sortby' option is handled here correctly, but at the backends at
34 * the page iterator not yet.
36 * TODO: order, sortby, limit, offset, rows arguments for multiple pages/multiple rows.
38 class _PageList_Column_base {
40 function _PageList_Column_base ($default_heading, $align = false) {
41 $this->_heading = $default_heading;
43 $this->_tdattr = array();
45 $this->_tdattr['align'] = $align;
48 function format ($pagelist, $page_handle, &$revision_handle) {
49 return HTML::td($this->_tdattr,
51 $this->_getValue($page_handle, &$revision_handle),
55 function setHeading ($heading) {
56 $this->_heading = $heading;
60 if (in_array($this->_field,array('pagename','mtime','hits'))) {
61 // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
62 // asc or desc: +pagename, -pagename
63 $sortby = '+' . $this->_field;
64 if ($sorted = $GLOBALS['request']->getArg('sortby')) {
66 if ($sorted == '+' . $this->_field)
67 $sortby = '-' . $this->_field;
68 elseif ($sorted == '-' . $this->_field)
69 $sortby = '+' . $this->_field;
71 $s = HTML::a(array('href' => $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby)),'class' => 'pagetitle', 'title' => sprintf(_("Sort by %s"),$this->_field)), HTML::nbsp(), HTML::u($this->_heading), HTML::nbsp());
73 $s = HTML(HTML::nbsp(), HTML::u($this->_heading), HTML::nbsp());
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_bool extends _PageList_Column {
103 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
104 $this->_PageList_Column($field, $default_heading, 'center');
105 $this->_textIfTrue = $text;
106 $this->_textIfFalse = new RawXml('—');
109 function _getValue ($page_handle, &$revision_handle) {
110 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
111 return $val ? $this->_textIfTrue : $this->_textIfFalse;
115 class _PageList_Column_checkbox extends _PageList_Column {
116 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
117 $this->_name = $name;
118 $this->_PageList_Column($field, $default_heading, 'center');
120 function _getValue ($pagelist, $page_handle, &$revision_handle) {
121 $pagename = $page_handle->getName();
122 if (!empty($pagelist->_selected[$pagename])) {
123 return HTML::input(array('type' => 'checkbox',
124 'name' => $this->_name . "[$pagename]",
125 'value' => $pagename,
128 return HTML::input(array('type' => 'checkbox',
129 'name' => $this->_name . "[$pagename]",
130 'value' => $pagename));
133 function format ($pagelist, $page_handle, &$revision_handle) {
134 return HTML::td($this->_tdattr,
136 $this->_getValue(&$pagelist, $page_handle, &$revision_handle),
141 class _PageList_Column_time extends _PageList_Column {
142 function _PageList_Column_time ($field, $default_heading) {
143 $this->_PageList_Column($field, $default_heading, 'right');
145 $this->Theme = &$Theme;
148 function _getValue ($page_handle, &$revision_handle) {
149 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
150 return $this->Theme->formatDateTime($time);
154 class _PageList_Column_version extends _PageList_Column {
155 function _getValue ($page_handle, &$revision_handle) {
156 if (!$revision_handle)
157 $revision_handle = $page_handle->getCurrentRevision();
158 return $revision_handle->getVersion();
162 class _PageList_Column_author extends _PageList_Column {
163 function _PageList_Column_author ($field, $default_heading, $align = false) {
164 _PageList_Column::_PageList_Column($field, $default_heading, $align);
165 global $WikiNameRegexp, $request;
166 $this->WikiNameRegexp = $WikiNameRegexp;
167 $this->dbi = &$request->getDbh();
170 function _getValue ($page_handle, &$revision_handle) {
171 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
172 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
173 return WikiLink($author);
179 class _PageList_Column_pagename extends _PageList_Column_base {
180 var $_field = 'pagename';
182 function _PageList_Column_pagename () {
183 $this->_PageList_Column_base(_("Page Name"));
185 $this->dbi = &$request->getDbh();
188 function _getValue ($page_handle, &$revision_handle) {
189 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
190 return WikiLink($page_handle);
192 return WikiLink($page_handle, 'unknown');
199 var $_group_rows = 3;
200 var $_columns = array();
201 var $_excluded_pages = array();
202 var $_rows = array();
204 var $_pagename_seen = false;
205 var $_types = array();
206 var $_options = array();
207 var $_selected = array();
209 function PageList ($columns = false, $exclude = false, $options = false) {
210 if ($columns == 'all') {
211 $this->_initAvailableColumns();
212 $columns = array_keys($this->_types);
216 if (!is_array($columns))
217 $columns = explode(',', $columns);
218 if (in_array('all',$columns)) { // e.g. 'checkbox,all'
219 $this->_initAvailableColumns();
220 $columns = array_merge($columns,array_keys($this->_types));
221 $columns = array_diff($columns,array('all'));
223 foreach ($columns as $col) {
224 $this->_addColumn($col);
227 $this->_addColumn('pagename');
230 if (!is_array($exclude))
231 $exclude = explode(',', $exclude);
232 $this->_excluded_pages = $exclude;
235 $this->_options = $options;
236 $this->_messageIfEmpty = _("<no matches>");
239 function setCaption ($caption_string) {
240 $this->_caption = $caption_string;
243 function getCaption () {
244 // put the total into the caption if needed
245 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
246 return sprintf($this->_caption, $this->getTotal());
247 return $this->_caption;
250 function setMessageIfEmpty ($msg) {
251 $this->_messageIfEmpty = $msg;
255 function getTotal () {
256 return count($this->_rows);
259 function isEmpty () {
260 return empty($this->_rows);
263 function addPage ($page_handle) {
264 if (is_string($page_handle)) {
265 if (in_array($page_handle, $this->_excluded_pages))
266 return; // exclude page.
267 $dbi = $GLOBALS['request']->getDbh();
268 $page_handle = $dbi->getPage($page_handle);
270 if (in_array($page_handle->getName(), $this->_excluded_pages))
271 return; // exclude page.
274 $group = (int)(count($this->_rows) / $this->_group_rows);
275 $class = ($group % 2) ? 'oddrow' : 'evenrow';
276 $revision_handle = false;
278 if (count($this->_columns) > 1) {
279 $row = HTML::tr(array('class' => $class));
280 foreach ($this->_columns as $col)
281 $row->pushContent($col->format(&$this, $page_handle, $revision_handle));
284 $col = $this->_columns[0];
285 $row = HTML::li(array('class' => $class),
286 $col->_getValue($page_handle, $revision_handle));
289 $this->_rows[] = $row;
292 function addPages ($page_iter) {
293 while ($page = $page_iter->next())
294 $this->addPage($page);
297 function addPageList (&$list) {
299 while ($page = next($list))
300 $this->addPage($page);
303 function getContent() {
304 // Note that the <caption> element wants inline content.
305 $caption = $this->getCaption();
307 if ($this->isEmpty())
308 return $this->_emptyList($caption);
309 elseif (count($this->_columns) == 1)
310 return $this->_generateList($caption);
312 return $this->_generateTable($caption);
315 function printXML() {
316 PrintXML($this->getContent());
320 return AsXML($this->getContent());
327 function _initAvailableColumns() {
328 if (!empty($this->_types))
334 => new _PageList_Column_checkbox('p', _("Selected")),
336 => new _PageList_Column_pagename,
339 => new _PageList_Column_time('rev:mtime',
342 => new _PageList_Column('hits', _("Hits"), 'right'),
345 => new _PageList_Column('rev:summary', _("Last Summary")),
348 => new _PageList_Column_version('rev:version', _("Version"),
351 => new _PageList_Column_author('rev:author',
354 => new _PageList_Column_bool('locked', _("Locked"),
357 => new _PageList_Column_bool('rev:is_minor_edit',
358 _("Minor Edit"), _("minor")),
360 => new _PageList_Column('rev:markup', _("Markup"))
364 function _addColumn ($column) {
366 $this->_initAvailableColumns();
368 if (isset($this->_columns_seen[$column]))
369 return false; // Already have this one.
370 $this->_columns_seen[$column] = true;
372 if (strstr($column, ':'))
373 list ($column, $heading) = explode(':', $column, 2);
375 if (!isset($this->_types[$column])) {
376 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
380 $col = $this->_types[$column];
381 if (!empty($heading))
382 $col->setHeading($heading);
384 $this->_columns[] = $col;
389 // make a table given the caption
390 function _generateTable($caption) {
391 $table = HTML::table(array('cellpadding' => 0,
394 'class' => 'pagelist'));
396 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
399 foreach ($this->_columns as $col) {
400 // Todo: add links to resort the table
401 $row->pushContent($col->heading());
402 $table_summary[] = $col->_heading;
404 // Table summary for non-visual browsers.
405 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
407 $table->pushContent(HTML::thead($row),
408 HTML::tbody(false, $this->_rows));
412 function _generateList($caption) {
413 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
414 return $caption ? HTML(HTML::p($caption), $list) : $list;
417 function _emptyList($caption) {
420 $html->pushContent(HTML::p($caption));
421 if ($this->_messageIfEmpty)
422 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
427 /* List pages with checkboxes to select from.
428 * Todo: All, None jscript buttons.
431 class PageList_Selectable
434 function PageList_Selectable ($columns=false, $exclude=false) {
435 PageList::PageList($columns,$exclude);
438 function addPageList ($array) {
439 while (list($pagename,$selected) = each($array)) {
440 if ($selected) $this->addPageSelected($pagename);
441 $this->addPage($pagename);
445 function addPageSelected ($pagename) {
446 $this->_selected[$pagename] = 1;
450 // (c-file-style: "gnu")
455 // c-hanging-comment-ender-p: nil
456 // indent-tabs-mode: nil