1 <?php rcs_id('$Id: PageList.php,v 1.17 2002-01-22 15:36:42 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_pagename extends _PageList_Column_base {
105 function _PageList_Column_pagename () {
106 $this->_PageList_Column_base(_("Page Name"));
109 function _getValue ($page_handle, &$revision_handle) {
110 return LinkExistingWikiWord($page_handle->getName());
116 function PageList () {
117 $this->_caption = "";
118 $this->_columns = array(new _PageList_Column_pagename);
119 $this->_pages = array();
120 $this->_messageIfEmpty = _("<no matches>");
121 $this->_group_rows = 3;
124 function setCaption ($caption_string) {
125 $this->_caption = $caption_string;
128 function getCaption () {
129 // put the total into the caption if needed
130 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
131 return sprintf($this->_caption, $this->getTotal());
132 return $this->_caption;
135 function setMessageIfEmpty ($msg) {
136 $this->_messageIfEmpty = $msg;
140 * Add a column to the listing.
142 * @input $column string Which column to add.
143 * $Column can be one of <ul>
152 * If you would like to specify an alternate heading for the
153 * column, concatenate the desired adding to $column, after adding
154 * a colon. E.g. 'hits:Page Views'.
156 function addColumn ($column) {
157 if (($col = $this->_getColumn($new_columnname)))
158 array_push($this->_columns, $col);
161 function insertColumn ($new_columnname) {
162 if (($col = $this->_getColumn($new_columnname)))
163 array_unshift($this->_columns, $col);
168 function addPage ($page_handle) {
169 array_push($this->_pages, &$page_handle);
172 function getTotal () {
173 return count($this->_pages);
176 function isEmpty () {
177 return empty($this->_pages);
181 function getContent() {
182 // Note that the <caption> element wants inline content.
183 $caption = $this->getCaption();
185 if ($this->isEmpty())
186 return $this->_emptyList($caption);
187 elseif (count($this->_columns) == 1)
188 return $this->_generateList($caption);
190 return $this->_generateTable($caption);
193 function printXML() {
194 PrintXML($this->getContent());
198 return AsXML($this->getContent());
205 function _getColumn ($column) {
208 $types = array( 'mtime'
209 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
211 => new _PageList_Column('hits', _("Hits"), 'right'),
213 => new _PageList_Column('rev:summary', _("Last Summary")),
215 => new _PageList_Column('rev:author', _("Last Author")),
217 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
219 => new _PageList_Column_bool('rev:is_minor_edit',
220 _("Minor Edit"), _("minor"))
224 if (strstr($column, ':'))
225 list ($column, $heading) = explode(':', $column, 2);
227 if (!isset($types[$column])) {
228 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
232 $col = $types[$column];
233 if (!empty($heading))
234 $col->setHeading($heading);
239 // make a table given the caption
240 function _generateTable($caption) {
241 $table = HTML::table(array('cellpadding' => 0,
244 'class' => 'pagelist'));
245 $table->setAttr('summary', "FIXME: add brief summary and column names");
249 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
252 foreach ($this->_columns as $col)
253 $row->pushContent($col->heading());
254 $table->pushContent(HTML::thead($row));
257 $tbody = HTML::tbody();
259 foreach ($this->_pages as $page_handle) {
261 $revision_handle = false;
262 foreach ($this->_columns as $col) {
263 $row->pushContent($col->format($page_handle, $revision_handle));
265 $group = (int)($n++ / $this->_group_rows);
266 $row->setAttr('class', ($group % 2) ? 'oddrow' : 'evenrow');
267 $tbody->pushContent($row);
269 $table->pushContent($tbody);
273 function _generateList($caption) {
274 $list = HTML::ul(array('class' => 'pagelist'));
276 foreach ($this->_pages as $page_handle) {
277 $group = (int)($n++ / $this->_group_rows);
278 $class = ($group % 2) ? 'oddrow' : 'evenrow';
280 $list->pushContent(HTML::li(array('class' => $class),
281 LinkWikiWord($page_handle->getName())));
284 $html[] = HTML::p($caption);
289 function _emptyList($caption) {
292 $html[] = HTML::p($caption);
293 if ($this->_messageIfEmpty)
294 $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
300 // (c-file-style: "gnu")
305 // c-hanging-comment-ender-p: nil
306 // indent-tabs-mode: nil