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