1 <?php rcs_id('$Id: PageList.php,v 1.20 2002-01-25 08:30:58 carstenklapp 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_author extends _PageList_Column {
105 function _getValue ($page_handle, &$revision_handle) {
106 if ($this->_need_rev) {
107 if (!$revision_handle)
108 $revision_handle = $page_handle->getCurrentRevision();
109 $author = $revision_handle->get($this->_field);
110 return $this->_authorLink($author);
113 $author = $page_handle->get($this->_field);
114 return $this->_authorLink($author);
117 // adapted from plugin/RecentChanges
118 function _authorLink($author) {
119 if ( $this->authorURL($author) ) {
121 return $Theme->linkExistingWikiWord($author);
125 function authorURL($author) {
126 global $WikiNameRegexp, $request;
127 $dbi = $request->getDbh();
128 if (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author))
134 class _PageList_Column_pagename extends _PageList_Column_base {
135 function _PageList_Column_pagename () {
136 $this->_PageList_Column_base(_("Page Name"));
139 function _getValue ($page_handle, &$revision_handle) {
141 return $Theme->LinkExistingWikiWord($page_handle->getName());
147 function PageList () {
148 $this->_caption = "";
149 $this->_columns = array(new _PageList_Column_pagename);
150 $this->_pages = array();
151 $this->_messageIfEmpty = _("<no matches>");
152 $this->_group_rows = 3;
155 function setCaption ($caption_string) {
156 $this->_caption = $caption_string;
159 function getCaption () {
160 // put the total into the caption if needed
161 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
162 return sprintf($this->_caption, $this->getTotal());
163 return $this->_caption;
166 function setMessageIfEmpty ($msg) {
167 $this->_messageIfEmpty = $msg;
171 * Add a column to the listing.
173 * @input $column string Which column to add.
174 * $Column can be one of <ul>
183 * If you would like to specify an alternate heading for the
184 * column, concatenate the desired adding to $column, after adding
185 * a colon. E.g. 'hits:Page Views'.
187 function addColumn ($column) {
188 if (($col = $this->_getColumn($new_columnname)))
189 array_push($this->_columns, $col);
192 function insertColumn ($new_columnname) {
193 if (($col = $this->_getColumn($new_columnname)))
194 array_unshift($this->_columns, $col);
199 function addPage ($page_handle) {
200 array_push($this->_pages, &$page_handle);
203 function getTotal () {
204 return count($this->_pages);
207 function isEmpty () {
208 return empty($this->_pages);
212 function getContent() {
213 // Note that the <caption> element wants inline content.
214 $caption = $this->getCaption();
216 if ($this->isEmpty())
217 return $this->_emptyList($caption);
218 elseif (count($this->_columns) == 1)
219 return $this->_generateList($caption);
221 return $this->_generateTable($caption);
224 function printXML() {
225 PrintXML($this->getContent());
229 return AsXML($this->getContent());
236 function _getColumn ($column) {
239 $types = array( 'mtime'
240 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
242 => new _PageList_Column('hits', _("Hits"), 'right'),
244 => new _PageList_Column('rev:summary', _("Last Summary")),
246 => new _PageList_Column_author('rev:author', _("Last Author")),
248 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
250 => new _PageList_Column_bool('rev:is_minor_edit',
251 _("Minor Edit"), _("minor"))
255 if (strstr($column, ':'))
256 list ($column, $heading) = explode(':', $column, 2);
258 if (!isset($types[$column])) {
259 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
263 $col = $types[$column];
264 if (!empty($heading))
265 $col->setHeading($heading);
270 // make a table given the caption
271 function _generateTable($caption) {
272 $table = HTML::table(array('cellpadding' => 0,
275 'class' => 'pagelist'));
276 $table->setAttr('summary', "FIXME: add brief summary and column names");
280 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
283 foreach ($this->_columns as $col)
284 $row->pushContent($col->heading());
285 $table->pushContent(HTML::thead($row));
288 $tbody = HTML::tbody();
290 foreach ($this->_pages as $page_handle) {
292 $revision_handle = false;
293 foreach ($this->_columns as $col) {
294 $row->pushContent($col->format($page_handle, $revision_handle));
296 $group = (int)($n++ / $this->_group_rows);
297 $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
298 $tbody->pushContent($row);
300 $table->pushContent($tbody);
304 function _generateList($caption) {
305 $list = HTML::ul(array('class' => 'pagelist'));
307 foreach ($this->_pages as $page_handle) {
308 $group = (int)($n++ / $this->_group_rows);
309 $class = ($group % 2) ? 'oddrow' : 'evenrow';
311 $list->pushContent(HTML::li(array('class' => $class),
312 LinkWikiWord($page_handle->getName())));
315 $html[] = HTML::p($caption);
320 function _emptyList($caption) {
323 $html[] = HTML::p($caption);
324 if ($this->_messageIfEmpty)
325 $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
331 // (c-file-style: "gnu")
336 // c-hanging-comment-ender-p: nil
337 // indent-tabs-mode: nil