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