]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
Sizeable refactor, minor cleanups.
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.14 2002-01-22 05:06:50 dairiki Exp $');
2
3 // This relieves some work for these plugins:
4 //
5 // BackLinks, LikePages, Mostpopular, TitleSearch
6 //
7 // It also allows dynamic expansion of those plugins to include more
8 // columns in their output.
9 //
10 //
11 // There are still a few rough edges.
12 //
13 // FIXME: Make caption work properly with new HtmlElement
14 //
15 // FIXME: Add error recovery! If the column title isn't found due to a
16 //        typo, the wiki craps out, you will not even be able to edit the
17 //        page again to fix the typo in the column name!
18
19
20 // FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
21 // number-of-revision.  Note the _ctime and _cauthor as they were implemented
22 // were somewhat flawed: revision 1 of a page doesn't have to exist in the
23 // database.  If lots of revisions have been made to a page, it's more than likely
24 // that some older revisions (include revision 1) have been cleaned (deleted).
25
26 class _PageList_Column_base {
27     function _PageList_Column_base ($default_heading, $align = false) {
28         $this->_heading = $default_heading;
29
30         $this->_tdattr = array();
31         if ($align)
32             $this->_tdattr['align'] = $align;
33     }
34     
35     function format ($page_handle, &$revision_handle) {
36         return HTML::td($this->_tdattr,
37                         NBSP,
38                         $this->_getValue($page_handle, &$revision_handle),
39                         NBSP);
40     }
41
42     function setHeading ($heading) {
43         $this->_heading = $heading;
44     }
45     
46     function heading () {
47         return HTML::td(array('align' => 'center'),
48                         NBSP, HTML::u($this->_heading), NBSP);
49     }
50 };
51
52 class _PageList_Column extends _PageList_Column_base {
53     function _PageList_Column ($field, $default_heading, $align = false) {
54         $this->_PageList_Column_base($default_heading, $align);
55         
56         $this->_need_rev = substr($field, 0, 4) == 'rev:';
57         if ($this->_need_rev)
58             $this->_field = substr($field, 4);
59         else
60             $this->_field = $field;
61     }
62     
63     function _getValue ($page_handle, &$revision_handle) {
64         if ($this->_need_rev) {
65             if (!$revision_handle)
66                 $revision_handle = $page_handle->getCurrentRevision();
67             return $revision_handle->get($this->_field);
68         }
69         else {
70             return $page_handle->get($this->_field);
71         }
72     }
73 };
74
75 class _PageList_Column_bool extends _PageList_Column {
76     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
77         $this->_PageList_Column($field, $default_heading, 'center');
78         $this->_textIfTrue = $text;
79         $this->_textIfFalse = new RawXml('&#8212;');
80     }
81     
82     function _getValue ($page_handle, &$revision_handle) {
83         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
84         return $val ? $this->_textIfTrue : $this->_textIfFalse;
85     }
86 };
87
88 class _PageList_Column_time extends _PageList_Column {
89     function _PageList_Column_time ($field, $default_heading) {
90         $this->_PageList_Column($field, $default_heading, 'right');
91     }
92     
93     function _getValue ($page_handle, &$revision_handle) {
94         global $Theme;
95         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
96         return $Theme->formatDateTime($time);
97     }
98 };
99
100 class _PageList_Column_pagename extends _PageList_Column_base {
101     function _PageList_Column_pagename () {
102         $this->_PageList_Column_base(_("Page Name"));
103     }
104     
105     function _getValue ($page_handle, &$revision_handle) {
106         return LinkExistingWikiWord($page_handle->getName());
107     }
108 };
109
110         
111 class PageList {
112     function PageList () {
113         $this->_caption = "";
114         $this->_columns = array(new _PageList_Column_pagename);
115         $this->_pages = array();
116         $this->_messageIfEmpty = _("<no matches>");
117     }
118
119     function setCaption ($caption_string) {
120         $this->_caption = $caption_string;
121     }
122
123     function getCaption () {
124         // put the total into the caption if needed
125         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
126             return sprintf($this->_caption, $this->getTotal());
127         return $this->_caption;
128     }
129
130     function setMessageIfEmpty ($msg) {
131         $this->_messageIfEmpty = $msg;
132     }
133
134     /**
135      * Add a column to the listing.
136      *
137      * @input $column string Which column to add.
138      * $Column can be one of <ul>
139      * <li>mtime
140      * <li>hits
141      * <li>summary
142      * <li>author
143      * <li>locked
144      * <li>minor
145      * </ul>
146      *
147      * If you would like to specify an alternate heading for the
148      * column, concatenate the desired adding to $column, after adding
149      * a colon.  E.g. 'hits:Page Views'.
150      */ 
151     function addColumn ($column) {
152         if (($col = $this->_getColumn($new_columnname)))
153             array_push($this->_columns, $col);
154     }
155
156     function insertColumn ($new_columnname) {
157         if (($col = $this->_getColumn($new_columnname)))
158             array_unshift($this->_columns, $col);
159     }
160
161
162
163     function addPage ($page_handle) {
164         array_push($this->_pages, &$page_handle);
165     }
166
167     function getTotal () {
168         return count($this->_pages);
169     }
170
171     function isEmpty () {
172         return empty($this->_pages);
173     }
174     
175
176     function getContent() {
177         // Note that the <caption> element wants inline content.
178         $caption = $this->getCaption();
179
180         if ($this->isEmpty())
181             return $this->_emptyList($caption);
182         elseif (count($this->_columns) == 1)
183             return $this->_generateList($caption);
184         else
185             return $this->_generateTable($caption);
186     }
187
188     function printXML() {
189         PrintXML($this->getContent());
190     }
191
192     function asXML() {
193         return AsXML($this->getContent());
194     }
195
196
197     ////////////////////
198     // private
199     ////////////////////
200     function _getColumn ($column) {
201         static $types;
202         if (empty($types)) {
203             $types = array( 'mtime'
204                             => new _PageList_Column_time('rev:mtime', _("Last Modified")),
205                             'hits'
206                             => new _PageList_Column('hits',  _("Hits"), 'right'),
207                             'summary'
208                             => new _PageList_Column('rev:summary',  _("Last Summary")),
209                             'author'
210                             => new _PageList_Column('rev:author',  _("Last Author")),
211                             'locked'
212                             => new _PageList_Column_bool('locked',  _("Locked"), _("locked")),
213                             'minor'
214                             => new _PageList_Column_bool('rev:is_minor_edit',
215                                                          _("Minor Edit"), _("minor"))
216                             );
217         }
218
219         if (strstr($column, ':'))
220             list ($column, $heading) = explode(':', $column, 2);
221
222         if (!isset($types[$column])) {
223             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
224             return false;
225         }
226
227         $col = $types[$column];
228         if (!empty($heading))
229             $col->setHeading($heading);
230         return $col;
231     }
232         
233     
234     // make a table given the caption
235     function _generateTable($caption) {
236         $table = HTML::table(array('cellpadding' => 0,
237                                    'cellspacing' => 1,
238                                    'border'      => 0));
239         $table->setAttr('summary', "FIXME: add brief summary and column names");
240
241
242         if ($caption)
243             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
244
245         $row = HTML::tr();
246         foreach ($this->_columns as $col)
247             $row->pushContent($col->heading());
248         $table->pushContent(HTML::thead($row));
249         
250
251         $tbody = HTML::tbody();
252         foreach ($this->_pages as $page_handle) {
253             $row = HTML::tr();
254             $revision_handle = false;
255             foreach ($this->_columns as $col)
256                 $row->pushContent($col->format($page_handle, $revision_handle));
257             $tbody->pushContent($row);
258         }
259         $table->pushContent($tbody);
260         return $table;
261     }
262
263     function _generateList($caption) {
264         $list = HTML::ul();
265         foreach ($this->_pages as $page_handle) {
266             $list->pushContent(HTML::li(LinkWikiWord($page_handle->getName())));
267         }
268         if ($caption)
269             $html[] = HTML::p($caption);
270         $html[] = $list;
271         return $html;
272     }
273
274     function _emptyList($caption) {
275         $html = array();
276         if ($caption)
277             $html[] = HTML::p($caption);
278         if ($this->_messageIfEmpty)
279             $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
280         return $html;
281     }
282 };
283
284
285 // (c-file-style: "gnu")
286 // Local Variables:
287 // mode: php
288 // tab-width: 8
289 // c-basic-offset: 4
290 // c-hanging-comment-ender-p: nil
291 // indent-tabs-mode: nil
292 // End:   
293 ?>