]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
Render nonexistant pagenames as unknown WikiLinks (for WantedPages). Moved globals...
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.38 2002-02-24 17:59:40 carstenklapp Exp $');
2
3 /**
4  * This library relieves some work for these plugins:
5  *
6  * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch
7  *
8  * It also allows dynamic expansion of those plugins to include more
9  * columns in their output.
10  *
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  *
24  * 'all'     All columns will be displayed. This argument must appear alone.
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 class _PageList_Column_base {
33     function _PageList_Column_base ($default_heading, $align = false) {
34         $this->_heading = $default_heading;
35
36         $this->_tdattr = array();
37         if ($align)
38             $this->_tdattr['align'] = $align;
39     }
40
41     function format ($page_handle, &$revision_handle) {
42         return HTML::td($this->_tdattr,
43                         NBSP,
44                         $this->_getValue($page_handle, &$revision_handle),
45                         NBSP);
46     }
47
48     function setHeading ($heading) {
49         $this->_heading = $heading;
50     }
51
52     function heading () {
53         return HTML::td(array('align' => 'center'),
54                         NBSP, HTML::u($this->_heading), NBSP);
55     }
56 };
57
58 class _PageList_Column extends _PageList_Column_base {
59     function _PageList_Column ($field, $default_heading, $align = false) {
60         $this->_PageList_Column_base($default_heading, $align);
61
62         $this->_need_rev = substr($field, 0, 4) == 'rev:';
63         if ($this->_need_rev)
64             $this->_field = substr($field, 4);
65         else
66             $this->_field = $field;
67     }
68
69     function _getValue ($page_handle, &$revision_handle) {
70         if ($this->_need_rev) {
71             if (!$revision_handle)
72                 $revision_handle = $page_handle->getCurrentRevision();
73             return $revision_handle->get($this->_field);
74         }
75         else {
76             return $page_handle->get($this->_field);
77         }
78     }
79 };
80
81 class _PageList_Column_bool extends _PageList_Column {
82     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
83         $this->_PageList_Column($field, $default_heading, 'center');
84         $this->_textIfTrue = $text;
85         $this->_textIfFalse = new RawXml('&#8212;');
86     }
87
88     function _getValue ($page_handle, &$revision_handle) {
89         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
90         return $val ? $this->_textIfTrue : $this->_textIfFalse;
91     }
92 };
93
94 class _PageList_Column_time extends _PageList_Column {
95     function _PageList_Column_time ($field, $default_heading) {
96         $this->_PageList_Column($field, $default_heading, 'right');
97         global $Theme;
98         $this->Theme = &$Theme;
99     }
100
101     function _getValue ($page_handle, &$revision_handle) {
102         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
103         return $this->Theme->formatDateTime($time);
104     }
105 };
106
107 class _PageList_Column_version extends _PageList_Column {
108     function _getValue ($page_handle, &$revision_handle) {
109         if (!$revision_handle)
110             $revision_handle = $page_handle->getCurrentRevision();
111         return $revision_handle->getVersion();
112     }
113 };
114
115 class _PageList_Column_author extends _PageList_Column {
116     function _PageList_Column_author ($field, $default_heading, $align = false) {
117         _PageList_Column::_PageList_Column($field, $default_heading, $align);
118         global $WikiNameRegexp, $request;
119         $this->WikiNameRegexp = $WikiNameRegexp;
120         $this->dbi = &$request->getDbh();
121     }
122
123     function _getValue ($page_handle, &$revision_handle) {
124         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
125         if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
126             return WikiLink($author);
127         else
128             return $author;
129     }
130 };
131
132 class _PageList_Column_pagename extends _PageList_Column_base {
133     function _PageList_Column_pagename () {
134         $this->_PageList_Column_base(_("Page Name"));
135         global $request;
136         $this->dbi = &$request->getDbh();
137     }
138
139     function _getValue ($page_handle, &$revision_handle) {
140         if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
141             return WikiLink($page_handle);
142         else
143             return WikiLink($page_handle, 'unknown');
144     }
145 };
146
147
148
149 class PageList {
150     var $_group_rows = 3;
151     var $_columns = array();
152     var $_excluded_pages = array();
153     var $_rows = array();
154     var $_caption = "";
155     var $_pagename_seen = false;
156     var $_types = array();
157
158     function PageList ($columns = false, $exclude = false) {
159         if ($columns == 'all') {
160             $this->_initAvailableColumns();
161             $columns = array_keys($this->_types);
162         }
163
164         if ($columns) {
165             if (!is_array($columns))
166                 $columns = explode(',', $columns);
167             foreach ($columns as $col)
168                 $this->_addColumn($col);
169         }
170         $this->_addColumn('pagename');
171
172         if ($exclude) {
173             if (!is_array($exclude))
174                 $exclude = explode(',', $exclude);
175             $this->_excluded_pages = $exclude;
176         }
177
178         $this->_messageIfEmpty = _("<no matches>");
179     }
180
181     function setCaption ($caption_string) {
182         $this->_caption = $caption_string;
183     }
184
185     function getCaption () {
186         // put the total into the caption if needed
187         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
188             return sprintf($this->_caption, $this->getTotal());
189         return $this->_caption;
190     }
191
192     function setMessageIfEmpty ($msg) {
193         $this->_messageIfEmpty = $msg;
194     }
195
196
197     function getTotal () {
198         return count($this->_rows);
199     }
200
201     function isEmpty () {
202         return empty($this->_rows);
203     }
204
205     function addPage ($page_handle) {
206         if (in_array($page_handle->getName(), $this->_excluded_pages))
207             return;             // exclude page.
208
209         $group = (int)(count($this->_rows) / $this->_group_rows);
210         $class = ($group % 2) ? 'oddrow' : 'evenrow';
211         $revision_handle = false;
212
213         if (count($this->_columns) > 1) {
214             $row = HTML::tr(array('class' => $class));
215             foreach ($this->_columns as $col)
216                 $row->pushContent($col->format($page_handle, $revision_handle));
217         }
218         else {
219             $col = $this->_columns[0];
220             $row = HTML::li(array('class' => $class),
221                             $col->_getValue($page_handle, $revision_handle));
222         }
223
224         $this->_rows[] = $row;
225     }
226
227     function addPages ($page_iter) {
228         while ($page = $page_iter->next())
229             $this->addPage($page);
230     }
231
232
233     function getContent() {
234         // Note that the <caption> element wants inline content.
235         $caption = $this->getCaption();
236
237         if ($this->isEmpty())
238             return $this->_emptyList($caption);
239         elseif (count($this->_columns) == 1)
240             return $this->_generateList($caption);
241         else
242             return $this->_generateTable($caption);
243     }
244
245     function printXML() {
246         PrintXML($this->getContent());
247     }
248
249     function asXML() {
250         return AsXML($this->getContent());
251     }
252
253
254     ////////////////////
255     // private
256     ////////////////////
257     function _initAvailableColumns() {
258         if (!empty($this->_types))
259             return;
260
261         $this->_types =
262             array('pagename'
263                   => new _PageList_Column_pagename,
264
265                   'mtime'
266                   => new _PageList_Column_time('rev:mtime',
267                                                _("Last Modified")),
268                   'hits'
269                   => new _PageList_Column('hits', _("Hits"), 'right'),
270
271                   'summary'
272                   => new _PageList_Column('rev:summary', _("Last Summary")),
273
274                   'version'
275                   => new _PageList_Column_version('rev:version', _("Version"),
276                                                   'right'),
277                   'author'
278                   => new _PageList_Column_author('rev:author',
279                                                  _("Last Author")),
280                   'locked'
281                   => new _PageList_Column_bool('locked', _("Locked"),
282                                                _("locked")),
283                   'minor'
284                   => new _PageList_Column_bool('rev:is_minor_edit',
285                                                _("Minor Edit"), _("minor")),
286                   'markup'
287                   => new _PageList_Column('rev:markup', _("Markup"))
288                   );
289     }
290
291     function _addColumn ($column) {
292
293         $this->_initAvailableColumns();
294
295         if (isset($this->_columns_seen[$column]))
296             return false;       // Already have this one.
297         $this->_columns_seen[$column] = true;
298
299
300
301
302
303
304         if (strstr($column, ':'))
305             list ($column, $heading) = explode(':', $column, 2);
306
307         if (!isset($this->_types[$column])) {
308             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
309             return false;
310         }
311
312         $col = $this->_types[$column];
313         if (!empty($heading))
314             $col->setHeading($heading);
315
316         $this->_columns[] = $col;
317
318         return true;
319     }
320
321     // make a table given the caption
322     function _generateTable($caption) {
323         $table = HTML::table(array('cellpadding' => 0,
324                                    'cellspacing' => 1,
325                                    'border'      => 0,
326                                    'class'       => 'pagelist'));
327         if ($caption)
328             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
329
330         $row = HTML::tr();
331         foreach ($this->_columns as $col) {
332             $row->pushContent($col->heading());
333             $table_summary[] = $col->_heading;
334         }
335         // Table summary for non-visual browsers.
336         $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
337
338         $table->pushContent(HTML::thead($row),
339                             HTML::tbody(false, $this->_rows));
340         return $table;
341     }
342
343     function _generateList($caption) {
344         $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
345         return $caption ? HTML(HTML::p($caption), $list) : $list;
346     }
347
348     function _emptyList($caption) {
349         $html = HTML();
350         if ($caption)
351             $html->pushContent(HTML::p($caption));
352         if ($this->_messageIfEmpty)
353             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
354         return $html;
355     }
356 };
357
358
359 // (c-file-style: "gnu")
360 // Local Variables:
361 // mode: php
362 // tab-width: 8
363 // c-basic-offset: 4
364 // c-hanging-comment-ender-p: nil
365 // indent-tabs-mode: nil
366 // End:
367 ?>