1 <?php rcs_id('$Id: PageList.php,v 1.32 2002-02-02 20:17:41 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;
113 $dbi = $request->getDbh();
115 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
116 if (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author))
117 return WikiLink($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) {
129 return WikiLink($page_handle);
136 var $_group_rows = 3;
137 var $_columns = array();
138 var $_excluded_pages = array();
139 var $_rows = array();
141 var $_pagename_seen = false;
143 function PageList ($columns = false, $exclude = false) {
145 if (!is_array($columns))
146 $columns = explode(',', $columns);
147 foreach ($columns as $col)
148 $this->_addColumn($col);
150 $this->_addColumn('pagename');
153 if (!is_array($exclude))
154 $exclude = explode(',', $exclude);
155 $this->_excluded_pages = $exclude;
158 $this->_messageIfEmpty = _("<no matches>");
161 function setCaption ($caption_string) {
162 $this->_caption = $caption_string;
165 function getCaption () {
166 // put the total into the caption if needed
167 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
168 return sprintf($this->_caption, $this->getTotal());
169 return $this->_caption;
172 function setMessageIfEmpty ($msg) {
173 $this->_messageIfEmpty = $msg;
177 function getTotal () {
178 return count($this->_rows);
181 function isEmpty () {
182 return empty($this->_rows);
185 function addPage ($page_handle) {
186 if (in_array($page_handle->getName(), $this->_excluded_pages))
187 return; // exclude page.
189 $group = (int)(count($this->_rows) / $this->_group_rows);
190 $class = ($group % 2) ? 'oddrow' : 'evenrow';
191 $revision_handle = false;
193 if (count($this->_columns) > 1) {
194 $row = HTML::tr(array('class' => $class));
195 foreach ($this->_columns as $col)
196 $row->pushContent($col->format($page_handle, $revision_handle));
199 $col = $this->_columns[0];
200 $row = HTML::li(array('class' => $class),
201 $col->_getValue($page_handle, $revision_handle));
204 $this->_rows[] = $row;
207 function addPages ($page_iter) {
208 while ($page = $page_iter->next())
209 $this->addPage($page);
213 function getContent() {
214 // Note that the <caption> element wants inline content.
215 $caption = $this->getCaption();
217 if ($this->isEmpty())
218 return $this->_emptyList($caption);
219 elseif (count($this->_columns) == 1)
220 return $this->_generateList($caption);
222 return $this->_generateTable($caption);
225 function printXML() {
226 PrintXML($this->getContent());
230 return AsXML($this->getContent());
237 function _addColumn ($column) {
240 $types = array( 'pagename'
241 => new _PageList_Column_pagename,
243 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
245 => new _PageList_Column('hits', _("Hits"), 'right'),
247 => new _PageList_Column('rev:summary', _("Last Summary")),
249 => new _PageList_Column_version('rev:version', _("Version"), 'right'),
251 => new _PageList_Column_author('rev:author', _("Last Author")),
253 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
255 => new _PageList_Column_bool('rev:is_minor_edit',
256 _("Minor Edit"), _("minor"))
260 if (isset($this->_columns_seen[$column]))
261 return false; // Already have this one.
262 $this->_columns_seen[$column] = true;
264 if (strstr($column, ':'))
265 list ($column, $heading) = explode(':', $column, 2);
267 if (!isset($types[$column])) {
268 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
272 $col = $types[$column];
273 if (!empty($heading))
274 $col->setHeading($heading);
276 $this->_columns[] = $col;
281 // make a table given the caption
282 function _generateTable($caption) {
283 $table = HTML::table(array('cellpadding' => 0,
286 'class' => 'pagelist'));
288 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
291 foreach ($this->_columns as $col) {
292 $row->pushContent($col->heading());
293 $table_summary[] = $col->_heading;
295 // Table summary for non-visual browsers.
296 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
298 $table->pushContent(HTML::thead($row),
299 HTML::tbody(false, $this->_rows));
303 function _generateList($caption) {
304 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
305 return $caption ? HTML(HTML::p($caption), $list) : $list;
308 function _emptyList($caption) {
311 $html->pushContent(HTML::p($caption));
312 if ($this->_messageIfEmpty)
313 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
319 // (c-file-style: "gnu")
324 // c-hanging-comment-ender-p: nil
325 // indent-tabs-mode: nil