]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
Replaced NBSP constant with a new nbsp() function. The return value of HTML::nbsp...
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.41 2002-10-29 01:12:24 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  *
23  * 'all'       All columns will be displayed. This argument must appear alone.
24  * 'checkbox'  A selectable checkbox appears at the left.
25  *
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).
31  *
32  * FIXME:
33  * The 'sortby' option is handled here correctly, but at the backends at 
34  * the page iterator not yet.
35  *
36  * TODO: order, sortby, limit, offset, rows arguments for multiple pages/multiple rows.
37  */
38 class _PageList_Column_base {
39
40     function _PageList_Column_base ($default_heading, $align = false) {
41         $this->_heading = $default_heading;
42
43         $this->_tdattr = array();
44         if ($align)
45             $this->_tdattr['align'] = $align;
46     }
47
48     function format ($pagelist, $page_handle, &$revision_handle) {
49         return HTML::td($this->_tdattr,
50                         HTML::nbsp(),
51                         $this->_getValue($page_handle, &$revision_handle),
52                         HTML::nbsp());
53     }
54
55     function setHeading ($heading) {
56         $this->_heading = $heading;
57     }
58
59     function heading () {
60         if (in_array($this->_field,array('pagename','mtime','hits'))) {
61             // Todo: multiple comma-delimited sortby args: "+hits,+pagename"
62             // asc or desc: +pagename, -pagename
63             $sortby = '+' . $this->_field;
64             if ($sorted = $GLOBALS['request']->getArg('sortby')) {
65                 // flip order
66                 if ($sorted == '+' . $this->_field)
67                     $sortby = '-' . $this->_field;
68                 elseif ($sorted == '-' . $this->_field)
69                     $sortby = '+' . $this->_field;
70             }
71             $s = HTML::a(array('href' => $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby)),'class' => 'pagetitle', 'title' => sprintf(_("Sort by %s"),$this->_field)), HTML::nbsp(), HTML::u($this->_heading), HTML::nbsp());
72         } else {
73             $s = HTML(HTML::nbsp(), HTML::u($this->_heading), HTML::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_bool extends _PageList_Column {
103     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
104         $this->_PageList_Column($field, $default_heading, 'center');
105         $this->_textIfTrue = $text;
106         $this->_textIfFalse = new RawXml('&#8212;');
107     }
108
109     function _getValue ($page_handle, &$revision_handle) {
110         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
111         return $val ? $this->_textIfTrue : $this->_textIfFalse;
112     }
113 };
114
115 class _PageList_Column_checkbox extends _PageList_Column {
116     function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
117         $this->_name = $name;
118         $this->_PageList_Column($field, $default_heading, 'center');
119     }
120     function _getValue ($pagelist, $page_handle, &$revision_handle) {
121         $pagename = $page_handle->getName();
122         if (!empty($pagelist->_selected[$pagename])) {
123             return HTML::input(array('type' => 'checkbox',
124                                      'name' => $this->_name . "[$pagename]",
125                                      'value' => $pagename,
126                                      'checked' => '1'));
127         } else {
128             return HTML::input(array('type' => 'checkbox',
129                                      'name' => $this->_name . "[$pagename]",
130                                      'value' => $pagename));
131         }
132     }
133     function format ($pagelist, $page_handle, &$revision_handle) {
134         return HTML::td($this->_tdattr,
135                         HTML::nbsp(),
136                         $this->_getValue(&$pagelist, $page_handle, &$revision_handle),
137                         HTML::nbsp());
138     }
139 };
140
141 class _PageList_Column_time extends _PageList_Column {
142     function _PageList_Column_time ($field, $default_heading) {
143         $this->_PageList_Column($field, $default_heading, 'right');
144         global $Theme;
145         $this->Theme = &$Theme;
146     }
147
148     function _getValue ($page_handle, &$revision_handle) {
149         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
150         return $this->Theme->formatDateTime($time);
151     }
152 };
153
154 class _PageList_Column_version extends _PageList_Column {
155     function _getValue ($page_handle, &$revision_handle) {
156         if (!$revision_handle)
157             $revision_handle = $page_handle->getCurrentRevision();
158         return $revision_handle->getVersion();
159     }
160 };
161
162 class _PageList_Column_author extends _PageList_Column {
163     function _PageList_Column_author ($field, $default_heading, $align = false) {
164         _PageList_Column::_PageList_Column($field, $default_heading, $align);
165         global $WikiNameRegexp, $request;
166         $this->WikiNameRegexp = $WikiNameRegexp;
167         $this->dbi = &$request->getDbh();
168     }
169
170     function _getValue ($page_handle, &$revision_handle) {
171         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
172         if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
173             return WikiLink($author);
174         else
175             return $author;
176     }
177 };
178
179 class _PageList_Column_pagename extends _PageList_Column_base {
180     var $_field = 'pagename';
181
182     function _PageList_Column_pagename () {
183         $this->_PageList_Column_base(_("Page Name"));
184         global $request;
185         $this->dbi = &$request->getDbh();
186     }
187
188     function _getValue ($page_handle, &$revision_handle) {
189         if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
190             return WikiLink($page_handle);
191         else
192             return WikiLink($page_handle, 'unknown');
193     }
194 };
195
196
197
198 class PageList {
199     var $_group_rows = 3;
200     var $_columns = array();
201     var $_excluded_pages = array();
202     var $_rows = array();
203     var $_caption = "";
204     var $_pagename_seen = false;
205     var $_types = array();
206     var $_options = array();
207     var $_selected = array();
208
209     function PageList ($columns = false, $exclude = false, $options = false) {
210         if ($columns == 'all') {
211             $this->_initAvailableColumns();
212             $columns = array_keys($this->_types);
213         }
214
215         if ($columns) {
216             if (!is_array($columns))
217                 $columns = explode(',', $columns);
218             if (in_array('all',$columns)) { // e.g. 'checkbox,all'
219                 $this->_initAvailableColumns();
220                 $columns = array_merge($columns,array_keys($this->_types));
221                 $columns = array_diff($columns,array('all'));
222             }
223             foreach ($columns as $col) {
224                 $this->_addColumn($col);
225             }
226         }
227         $this->_addColumn('pagename');
228
229         if ($exclude) {
230             if (!is_array($exclude))
231                 $exclude = explode(',', $exclude);
232             $this->_excluded_pages = $exclude;
233         }
234
235         $this->_options = $options;
236         $this->_messageIfEmpty = _("<no matches>");
237     }
238
239     function setCaption ($caption_string) {
240         $this->_caption = $caption_string;
241     }
242
243     function getCaption () {
244         // put the total into the caption if needed
245         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
246             return sprintf($this->_caption, $this->getTotal());
247         return $this->_caption;
248     }
249
250     function setMessageIfEmpty ($msg) {
251         $this->_messageIfEmpty = $msg;
252     }
253
254
255     function getTotal () {
256         return count($this->_rows);
257     }
258
259     function isEmpty () {
260         return empty($this->_rows);
261     }
262
263     function addPage ($page_handle) {
264         if (is_string($page_handle)) {
265             if (in_array($page_handle, $this->_excluded_pages))
266                 return;             // exclude page.
267             $dbi = $GLOBALS['request']->getDbh();
268             $page_handle = $dbi->getPage($page_handle);
269         } else {
270           if (in_array($page_handle->getName(), $this->_excluded_pages))
271             return;             // exclude page.
272         }
273
274         $group = (int)(count($this->_rows) / $this->_group_rows);
275         $class = ($group % 2) ? 'oddrow' : 'evenrow';
276         $revision_handle = false;
277
278         if (count($this->_columns) > 1) {
279             $row = HTML::tr(array('class' => $class));
280             foreach ($this->_columns as $col)
281                 $row->pushContent($col->format(&$this, $page_handle, $revision_handle));
282         }
283         else {
284             $col = $this->_columns[0];
285             $row = HTML::li(array('class' => $class),
286                             $col->_getValue($page_handle, $revision_handle));
287         }
288
289         $this->_rows[] = $row;
290     }
291
292     function addPages ($page_iter) {
293         while ($page = $page_iter->next())
294             $this->addPage($page);
295     }
296
297     function addPageList (&$list) {
298         reset ($list);
299         while ($page = next($list))
300             $this->addPage($page);
301     }
302
303     function getContent() {
304         // Note that the <caption> element wants inline content.
305         $caption = $this->getCaption();
306
307         if ($this->isEmpty())
308             return $this->_emptyList($caption);
309         elseif (count($this->_columns) == 1)
310             return $this->_generateList($caption);
311         else
312             return $this->_generateTable($caption);
313     }
314
315     function printXML() {
316         PrintXML($this->getContent());
317     }
318
319     function asXML() {
320         return AsXML($this->getContent());
321     }
322
323
324     ////////////////////
325     // private
326     ////////////////////
327     function _initAvailableColumns() {
328         if (!empty($this->_types))
329             return;
330
331         $this->_types =
332             array(
333                   'checkbox'
334                   => new _PageList_Column_checkbox('p', _("Selected")),
335                   'pagename'
336                   => new _PageList_Column_pagename,
337
338                   'mtime'
339                   => new _PageList_Column_time('rev:mtime',
340                                                _("Last Modified")),
341                   'hits'
342                   => new _PageList_Column('hits', _("Hits"), 'right'),
343
344                   'summary'
345                   => new _PageList_Column('rev:summary', _("Last Summary")),
346
347                   'version'
348                   => new _PageList_Column_version('rev:version', _("Version"),
349                                                   'right'),
350                   'author'
351                   => new _PageList_Column_author('rev:author',
352                                                  _("Last Author")),
353                   'locked'
354                   => new _PageList_Column_bool('locked', _("Locked"),
355                                                _("locked")),
356                   'minor'
357                   => new _PageList_Column_bool('rev:is_minor_edit',
358                                                _("Minor Edit"), _("minor")),
359                   'markup'
360                   => new _PageList_Column('rev:markup', _("Markup"))
361                   );
362     }
363
364     function _addColumn ($column) {
365
366         $this->_initAvailableColumns();
367
368         if (isset($this->_columns_seen[$column]))
369             return false;       // Already have this one.
370         $this->_columns_seen[$column] = true;
371
372         if (strstr($column, ':'))
373             list ($column, $heading) = explode(':', $column, 2);
374
375         if (!isset($this->_types[$column])) {
376             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
377             return false;
378         }
379
380         $col = $this->_types[$column];
381         if (!empty($heading))
382             $col->setHeading($heading);
383
384         $this->_columns[] = $col;
385
386         return true;
387     }
388
389     // make a table given the caption
390     function _generateTable($caption) {
391         $table = HTML::table(array('cellpadding' => 0,
392                                    'cellspacing' => 1,
393                                    'border'      => 0,
394                                    'class'       => 'pagelist'));
395         if ($caption)
396             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
397
398         $row = HTML::tr();
399         foreach ($this->_columns as $col) {
400             // Todo: add links to resort the table
401             $row->pushContent($col->heading());
402             $table_summary[] = $col->_heading;
403         }
404         // Table summary for non-visual browsers.
405         $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
406
407         $table->pushContent(HTML::thead($row),
408                             HTML::tbody(false, $this->_rows));
409         return $table;
410     }
411
412     function _generateList($caption) {
413         $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
414         return $caption ? HTML(HTML::p($caption), $list) : $list;
415     }
416
417     function _emptyList($caption) {
418         $html = HTML();
419         if ($caption)
420             $html->pushContent(HTML::p($caption));
421         if ($this->_messageIfEmpty)
422             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
423         return $html;
424     }
425 };
426
427 /* List pages with checkboxes to select from.
428  * Todo: All, None jscript buttons.
429  */
430
431 class PageList_Selectable
432 extends PageList {
433
434     function PageList_Selectable ($columns=false, $exclude=false) {
435         PageList::PageList($columns,$exclude);
436     }
437
438     function addPageList ($array) {
439         while (list($pagename,$selected) = each($array)) {
440             if ($selected) $this->addPageSelected($pagename);
441             $this->addPage($pagename);
442         }
443     }
444
445     function addPageSelected ($pagename) {
446         $this->_selected[$pagename] = 1;
447     }
448 }
449
450 // (c-file-style: "gnu")
451 // Local Variables:
452 // mode: php
453 // tab-width: 8
454 // c-basic-offset: 4
455 // c-hanging-comment-ender-p: nil
456 // indent-tabs-mode: nil
457 // End:
458 ?>