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