1 <?php rcs_id('$Id: PageList.php,v 1.26 2002-01-27 04:23:26 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->_messageIfEmpty = _("<no matches>");
141 $this->_group_rows = 3;
144 function setCaption ($caption_string) {
145 $this->_caption = $caption_string;
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;
155 function setMessageIfEmpty ($msg) {
156 $this->_messageIfEmpty = $msg;
160 * Add a column to the listing.
162 * @input $column string Which column to add.
163 * $Column can be one of <ul>
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'.
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);
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);
193 function column_exists ($heading) {
194 foreach ($this->_columns as $val) {
195 if ($val->_heading == $heading)
202 function addPage ($page_handle) {
203 array_push($this->_pages, &$page_handle);
206 function getTotal () {
207 return count($this->_pages);
210 function isEmpty () {
211 return empty($this->_pages);
215 function getContent() {
216 // Note that the <caption> element wants inline content.
217 $caption = $this->getCaption();
219 if ($this->isEmpty())
220 return $this->_emptyList($caption);
221 elseif (count($this->_columns) == 1)
222 return $this->_generateList($caption);
224 return $this->_generateTable($caption);
227 function printXML() {
228 PrintXML($this->getContent());
232 return AsXML($this->getContent());
239 function _getColumn ($column) {
242 $types = array( 'mtime'
243 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
245 => new _PageList_Column('hits', _("Hits"), 'right'),
247 => new _PageList_Column('rev:summary', _("Last Summary")),
249 => new _PageList_Column_version('rev:version', _("Version"), 'right'),
251 => new _PageList_Column_author('rev:author', _("Last Author")),
253 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
255 => new _PageList_Column_bool('rev:is_minor_edit',
256 _("Minor Edit"), _("minor"))
260 if (strstr($column, ':'))
261 list ($column, $heading) = explode(':', $column, 2);
263 if (!isset($types[$column])) {
264 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
268 $col = $types[$column];
269 if (!empty($heading))
270 $col->setHeading($heading);
275 // make a table given the caption
276 function _generateTable($caption) {
277 $table = HTML::table(array('cellpadding' => 0,
280 'class' => 'pagelist'));
281 $table->setAttr('summary', "FIXME: add brief summary and column names");
285 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
288 foreach ($this->_columns as $col)
289 $row->pushContent($col->heading());
290 $table->pushContent(HTML::thead($row));
293 $tbody = HTML::tbody();
295 foreach ($this->_pages as $page_handle) {
297 $revision_handle = false;
298 foreach ($this->_columns as $col) {
299 $row->pushContent($col->format($page_handle, $revision_handle));
301 $group = (int)($n++ / $this->_group_rows);
302 $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
303 $tbody->pushContent($row);
305 $table->pushContent($tbody);
309 function _generateList($caption) {
310 $list = HTML::ul(array('class' => 'pagelist'));
312 foreach ($this->_pages as $page_handle) {
313 $group = (int)($n++ / $this->_group_rows);
314 $class = ($group % 2) ? 'oddrow' : 'evenrow';
316 $list->pushContent(HTML::li(array('class' => $class),
317 LinkWikiWord($page_handle->getName())));
320 $html[] = HTML::p($caption);
325 function _emptyList($caption) {
328 $html[] = HTML::p($caption);
329 if ($this->_messageIfEmpty)
330 $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
336 // (c-file-style: "gnu")
341 // c-hanging-comment-ender-p: nil
342 // indent-tabs-mode: nil