1 <?php rcs_id('$Id: PageList.php,v 1.28 2002-01-29 20:04:19 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)
201 function page_exists ($page) {
202 foreach ($this->_pages as $val) {
203 if ($val->getName() == $page->getName())
209 function addPage ($page_handle) {
210 if(! $this->page_exists(&$page_handle)) {
211 array_push($this->_pages, &$page_handle);
215 function getTotal () {
216 return count($this->_pages);
219 function isEmpty () {
220 return empty($this->_pages);
224 function getContent() {
225 // Note that the <caption> element wants inline content.
226 $caption = $this->getCaption();
228 if ($this->isEmpty())
229 return $this->_emptyList($caption);
230 elseif (count($this->_columns) == 1)
231 return $this->_generateList($caption);
233 return $this->_generateTable($caption);
236 function printXML() {
237 PrintXML($this->getContent());
241 return AsXML($this->getContent());
248 function _getColumn ($column) {
251 $types = array( 'mtime'
252 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
254 => new _PageList_Column('hits', _("Hits"), 'right'),
256 => new _PageList_Column('rev:summary', _("Last Summary")),
258 => new _PageList_Column_version('rev:version', _("Version"), 'right'),
260 => new _PageList_Column_author('rev:author', _("Last Author")),
262 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
264 => new _PageList_Column_bool('rev:is_minor_edit',
265 _("Minor Edit"), _("minor"))
269 if (strstr($column, ':'))
270 list ($column, $heading) = explode(':', $column, 2);
272 if (!isset($types[$column])) {
273 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
277 $col = $types[$column];
278 if (!empty($heading))
279 $col->setHeading($heading);
284 // make a table given the caption
285 function _generateTable($caption) {
286 $table = HTML::table(array('cellpadding' => 0,
289 'class' => 'pagelist'));
290 $table->setAttr('summary', "FIXME: add brief summary and column names");
294 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
297 foreach ($this->_columns as $col)
298 $row->pushContent($col->heading());
299 $table->pushContent(HTML::thead($row));
302 $tbody = HTML::tbody();
304 foreach ($this->_pages as $page_handle) {
306 $revision_handle = false;
307 foreach ($this->_columns as $col) {
308 $row->pushContent($col->format($page_handle, $revision_handle));
310 $group = (int)($n++ / $this->_group_rows);
311 $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
312 $tbody->pushContent($row);
314 $table->pushContent($tbody);
318 function _generateList($caption) {
319 $list = HTML::ul(array('class' => 'pagelist'));
321 foreach ($this->_pages as $page_handle) {
322 $group = (int)($n++ / $this->_group_rows);
323 $class = ($group % 2) ? 'oddrow' : 'evenrow';
325 $list->pushContent(HTML::li(array('class' => $class),
326 LinkWikiWord($page_handle->getName())));
329 return $caption ? HTML(HTML::p($caption), $list) : $list;
332 function _emptyList($caption) {
335 $html->pushContent(HTML::p($caption));
336 if ($this->_messageIfEmpty)
337 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
343 // (c-file-style: "gnu")
348 // c-hanging-comment-ender-p: nil
349 // indent-tabs-mode: nil