1 <?php rcs_id('$Id: PageList.php,v 1.39 2002-08-27 21:51:31 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 * '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: 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 // asc or desc: +pagename, -pagename
62 $sortby = '+' . $this->_field;
63 if ($sorted = $GLOBALS['request']->getArg('sortby')) {
65 if ($sorted == '+' . $this->_field)
66 $sortby = '-' . $this->_field;
67 elseif ($sorted == '-' . $this->_field)
68 $sortby = '+' . $this->_field;
70 $s = HTML::a(array('href' => $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby)),'class' => 'pagetitle', 'title' => sprintf(_("Sort by %s"),$this->_field)), NBSP, HTML::u($this->_heading), NBSP);
72 $s = HTML(NBSP, HTML::u($this->_heading), NBSP);
74 return HTML::td(array('align' => 'center'),$s);
78 class _PageList_Column extends _PageList_Column_base {
79 function _PageList_Column ($field, $default_heading, $align = false) {
80 $this->_PageList_Column_base($default_heading, $align);
82 $this->_need_rev = substr($field, 0, 4) == 'rev:';
84 $this->_field = substr($field, 4);
86 $this->_field = $field;
89 function _getValue ($page_handle, &$revision_handle) {
90 if ($this->_need_rev) {
91 if (!$revision_handle)
92 $revision_handle = $page_handle->getCurrentRevision();
93 return $revision_handle->get($this->_field);
96 return $page_handle->get($this->_field);
101 class _PageList_Column_bool extends _PageList_Column {
102 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
103 $this->_PageList_Column($field, $default_heading, 'center');
104 $this->_textIfTrue = $text;
105 $this->_textIfFalse = new RawXml('—');
108 function _getValue ($page_handle, &$revision_handle) {
109 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
110 return $val ? $this->_textIfTrue : $this->_textIfFalse;
114 class _PageList_Column_checkbox extends _PageList_Column {
115 function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
116 $this->_name = $name;
117 $this->_PageList_Column($field, $default_heading, 'center');
119 function _getValue ($pagelist, $page_handle, &$revision_handle) {
120 $pagename = $page_handle->getName();
121 if (!empty($pagelist->_selected[$pagename])) {
122 return HTML::input(array('type' => 'checkbox',
123 'name' => $this->_name . "[$pagename]",
124 'value' => $pagename,
127 return HTML::input(array('type' => 'checkbox',
128 'name' => $this->_name . "[$pagename]",
129 'value' => $pagename));
132 function format ($pagelist, $page_handle, &$revision_handle) {
133 return HTML::td($this->_tdattr,
135 $this->_getValue(&$pagelist, $page_handle, &$revision_handle),
140 class _PageList_Column_time extends _PageList_Column {
141 function _PageList_Column_time ($field, $default_heading) {
142 $this->_PageList_Column($field, $default_heading, 'right');
144 $this->Theme = &$Theme;
147 function _getValue ($page_handle, &$revision_handle) {
148 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
149 return $this->Theme->formatDateTime($time);
153 class _PageList_Column_version extends _PageList_Column {
154 function _getValue ($page_handle, &$revision_handle) {
155 if (!$revision_handle)
156 $revision_handle = $page_handle->getCurrentRevision();
157 return $revision_handle->getVersion();
161 class _PageList_Column_author extends _PageList_Column {
162 function _PageList_Column_author ($field, $default_heading, $align = false) {
163 _PageList_Column::_PageList_Column($field, $default_heading, $align);
164 global $WikiNameRegexp, $request;
165 $this->WikiNameRegexp = $WikiNameRegexp;
166 $this->dbi = &$request->getDbh();
169 function _getValue ($page_handle, &$revision_handle) {
170 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
171 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
172 return WikiLink($author);
178 class _PageList_Column_pagename extends _PageList_Column_base {
179 var $_field = 'pagename';
181 function _PageList_Column_pagename () {
182 $this->_PageList_Column_base(_("Page Name"));
184 $this->dbi = &$request->getDbh();
187 function _getValue ($page_handle, &$revision_handle) {
188 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
189 return WikiLink($page_handle);
191 return WikiLink($page_handle, 'unknown');
198 var $_group_rows = 3;
199 var $_columns = array();
200 var $_excluded_pages = array();
201 var $_rows = array();
203 var $_pagename_seen = false;
204 var $_types = array();
205 var $_options = array();
206 var $_selected = array();
208 function PageList ($columns = false, $exclude = false, $options = false) {
209 if ($columns == 'all') {
210 $this->_initAvailableColumns();
211 $columns = array_keys($this->_types);
215 if (!is_array($columns))
216 $columns = explode(',', $columns);
217 if (in_array('all',$columns)) { // e.g. 'checkbox,all'
218 $this->_initAvailableColumns();
219 $columns = array_merge($columns,array_keys($this->_types));
220 $columns = array_diff($columns,array('all'));
222 foreach ($columns as $col) {
223 $this->_addColumn($col);
226 $this->_addColumn('pagename');
229 if (!is_array($exclude))
230 $exclude = explode(',', $exclude);
231 $this->_excluded_pages = $exclude;
234 $this->_options = $options;
235 $this->_messageIfEmpty = _("<no matches>");
238 function setCaption ($caption_string) {
239 $this->_caption = $caption_string;
242 function getCaption () {
243 // put the total into the caption if needed
244 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
245 return sprintf($this->_caption, $this->getTotal());
246 return $this->_caption;
249 function setMessageIfEmpty ($msg) {
250 $this->_messageIfEmpty = $msg;
254 function getTotal () {
255 return count($this->_rows);
258 function isEmpty () {
259 return empty($this->_rows);
262 function addPage ($page_handle) {
263 if (is_string($page_handle)) {
264 if (in_array($page_handle, $this->_excluded_pages))
265 return; // exclude page.
266 $dbi = $GLOBALS['request']->getDbh();
267 $page_handle = $dbi->getPage($page_handle);
269 if (in_array($page_handle->getName(), $this->_excluded_pages))
270 return; // exclude page.
273 $group = (int)(count($this->_rows) / $this->_group_rows);
274 $class = ($group % 2) ? 'oddrow' : 'evenrow';
275 $revision_handle = false;
277 if (count($this->_columns) > 1) {
278 $row = HTML::tr(array('class' => $class));
279 foreach ($this->_columns as $col)
280 $row->pushContent($col->format(&$this, $page_handle, $revision_handle));
283 $col = $this->_columns[0];
284 $row = HTML::li(array('class' => $class),
285 $col->_getValue($page_handle, $revision_handle));
288 $this->_rows[] = $row;
291 function addPages ($page_iter) {
292 while ($page = $page_iter->next())
293 $this->addPage($page);
296 function addPageList (&$list) {
298 while ($page = next($list))
299 $this->addPage($page);
302 function getContent() {
303 // Note that the <caption> element wants inline content.
304 $caption = $this->getCaption();
306 if ($this->isEmpty())
307 return $this->_emptyList($caption);
308 elseif (count($this->_columns) == 1)
309 return $this->_generateList($caption);
311 return $this->_generateTable($caption);
314 function printXML() {
315 PrintXML($this->getContent());
319 return AsXML($this->getContent());
326 function _initAvailableColumns() {
327 if (!empty($this->_types))
333 => new _PageList_Column_checkbox('p', _("Selected")),
335 => new _PageList_Column_pagename,
338 => new _PageList_Column_time('rev:mtime',
341 => new _PageList_Column('hits', _("Hits"), 'right'),
344 => new _PageList_Column('rev:summary', _("Last Summary")),
347 => new _PageList_Column_version('rev:version', _("Version"),
350 => new _PageList_Column_author('rev:author',
353 => new _PageList_Column_bool('locked', _("Locked"),
356 => new _PageList_Column_bool('rev:is_minor_edit',
357 _("Minor Edit"), _("minor")),
359 => new _PageList_Column('rev:markup', _("Markup"))
363 function _addColumn ($column) {
365 $this->_initAvailableColumns();
367 if (isset($this->_columns_seen[$column]))
368 return false; // Already have this one.
369 $this->_columns_seen[$column] = true;
371 if (strstr($column, ':'))
372 list ($column, $heading) = explode(':', $column, 2);
374 if (!isset($this->_types[$column])) {
375 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
379 $col = $this->_types[$column];
380 if (!empty($heading))
381 $col->setHeading($heading);
383 $this->_columns[] = $col;
388 // make a table given the caption
389 function _generateTable($caption) {
390 $table = HTML::table(array('cellpadding' => 0,
393 'class' => 'pagelist'));
395 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
398 foreach ($this->_columns as $col) {
399 // Todo: add links to resort the table
400 $row->pushContent($col->heading());
401 $table_summary[] = $col->_heading;
403 // Table summary for non-visual browsers.
404 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
406 $table->pushContent(HTML::thead($row),
407 HTML::tbody(false, $this->_rows));
411 function _generateList($caption) {
412 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
413 return $caption ? HTML(HTML::p($caption), $list) : $list;
416 function _emptyList($caption) {
419 $html->pushContent(HTML::p($caption));
420 if ($this->_messageIfEmpty)
421 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
426 /* List pages with checkboxes to select from.
427 * Todo: All, None jscript buttons.
430 class PageList_Selectable
433 function PageList_Selectable ($columns=false, $exclude=false) {
434 PageList::PageList($columns,$exclude);
437 function addPageList ($array) {
438 while (list($pagename,$selected) = each($array)) {
439 if ($selected) $this->addPageSelected($pagename);
440 $this->addPage($pagename);
444 function addPageSelected ($pagename) {
445 $this->_selected[$pagename] = 1;
449 // (c-file-style: "gnu")
454 // c-hanging-comment-ender-p: nil
455 // indent-tabs-mode: nil