1 <?php rcs_id('$Id: PageList.php,v 1.35 2002-02-08 02:32:49 dairiki 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")
21 * 'markup' _("Markup")
23 * FIXME: In this refactoring I have un-implemented _ctime, _cauthor, and
24 * number-of-revision. Note the _ctime and _cauthor as they were implemented
25 * were somewhat flawed: revision 1 of a page doesn't have to exist in the
26 * database. If lots of revisions have been made to a page, it's more than likely
27 * that some older revisions (include revision 1) have been cleaned (deleted).
29 class _PageList_Column_base {
30 function _PageList_Column_base ($default_heading, $align = false) {
31 $this->_heading = $default_heading;
33 $this->_tdattr = array();
35 $this->_tdattr['align'] = $align;
38 function format ($page_handle, &$revision_handle) {
39 return HTML::td($this->_tdattr,
41 $this->_getValue($page_handle, &$revision_handle),
45 function setHeading ($heading) {
46 $this->_heading = $heading;
50 return HTML::td(array('align' => 'center'),
51 NBSP, HTML::u($this->_heading), NBSP);
55 class _PageList_Column extends _PageList_Column_base {
56 function _PageList_Column ($field, $default_heading, $align = false) {
57 $this->_PageList_Column_base($default_heading, $align);
59 $this->_need_rev = substr($field, 0, 4) == 'rev:';
61 $this->_field = substr($field, 4);
63 $this->_field = $field;
66 function _getValue ($page_handle, &$revision_handle) {
67 if ($this->_need_rev) {
68 if (!$revision_handle)
69 $revision_handle = $page_handle->getCurrentRevision();
70 return $revision_handle->get($this->_field);
73 return $page_handle->get($this->_field);
78 class _PageList_Column_bool extends _PageList_Column {
79 function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
80 $this->_PageList_Column($field, $default_heading, 'center');
81 $this->_textIfTrue = $text;
82 $this->_textIfFalse = new RawXml('—');
85 function _getValue ($page_handle, &$revision_handle) {
86 $val = _PageList_Column::_getValue($page_handle, $revision_handle);
87 return $val ? $this->_textIfTrue : $this->_textIfFalse;
91 class _PageList_Column_time extends _PageList_Column {
92 function _PageList_Column_time ($field, $default_heading) {
93 $this->_PageList_Column($field, $default_heading, 'right');
96 function _getValue ($page_handle, &$revision_handle) {
98 $time = _PageList_Column::_getValue($page_handle, $revision_handle);
99 return $Theme->formatDateTime($time);
103 class _PageList_Column_version extends _PageList_Column {
104 function _getValue ($page_handle, &$revision_handle) {
105 if (!$revision_handle)
106 $revision_handle = $page_handle->getCurrentRevision();
107 return $revision_handle->getVersion();
111 class _PageList_Column_author extends _PageList_Column {
112 function _getValue ($page_handle, &$revision_handle) {
113 global $WikiNameRegexp, $request;
114 $dbi = $request->getDbh();
116 $author = _PageList_Column::_getValue($page_handle, $revision_handle);
117 if (preg_match("/^$WikiNameRegexp\$/", $author) && $dbi->isWikiPage($author))
118 return WikiLink($author);
124 class _PageList_Column_pagename extends _PageList_Column_base {
125 function _PageList_Column_pagename () {
126 $this->_PageList_Column_base(_("Page Name"));
129 function _getValue ($page_handle, &$revision_handle) {
130 return WikiLink($page_handle);
137 var $_group_rows = 3;
138 var $_columns = array();
139 var $_excluded_pages = array();
140 var $_rows = array();
142 var $_pagename_seen = false;
144 function PageList ($columns = false, $exclude = false) {
146 if (!is_array($columns))
147 $columns = explode(',', $columns);
148 foreach ($columns as $col)
149 $this->_addColumn($col);
151 $this->_addColumn('pagename');
154 if (!is_array($exclude))
155 $exclude = explode(',', $exclude);
156 $this->_excluded_pages = $exclude;
159 $this->_messageIfEmpty = _("<no matches>");
162 function setCaption ($caption_string) {
163 $this->_caption = $caption_string;
166 function getCaption () {
167 // put the total into the caption if needed
168 if (is_string($this->_caption) && strstr($this->_caption, '%d'))
169 return sprintf($this->_caption, $this->getTotal());
170 return $this->_caption;
173 function setMessageIfEmpty ($msg) {
174 $this->_messageIfEmpty = $msg;
178 function getTotal () {
179 return count($this->_rows);
182 function isEmpty () {
183 return empty($this->_rows);
186 function addPage ($page_handle) {
187 if (in_array($page_handle->getName(), $this->_excluded_pages))
188 return; // exclude page.
190 $group = (int)(count($this->_rows) / $this->_group_rows);
191 $class = ($group % 2) ? 'oddrow' : 'evenrow';
192 $revision_handle = false;
194 if (count($this->_columns) > 1) {
195 $row = HTML::tr(array('class' => $class));
196 foreach ($this->_columns as $col)
197 $row->pushContent($col->format($page_handle, $revision_handle));
200 $col = $this->_columns[0];
201 $row = HTML::li(array('class' => $class),
202 $col->_getValue($page_handle, $revision_handle));
205 $this->_rows[] = $row;
208 function addPages ($page_iter) {
209 while ($page = $page_iter->next())
210 $this->addPage($page);
214 function getContent() {
215 // Note that the <caption> element wants inline content.
216 $caption = $this->getCaption();
218 if ($this->isEmpty())
219 return $this->_emptyList($caption);
220 elseif (count($this->_columns) == 1)
221 return $this->_generateList($caption);
223 return $this->_generateTable($caption);
226 function printXML() {
227 PrintXML($this->getContent());
231 return AsXML($this->getContent());
238 function _addColumn ($column) {
241 $types = array( 'pagename'
242 => new _PageList_Column_pagename,
244 => new _PageList_Column_time('rev:mtime', _("Last Modified")),
246 => new _PageList_Column('hits', _("Hits"), 'right'),
248 => new _PageList_Column('rev:summary', _("Last Summary")),
250 => new _PageList_Column_version('rev:version', _("Version"), 'right'),
252 => new _PageList_Column_author('rev:author', _("Last Author")),
254 => new _PageList_Column_bool('locked', _("Locked"), _("locked")),
256 => new _PageList_Column_bool('rev:is_minor_edit',
257 _("Minor Edit"), _("minor")),
259 => new _PageList_Column('rev:markup', _("Markup"))
263 if (isset($this->_columns_seen[$column]))
264 return false; // Already have this one.
265 $this->_columns_seen[$column] = true;
272 if (strstr($column, ':'))
273 list ($column, $heading) = explode(':', $column, 2);
275 if (!isset($types[$column])) {
276 trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
280 $col = $types[$column];
281 if (!empty($heading))
282 $col->setHeading($heading);
284 $this->_columns[] = $col;
289 // make a table given the caption
290 function _generateTable($caption) {
291 $table = HTML::table(array('cellpadding' => 0,
294 'class' => 'pagelist'));
296 $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
299 foreach ($this->_columns as $col) {
300 $row->pushContent($col->heading());
301 $table_summary[] = $col->_heading;
303 // Table summary for non-visual browsers.
304 $table->setAttr('summary', sprintf(_("Columns: %s."), implode(", ", $table_summary)));
306 $table->pushContent(HTML::thead($row),
307 HTML::tbody(false, $this->_rows));
311 function _generateList($caption) {
312 $list = HTML::ul(array('class' => 'pagelist'), $this->_rows);
313 return $caption ? HTML(HTML::p($caption), $list) : $list;
316 function _emptyList($caption) {
319 $html->pushContent(HTML::p($caption));
320 if ($this->_messageIfEmpty)
321 $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
327 // (c-file-style: "gnu")
332 // c-hanging-comment-ender-p: nil
333 // indent-tabs-mode: nil