]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
added page exclusion functions
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.29 2002-01-30 18:25:18 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->_pages_excluded = array();
141         $this->_messageIfEmpty = _("<no matches>");
142         $this->_group_rows = 3;
143     }
144
145     function setCaption ($caption_string) {
146         $this->_caption = $caption_string;
147     }
148
149     function getCaption () {
150         // put the total into the caption if needed
151         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
152             return sprintf($this->_caption, $this->getTotal());
153         return $this->_caption;
154     }
155
156     function setMessageIfEmpty ($msg) {
157         $this->_messageIfEmpty = $msg;
158     }
159
160     /**
161      * Add a column to the listing.
162      *
163      * @input $column string Which column to add.
164      * $Column can be one of <ul>
165      * <li>mtime
166      * <li>hits
167      * <li>summary
168      * <li>version
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     function page_exists ($page) {
203         foreach ($this->_pages as $val) {
204             if ($val->getName() == $page->getName())
205                 return true;
206         }
207         return false;
208     }
209
210     function addPage ($page_handle) {
211         if(! $this->page_excluded($page_handle->getName())) {
212             if(! $this->page_exists(&$page_handle)) {
213                 array_push($this->_pages, &$page_handle);
214             }
215         }
216     }
217
218     function excludePageName ($pagename) {
219         if(! $this->page_excluded($pagename)) {
220             array_push($this->_pages_excluded, $pagename);
221         }
222     }
223
224     function page_excluded ($pagename) {
225         foreach ($this->_pages_excluded as $val) {
226             if ($val == $pagename)
227                 return true;
228         }
229         return false;
230     }
231
232     function getTotal () {
233         return count($this->_pages);
234     }
235
236     function isEmpty () {
237         return empty($this->_pages);
238     }
239     
240
241     function getContent() {
242         // Note that the <caption> element wants inline content.
243         $caption = $this->getCaption();
244
245         if ($this->isEmpty())
246             return $this->_emptyList($caption);
247         elseif (count($this->_columns) == 1)
248             return $this->_generateList($caption);
249         else
250             return $this->_generateTable($caption);
251     }
252
253     function printXML() {
254         PrintXML($this->getContent());
255     }
256
257     function asXML() {
258         return AsXML($this->getContent());
259     }
260
261
262     ////////////////////
263     // private
264     ////////////////////
265     function _getColumn ($column) {
266         static $types;
267         if (empty($types)) {
268             $types = array( 'mtime'
269                             => new _PageList_Column_time('rev:mtime', _("Last Modified")),
270                             'hits'
271                             => new _PageList_Column('hits',  _("Hits"), 'right'),
272                             'summary'
273                             => new _PageList_Column('rev:summary',  _("Last Summary")),
274                             'version'
275                             => new _PageList_Column_version('rev:version',  _("Version"), 'right'),
276                             'author'
277                             => new _PageList_Column_author('rev:author',  _("Last Author")),
278                             'locked'
279                             => new _PageList_Column_bool('locked',  _("Locked"), _("locked")),
280                             'minor'
281                             => new _PageList_Column_bool('rev:is_minor_edit',
282                                                          _("Minor Edit"), _("minor"))
283                             );
284         }
285
286         if (strstr($column, ':'))
287             list ($column, $heading) = explode(':', $column, 2);
288
289         if (!isset($types[$column])) {
290             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
291             return false;
292         }
293
294         $col = $types[$column];
295         if (!empty($heading))
296             $col->setHeading($heading);
297         return $col;
298     }
299         
300     
301     // make a table given the caption
302     function _generateTable($caption) {
303         $table = HTML::table(array('cellpadding' => 0,
304                                    'cellspacing' => 1,
305                                    'border'      => 0,
306                                    'class'       => 'pagelist'));
307         $table->setAttr('summary', "FIXME: add brief summary and column names");
308
309
310         if ($caption)
311             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
312
313         $row = HTML::tr();
314         foreach ($this->_columns as $col)
315             $row->pushContent($col->heading());
316         $table->pushContent(HTML::thead($row));
317         
318
319         $tbody = HTML::tbody();
320         $n = 0;
321         foreach ($this->_pages as $page_handle) {
322             $row = HTML::tr();
323             $revision_handle = false;
324             foreach ($this->_columns as $col) {
325                 $row->pushContent($col->format($page_handle, $revision_handle));
326             }
327             $group = (int)($n++ / $this->_group_rows);
328             $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
329             $tbody->pushContent($row);
330         }
331         $table->pushContent($tbody);
332         return $table;
333     }
334
335     function _generateList($caption) {
336         $list = HTML::ul(array('class' => 'pagelist'));
337         $n = 0;
338         foreach ($this->_pages as $page_handle) {
339             $group = (int)($n++ / $this->_group_rows);
340             $class = ($group % 2) ? 'oddrow' : 'evenrow';
341
342             $list->pushContent(HTML::li(array('class' => $class),
343                                         LinkWikiWord($page_handle->getName())));
344         }
345
346         return $caption ? HTML(HTML::p($caption), $list) : $list;
347     }
348
349     function _emptyList($caption) {
350         $html = HTML();
351         if ($caption)
352             $html->pushContent(HTML::p($caption));
353         if ($this->_messageIfEmpty)
354             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
355         return $html;
356     }
357 };
358
359
360 // (c-file-style: "gnu")
361 // Local Variables:
362 // mode: php
363 // tab-width: 8
364 // c-basic-offset: 4
365 // c-hanging-comment-ender-p: nil
366 // indent-tabs-mode: nil
367 // End:   
368 ?>