]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
PageList sortby support in PearDB and ADODB backends
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.47 2004-01-25 07:58:29 rurban Exp $');
2
3 /**
4  * This library relieves some work for these plugins:
5  *
6  * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch and more
7  *
8  * It also allows dynamic expansion of those plugins to include more
9  * columns in their output.
10  *
11  * Column 'info=' arguments:
12  *
13  * 'pagename' _("Page Name")
14  * 'mtime'    _("Last Modified")
15  * 'hits'     _("Hits")
16  * 'summary'  _("Last Summary")
17  * 'version'  _("Version")),
18  * 'author'   _("Last Author")),
19  * 'locked'   _("Locked"), _("locked")
20  * 'minor'    _("Minor Edit"), _("minor")
21  * 'markup'   _("Markup")
22  * 'size'     _("Size")
23  * 'remove'   _("Remove") //admin action, not really an info column
24  *
25  * 'all'       All columns will be displayed. This argument must appear alone.
26  * 'checkbox'  A selectable checkbox appears at the left.
27  *
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).
33  *
34  * FIXME:
35  * The 'sortby' option is handled here correctly, but at the backends at 
36  * the page iterator not yet.
37  *
38  * TODO: order, sortby, limit, offset, rows arguments for multiple pages/multiple rows.
39  */
40 class _PageList_Column_base {
41     var $_tdattr = array();
42
43     function _PageList_Column_base ($default_heading, $align = false) {
44         $this->_heading = $default_heading;
45
46         if ($align) {
47             // align="char" isn't supported by any browsers yet :(
48             //if (is_array($align))
49             //    $this->_tdattr = $align;
50             //else
51             $this->_tdattr['align'] = $align;
52         }
53     }
54
55     function format ($pagelist, $page_handle, &$revision_handle) {
56         return HTML::td($this->_tdattr,
57                         HTML::raw('&nbsp;'),
58                         $this->_getValue($page_handle, $revision_handle),
59                         HTML::raw('&nbsp;'));
60     }
61
62     function setHeading ($heading) {
63         $this->_heading = $heading;
64     }
65
66     function 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')) {
72                 // flip order
73                 if ($sorted == '+' . $this->_field)
74                     $sortby = '-' . $this->_field;
75                 elseif ($sorted == '-' . $this->_field)
76                     $sortby = '+' . $this->_field;
77             }
78             $s = HTML::a(array('href' => $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby)),'class' => 'pagetitle', 'title' => sprintf(_("Sort by %s"),$this->_field)), HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
79         } else {
80             $s = HTML(HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
81         }
82         return HTML::td(array('align' => 'center'),$s);
83     }
84 };
85
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);
89
90         $this->_need_rev = substr($field, 0, 4) == 'rev:';
91         if ($this->_need_rev)
92             $this->_field = substr($field, 4);
93         else
94             $this->_field = $field;
95     }
96
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);
102         }
103         else {
104             return $page_handle->get($this->_field);
105         }
106     }
107 };
108
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);
114     }
115
116     function _getSize($revision_handle) {
117         $bytes = strlen($revision_handle->_data['%content']);
118         return ByteFormatter($bytes);
119     }
120 }
121
122
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('&#8212;'); //mdash
128     }
129
130     function _getValue ($page_handle, &$revision_handle) {
131         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
132         return $val ? $this->_textIfTrue : $this->_textIfFalse;
133     }
134 };
135
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');
140     }
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,
147                                      'checked' => '1'));
148         } else {
149             return HTML::input(array('type' => 'checkbox',
150                                      'name' => $this->_name . "[$pagename]",
151                                      'value' => $pagename));
152         }
153     }
154     function format ($pagelist, $page_handle, &$revision_handle) {
155         return HTML::td($this->_tdattr,
156                         HTML::raw('&nbsp;'),
157                         $this->_getValue($pagelist, $page_handle, $revision_handle),
158                         HTML::raw('&nbsp;'));
159     }
160 };
161
162 class _PageList_Column_time extends _PageList_Column {
163     function _PageList_Column_time ($field, $default_heading) {
164         $this->_PageList_Column($field, $default_heading, 'right');
165         global $Theme;
166         $this->Theme = &$Theme;
167     }
168
169     function _getValue ($page_handle, &$revision_handle) {
170         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
171         return $this->Theme->formatDateTime($time);
172     }
173 };
174
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();
180     }
181 };
182
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());
189     }
190 };
191
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);
198         $this->bytes = 50;
199         $this->_heading .= sprintf(_(" ... first %d bytes"),
200                                    $this->bytes);
201     }
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);
210         }
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));
219     }
220 };
221
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();
228     }
229
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);
234         else
235             return $author;
236     }
237 };
238
239 class _PageList_Column_pagename extends _PageList_Column_base {
240     var $_field = 'pagename';
241
242     function _PageList_Column_pagename () {
243         $this->_PageList_Column_base(_("Page Name"));
244         global $request;
245         $this->dbi = &$request->getDbh();
246     }
247
248     function _getValue ($page_handle, &$revision_handle) {
249         if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
250             return WikiLink($page_handle);
251         else
252             return WikiLink($page_handle, 'unknown');
253     }
254 };
255
256
257
258 class PageList {
259     var $_group_rows = 3;
260     var $_columns = array();
261     var $_excluded_pages = array();
262     var $_rows = array();
263     var $_caption = "";
264     var $_pagename_seen = false;
265     var $_types = array();
266     var $_options = array();
267     var $_selected = array();
268
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
275             // specified.
276         }
277
278         if ($columns) {
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'));
285             }
286             foreach ($columns as $col) {
287                 $this->_addColumn($col);
288             }
289         }
290         $this->_addColumn('pagename');
291
292         if ($exclude) {
293             if (!is_array($exclude))
294                 $exclude = explode(',', $exclude);
295             $this->_excluded_pages = $exclude;
296         }
297
298         $this->_options = $options;
299         $this->_messageIfEmpty = _("<no matches>");
300     }
301
302     function setCaption ($caption_string) {
303         $this->_caption = $caption_string;
304     }
305
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;
311     }
312
313     function setMessageIfEmpty ($msg) {
314         $this->_messageIfEmpty = $msg;
315     }
316
317
318     function getTotal () {
319         return count($this->_rows);
320     }
321
322     function isEmpty () {
323         return empty($this->_rows);
324     }
325
326     // $action = flip_order, db
327     function sortby ($string, $action) {
328         $order = '+';
329         if (substr($string,0,1) == '+') {
330             $order = '+'; $string = substr($string,1);
331         } elseif (substr($string,0,1) == '-') {
332             $order = '-'; $string = substr($string,1);
333         }
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');
341             }
342         }
343         return '';
344     }
345
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);
352         } else {
353           if (in_array($page_handle->getName(), $this->_excluded_pages))
354             return;             // exclude page.
355         }
356
357         $group = (int)(count($this->_rows) / $this->_group_rows);
358         $class = ($group % 2) ? 'oddrow' : 'evenrow';
359         $revision_handle = false;
360
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));
365         }
366         else {
367             $col = $this->_columns[0];
368             $row = HTML::li(array('class' => $class),
369                             $col->_getValue($page_handle, $revision_handle));
370         }
371
372         $this->_rows[] = $row;
373     }
374
375     function addPages ($page_iter) {
376         while ($page = $page_iter->next())
377             $this->addPage($page);
378     }
379
380     function addPageList (&$list) {
381         reset ($list);
382         while ($page = next($list))
383             $this->addPage($page);
384     }
385
386     function getContent() {
387         // Note that the <caption> element wants inline content.
388         $caption = $this->getCaption();
389
390         if ($this->isEmpty())
391             return $this->_emptyList($caption);
392         elseif (count($this->_columns) == 1)
393             return $this->_generateList($caption);
394         else
395             return $this->_generateTable($caption);
396     }
397
398     function printXML() {
399         PrintXML($this->getContent());
400     }
401
402     function asXML() {
403         return AsXML($this->getContent());
404     }
405
406
407     ////////////////////
408     // private
409     ////////////////////
410     function _initAvailableColumns() {
411         if (!empty($this->_types))
412             return;
413
414         $this->_types =
415             array(
416                   'content'
417                   => new _PageList_Column_content('content', _("Content")),
418
419                   'remove'
420                   => new _PageList_Column_remove('remove', _("Remove")),
421
422                   'checkbox'
423                   => new _PageList_Column_checkbox('p', _("Selected")),
424
425                   'pagename'
426                   => new _PageList_Column_pagename,
427
428                   'mtime'
429                   => new _PageList_Column_time('rev:mtime',
430                                                _("Last Modified")),
431                   'hits'
432                   => new _PageList_Column('hits', _("Hits"), 'right'),
433
434                   'size'
435                   => new _PageList_Column_size('size', _("Size"), 'right'),
436                                                /*array('align' => 'char', 'char' => ' ')*/
437
438                   'summary'
439                   => new _PageList_Column('rev:summary', _("Last Summary")),
440
441                   'version'
442                   => new _PageList_Column_version('rev:version', _("Version"),
443                                                   'right'),
444                   'author'
445                   => new _PageList_Column_author('rev:author',
446                                                  _("Last Author")),
447                   'locked'
448                   => new _PageList_Column_bool('locked', _("Locked"),
449                                                _("locked")),
450                   'minor'
451                   => new _PageList_Column_bool('rev:is_minor_edit',
452                                                _("Minor Edit"), _("minor")),
453                   'markup'
454                   => new _PageList_Column('rev:markup', _("Markup"))
455                   );
456     }
457
458     function _addColumn ($column) {
459
460         $this->_initAvailableColumns();
461
462         if (isset($this->_columns_seen[$column]))
463             return false;       // Already have this one.
464         $this->_columns_seen[$column] = true;
465
466         if (strstr($column, ':'))
467             list ($column, $heading) = explode(':', $column, 2);
468
469         if (!isset($this->_types[$column])) {
470             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
471             return false;
472         }
473
474         $col = $this->_types[$column];
475         if (!empty($heading))
476             $col->setHeading($heading);
477
478         $this->_columns[] = $col;
479
480         return true;
481     }
482
483     // make a table given the caption
484     function _generateTable($caption) {
485         $table = HTML::table(array('cellpadding' => 0,
486                                    'cellspacing' => 1,
487                                    'border'      => 0,
488                                    'class'       => 'pagelist'));
489         if ($caption)
490             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
491
492         $row = HTML::tr();
493         foreach ($this->_columns as $col) {
494             // Todo: add links to resort the table
495             $row->pushContent($col->heading());
496             $table_summary[] = $col->_heading;
497         }
498         // Table summary for non-visual browsers.
499         $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
500
501         $table->pushContent(HTML::thead($row),
502                             HTML::tbody(false, $this->_rows));
503         return $table;
504     }
505
506     function _generateList($caption) {
507         $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
508         return $caption ? HTML(HTML::p($caption), $list) : $list;
509     }
510
511     function _emptyList($caption) {
512         $html = HTML();
513         if ($caption)
514             $html->pushContent(HTML::p($caption));
515         if ($this->_messageIfEmpty)
516             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
517         return $html;
518     }
519 };
520
521 /* List pages with checkboxes to select from.
522  * Todo: All, None jscript buttons.
523  */
524
525 class PageList_Selectable
526 extends PageList {
527
528     function PageList_Selectable ($columns=false, $exclude=false) {
529         PageList::PageList($columns,$exclude);
530     }
531
532     function addPageList ($array) {
533         while (list($pagename,$selected) = each($array)) {
534             if ($selected) $this->addPageSelected($pagename);
535             $this->addPage($pagename);
536         }
537     }
538
539     function addPageSelected ($pagename) {
540         $this->_selected[$pagename] = 1;
541     }
542 }
543
544 // (c-file-style: "gnu")
545 // Local Variables:
546 // mode: php
547 // tab-width: 8
548 // c-basic-offset: 4
549 // c-hanging-comment-ender-p: nil
550 // indent-tabs-mode: nil
551 // End:
552 ?>