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