1 <?php rcs_id('$Id: PageList.php,v 1.38 2002-02-24 17:59:40 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.
12 * Column 'info=' arguments:
14 * 'pagename' _("Page Name")
15 * 'mtime' _("Last Modified")
17 * 'summary' _("Last Summary")
18 * 'version' _("Version")),
19 * 'author' _("Last Author")),
20 * 'locked' _("Locked"), _("locked")
21 * 'minor' _("Minor Edit"), _("minor")
22 * 'markup' _("Markup")
24 * 'all' All columns will be displayed. This argument must appear alone.
26 * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
27 * number-of-revision. Note the _ctime and _cauthor as they were implemented
28 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
29 * database. If lots of revisions have been made to a page, it's more than likely
30 * that some older revisions (include revision 1) have been cleaned (deleted).
32 class _PageList_Column_base {
33 function _PageList_Column_base ($default_heading, $align = false) {
34 $this->_heading = $default_heading;
36 $this->_tdattr = array();
38 $this->_tdattr['align'] = $align;
41 function format ($page_handle, &$revision_handle) {
42 return HTML::td($this->_tdattr,
44 $this->_getValue($page_handle, &$revision_handle),
48 function setHeading ($heading) {
49 $this->_heading = $heading;
53 return HTML::td(array('align' => 'center'),
54 NBSP, HTML::u($this->_heading), NBSP);
58 class _PageList_Column extends _PageList_Column_base {
59 function _PageList_Column ($field, $default_heading, $align = false) {
60 $this->_PageList_Column_base($default_heading, $align);
62 $this->_need_rev = substr($field, 0, 4) == 'rev:';
64 $this->_field = substr($field, 4);
66 $this->_field = $field;
69 function _getValue ($page_handle, &$revision_handle) {
70 if ($this->_need_rev) {
71 if (!$revision_handle)
72 $revision_handle = $page_handle->getCurrentRevision();
73 return $revision_handle->get($this->_field);
76 return $page_handle->get($this->_field);
81 class _PageList_Column_bool extends _PageList_Column {
82 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
83 $this->_PageList_Column($field, $default_heading, 'center');
84 $this->_textIfTrue = $text;
85 $this->_textIfFalse = new RawXml('—');
88 function _getValue ($page_handle, &$revision_handle) {
89 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
90 return $val ? $this->_textIfTrue : $this->_textIfFalse;
94 class _PageList_Column_time extends _PageList_Column {
95 function _PageList_Column_time ($field, $default_heading) {
96 $this->_PageList_Column($field, $default_heading, 'right');
98 $this->Theme = &$Theme;
101 function _getValue ($page_handle, &$revision_handle) {
102 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
103 return $this->Theme->formatDateTime($time);
107 class _PageList_Column_version extends _PageList_Column {
108 function _getValue ($page_handle, &$revision_handle) {
109 if (!$revision_handle)
110 $revision_handle = $page_handle->getCurrentRevision();
111 return $revision_handle->getVersion();
115 class _PageList_Column_author extends _PageList_Column {
116 function _PageList_Column_author ($field, $default_heading, $align = false) {
117 _PageList_Column::_PageList_Column($field, $default_heading, $align);
118 global $WikiNameRegexp, $request;
119 $this->WikiNameRegexp = $WikiNameRegexp;
120 $this->dbi = &$request->getDbh();
123 function _getValue ($page_handle, &$revision_handle) {
124 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
125 if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
126 return WikiLink($author);
132 class _PageList_Column_pagename extends _PageList_Column_base {
133 function _PageList_Column_pagename () {
134 $this->_PageList_Column_base(_("Page Name"));
136 $this->dbi = &$request->getDbh();
139 function _getValue ($page_handle, &$revision_handle) {
140 if ($this->dbi->isWikiPage($pagename = $page_handle->getName()))
141 return WikiLink($page_handle);
143 return WikiLink($page_handle, 'unknown');
150 var $_group_rows = 3;
151 var $_columns = array();
152 var $_excluded_pages = array();
153 var $_rows = array();
155 var $_pagename_seen = false;
156 var $_types = array();
158 function PageList ($columns = false, $exclude = false) {
159 if ($columns == 'all') {
160 $this->_initAvailableColumns();
161 $columns = array_keys($this->_types);
165 if (!is_array($columns))
166 $columns = explode(',', $columns);
167 foreach ($columns as $col)
168 $this->_addColumn($col);
170 $this->_addColumn('pagename');
173 if (!is_array($exclude))
174 $exclude = explode(',', $exclude);
175 $this->_excluded_pages = $exclude;
178 $this->_messageIfEmpty = _("<no matches>");
181 function setCaption ($caption_string) {
182 $this->_caption = $caption_string;
185 function getCaption () {
186 // put the total into the caption if needed
187 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
188 return sprintf($this->_caption, $this->getTotal());
189 return $this->_caption;
192 function setMessageIfEmpty ($msg) {
193 $this->_messageIfEmpty = $msg;
197 function getTotal () {
198 return count($this->_rows);
201 function isEmpty () {
202 return empty($this->_rows);
205 function addPage ($page_handle) {
206 if (in_array($page_handle->getName(), $this->_excluded_pages))
207 return; // exclude page.
209 $group = (int)(count($this->_rows) / $this->_group_rows);
210 $class = ($group % 2) ? 'oddrow' : 'evenrow';
211 $revision_handle = false;
213 if (count($this->_columns) > 1) {
214 $row = HTML::tr(array('class' => $class));
215 foreach ($this->_columns as $col)
216 $row->pushContent($col->format($page_handle, $revision_handle));
219 $col = $this->_columns[0];
220 $row = HTML::li(array('class' => $class),
221 $col->_getValue($page_handle, $revision_handle));
224 $this->_rows[] = $row;
227 function addPages ($page_iter) {
228 while ($page = $page_iter->next())
229 $this->addPage($page);
233 function getContent() {
234 // Note that the <caption> element wants inline content.
235 $caption = $this->getCaption();
237 if ($this->isEmpty())
238 return $this->_emptyList($caption);
239 elseif (count($this->_columns) == 1)
240 return $this->_generateList($caption);
242 return $this->_generateTable($caption);
245 function printXML() {
246 PrintXML($this->getContent());
250 return AsXML($this->getContent());
257 function _initAvailableColumns() {
258 if (!empty($this->_types))
263 => new _PageList_Column_pagename,
266 => new _PageList_Column_time('rev:mtime',
269 => new _PageList_Column('hits', _("Hits"), 'right'),
272 => new _PageList_Column('rev:summary', _("Last Summary")),
275 => new _PageList_Column_version('rev:version', _("Version"),
278 => new _PageList_Column_author('rev:author',
281 => new _PageList_Column_bool('locked', _("Locked"),
284 => new _PageList_Column_bool('rev:is_minor_edit',
285 _("Minor Edit"), _("minor")),
287 => new _PageList_Column('rev:markup', _("Markup"))
291 function _addColumn ($column) {
293 $this->_initAvailableColumns();
295 if (isset($this->_columns_seen[$column]))
296 return false; // Already have this one.
297 $this->_columns_seen[$column] = true;
304 if (strstr($column, ':'))
305 list ($column, $heading) = explode(':', $column, 2);
307 if (!isset($this->_types[$column])) {
308 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
312 $col = $this->_types[$column];
313 if (!empty($heading))
314 $col->setHeading($heading);
316 $this->_columns[] = $col;
321 // make a table given the caption
322 function _generateTable($caption) {
323 $table = HTML::table(array('cellpadding' => 0,
326 'class' => 'pagelist'));
328 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
331 foreach ($this->_columns as $col) {
332 $row->pushContent($col->heading());
333 $table_summary[] = $col->_heading;
335 // Table summary for non-visual browsers.
336 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
338 $table->pushContent(HTML::thead($row),
339 HTML::tbody(false, $this->_rows));
343 function _generateList($caption) {
344 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
345 return $caption ? HTML(HTML::p($caption), $list) : $list;
348 function _emptyList($caption) {
351 $html->pushContent(HTML::p($caption));
352 if ($this->_messageIfEmpty)
353 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
359 // (c-file-style: "gnu")
364 // c-hanging-comment-ender-p: nil
365 // indent-tabs-mode: nil