1 <?php rcs_id('$Id: PageList.php,v 1.25 2002-01-27 04:19:28 dairiki Exp $');
4 * This library relieves some work for these plugins:
6 * 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 * 'author' _("Last Author")),
18 * 'locked' _("Locked"), _("locked")
19 * 'minor' _("Minor Edit"), _("minor")
22 * FIXME?: Make caption work properly with new HtmlElement
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).
30 class _PageList_Column_base {
31 function _PageList_Column_base ($default_heading, $align = false) {
32 $this->_heading = $default_heading;
34 $this->_tdattr = array();
36 $this->_tdattr['align'] = $align;
39 function format ($page_handle, &$revision_handle) {
40 return HTML::td($this->_tdattr,
42 $this->_getValue($page_handle, &$revision_handle),
46 function setHeading ($heading) {
47 $this->_heading = $heading;
51 return HTML::td(array('align' => 'center'),
52 NBSP, HTML::u($this->_heading), NBSP);
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);
60 $this->_need_rev = substr($field, 0, 4) == 'rev:';
62 $this->_field = substr($field, 4);
64 $this->_field = $field;
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);
74 return $page_handle->get($this->_field);
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('—');
86 function _getValue ($page_handle, &$revision_handle) {
87 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
88 return $val ? $this->_textIfTrue : $this->_textIfFalse;
92 class _PageList_Column_time extends _PageList_Column {
93 function _PageList_Column_time ($field, $default_heading) {
94 $this->_PageList_Column($field, $default_heading, 'right');
97 function _getValue ($page_handle, &$revision_handle) {
99 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
100 return $Theme->formatDateTime($time);
104 class _PageList_Column_version extends _PageList_Column {
105 function _getValue ($page_handle, &$revision_handle) {
106 if (!$revision_handle)
107 $revision_handle = $page_handle->getCurrentRevision();
108 return $revision_handle->getVersion();
112 class _PageList_Column_author extends _PageList_Column {
113 function _getValue ($page_handle, &$revision_handle) {
114 global $WikiNameRegexp, $request, $Theme;
115 $dbi = $request->getDbh();
117 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
118 if (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author))
119 return $Theme->linkExistingWikiWord($author);
125 class _PageList_Column_pagename extends _PageList_Column_base {
126 function _PageList_Column_pagename () {
127 $this->_PageList_Column_base(_("Page Name"));
130 function _getValue ($page_handle, &$revision_handle) {
132 return $Theme->LinkExistingWikiWord($page_handle->getName());
138 function PageList () {
139 $this->_caption = "";
140 $this->_columns = array(new _PageList_Column_pagename);
141 $this->_pages = array();
142 $this->_messageIfEmpty = _("<no matches>");
143 $this->_group_rows = 3;
146 function setCaption ($caption_string) {
147 $this->_caption = $caption_string;
150 function getCaption () {
151 // put the total into the caption if needed
152 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
153 return sprintf($this->_caption, $this->getTotal());
154 return $this->_caption;
157 function setMessageIfEmpty ($msg) {
158 $this->_messageIfEmpty = $msg;
162 * Add a column to the listing.
164 * @input $column string Which column to add.
165 * $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)
203 function addPage ($page_handle) {
204 array_push($this->_pages, &$page_handle);
207 function getTotal () {
208 return count($this->_pages);
211 function isEmpty () {
212 return empty($this->_pages);
216 function getContent() {
217 // Note that the <caption> element wants inline content.
218 $caption = $this->getCaption();
220 if ($this->isEmpty())
221 return $this->_emptyList($caption);
222 elseif (count($this->_columns) == 1)
223 return $this->_generateList($caption);
225 return $this->_generateTable($caption);
228 function printXML() {
229 PrintXML($this->getContent());
233 return AsXML($this->getContent());
240 function _getColumn ($column) {
243 $types = array( 'mtime'
244 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
246 => new _PageList_Column('hits', _("Hits"), 'right'),
248 => new _PageList_Column('rev:summary', _("Last Summary")),
250 => new _PageList_Column_version('rev:version', _("Version"), 'right'),
252 => new _PageList_Column_author('rev:author', _("Last Author")),
254 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
256 => new _PageList_Column_bool('rev:is_minor_edit',
257 _("Minor Edit"), _("minor"))
261 if (strstr($column, ':'))
262 list ($column, $heading) = explode(':', $column, 2);
264 if (!isset($types[$column])) {
265 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
269 $col = $types[$column];
270 if (!empty($heading))
271 $col->setHeading($heading);
276 // make a table given the caption
277 function _generateTable($caption) {
278 $table = HTML::table(array('cellpadding' => 0,
281 'class' => 'pagelist'));
282 $table->setAttr('summary', "FIXME: add brief summary and column names");
286 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
289 foreach ($this->_columns as $col)
290 $row->pushContent($col->heading());
291 $table->pushContent(HTML::thead($row));
294 $tbody = HTML::tbody();
296 foreach ($this->_pages as $page_handle) {
298 $revision_handle = false;
299 foreach ($this->_columns as $col) {
300 $row->pushContent($col->format($page_handle, $revision_handle));
302 $group = (int)($n++ / $this->_group_rows);
303 $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
304 $tbody->pushContent($row);
306 $table->pushContent($tbody);
310 function _generateList($caption) {
311 $list = HTML::ul(array('class' => 'pagelist'));
313 foreach ($this->_pages as $page_handle) {
314 $group = (int)($n++ / $this->_group_rows);
315 $class = ($group % 2) ? 'oddrow' : 'evenrow';
317 $list->pushContent(HTML::li(array('class' => $class),
318 LinkWikiWord($page_handle->getName())));
321 $html[] = HTML::p($caption);
326 function _emptyList($caption) {
329 $html[] = HTML::p($caption);
330 if ($this->_messageIfEmpty)
331 $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
337 // (c-file-style: "gnu")
342 // c-hanging-comment-ender-p: nil
343 // indent-tabs-mode: nil