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