1 <?php rcs_id('$Id: PageList.php,v 1.29 2002-01-30 18:25:18 carstenklapp Exp $');
4 * This library relieves some work for these plugins:
6 * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch
8 * It also allows dynamic expansion of those plugins to include more
9 * columns in their output.
14 * 'mtime' _("Last Modified")
16 * 'summary' _("Last Summary")
17 * 'Version' _("Version")),
18 * 'author' _("Last Author")),
19 * 'locked' _("Locked"), _("locked")
20 * 'minor' _("Minor Edit"), _("minor")
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).
28 class _PageList_Column_base {
29 function _PageList_Column_base ($default_heading, $align = false) {
30 $this->_heading = $default_heading;
32 $this->_tdattr = array();
34 $this->_tdattr['align'] = $align;
37 function format ($page_handle, &$revision_handle) {
38 return HTML::td($this->_tdattr,
40 $this->_getValue($page_handle, &$revision_handle),
44 function setHeading ($heading) {
45 $this->_heading = $heading;
49 return HTML::td(array('align' => 'center'),
50 NBSP, HTML::u($this->_heading), NBSP);
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);
58 $this->_need_rev = substr($field, 0, 4) == 'rev:';
60 $this->_field = substr($field, 4);
62 $this->_field = $field;
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);
72 return $page_handle->get($this->_field);
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('—');
84 function _getValue ($page_handle, &$revision_handle) {
85 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
86 return $val ? $this->_textIfTrue : $this->_textIfFalse;
90 class _PageList_Column_time extends _PageList_Column {
91 function _PageList_Column_time ($field, $default_heading) {
92 $this->_PageList_Column($field, $default_heading, 'right');
95 function _getValue ($page_handle, &$revision_handle) {
97 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
98 return $Theme->formatDateTime($time);
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();
110 class _PageList_Column_author extends _PageList_Column {
111 function _getValue ($page_handle, &$revision_handle) {
112 global $WikiNameRegexp, $request, $Theme;
113 $dbi = $request->getDbh();
115 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
116 if (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author))
117 return $Theme->linkExistingWikiWord($author);
123 class _PageList_Column_pagename extends _PageList_Column_base {
124 function _PageList_Column_pagename () {
125 $this->_PageList_Column_base(_("Page Name"));
128 function _getValue ($page_handle, &$revision_handle) {
130 return $Theme->LinkExistingWikiWord($page_handle->getName());
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;
145 function setCaption ($caption_string) {
146 $this->_caption = $caption_string;
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;
156 function setMessageIfEmpty ($msg) {
157 $this->_messageIfEmpty = $msg;
161 * Add a column to the listing.
163 * @input $column string Which column to add.
164 * $Column can be one of <ul>
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'.
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);
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);
194 function column_exists ($heading) {
195 foreach ($this->_columns as $val) {
196 if ($val->_heading == $heading)
202 function page_exists ($page) {
203 foreach ($this->_pages as $val) {
204 if ($val->getName() == $page->getName())
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);
218 function excludePageName ($pagename) {
219 if(! $this->page_excluded($pagename)) {
220 array_push($this->_pages_excluded, $pagename);
224 function page_excluded ($pagename) {
225 foreach ($this->_pages_excluded as $val) {
226 if ($val == $pagename)
232 function getTotal () {
233 return count($this->_pages);
236 function isEmpty () {
237 return empty($this->_pages);
241 function getContent() {
242 // Note that the <caption> element wants inline content.
243 $caption = $this->getCaption();
245 if ($this->isEmpty())
246 return $this->_emptyList($caption);
247 elseif (count($this->_columns) == 1)
248 return $this->_generateList($caption);
250 return $this->_generateTable($caption);
253 function printXML() {
254 PrintXML($this->getContent());
258 return AsXML($this->getContent());
265 function _getColumn ($column) {
268 $types = array( 'mtime'
269 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
271 => new _PageList_Column('hits', _("Hits"), 'right'),
273 => new _PageList_Column('rev:summary', _("Last Summary")),
275 => new _PageList_Column_version('rev:version', _("Version"), 'right'),
277 => new _PageList_Column_author('rev:author', _("Last Author")),
279 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
281 => new _PageList_Column_bool('rev:is_minor_edit',
282 _("Minor Edit"), _("minor"))
286 if (strstr($column, ':'))
287 list ($column, $heading) = explode(':', $column, 2);
289 if (!isset($types[$column])) {
290 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
294 $col = $types[$column];
295 if (!empty($heading))
296 $col->setHeading($heading);
301 // make a table given the caption
302 function _generateTable($caption) {
303 $table = HTML::table(array('cellpadding' => 0,
306 'class' => 'pagelist'));
307 $table->setAttr('summary', "FIXME: add brief summary and column names");
311 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
314 foreach ($this->_columns as $col)
315 $row->pushContent($col->heading());
316 $table->pushContent(HTML::thead($row));
319 $tbody = HTML::tbody();
321 foreach ($this->_pages as $page_handle) {
323 $revision_handle = false;
324 foreach ($this->_columns as $col) {
325 $row->pushContent($col->format($page_handle, $revision_handle));
327 $group = (int)($n++ / $this->_group_rows);
328 $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
329 $tbody->pushContent($row);
331 $table->pushContent($tbody);
335 function _generateList($caption) {
336 $list = HTML::ul(array('class' => 'pagelist'));
338 foreach ($this->_pages as $page_handle) {
339 $group = (int)($n++ / $this->_group_rows);
340 $class = ($group % 2) ? 'oddrow' : 'evenrow';
342 $list->pushContent(HTML::li(array('class' => $class),
343 LinkWikiWord($page_handle->getName())));
346 return $caption ? HTML(HTML::p($caption), $list) : $list;
349 function _emptyList($caption) {
352 $html->pushContent(HTML::p($caption));
353 if ($this->_messageIfEmpty)
354 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
360 // (c-file-style: "gnu")
365 // c-hanging-comment-ender-p: nil
366 // indent-tabs-mode: nil