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