]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
added info=some + info=most, simplified info handling
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.50 2004-02-11 19:46:54 rurban Exp $');
2
3 /**
4  * List a number of pagenames, optionally as table with various columns.
5  * This library relieves some work for these plugins:
6  *
7  * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch and more
8  *
9  * It also allows dynamic expansion of those plugins to include more
10  * columns in their output.
11  *
12  * Column 'info=' arguments:
13  *
14  * 'pagename' _("Page Name")
15  * 'mtime'    _("Last Modified")
16  * 'hits'     _("Hits")
17  * 'summary'  _("Last Summary")
18  * 'version'  _("Version")),
19  * 'author'   _("Last Author")),
20  * 'locked'   _("Locked"), _("locked")
21  * 'minor'    _("Minor Edit"), _("minor")
22  * 'markup'   _("Markup")
23  * 'size'     _("Size")
24  * 'remove'   _("Remove") //todo: move this admin action away, not really an info column
25  *
26  * 'checkbox'  A selectable checkbox appears at the left.
27  * 'all'       All columns except remove, content and renamed_pagename
28  * 'most'      pagename, mtime, author, size, hits, ...
29  * 'some'      pagename, mtime, author
30  *
31  * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
32  * number-of-revision.  Note the _ctime and _cauthor as they were implemented
33  * were somewhat flawed: revision 1 of a page doesn't have to exist in the
34  * database.  If lots of revisions have been made to a page, it's more than likely
35  * that some older revisions (include revision 1) have been cleaned (deleted).
36  *
37  * TODO: limit, offset, rows arguments for multiple pages/multiple rows.
38  *       check PagePerm "list" access-type
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             // multiple comma-delimited sortby args: "+hits,+pagename"
69             // asc or desc: +pagename, -pagename
70             $sortby = PageList::sortby($this->_field,'flip_order');
71             $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;'));
72         } else {
73             $s = HTML(HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
74         }
75         return HTML::td(array('align' => 'center'),$s);
76     }
77 };
78
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);
82
83         $this->_need_rev = substr($field, 0, 4) == 'rev:';
84         if ($this->_need_rev)
85             $this->_field = substr($field, 4);
86         else
87             $this->_field = $field;
88     }
89
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);
95         }
96         else {
97             return $page_handle->get($this->_field);
98         }
99     }
100 };
101
102 class _PageList_Column_size extends _PageList_Column {
103     function _getValue ($page_handle, &$revision_handle) {
104         if (!$revision_handle)
105             $revision_handle = $page_handle->getCurrentRevision();
106         return $this->_getSize($revision_handle);
107     }
108
109     function _getSize($revision_handle) {
110         $bytes = strlen($revision_handle->_data['%content']);
111         return ByteFormatter($bytes);
112     }
113 }
114
115
116 class _PageList_Column_bool extends _PageList_Column {
117     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
118         $this->_PageList_Column($field, $default_heading, 'center');
119         $this->_textIfTrue = $text;
120         $this->_textIfFalse = new RawXml('&#8212;'); //mdash
121     }
122
123     function _getValue ($page_handle, &$revision_handle) {
124         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
125         return $val ? $this->_textIfTrue : $this->_textIfFalse;
126     }
127 };
128
129 class _PageList_Column_checkbox extends _PageList_Column {
130     function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
131         $this->_name = $name;
132         $this->_PageList_Column($field, $default_heading, 'center');
133     }
134     function _getValue ($pagelist, $page_handle, &$revision_handle) {
135         $pagename = $page_handle->getName();
136         if (!empty($pagelist->_selected[$pagename])) {
137             return HTML::input(array('type' => 'checkbox',
138                                      'name' => $this->_name . "[$pagename]",
139                                      'value' => $pagename,
140                                      'checked' => '1'));
141         } else {
142             return HTML::input(array('type' => 'checkbox',
143                                      'name' => $this->_name . "[$pagename]",
144                                      'value' => $pagename));
145         }
146     }
147     function format ($pagelist, $page_handle, &$revision_handle) {
148         return HTML::td($this->_tdattr,
149                         HTML::raw('&nbsp;'),
150                         $this->_getValue($pagelist, $page_handle, $revision_handle),
151                         HTML::raw('&nbsp;'));
152     }
153 };
154
155 class _PageList_Column_time extends _PageList_Column {
156     function _PageList_Column_time ($field, $default_heading) {
157         $this->_PageList_Column($field, $default_heading, 'right');
158         global $Theme;
159         $this->Theme = &$Theme;
160     }
161
162     function _getValue ($page_handle, &$revision_handle) {
163         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
164         return $this->Theme->formatDateTime($time);
165     }
166 };
167
168 class _PageList_Column_version extends _PageList_Column {
169     function _getValue ($page_handle, &$revision_handle) {
170         if (!$revision_handle)
171             $revision_handle = $page_handle->getCurrentRevision();
172         return $revision_handle->getVersion();
173     }
174 };
175
176 // If needed this could eventually become a subclass
177 // of a new _PageList_Column_action class for other actions.
178 // only for WikiAdminRemove or WikiAdminSelect
179 class _PageList_Column_remove extends _PageList_Column {
180     function _getValue ($page_handle, &$revision_handle) {
181         return Button(array('action' => 'remove'), _("Remove"),
182                       $page_handle->getName());
183     }
184 };
185
186 // only for WikiAdminRename
187 class _PageList_Column_renamed_pagename extends _PageList_Column {
188     function _getValue ($page_handle, &$revision_handle) {
189         $post_args = $GLOBALS['request']->getArg('admin_rename');
190         $value = str_replace($post_args['from'], $post_args['to'],$page_handle->getName());
191         return HTML::div(" => ",HTML::input(array('type' => 'text',
192                                                   'name' => 'rename[]',
193                                                   'value' => $value)));
194     }
195 };
196
197 // Output is hardcoded to limit of first 50 bytes. Otherwise
198 // on very large Wikis this will fail if used with AllPages
199 // (PHP memory limit exceeded)
200 class _PageList_Column_content extends _PageList_Column {
201     function _PageList_Column_content ($field, $default_heading, $align = false) {
202         _PageList_Column::_PageList_Column($field, $default_heading, $align);
203         $this->bytes = 50;
204         $this->_heading .= sprintf(_(" ... first %d bytes"),
205                                    $this->bytes);
206     }
207     function _getValue ($page_handle, &$revision_handle) {
208         if (!$revision_handle)
209             $revision_handle = $page_handle->getCurrentRevision();
210         // Not sure why implode is needed here, I thought
211         // getContent() already did this, but it seems necessary.
212         $c = implode("\n", $revision_handle->getContent());
213         if (($len = strlen($c)) > $this->bytes) {
214             $c = substr($c, 0, $this->bytes);
215         }
216         include_once('lib/BlockParser.php');
217         // false --> don't bother processing hrefs for embedded WikiLinks
218         $ct = TransformText($c, $revision_handle->get('markup'), false);
219         return HTML::div(array('style' => 'font-size:xx-small'),
220                          HTML::div(array('class' => 'transclusion'), $ct),
221                          // TODO: Don't show bytes here if size column present too
222                          /* Howto??? $this->parent->_columns['size'] ? "" :*/
223                          ByteFormatter($len, /*$longformat = */true));
224     }
225 };
226
227 class _PageList_Column_author extends _PageList_Column {
228     function _PageList_Column_author ($field, $default_heading, $align = false) {
229         _PageList_Column::_PageList_Column($field, $default_heading, $align);
230         global $WikiNameRegexp, $request;
231         $this->WikiNameRegexp = $WikiNameRegexp;
232         $this->dbi = &$request->getDbh();
233     }
234
235     function _getValue ($page_handle, &$revision_handle) {
236         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
237         if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
238             return WikiLink($author);
239         else
240             return $author;
241     }
242 };
243
244 class _PageList_Column_pagename extends _PageList_Column_base {
245     var $_field = 'pagename';
246
247     function _PageList_Column_pagename () {
248         $this->_PageList_Column_base(_("Page Name"));
249         global $request;
250         $this->dbi = &$request->getDbh();
251     }
252
253     function _getValue ($page_handle, &$revision_handle) {
254         if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
255             return WikiLink($page_handle);
256         else
257             return WikiLink($page_handle, 'unknown');
258     }
259 };
260
261
262
263 class PageList {
264     var $_group_rows = 3;
265     var $_columns = array();
266     var $_excluded_pages = array();
267     var $_rows = array();
268     var $_caption = "";
269     var $_pagename_seen = false;
270     var $_types = array();
271     var $_options = array();
272     var $_selected = array();
273
274     function PageList ($columns = false, $exclude = false, $options = false) {
275         $this->_initAvailableColumns();
276         $symbolic_columns = 
277             array(
278                   'all' =>  array_diff(array_keys($this->_types),
279                                        array('checkbox','remove','renamed_pagename','content')),
280                   'most' => array('pagename','mtime','author','size','hits'),
281                   'some' => array('pagename','mtime','author')
282                   );
283         if ($columns) {
284             if (!is_array($columns))
285                 $columns = explode(',', $columns);
286             // expand symbolic columns:
287             foreach ($symbolic_columns as $symbol => $cols) {
288                 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
289                     $columns = array_diff(array_merge($columns,$cols),array($symbol));
290                 }
291             }
292             foreach ($columns as $col) {
293                 $this->_addColumn($col);
294             }
295         }
296         $this->_addColumn('pagename');
297
298         if ($exclude) {
299             if (!is_array($exclude))
300                 $exclude = explode(',', $exclude);
301             $this->_excluded_pages = $exclude;
302         }
303
304         $this->_options = $options;
305         $this->_messageIfEmpty = _("<no matches>");
306     }
307
308     function setCaption ($caption_string) {
309         $this->_caption = $caption_string;
310     }
311
312     function getCaption () {
313         // put the total into the caption if needed
314         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
315             return sprintf($this->_caption, $this->getTotal());
316         return $this->_caption;
317     }
318
319     function setMessageIfEmpty ($msg) {
320         $this->_messageIfEmpty = $msg;
321     }
322
323
324     function getTotal () {
325         return count($this->_rows);
326     }
327
328     function isEmpty () {
329         return empty($this->_rows);
330     }
331
332     function addPage ($page_handle) {
333         if (is_string($page_handle)) {
334             if (in_array($page_handle, $this->_excluded_pages))
335                 return;             // exclude page.
336             $dbi = $GLOBALS['request']->getDbh();
337             $page_handle = $dbi->getPage($page_handle);
338         } else {
339           if (in_array($page_handle->getName(), $this->_excluded_pages))
340             return;             // exclude page.
341         }
342
343         $group = (int)(count($this->_rows) / $this->_group_rows);
344         $class = ($group % 2) ? 'oddrow' : 'evenrow';
345         $revision_handle = false;
346
347         if (count($this->_columns) > 1) {
348             $row = HTML::tr(array('class' => $class));
349             foreach ($this->_columns as $col)
350                 $row->pushContent($col->format($this, $page_handle, $revision_handle));
351         }
352         else {
353             $col = $this->_columns[0];
354             $row = HTML::li(array('class' => $class),
355                             $col->_getValue($page_handle, $revision_handle));
356         }
357
358         $this->_rows[] = $row;
359     }
360
361     function addPages ($page_iter) {
362         while ($page = $page_iter->next())
363             $this->addPage($page);
364     }
365
366     function addPageList (&$list) {
367         reset ($list);
368         while ($page = next($list))
369             $this->addPage($page);
370     }
371
372     function getContent() {
373         // Note that the <caption> element wants inline content.
374         $caption = $this->getCaption();
375
376         if ($this->isEmpty())
377             return $this->_emptyList($caption);
378         elseif (count($this->_columns) == 1)
379             return $this->_generateList($caption);
380         else
381             return $this->_generateTable($caption);
382     }
383
384     function printXML() {
385         PrintXML($this->getContent());
386     }
387
388     function asXML() {
389         return AsXML($this->getContent());
390     }
391
392     /** 
393      * handles sortby requests for the DB iterator and table header links
394      * prefix the column with + or - like "+pagename","-mtime", ...
395      * supported column: 'pagename','mtime','hits'
396      * supported action: 'flip_order', 'db'
397      */
398     function sortby ($column, $action) {
399         if (substr($column,0,1) == '+') {
400             $order = '+'; $column = substr($column,1);
401         } elseif (substr($column,0,1) == '-') {
402             $order = '-'; $column = substr($column,1);
403         }
404         if (in_array($column,array('pagename','mtime','hits'))) {
405             // default order: +pagename, -mtime, -hits
406             if (empty($order))
407                 if (in_array($column,array('mtime','hits')))
408                     $order = '-';
409                 else
410                     $order = '+';
411             //TODO: multiple comma-delimited sortby args: "+hits,+pagename"
412             if ($action == 'flip_order') {
413                 return ($order == '+' ? '-' : '+') . $column;
414             } elseif ($action == 'db') {
415                 // asc or desc: +pagename, -pagename
416                 return $column . ($order == '+' ? ' ASC' : ' DESC');
417             }
418         }
419         return '';
420     }
421
422     // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
423     function explodePageList($input, $perm = false, $sortby = '') {
424         // expand wildcards from list of all pages
425         if (preg_match('/[\?\*]/',$input)) {
426             $dbi = $GLOBALS['request']->getDbh();
427             $allPagehandles = $dbi->getAllPages($perm,$sortby);
428             while ($pagehandle = $allPagehandles->next()) {
429                 $allPages[] = $pagehandle->getName();
430             }
431             return explodeList($input, $allPages);
432         } else {
433             //TODO: do the sorting
434             return explode(',',$input);
435         }
436     }
437
438
439     ////////////////////
440     // private
441     ////////////////////
442     function _initAvailableColumns() {
443         if (!empty($this->_types))
444             return;
445
446         $this->_types =
447             array(
448                   'content'
449                   => new _PageList_Column_content('content', _("Content")),
450
451                   'remove'
452                   => new _PageList_Column_remove('remove', _("Remove")),
453
454                   'renamed_pagename'
455                   => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
456
457                   'checkbox'
458                   => new _PageList_Column_checkbox('p', _("Selected")),
459
460                   'pagename'
461                   => new _PageList_Column_pagename,
462
463                   'mtime'
464                   => new _PageList_Column_time('rev:mtime',
465                                                _("Last Modified")),
466                   'hits'
467                   => new _PageList_Column('hits', _("Hits"), 'right'),
468
469                   'size'
470                   => new _PageList_Column_size('size', _("Size"), 'right'),
471                                                /*array('align' => 'char', 'char' => ' ')*/
472
473                   'summary'
474                   => new _PageList_Column('rev:summary', _("Last Summary")),
475
476                   'version'
477                   => new _PageList_Column_version('rev:version', _("Version"),
478                                                   'right'),
479                   'author'
480                   => new _PageList_Column_author('rev:author',
481                                                  _("Last Author")),
482                   'locked'
483                   => new _PageList_Column_bool('locked', _("Locked"),
484                                                _("locked")),
485                   'minor'
486                   => new _PageList_Column_bool('rev:is_minor_edit',
487                                                _("Minor Edit"), _("minor")),
488                   'markup'
489                   => new _PageList_Column('rev:markup', _("Markup"))
490                   );
491     }
492
493     function _addColumn ($column) {
494
495         $this->_initAvailableColumns();
496
497         if (isset($this->_columns_seen[$column]))
498             return false;       // Already have this one.
499         $this->_columns_seen[$column] = true;
500
501         if (strstr($column, ':'))
502             list ($column, $heading) = explode(':', $column, 2);
503
504         if (!isset($this->_types[$column])) {
505             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
506             return false;
507         }
508
509         $col = $this->_types[$column];
510         if (!empty($heading))
511             $col->setHeading($heading);
512
513         $this->_columns[] = $col;
514
515         return true;
516     }
517
518     // make a table given the caption
519     function _generateTable($caption) {
520         $table = HTML::table(array('cellpadding' => 0,
521                                    'cellspacing' => 1,
522                                    'border'      => 0,
523                                    'class'       => 'pagelist'));
524         if ($caption)
525             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
526
527         $row = HTML::tr();
528         foreach ($this->_columns as $col) {
529             //TODO: add links to resort the table
530             $row->pushContent($col->heading());
531             $table_summary[] = $col->_heading;
532         }
533         // Table summary for non-visual browsers.
534         $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
535
536         $table->pushContent(HTML::thead($row),
537                             HTML::tbody(false, $this->_rows));
538         return $table;
539     }
540
541     function _generateList($caption) {
542         $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
543         return $caption ? HTML(HTML::p($caption), $list) : $list;
544     }
545
546     function _emptyList($caption) {
547         $html = HTML();
548         if ($caption)
549             $html->pushContent(HTML::p($caption));
550         if ($this->_messageIfEmpty)
551             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
552         return $html;
553     }
554 };
555
556 /* List pages with checkboxes to select from.
557  * Todo: All, None jscript buttons.
558  */
559
560 class PageList_Selectable
561 extends PageList {
562
563     function PageList_Selectable ($columns=false, $exclude=false) {
564         PageList::PageList($columns,$exclude);
565     }
566
567     function addPageList ($array) {
568         while (list($pagename,$selected) = each($array)) {
569             if ($selected) $this->addPageSelected($pagename);
570             $this->addPage($pagename);
571         }
572     }
573
574     function addPageSelected ($pagename) {
575         $this->_selected[$pagename] = 1;
576     }
577     //Todo:
578     //insert javascript when clicked on Selected Select/Deselect all
579 }
580
581 // (c-file-style: "gnu")
582 // Local Variables:
583 // mode: php
584 // tab-width: 8
585 // c-basic-offset: 4
586 // c-hanging-comment-ender-p: nil
587 // indent-tabs-mode: nil
588 // End:
589 ?>