]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
added sortby support to explodePageList() and UnfoldSubpages
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.48 2004-01-25 10:52:18 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  * TODO: limit, offset, rows arguments for multiple pages/multiple rows.
35  */
36 class _PageList_Column_base {
37     var $_tdattr = array();
38
39     function _PageList_Column_base ($default_heading, $align = false) {
40         $this->_heading = $default_heading;
41
42         if ($align) {
43             // align="char" isn't supported by any browsers yet :(
44             //if (is_array($align))
45             //    $this->_tdattr = $align;
46             //else
47             $this->_tdattr['align'] = $align;
48         }
49     }
50
51     function format ($pagelist, $page_handle, &$revision_handle) {
52         return HTML::td($this->_tdattr,
53                         HTML::raw('&nbsp;'),
54                         $this->_getValue($page_handle, $revision_handle),
55                         HTML::raw('&nbsp;'));
56     }
57
58     function setHeading ($heading) {
59         $this->_heading = $heading;
60     }
61
62     function heading () {
63         if (in_array($this->_field,array('pagename','mtime','hits'))) {
64             // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
65             // asc or desc: +pagename, -pagename
66             $sortby = '+' . $this->_field;
67             if ($sorted = $GLOBALS['request']->getArg('sortby')) {
68                 // flip order
69                 if ($sorted == '+' . $this->_field)
70                     $sortby = '-' . $this->_field;
71                 elseif ($sorted == '-' . $this->_field)
72                     $sortby = '+' . $this->_field;
73             }
74             $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;'));
75         } else {
76             $s = HTML(HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
77         }
78         return HTML::td(array('align' => 'center'),$s);
79     }
80 };
81
82 class _PageList_Column extends _PageList_Column_base {
83     function _PageList_Column ($field, $default_heading, $align = false) {
84         $this->_PageList_Column_base($default_heading, $align);
85
86         $this->_need_rev = substr($field, 0, 4) == 'rev:';
87         if ($this->_need_rev)
88             $this->_field = substr($field, 4);
89         else
90             $this->_field = $field;
91     }
92
93     function _getValue ($page_handle, &$revision_handle) {
94         if ($this->_need_rev) {
95             if (!$revision_handle)
96                 $revision_handle = $page_handle->getCurrentRevision();
97             return $revision_handle->get($this->_field);
98         }
99         else {
100             return $page_handle->get($this->_field);
101         }
102     }
103 };
104
105 class _PageList_Column_size extends _PageList_Column {
106     function _getValue ($page_handle, &$revision_handle) {
107         if (!$revision_handle)
108             $revision_handle = $page_handle->getCurrentRevision();
109         return $this->_getSize($revision_handle);
110     }
111
112     function _getSize($revision_handle) {
113         $bytes = strlen($revision_handle->_data['%content']);
114         return ByteFormatter($bytes);
115     }
116 }
117
118
119 class _PageList_Column_bool extends _PageList_Column {
120     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
121         $this->_PageList_Column($field, $default_heading, 'center');
122         $this->_textIfTrue = $text;
123         $this->_textIfFalse = new RawXml('&#8212;'); //mdash
124     }
125
126     function _getValue ($page_handle, &$revision_handle) {
127         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
128         return $val ? $this->_textIfTrue : $this->_textIfFalse;
129     }
130 };
131
132 class _PageList_Column_checkbox extends _PageList_Column {
133     function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
134         $this->_name = $name;
135         $this->_PageList_Column($field, $default_heading, 'center');
136     }
137     function _getValue ($pagelist, $page_handle, &$revision_handle) {
138         $pagename = $page_handle->getName();
139         if (!empty($pagelist->_selected[$pagename])) {
140             return HTML::input(array('type' => 'checkbox',
141                                      'name' => $this->_name . "[$pagename]",
142                                      'value' => $pagename,
143                                      'checked' => '1'));
144         } else {
145             return HTML::input(array('type' => 'checkbox',
146                                      'name' => $this->_name . "[$pagename]",
147                                      'value' => $pagename));
148         }
149     }
150     function format ($pagelist, $page_handle, &$revision_handle) {
151         return HTML::td($this->_tdattr,
152                         HTML::raw('&nbsp;'),
153                         $this->_getValue($pagelist, $page_handle, $revision_handle),
154                         HTML::raw('&nbsp;'));
155     }
156 };
157
158 class _PageList_Column_time extends _PageList_Column {
159     function _PageList_Column_time ($field, $default_heading) {
160         $this->_PageList_Column($field, $default_heading, 'right');
161         global $Theme;
162         $this->Theme = &$Theme;
163     }
164
165     function _getValue ($page_handle, &$revision_handle) {
166         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
167         return $this->Theme->formatDateTime($time);
168     }
169 };
170
171 class _PageList_Column_version extends _PageList_Column {
172     function _getValue ($page_handle, &$revision_handle) {
173         if (!$revision_handle)
174             $revision_handle = $page_handle->getCurrentRevision();
175         return $revision_handle->getVersion();
176     }
177 };
178
179 // If needed this could eventually become a subclass
180 // of a new _PageList_Column_action class for other actions.
181 class _PageList_Column_remove extends _PageList_Column {
182     function _getValue ($page_handle, &$revision_handle) {
183         return Button(array('action' => 'remove'), _("Remove"),
184                       $page_handle->getName());
185     }
186 };
187
188 // Output is hardcoded to limit of first 50 bytes. Otherwise
189 // on very large Wikis this will fail if used with AllPages
190 // (PHP memory limit exceeded)
191 class _PageList_Column_content extends _PageList_Column {
192     function _PageList_Column_content ($field, $default_heading, $align = false) {
193         _PageList_Column::_PageList_Column($field, $default_heading, $align);
194         $this->bytes = 50;
195         $this->_heading .= sprintf(_(" ... first %d bytes"),
196                                    $this->bytes);
197     }
198     function _getValue ($page_handle, &$revision_handle) {
199         if (!$revision_handle)
200             $revision_handle = $page_handle->getCurrentRevision();
201         // Not sure why implode is needed here, I thought
202         // getContent() already did this, but it seems necessary.
203         $c = implode("\n", $revision_handle->getContent());
204         if (($len = strlen($c)) > $this->bytes) {
205             $c = substr($c, 0, $this->bytes);
206         }
207         include_once('lib/BlockParser.php');
208         // false --> don't bother processing hrefs for embedded WikiLinks
209         $ct = TransformText($c, $revision_handle->get('markup'), false);
210         return HTML::div(array('style' => 'font-size:xx-small'),
211                          HTML::div(array('class' => 'transclusion'), $ct),
212                          // TODO: Don't show bytes here if size column present too
213                          /* Howto??? $this->parent->_columns['size'] ? "" :*/
214                          ByteFormatter($len, /*$longformat = */true));
215     }
216 };
217
218 class _PageList_Column_author extends _PageList_Column {
219     function _PageList_Column_author ($field, $default_heading, $align = false) {
220         _PageList_Column::_PageList_Column($field, $default_heading, $align);
221         global $WikiNameRegexp, $request;
222         $this->WikiNameRegexp = $WikiNameRegexp;
223         $this->dbi = &$request->getDbh();
224     }
225
226     function _getValue ($page_handle, &$revision_handle) {
227         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
228         if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
229             return WikiLink($author);
230         else
231             return $author;
232     }
233 };
234
235 class _PageList_Column_pagename extends _PageList_Column_base {
236     var $_field = 'pagename';
237
238     function _PageList_Column_pagename () {
239         $this->_PageList_Column_base(_("Page Name"));
240         global $request;
241         $this->dbi = &$request->getDbh();
242     }
243
244     function _getValue ($page_handle, &$revision_handle) {
245         if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
246             return WikiLink($page_handle);
247         else
248             return WikiLink($page_handle, 'unknown');
249     }
250 };
251
252
253
254 class PageList {
255     var $_group_rows = 3;
256     var $_columns = array();
257     var $_excluded_pages = array();
258     var $_rows = array();
259     var $_caption = "";
260     var $_pagename_seen = false;
261     var $_types = array();
262     var $_options = array();
263     var $_selected = array();
264
265     function PageList ($columns = false, $exclude = false, $options = false) {
266         if ($columns == 'all') {
267             $this->_initAvailableColumns();
268             $columns = array_keys($this->_types);
269             // FIXME: Probably a good idea to NOT include the
270             // columns 'content' and 'remove' when 'all' is
271             // specified.
272         }
273
274         if ($columns) {
275             if (!is_array($columns))
276                 $columns = explode(',', $columns);
277             if (in_array('all',$columns)) { // e.g. 'checkbox,all'
278                 $this->_initAvailableColumns();
279                 $columns = array_merge($columns,array_keys($this->_types));
280                 $columns = array_diff($columns,array('all'));
281             }
282             foreach ($columns as $col) {
283                 $this->_addColumn($col);
284             }
285         }
286         $this->_addColumn('pagename');
287
288         if ($exclude) {
289             if (!is_array($exclude))
290                 $exclude = explode(',', $exclude);
291             $this->_excluded_pages = $exclude;
292         }
293
294         $this->_options = $options;
295         $this->_messageIfEmpty = _("<no matches>");
296     }
297
298     function setCaption ($caption_string) {
299         $this->_caption = $caption_string;
300     }
301
302     function getCaption () {
303         // put the total into the caption if needed
304         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
305             return sprintf($this->_caption, $this->getTotal());
306         return $this->_caption;
307     }
308
309     function setMessageIfEmpty ($msg) {
310         $this->_messageIfEmpty = $msg;
311     }
312
313
314     function getTotal () {
315         return count($this->_rows);
316     }
317
318     function isEmpty () {
319         return empty($this->_rows);
320     }
321
322     // $action = flip_order, db
323     function sortby ($string, $action) {
324         $order = '+';
325         if (substr($string,0,1) == '+') {
326             $order = '+'; $string = substr($string,1);
327         } elseif (substr($string,0,1) == '-') {
328             $order = '-'; $string = substr($string,1);
329         }
330         if (in_array($string,array('pagename','mtime','hits'))) {
331             // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
332             // asc or desc: +pagename, -pagename
333             if ($action == 'flip_order') {
334                 return ($order == '+' ? '-' : '+') . $string;
335             } elseif ($action == 'db') {
336                 return $string . ($order == '+' ? ' ASC' : ' DESC');
337             }
338         }
339         return '';
340     }
341
342     function addPage ($page_handle) {
343         if (is_string($page_handle)) {
344             if (in_array($page_handle, $this->_excluded_pages))
345                 return;             // exclude page.
346             $dbi = $GLOBALS['request']->getDbh();
347             $page_handle = $dbi->getPage($page_handle);
348         } else {
349           if (in_array($page_handle->getName(), $this->_excluded_pages))
350             return;             // exclude page.
351         }
352
353         $group = (int)(count($this->_rows) / $this->_group_rows);
354         $class = ($group % 2) ? 'oddrow' : 'evenrow';
355         $revision_handle = false;
356
357         if (count($this->_columns) > 1) {
358             $row = HTML::tr(array('class' => $class));
359             foreach ($this->_columns as $col)
360                 $row->pushContent($col->format($this, $page_handle, $revision_handle));
361         }
362         else {
363             $col = $this->_columns[0];
364             $row = HTML::li(array('class' => $class),
365                             $col->_getValue($page_handle, $revision_handle));
366         }
367
368         $this->_rows[] = $row;
369     }
370
371     function addPages ($page_iter) {
372         while ($page = $page_iter->next())
373             $this->addPage($page);
374     }
375
376     function addPageList (&$list) {
377         reset ($list);
378         while ($page = next($list))
379             $this->addPage($page);
380     }
381
382     function getContent() {
383         // Note that the <caption> element wants inline content.
384         $caption = $this->getCaption();
385
386         if ($this->isEmpty())
387             return $this->_emptyList($caption);
388         elseif (count($this->_columns) == 1)
389             return $this->_generateList($caption);
390         else
391             return $this->_generateTable($caption);
392     }
393
394     function printXML() {
395         PrintXML($this->getContent());
396     }
397
398     function asXML() {
399         return AsXML($this->getContent());
400     }
401
402
403     ////////////////////
404     // private
405     ////////////////////
406     function _initAvailableColumns() {
407         if (!empty($this->_types))
408             return;
409
410         $this->_types =
411             array(
412                   'content'
413                   => new _PageList_Column_content('content', _("Content")),
414
415                   'remove'
416                   => new _PageList_Column_remove('remove', _("Remove")),
417
418                   'checkbox'
419                   => new _PageList_Column_checkbox('p', _("Selected")),
420
421                   'pagename'
422                   => new _PageList_Column_pagename,
423
424                   'mtime'
425                   => new _PageList_Column_time('rev:mtime',
426                                                _("Last Modified")),
427                   'hits'
428                   => new _PageList_Column('hits', _("Hits"), 'right'),
429
430                   'size'
431                   => new _PageList_Column_size('size', _("Size"), 'right'),
432                                                /*array('align' => 'char', 'char' => ' ')*/
433
434                   'summary'
435                   => new _PageList_Column('rev:summary', _("Last Summary")),
436
437                   'version'
438                   => new _PageList_Column_version('rev:version', _("Version"),
439                                                   'right'),
440                   'author'
441                   => new _PageList_Column_author('rev:author',
442                                                  _("Last Author")),
443                   'locked'
444                   => new _PageList_Column_bool('locked', _("Locked"),
445                                                _("locked")),
446                   'minor'
447                   => new _PageList_Column_bool('rev:is_minor_edit',
448                                                _("Minor Edit"), _("minor")),
449                   'markup'
450                   => new _PageList_Column('rev:markup', _("Markup"))
451                   );
452     }
453
454     function _addColumn ($column) {
455
456         $this->_initAvailableColumns();
457
458         if (isset($this->_columns_seen[$column]))
459             return false;       // Already have this one.
460         $this->_columns_seen[$column] = true;
461
462         if (strstr($column, ':'))
463             list ($column, $heading) = explode(':', $column, 2);
464
465         if (!isset($this->_types[$column])) {
466             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
467             return false;
468         }
469
470         $col = $this->_types[$column];
471         if (!empty($heading))
472             $col->setHeading($heading);
473
474         $this->_columns[] = $col;
475
476         return true;
477     }
478
479     // make a table given the caption
480     function _generateTable($caption) {
481         $table = HTML::table(array('cellpadding' => 0,
482                                    'cellspacing' => 1,
483                                    'border'      => 0,
484                                    'class'       => 'pagelist'));
485         if ($caption)
486             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
487
488         $row = HTML::tr();
489         foreach ($this->_columns as $col) {
490             // Todo: add links to resort the table
491             $row->pushContent($col->heading());
492             $table_summary[] = $col->_heading;
493         }
494         // Table summary for non-visual browsers.
495         $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
496
497         $table->pushContent(HTML::thead($row),
498                             HTML::tbody(false, $this->_rows));
499         return $table;
500     }
501
502     function _generateList($caption) {
503         $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
504         return $caption ? HTML(HTML::p($caption), $list) : $list;
505     }
506
507     function _emptyList($caption) {
508         $html = HTML();
509         if ($caption)
510             $html->pushContent(HTML::p($caption));
511         if ($this->_messageIfEmpty)
512             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
513         return $html;
514     }
515 };
516
517 /* List pages with checkboxes to select from.
518  * Todo: All, None jscript buttons.
519  */
520
521 class PageList_Selectable
522 extends PageList {
523
524     function PageList_Selectable ($columns=false, $exclude=false) {
525         PageList::PageList($columns,$exclude);
526     }
527
528     function addPageList ($array) {
529         while (list($pagename,$selected) = each($array)) {
530             if ($selected) $this->addPageSelected($pagename);
531             $this->addPage($pagename);
532         }
533     }
534
535     function addPageSelected ($pagename) {
536         $this->_selected[$pagename] = 1;
537     }
538 }
539
540 // (c-file-style: "gnu")
541 // Local Variables:
542 // mode: php
543 // tab-width: 8
544 // c-basic-offset: 4
545 // c-hanging-comment-ender-p: nil
546 // indent-tabs-mode: nil
547 // End:
548 ?>