]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
themable paging support
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.78 2004-04-20 00:06:03 rurban Exp $');
2
3 /**
4  * List a number of pagenames, optionally as table with various columns.
5  * This library relieves some work for these plugins:
6  *
7  * AllPages, BackLinks, LikePages, Mostpopular, TitleSearch and more
8  *
9  * It also allows dynamic expansion of those plugins to include more
10  * columns in their output.
11  *
12  * Column 'info=' arguments:
13  *
14  * 'pagename' _("Page Name")
15  * 'mtime'    _("Last Modified")
16  * 'hits'     _("Hits")
17  * 'summary'  _("Last Summary")
18  * 'version'  _("Version")),
19  * 'author'   _("Last Author")),
20  * 'locked'   _("Locked"), _("locked")
21  * 'minor'    _("Minor Edit"), _("minor")
22  * 'markup'   _("Markup")
23  * 'size'     _("Size")
24  * 'owner'    _("Owner"),  //todo: implement this again for PagePerm
25  * 'group'    _("Group"),  //todo: implement this for PagePerm
26  * 'checkbox'  A selectable checkbox appears at the left.
27  *             Todo: move this admin action away, not really an info column
28  * 'content'  
29  *
30  * Special, custom columns: Either theme or plugin specific.
31  * 'remove'   _("Remove")     
32  * 'perm'     _("Permission Mask")
33  * 'acl'      _("ACL")
34  * 'renamed_pagename'   _("Rename to")
35  * 'rating'   wikilens theme specific.
36  * 'custom'   See plugin/WikiTranslation
37  *
38  * Symbolic 'info=' arguments:
39  * 'all'       All columns except the special columns
40  * 'most'      pagename, mtime, author, size, hits, ...
41  * 'some'      pagename, mtime, author
42  *
43  * FIXME: In this refactoring I (Jeff) have un-implemented _ctime, _cauthor, and
44  * number-of-revision.  Note the _ctime and _cauthor as they were implemented
45  * were somewhat flawed: revision 1 of a page doesn't have to exist in the
46  * database.  If lots of revisions have been made to a page, it's more than likely
47  * that some older revisions (include revision 1) have been cleaned (deleted).
48  *
49  * DONE: 
50  *   check PagePerm "list" access-type
51  *
52  * TODO: 
53  *   limit, offset, rows arguments for multiple pages/multiple rows.
54  *
55  *   ->supportedArgs() which arguments are supported, so that the plugin 
56  *                     doesn't explictly need to declare it
57  *   new method:
58  *     list not as <ul> or table, but as simple comma-seperated list
59  */
60 class _PageList_Column_base {
61     var $_tdattr = array();
62
63     function _PageList_Column_base ($default_heading, $align = false) {
64         $this->_heading = $default_heading;
65
66         if ($align) {
67             // align="char" isn't supported by any browsers yet :(
68             //if (is_array($align))
69             //    $this->_tdattr = $align;
70             //else
71             $this->_tdattr['align'] = $align;
72         }
73     }
74
75     function format ($pagelist, $page_handle, &$revision_handle) {
76         return HTML::td($this->_tdattr,
77                         HTML::raw('&nbsp;'),
78                         $this->_getValue($page_handle, $revision_handle),
79                         HTML::raw('&nbsp;'));
80     }
81
82     function setHeading ($heading) {
83         $this->_heading = $heading;
84     }
85
86     function heading () {
87         // allow sorting?
88         if (in_array($this->_field,PageList::sortable_columns())) {
89             // multiple comma-delimited sortby args: "+hits,+pagename"
90             // asc or desc: +pagename, -pagename
91             $sortby = PageList::sortby($this->_field,'flip_order');
92             //Fixme: pass all also other GET args along. (limit, p[])
93             $s = HTML::a(array('href' => 
94                                $GLOBALS['request']->GetURLtoSelf(array('sortby' => $sortby,
95                                                                        'nopurge' => '1')),
96                                'class' => 'pagetitle',
97                                'title' => sprintf(_("Sort by %s"),$this->_field)), 
98                          HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
99         } else {
100             $s = HTML(HTML::raw('&nbsp;'), HTML::u($this->_heading), HTML::raw('&nbsp;'));
101         }
102         return HTML::th(array('align' => 'center'),$s);
103     }
104
105     // new grid-style
106     // see activeui.js 
107     function button_heading () {
108         global $Theme, $request;
109         // allow sorting?
110         if (in_array($this->_field,PageList::sortable_columns())) {
111             // multiple comma-delimited sortby args: "+hits,+pagename"
112             $src = false; 
113             $noimg = HTML::img(array('src' => $Theme->getButtonURL('no_order'),
114                                      'width' => '7', 
115                                      'height' => '7',
116                                      'alt'    => '.'));
117             if ($request->getArg('sortby')) {
118                 if (PageList::sortby($this->_field,'check')) { // show icon?
119                     $sortby = PageList::sortby($request->getArg('sortby'),'flip_order');
120                     $request->setArg('sortby',$sortby);
121                     $desc = (substr($sortby,0,1) == '-');      // asc or desc? (+pagename, -pagename)
122                     $src = $Theme->getButtonURL($desc ? 'asc_order' : 'desc_order');
123                 } else {
124                     $sortby = PageList::sortby($this->_field,'init');
125                 }
126             } else {
127                 $sortby = PageList::sortby($this->_field,'init');
128             }
129             if (!$src) {
130                 $img = $noimg;
131                 $img->setAttr('alt', _("Click to sort"));
132             } else {
133                 $img = HTML::img(array('src' => $src, 
134                                        'width' => '7', 
135                                        'height' => '7', 
136                                        'alt' => _("Click to reverse sort order")));
137             }
138             $s = HTML::a(array('href' => 
139                                //Fixme: pass all also other GET args along. (limit, p[])
140                                //Fixme: convert to POST submit[sortby]
141                                $request->GetURLtoSelf(array('sortby' => $sortby,
142                                                             'nopurge' => '1')),
143                                'class' => 'gridbutton', 
144                                'title' => sprintf(_("Click to sort by %s"),$this->_field)),
145                          HTML::raw('&nbsp;'),
146                          $noimg,
147                          HTML::raw('&nbsp;'),
148                          $this->_heading,
149                          HTML::raw('&nbsp;'),
150                          $img,
151                          HTML::raw('&nbsp;'));
152         } else {
153             $s = HTML(HTML::raw('&nbsp;'), $this->_heading, HTML::raw('&nbsp;'));
154         }
155         return HTML::th(array('align' => 'center', 'valign' => 'middle', 
156                               'class' => 'gridbutton'), $s);
157     }
158 };
159
160 class _PageList_Column extends _PageList_Column_base {
161     function _PageList_Column ($field, $default_heading, $align = false) {
162         $this->_PageList_Column_base($default_heading, $align);
163
164         $this->_need_rev = substr($field, 0, 4) == 'rev:';
165         $this->_iscustom = substr($field, 0, 7) == 'custom:';
166         if ($this->_iscustom)
167             $this->_field = substr($field, 7);
168         elseif ($this->_need_rev)
169             $this->_field = substr($field, 4);
170         else
171             $this->_field = $field;
172     }
173
174     function _getValue ($page_handle, &$revision_handle) {
175         if ($this->_need_rev) {
176             if (!$revision_handle)
177                 $revision_handle = $page_handle->getCurrentRevision();
178             return $revision_handle->get($this->_field);
179         }
180         else {
181             return $page_handle->get($this->_field);
182         }
183     }
184 };
185
186 class _PageList_Column_size extends _PageList_Column {
187     function _getValue ($page_handle, &$revision_handle) {
188         if (!$revision_handle)
189             $revision_handle = $page_handle->getCurrentRevision();
190         return $this->_getSize($revision_handle);
191     }
192
193     function _getSize($revision_handle) {
194         $bytes = @strlen($revision_handle->_data['%content']);
195         return ByteFormatter($bytes);
196     }
197 }
198
199
200 class _PageList_Column_bool extends _PageList_Column {
201     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
202         $this->_PageList_Column($field, $default_heading, 'center');
203         $this->_textIfTrue = $text;
204         $this->_textIfFalse = new RawXml('&#8212;'); //mdash
205     }
206
207     function _getValue ($page_handle, &$revision_handle) {
208         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
209         return $val ? $this->_textIfTrue : $this->_textIfFalse;
210     }
211 };
212
213 class _PageList_Column_checkbox extends _PageList_Column {
214     function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
215         $this->_name = $name;
216         $heading = HTML::input(array('type'  => 'button',
217                                      'title' => _("Click to de-/select all pages"),
218                                      //'width' => '100%',
219                                      'name'  => $default_heading,
220                                      'value' => $default_heading,
221                                      'onclick' => "flipAll(this.form)"
222                                      ));
223         $this->_PageList_Column($field, $heading, 'center');
224     }
225     function _getValue ($pagelist, $page_handle, &$revision_handle) {
226         $pagename = $page_handle->getName();
227         $selected = !empty($pagelist->_selected[$pagename]);
228         if (strstr($pagename,'[') or strstr($pagename,']')) {
229             $pagename = str_replace(array('[',']'),array('%5B','%5D'),$pagename);
230         }
231         if ($selected) {
232             return HTML::input(array('type' => 'checkbox',
233                                      'name' => $this->_name . "[$pagename]",
234                                      'value' => 1,
235                                      'checked' => 'CHECKED'));
236         } else {
237             return HTML::input(array('type' => 'checkbox',
238                                      'name' => $this->_name . "[$pagename]",
239                                      'value' => 1));
240         }
241     }
242     function format ($pagelist, $page_handle, &$revision_handle) {
243         return HTML::td($this->_tdattr,
244                         HTML::raw('&nbsp;'),
245                         $this->_getValue($pagelist, $page_handle, $revision_handle),
246                         HTML::raw('&nbsp;'));
247     }
248 };
249
250 class _PageList_Column_time extends _PageList_Column {
251     function _PageList_Column_time ($field, $default_heading) {
252         $this->_PageList_Column($field, $default_heading, 'right');
253         global $Theme;
254         $this->Theme = &$Theme;
255     }
256
257     function _getValue ($page_handle, &$revision_handle) {
258         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
259         return $this->Theme->formatDateTime($time);
260     }
261 };
262
263 class _PageList_Column_version extends _PageList_Column {
264     function _getValue ($page_handle, &$revision_handle) {
265         if (!$revision_handle)
266             $revision_handle = $page_handle->getCurrentRevision();
267         return $revision_handle->getVersion();
268     }
269 };
270
271 // Output is hardcoded to limit of first 50 bytes. Otherwise
272 // on very large Wikis this will fail if used with AllPages
273 // (PHP memory limit exceeded)
274 // FIXME: old PHP without superglobals
275 class _PageList_Column_content extends _PageList_Column {
276     function _PageList_Column_content ($field, $default_heading, $align = false) {
277         _PageList_Column::_PageList_Column($field, $default_heading, $align);
278         $this->bytes = 50;
279         if ($field == 'content') {
280             $this->_heading .= sprintf(_(" ... first %d bytes"),
281                                        $this->bytes);
282         } elseif ($field == 'hi_content') {
283             if (!empty($_POST['admin_replace'])) {
284                 $search = $_POST['admin_replace']['from'];
285                 $this->_heading .= sprintf(_(" ... around %s"),
286                                            '»'.$search.'«');
287             }
288         }
289     }
290     function _getValue ($page_handle, &$revision_handle) {
291         if (!$revision_handle)
292             $revision_handle = $page_handle->getCurrentRevision();
293         // Not sure why implode is needed here, I thought
294         // getContent() already did this, but it seems necessary.
295         $c = implode("\n", $revision_handle->getContent());
296         if ($this->_field == 'hi_content') {
297             $search = $_POST['admin_replace']['from'];
298             if ($search and ($i = strpos($c,$search))) {
299                 $l = strlen($search);
300                 $j = max(0,$i - ($this->bytes / 2));
301                 return HTML::div(array('style' => 'font-size:x-small'),
302                                  HTML::div(array('class' => 'transclusion'),
303                                            HTML::span(substr($c, $j, ($this->bytes / 2))),
304                                            HTML::span(array("style"=>"background:yellow"),$search),
305                                            HTML::span(substr($c, $i+$l, ($this->bytes / 2))))
306                                  );
307             } else {
308                 $c = sprintf(_("%s not found"),
309                              '»'.$search.'«');
310                 return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
311                                  $c);
312             }
313         } elseif (($len = strlen($c)) > $this->bytes) {
314             $c = substr($c, 0, $this->bytes);
315         }
316         include_once('lib/BlockParser.php');
317         // false --> don't bother processing hrefs for embedded WikiLinks
318         $ct = TransformText($c, $revision_handle->get('markup'), false);
319         return HTML::div(array('style' => 'font-size:x-small'),
320                          HTML::div(array('class' => 'transclusion'), $ct),
321                          // Don't show bytes here if size column present too
322                          ($this->parent->_columns_seen['size'] or !$len) ? "" :
323                            ByteFormatter($len, /*$longformat = */true));
324     }
325 };
326
327 class _PageList_Column_author extends _PageList_Column {
328     function _PageList_Column_author ($field, $default_heading, $align = false) {
329         _PageList_Column::_PageList_Column($field, $default_heading, $align);
330         global $WikiNameRegexp, $request;
331         $this->WikiNameRegexp = $WikiNameRegexp;
332         $this->dbi = &$request->getDbh();
333     }
334
335     function _getValue ($page_handle, &$revision_handle) {
336         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
337         if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
338             return WikiLink($author);
339         else
340             return $author;
341     }
342 };
343
344 // DONE: only if RateIt is used
345 // class _PageList_Column_rating extends _PageList_Column
346 // moved to theme/wikilens/themeinfo.php
347
348 class _PageList_Column_pagename extends _PageList_Column_base {
349     var $_field = 'pagename';
350
351     function _PageList_Column_pagename () {
352         $this->_PageList_Column_base(_("Page Name"));
353         global $request;
354         $this->dbi = &$request->getDbh();
355     }
356
357     function _getValue ($page_handle, &$revision_handle) {
358         if ($this->dbi->isWikiPage($page_handle->getName()))
359             return WikiLink($page_handle);
360         else
361             return WikiLink($page_handle, 'unknown');
362     }
363 };
364
365
366
367 class PageList {
368     var $_group_rows = 3;
369     var $_columns = array();
370     var $_excluded_pages = array();
371     var $_rows = array();
372     var $_caption = "";
373     var $_pagename_seen = false;
374     var $_types = array();
375     var $_options = array();
376     var $_selected = array();
377     var $_sortby = array();
378
379     function PageList ($columns = false, $exclude = false, $options = false) {
380         // let plugins predefine only certain objects, such its own custom pagelist columns
381         if (!empty($options['types'])) {
382             $this->_types = $options['types'];
383             unset($options['types']);
384         }
385         $this->_initAvailableColumns();
386         $symbolic_columns = 
387             array(
388                   'all' =>  array_diff(array_keys($this->_types), // all but...
389                                        array('checkbox','remove','renamed_pagename',
390                                              'content','hi_content','perm','acl')),
391                   'most' => array('pagename','mtime','author','size','hits'),
392                   'some' => array('pagename','mtime','author')
393                   );
394         if ($columns) {
395             if (!is_array($columns))
396                 $columns = explode(',', $columns);
397             // expand symbolic columns:
398             foreach ($symbolic_columns as $symbol => $cols) {
399                 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
400                     $columns = array_diff(array_merge($columns,$cols),array($symbol));
401                 }
402             }
403             if (!in_array('pagename',$columns))
404                 $this->_addColumn('pagename');
405             foreach ($columns as $col) {
406                 $this->_addColumn($col);
407             }
408         }
409         $this->_addColumn('pagename');
410
411         $this->_options = $options;
412         foreach (array('sortby','limit','paging','count') as $key) {
413           if (!empty($options) and !empty($options[$key])) {
414             $this->_options[$key] = $options[$key];
415           } else {
416             $this->_options[$key] = $GLOBALS['request']->getArg($key);
417           }
418         }
419         $this->_options['sortby'] = $this->sortby($this->_options['sortby'], 'init');
420         if ($exclude) {
421             if (!is_array($exclude))
422                 $exclude = $this->explodePageList($exclude,false,
423                                                   $this->_options['sortby'],
424                                                   $this->_options['limit']);
425             $this->_excluded_pages = $exclude;
426         }
427         $this->_messageIfEmpty = _("<no matches>");
428     }
429
430     // Currently PageList takes these arguments:
431     // 1: info, 2: exclude, 3: hash of options
432     // Here we declare which options are supported, so that 
433     // the calling plugin may simply merge this with its own default arguments 
434     function supportedArgs () {
435         return array(//Currently supported options:
436                      'info'              => 'pagename',
437                      'exclude'           => '',          // also wildcards and comma-seperated lists
438
439                      // for the sort buttons in <th>
440                      'sortby'            => '',   // same as for WikiDB::getAllPages
441
442                      //PageList pager options:
443                      // These options may also be given to _generate(List|Table) later
444                      // But limit and offset might help the query WikiDB::getAllPages()
445                      'cols'     => 1,       // side-by-side display of list (1-3)
446                      'limit'    => 50,      // number of rows
447                      'paging'   => 'auto',  // 'auto'  normal paging mode
448                      //                     // 'smart' drop 'info' columns and enhance rows 
449                      //                     //         when the list becomes large
450                      //                     // 'none'  don't page at all
451                      //'azhead' => 0        // provide shortcut links to pages starting with different letters
452                      );
453     }
454
455     function setCaption ($caption_string) {
456         $this->_caption = $caption_string;
457     }
458
459     function getCaption () {
460         // put the total into the caption if needed
461         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
462             return sprintf($this->_caption, $this->getTotal());
463         return $this->_caption;
464     }
465
466     function setMessageIfEmpty ($msg) {
467         $this->_messageIfEmpty = $msg;
468     }
469
470
471     function getTotal () {
472         return !empty($this->_options['count'])
473                ? $this->_options['count'] : count($this->_rows);
474     }
475
476     function isEmpty () {
477         return empty($this->_rows);
478     }
479
480     function addPage ($page_handle) {
481         if (is_string($page_handle)) {
482             if ($page_handle == '') return;
483             if (in_array($page_handle, $this->_excluded_pages))
484                 return;             // exclude page.
485             $dbi = $GLOBALS['request']->getDbh();
486             $page_handle = $dbi->getPage($page_handle);
487         } elseif (is_object($page_handle)) {
488           if (in_array($page_handle->getName(), $this->_excluded_pages))
489             return;             // exclude page.
490         }
491         //FIXME. only on sf.net
492         if (!is_object($page_handle)) {
493             trigger_error("PageList: Invalid page_handle $page_handle", E_USER_WARNING);
494             return;
495         }
496         // enforce view permission
497         if (!mayAccessPage('view',$page_handle->getName()))
498             return;
499
500         $group = (int)(count($this->_rows) / $this->_group_rows);
501         $class = ($group % 2) ? 'oddrow' : 'evenrow';
502         $revision_handle = false;
503
504         if (count($this->_columns) > 1) {
505             $row = HTML::tr(array('class' => $class));
506             foreach ($this->_columns as $col)
507                 $row->pushContent($col->format($this, $page_handle, $revision_handle));
508         }
509         else {
510             $col = $this->_columns[0];
511             $row = $col->_getValue($page_handle, $revision_handle);
512         }
513
514         $this->_rows[] = $row;
515     }
516
517     function addPages ($page_iter) {
518         while ($page = $page_iter->next())
519             $this->addPage($page);
520     }
521
522     function addPageList (&$list) {
523         reset ($list);
524         while ($page = next($list))
525             $this->addPage((string)$page);
526     }
527
528     function getContent() {
529         // Note that the <caption> element wants inline content.
530         $caption = $this->getCaption();
531
532         if ($this->isEmpty())
533             return $this->_emptyList($caption);
534         elseif (count($this->_columns) == 1)
535             return $this->_generateList($caption);
536         else
537             return $this->_generateTable($caption);
538     }
539
540     function printXML() {
541         PrintXML($this->getContent());
542     }
543
544     function asXML() {
545         return AsXML($this->getContent());
546     }
547
548     function sortable_columns() {
549         return array('pagename','mtime','hits');
550     }
551
552     /** 
553      * Handle sortby requests for the DB iterator and table header links.
554      * Prefix the column with + or - like "+pagename","-mtime", ...
555      * db supported columns: 'pagename','mtime','hits'
556      * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
557      *                    'db'         "-pagename" => "pagename DESC"
558      * which other column types should be sortable?
559      */
560     function sortby ($column, $action) {
561         if (empty($column)) return;
562         //support multiple comma-delimited sortby args: "+hits,+pagename"
563         if (strstr($column,',')) {
564             $result = array();
565             foreach (explode(',',$column) as $col) {
566                 $result[] = $this->sortby($col,$action);
567             }
568             return join(",",$result);
569         }
570         if (substr($column,0,1) == '+') {
571             $order = '+'; $column = substr($column,1);
572         } elseif (substr($column,0,1) == '-') {
573             $order = '-'; $column = substr($column,1);
574         }
575         if (in_array($column,PageList::sortable_columns())) {
576             // default order: +pagename, -mtime, -hits
577             if (empty($order))
578                 if (in_array($column,array('mtime','hits')))
579                     $order = '-';
580                 else
581                     $order = '+';
582             if ($action == 'flip_order') {
583                 return ($order == '+' ? '-' : '+') . $column;
584             } elseif ($action == 'init') {
585                 $this->_sortby[$column] = $order;
586                 return $order . $column;
587             } elseif ($action == 'check') {
588                 return (!empty($this->_sortby[$column]) or 
589                         ($GLOBALS['request']->getArg('sortby') and 
590                          strstr($GLOBALS['request']->getArg('sortby'),$column)));
591             } elseif ($action == 'db') {
592                 // asc or desc: +pagename, -pagename
593                 return $column . ($order == '+' ? ' ASC' : ' DESC');
594             }
595         }
596         return '';
597     }
598
599     // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
600     function explodePageList($input, $perm = false, $sortby=false, $limit=false) {
601         // expand wildcards from list of all pages
602         if (preg_match('/[\?\*]/',$input)) {
603             $dbi = $GLOBALS['request']->getDbh();
604             $allPagehandles = $dbi->getAllPages($perm,$sortby,$limit);
605             while ($pagehandle = $allPagehandles->next()) {
606                 $allPages[] = $pagehandle->getName();
607             }
608             return explodeList($input, $allPages);
609         } else {
610             //TODO: do the sorting, normally not needed if used for exclude only
611             return explode(',',$input);
612         }
613     }
614
615
616     ////////////////////
617     // private
618     ////////////////////
619     /** Plugin and theme hooks: 
620      *  If the pageList is initialized with $options['types'] these types are also initialized, 
621      *  overriding the standard types.
622      */
623     function _initAvailableColumns() {
624         global $customPageListColumns;
625         $standard_types =
626             array(
627                   'content'
628                   => new _PageList_Column_content('rev:content', _("Content")),
629                   // new: plugin specific column types initialised by the relevant plugins
630                   /*
631                   'hi_content' // with highlighted search for SearchReplace
632                   => new _PageList_Column_content('rev:hi_content', _("Content")),
633                   'remove'
634                   => new _PageList_Column_remove('remove', _("Remove")),
635                   // initialised by the plugin
636                   'renamed_pagename'
637                   => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
638                   'perm'
639                   => new _PageList_Column_perm('perm', _("Permission")),
640                   'acl'
641                   => new _PageList_Column_acl('acl', _("ACL")),
642                   */
643                   'checkbox'
644                   => new _PageList_Column_checkbox('p', _("Select")),
645                   'pagename'
646                   => new _PageList_Column_pagename,
647                   'mtime'
648                   => new _PageList_Column_time('rev:mtime', _("Last Modified")),
649                   'hits'
650                   => new _PageList_Column('hits', _("Hits"), 'right'),
651                   'size'
652                   => new _PageList_Column_size('rev:size', _("Size"), 'right'),
653                                               /*array('align' => 'char', 'char' => ' ')*/
654                   'summary'
655                   => new _PageList_Column('rev:summary', _("Last Summary")),
656                   'version'
657                   => new _PageList_Column_version('rev:version', _("Version"),
658                                                  'right'),
659                   'author'
660                   => new _PageList_Column_author('rev:author', _("Last Author")),
661                   'owner'
662                   => new _PageList_Column_author('owner', _("Owner")),
663                   'group'
664                   => new _PageList_Column_author('group', _("Group")),
665                   'locked'
666                   => new _PageList_Column_bool('locked', _("Locked"),
667                                                _("locked")),
668                   'minor'
669                   => new _PageList_Column_bool('rev:is_minor_edit',
670                                                _("Minor Edit"), _("minor")),
671                   'markup'
672                   => new _PageList_Column('rev:markup', _("Markup")),
673                   // 'rating' initialised by the wikilens theme hook: addPageListColumn
674                   /*
675                   'rating'
676                   => new _PageList_Column_rating('rating', _("Rate")),
677                   */
678                   );
679         if (empty($this->_types))
680             $this->_types = array();
681         // add plugin specific pageList columns, initialized by $options['types']
682         $this->_types = array_merge($standard_types, $this->_types);
683         // add theme specific pageList columns
684         if (!empty($customPageListColumns))
685             $this->_types = array_merge($this->_types, $customPageListColumns);
686     }
687
688     function _addColumn ($column) {
689         
690         if (isset($this->_columns_seen[$column]))
691             return false;       // Already have this one.
692         if (!isset($this->_types[$column]))
693             $this->_initAvailableColumns();
694         $this->_columns_seen[$column] = true;
695
696         if (strstr($column, ':'))
697             list ($column, $heading) = explode(':', $column, 2);
698
699         if (!isset($this->_types[$column])) {
700             //trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
701             return false;
702         }
703         if ($column == 'rating' and !$GLOBALS['request']->_user->isSignedIn())
704             return;
705
706         $col = $this->_types[$column];
707         if (!empty($heading))
708             $col->setHeading($heading);
709
710         $this->_columns[] = $col;
711
712         return true;
713     }
714
715     // make a table given the caption
716     function _generateTable($caption) {
717         $table = HTML::table(array('cellpadding' => 0,
718                                    'cellspacing' => 1,
719                                    'border'      => 0,
720                                    'class'       => 'pagelist'));
721         if ($caption)
722             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
723
724         //Warning: This is quite fragile. It depends solely on a private variable
725         //         in ->_addColumn()
726         if (!empty($this->_columns_seen['checkbox'])) {
727             $table->pushContent($this->_jsFlipAll());
728         }
729         $row = HTML::tr();
730         $table_summary = array();
731         foreach ($this->_columns as $col) {
732             $row->pushContent($col->button_heading());
733             if (is_string($col->_heading))
734                 $table_summary[] = $col->_heading;
735         }
736         // Table summary for non-visual browsers.
737         $table->setAttr('summary', sprintf(_("Columns: %s."), 
738                                            implode(", ", $table_summary)));
739
740         if ( isset($this->_options['paging']) and 
741              !empty($this->_options['limit']) and $this->getTotal() and
742              $this->_options['paging'] != 'none')
743         {
744             // if there are more pages than the limit, show a table-header, -footer
745             if (strstr($this->_options['limit'],','))
746                 list($offset,$pagesize) = split(',',$this->_options['limit']);
747             else {    
748                 $offset = 0; 
749                 $pagesize = $this->_options['limit'];
750             }
751             $numrows = $this->getTotal();
752             if ($numrows <= $pagesize or ($offset + $pagesize < 0))
753                 return $table;
754
755             global $request;
756             include_once('lib/Template.php');
757
758             $tokens = array();
759             $pagename = $request->getArg('pagename');
760             $defargs = $request->args;
761             unset($defargs['pagename']); unset($defargs['action']);
762             //$defargs['nocache'] = 1;
763             $prev = $defargs;
764             $tokens['PREV'] = false; $tokens['PREV_LINK'] = "";
765             $tokens['COLS'] = count($this->_columns);
766             $tokens['COUNT'] = $numrows; 
767             $tokens['OFFSET'] = $offset; 
768             $tokens['SIZE'] = $pagesize;
769             $tokens['NUMPAGES'] = (int)($numrows / $pagesize)+1;
770             $tokens['ACTPAGE'] = (int) (($offset+1) / $pagesize)+1;
771             if ($offset >= $pagesize) {
772                 $prev['limit'] = $offset - $pagesize . ',' . $pagesize;
773                 $prev['count'] = $numrows;
774                 $tokens['LIMIT'] = $prev['limit'];
775                 $tokens['PREV'] = true;
776                 $tokens['PREV_LINK'] = WikiURL($pagename,$prev);
777                 $prev['limit'] = "0,$pagesize";
778                 $tokens['FIRST_LINK'] = WikiURL($pagename,$prev);
779             }
780             $next = $defargs;
781             $tokens['NEXT'] = false; $tokens['NEXT_LINK'] = "";
782             if ($offset + $pagesize < $numrows) {
783                 $next['limit'] = $offset + $pagesize . ',' . $pagesize;
784                 $next['count'] = $numrows;
785                 $tokens['LIMIT'] = $next['limit'];
786                 $tokens['NEXT'] = true;
787                 $tokens['NEXT_LINK'] = WikiURL($pagename,$next);
788                 $next['limit'] = $numrows - $pagesize . ",$pagesize";
789                 $tokens['LAST_LINK'] = WikiURL($pagename,$next);
790             }
791             $paging = new Template("pagelink", $request, $tokens);
792             $table->pushContent(HTML::thead($paging),
793                                 HTML::tbody(false,HTML($row,$this->_rows)),
794                                 HTML::tfoot($paging));
795             return $table;
796         }
797         $table->pushContent(HTML::thead($row),
798                             HTML::tbody(false, $this->_rows));
799         return $table;
800     }
801
802     function _jsFlipAll() {
803       return JavaScript("
804 function flipAll(formObj) {
805   var isFirstSet = -1;
806   for (var i=0;i < formObj.length;i++) {
807       fldObj = formObj.elements[i];
808       if (fldObj.type == 'checkbox') { 
809          if (isFirstSet == -1)
810            isFirstSet = (fldObj.checked) ? true : false;
811          fldObj.checked = (isFirstSet) ? false : true;
812        }
813    }
814 }");
815     }
816
817     function _generateList($caption) {
818         $list = HTML::ul(array('class' => 'pagelist'));
819         $i = 0;
820         foreach ($this->_rows as $page) {
821             $group = ($i++ / $this->_group_rows);
822             $class = ($group % 2) ? 'oddrow' : 'evenrow';
823             $list->pushContent(HTML::li(array('class' => $class),$page));
824         }
825         $out = HTML();
826         //Warning: This is quite fragile. It depends solely on a private variable
827         //         in ->_addColumn()
828         // Questionable if its of use here anyway. This is a one-col pagename list only.
829         //if (!empty($this->_columns_seen['checkbox'])) $out->pushContent($this->_jsFlipAll());
830         if ($caption)
831             $out->pushContent(HTML::p($caption));
832         $out->pushContent($list);
833         return $out;
834     }
835
836     function _emptyList($caption) {
837         $html = HTML();
838         if ($caption)
839             $html->pushContent(HTML::p($caption));
840         if ($this->_messageIfEmpty)
841             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
842         return $html;
843     }
844
845     // Condense list: "Page1, Page2, ..." 
846     // Alternative $seperator = HTML::Raw(' &middot; ')
847     function _generateCommaList($seperator = ', ') {
848         return HTML(join($seperator, $list));
849     }
850
851 };
852
853 /* List pages with checkboxes to select from.
854  * The [Select] button toggles via _jsFlipAll
855  */
856
857 class PageList_Selectable
858 extends PageList {
859
860     function PageList_Selectable ($columns=false, $exclude=false) {
861         if ($columns) {
862             if (!is_array($columns))
863                 $columns = explode(',', $columns);
864             if (!in_array('checkbox',$columns))
865                 array_unshift($columns,'checkbox');
866         } else {
867             $columns = array('checkbox','pagename');
868         }
869         PageList::PageList($columns,$exclude);
870     }
871
872     function addPageList ($array) {
873         while (list($pagename,$selected) = each($array)) {
874             if ($selected) $this->addPageSelected((string)$pagename);
875             $this->addPage((string)$pagename);
876         }
877     }
878
879     function addPageSelected ($pagename) {
880         $this->_selected[$pagename] = 1;
881     }
882     //Todo:
883     //insert javascript when clicked on Selected Select/Deselect all
884 }
885
886 // $Log: not supported by cvs2svn $
887 // Revision 1.77  2004/04/18 01:11:51  rurban
888 // more numeric pagename fixes.
889 // fixed action=upload with merge conflict warnings.
890 // charset changed from constant to global (dynamic utf-8 switching)
891 //
892
893 // (c-file-style: "gnu")
894 // Local Variables:
895 // mode: php
896 // tab-width: 8
897 // c-basic-offset: 4
898 // c-hanging-comment-ender-p: nil
899 // indent-tabs-mode: nil
900 // End:
901 ?>