]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
don't add duplicate page names
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.28 2002-01-29 20:04:19 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 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, $Theme;
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 $Theme->linkExistingWikiWord($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         global $Theme;
130         return $Theme->LinkExistingWikiWord($page_handle->getName());
131     }
132 };
133
134         
135 class PageList {
136     function PageList () {
137         $this->_caption = "";
138         $this->_columns = array(new _PageList_Column_pagename);
139         $this->_pages = array();
140         $this->_messageIfEmpty = _("<no matches>");
141         $this->_group_rows = 3;
142     }
143
144     function setCaption ($caption_string) {
145         $this->_caption = $caption_string;
146     }
147
148     function getCaption () {
149         // put the total into the caption if needed
150         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
151             return sprintf($this->_caption, $this->getTotal());
152         return $this->_caption;
153     }
154
155     function setMessageIfEmpty ($msg) {
156         $this->_messageIfEmpty = $msg;
157     }
158
159     /**
160      * Add a column to the listing.
161      *
162      * @input $column string Which column to add.
163      * $Column can be one of <ul>
164      * <li>mtime
165      * <li>hits
166      * <li>summary
167      * <li>version
168      * <li>author
169      * <li>locked
170      * <li>minor
171      * </ul>
172      *
173      * If you would like to specify an alternate heading for the
174      * column, concatenate the desired adding to $column, after adding
175      * a colon.  E.g. 'hits:Page Views'.
176      */ 
177     function addColumn ($new_columnname) {
178         if (($col = $this->_getColumn($new_columnname))) {
179            if(! $this->column_exists($col->_heading)) {
180                 array_push($this->_columns, $col);
181             }
182         }
183     }
184
185     function insertColumn ($new_columnname) {
186         if (($col = $this->_getColumn($new_columnname))) {
187            if(! $this->column_exists($col->_heading)) {
188                 array_unshift($this->_columns, $col);
189             }
190         }
191     }
192
193     function column_exists ($heading) {
194         foreach ($this->_columns as $val) {
195             if ($val->_heading == $heading)
196                 return true;
197         }
198         return false;
199     }
200
201     function page_exists ($page) {
202         foreach ($this->_pages as $val) {
203             if ($val->getName() == $page->getName())
204                 return true;
205         }
206         return false;
207     }
208
209     function addPage ($page_handle) {
210         if(! $this->page_exists(&$page_handle)) {
211             array_push($this->_pages, &$page_handle);
212         }
213     }
214
215     function getTotal () {
216         return count($this->_pages);
217     }
218
219     function isEmpty () {
220         return empty($this->_pages);
221     }
222     
223
224     function getContent() {
225         // Note that the <caption> element wants inline content.
226         $caption = $this->getCaption();
227
228         if ($this->isEmpty())
229             return $this->_emptyList($caption);
230         elseif (count($this->_columns) == 1)
231             return $this->_generateList($caption);
232         else
233             return $this->_generateTable($caption);
234     }
235
236     function printXML() {
237         PrintXML($this->getContent());
238     }
239
240     function asXML() {
241         return AsXML($this->getContent());
242     }
243
244
245     ////////////////////
246     // private
247     ////////////////////
248     function _getColumn ($column) {
249         static $types;
250         if (empty($types)) {
251             $types = array( 'mtime'
252                             => new _PageList_Column_time('rev:mtime', _("Last Modified")),
253                             'hits'
254                             => new _PageList_Column('hits',  _("Hits"), 'right'),
255                             'summary'
256                             => new _PageList_Column('rev:summary',  _("Last Summary")),
257                             'version'
258                             => new _PageList_Column_version('rev:version',  _("Version"), 'right'),
259                             'author'
260                             => new _PageList_Column_author('rev:author',  _("Last Author")),
261                             'locked'
262                             => new _PageList_Column_bool('locked',  _("Locked"), _("locked")),
263                             'minor'
264                             => new _PageList_Column_bool('rev:is_minor_edit',
265                                                          _("Minor Edit"), _("minor"))
266                             );
267         }
268
269         if (strstr($column, ':'))
270             list ($column, $heading) = explode(':', $column, 2);
271
272         if (!isset($types[$column])) {
273             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
274             return false;
275         }
276
277         $col = $types[$column];
278         if (!empty($heading))
279             $col->setHeading($heading);
280         return $col;
281     }
282         
283     
284     // make a table given the caption
285     function _generateTable($caption) {
286         $table = HTML::table(array('cellpadding' => 0,
287                                    'cellspacing' => 1,
288                                    'border'      => 0,
289                                    'class'       => 'pagelist'));
290         $table->setAttr('summary', "FIXME: add brief summary and column names");
291
292
293         if ($caption)
294             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
295
296         $row = HTML::tr();
297         foreach ($this->_columns as $col)
298             $row->pushContent($col->heading());
299         $table->pushContent(HTML::thead($row));
300         
301
302         $tbody = HTML::tbody();
303         $n = 0;
304         foreach ($this->_pages as $page_handle) {
305             $row = HTML::tr();
306             $revision_handle = false;
307             foreach ($this->_columns as $col) {
308                 $row->pushContent($col->format($page_handle, $revision_handle));
309             }
310             $group = (int)($n++ / $this->_group_rows);
311             $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
312             $tbody->pushContent($row);
313         }
314         $table->pushContent($tbody);
315         return $table;
316     }
317
318     function _generateList($caption) {
319         $list = HTML::ul(array('class' => 'pagelist'));
320         $n = 0;
321         foreach ($this->_pages as $page_handle) {
322             $group = (int)($n++ / $this->_group_rows);
323             $class = ($group % 2) ? 'oddrow' : 'evenrow';
324
325             $list->pushContent(HTML::li(array('class' => $class),
326                                         LinkWikiWord($page_handle->getName())));
327         }
328
329         return $caption ? HTML(HTML::p($caption), $list) : $list;
330     }
331
332     function _emptyList($caption) {
333         $html = HTML();
334         if ($caption)
335             $html->pushContent(HTML::p($caption));
336         if ($this->_messageIfEmpty)
337             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
338         return $html;
339     }
340 };
341
342
343 // (c-file-style: "gnu")
344 // Local Variables:
345 // mode: php
346 // tab-width: 8
347 // c-basic-offset: 4
348 // c-hanging-comment-ender-p: nil
349 // indent-tabs-mode: nil
350 // End:   
351 ?>