1 <?php rcs_id('$Id: PageList.php,v 1.14 2002-01-22 05:06:50 dairiki Exp $');
3 // This relieves some work for these plugins:
5 // BackLinks, LikePages, Mostpopular, TitleSearch
7 // It also allows dynamic expansion of those plugins to include more
8 // columns in their output.
11 // There are still a few rough edges.
13 // FIXME: Make caption work properly with new HtmlElement
15 // FIXME: Add error recovery! If the column title isn't found due to a
16 // typo, the wiki craps out, you will not even be able to edit the
17 // page again to fix the typo in the column name!
20 // FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
21 // number-of-revision. Note the _ctime and _cauthor as they were implemented
22 // were somewhat flawed: revision 1 of a page doesn't have to exist in the
23 // database. If lots of revisions have been made to a page, it's more than likely
24 // that some older revisions (include revision 1) have been cleaned (deleted).
26 class _PageList_Column_base {
27 function _PageList_Column_base ($default_heading, $align = false) {
28 $this->_heading = $default_heading;
30 $this->_tdattr = array();
32 $this->_tdattr['align'] = $align;
35 function format ($page_handle, &$revision_handle) {
36 return HTML::td($this->_tdattr,
38 $this->_getValue($page_handle, &$revision_handle),
42 function setHeading ($heading) {
43 $this->_heading = $heading;
47 return HTML::td(array('align' => 'center'),
48 NBSP, HTML::u($this->_heading), NBSP);
52 class _PageList_Column extends _PageList_Column_base {
53 function _PageList_Column ($field, $default_heading, $align = false) {
54 $this->_PageList_Column_base($default_heading, $align);
56 $this->_need_rev = substr($field, 0, 4) == 'rev:';
58 $this->_field = substr($field, 4);
60 $this->_field = $field;
63 function _getValue ($page_handle, &$revision_handle) {
64 if ($this->_need_rev) {
65 if (!$revision_handle)
66 $revision_handle = $page_handle->getCurrentRevision();
67 return $revision_handle->get($this->_field);
70 return $page_handle->get($this->_field);
75 class _PageList_Column_bool extends _PageList_Column {
76 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
77 $this->_PageList_Column($field, $default_heading, 'center');
78 $this->_textIfTrue = $text;
79 $this->_textIfFalse = new RawXml('—');
82 function _getValue ($page_handle, &$revision_handle) {
83 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
84 return $val ? $this->_textIfTrue : $this->_textIfFalse;
88 class _PageList_Column_time extends _PageList_Column {
89 function _PageList_Column_time ($field, $default_heading) {
90 $this->_PageList_Column($field, $default_heading, 'right');
93 function _getValue ($page_handle, &$revision_handle) {
95 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
96 return $Theme->formatDateTime($time);
100 class _PageList_Column_pagename extends _PageList_Column_base {
101 function _PageList_Column_pagename () {
102 $this->_PageList_Column_base(_("Page Name"));
105 function _getValue ($page_handle, &$revision_handle) {
106 return LinkExistingWikiWord($page_handle->getName());
112 function PageList () {
113 $this->_caption = "";
114 $this->_columns = array(new _PageList_Column_pagename);
115 $this->_pages = array();
116 $this->_messageIfEmpty = _("<no matches>");
119 function setCaption ($caption_string) {
120 $this->_caption = $caption_string;
123 function getCaption () {
124 // put the total into the caption if needed
125 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
126 return sprintf($this->_caption, $this->getTotal());
127 return $this->_caption;
130 function setMessageIfEmpty ($msg) {
131 $this->_messageIfEmpty = $msg;
135 * Add a column to the listing.
137 * @input $column string Which column to add.
138 * $Column can be one of <ul>
147 * If you would like to specify an alternate heading for the
148 * column, concatenate the desired adding to $column, after adding
149 * a colon. E.g. 'hits:Page Views'.
151 function addColumn ($column) {
152 if (($col = $this->_getColumn($new_columnname)))
153 array_push($this->_columns, $col);
156 function insertColumn ($new_columnname) {
157 if (($col = $this->_getColumn($new_columnname)))
158 array_unshift($this->_columns, $col);
163 function addPage ($page_handle) {
164 array_push($this->_pages, &$page_handle);
167 function getTotal () {
168 return count($this->_pages);
171 function isEmpty () {
172 return empty($this->_pages);
176 function getContent() {
177 // Note that the <caption> element wants inline content.
178 $caption = $this->getCaption();
180 if ($this->isEmpty())
181 return $this->_emptyList($caption);
182 elseif (count($this->_columns) == 1)
183 return $this->_generateList($caption);
185 return $this->_generateTable($caption);
188 function printXML() {
189 PrintXML($this->getContent());
193 return AsXML($this->getContent());
200 function _getColumn ($column) {
203 $types = array( 'mtime'
204 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
206 => new _PageList_Column('hits', _("Hits"), 'right'),
208 => new _PageList_Column('rev:summary', _("Last Summary")),
210 => new _PageList_Column('rev:author', _("Last Author")),
212 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
214 => new _PageList_Column_bool('rev:is_minor_edit',
215 _("Minor Edit"), _("minor"))
219 if (strstr($column, ':'))
220 list ($column, $heading) = explode(':', $column, 2);
222 if (!isset($types[$column])) {
223 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
227 $col = $types[$column];
228 if (!empty($heading))
229 $col->setHeading($heading);
234 // make a table given the caption
235 function _generateTable($caption) {
236 $table = HTML::table(array('cellpadding' => 0,
239 $table->setAttr('summary', "FIXME: add brief summary and column names");
243 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
246 foreach ($this->_columns as $col)
247 $row->pushContent($col->heading());
248 $table->pushContent(HTML::thead($row));
251 $tbody = HTML::tbody();
252 foreach ($this->_pages as $page_handle) {
254 $revision_handle = false;
255 foreach ($this->_columns as $col)
256 $row->pushContent($col->format($page_handle, $revision_handle));
257 $tbody->pushContent($row);
259 $table->pushContent($tbody);
263 function _generateList($caption) {
265 foreach ($this->_pages as $page_handle) {
266 $list->pushContent(HTML::li(LinkWikiWord($page_handle->getName())));
269 $html[] = HTML::p($caption);
274 function _emptyList($caption) {
277 $html[] = HTML::p($caption);
278 if ($this->_messageIfEmpty)
279 $html[] = HTML::blockquote(HTML::p($this->_messageIfEmpty));
285 // (c-file-style: "gnu")
290 // c-hanging-comment-ender-p: nil
291 // indent-tabs-mode: nil