]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
fixed yet another Prefs bug
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.70 2004-03-30 02:14: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  *
29  * Special, custom columns:
30  * 'remove'   _("Remove")     
31  * 'perm'     _("Permission Mask")
32  * 'acl'      _("ACL")
33  * 'renamed_pagename'   _("Rename to")
34  * 'content'  
35  * 'custom'   TODO: implement the above as custom columns.
36  *            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 // If needed this could eventually become a subclass
272 // of a new _PageList_Column_action class for other actions.
273 // only for WikiAdminRemove or WikiAdminSelect
274 class _PageList_Column_remove extends _PageList_Column {
275     function _getValue ($page_handle, &$revision_handle) {
276         return Button(array('action' => 'remove'), _("Remove"),
277                       $page_handle->getName());
278     }
279 };
280
281 // only for WikiAdminRename
282 class _PageList_Column_renamed_pagename extends _PageList_Column {
283     function _getValue ($page_handle, &$revision_handle) {
284         $post_args = $GLOBALS['request']->getArg('admin_rename');
285         $value = str_replace($post_args['from'], $post_args['to'],$page_handle->getName());
286         $div = HTML::div(" => ",HTML::input(array('type' => 'text',
287                                                   'name' => 'rename[]',
288                                                   'value' => $value)));
289         $new_page = $GLOBALS['request']->getPage($value);
290         if ($new_page->exists()) {
291             $div->setAttr('class','error');
292             $div->setAttr('title',_("This page already exists"));
293         }
294         return $div;
295     }
296 };
297
298 // Output is hardcoded to limit of first 50 bytes. Otherwise
299 // on very large Wikis this will fail if used with AllPages
300 // (PHP memory limit exceeded)
301 class _PageList_Column_content extends _PageList_Column {
302     function _PageList_Column_content ($field, $default_heading, $align = false) {
303         _PageList_Column::_PageList_Column($field, $default_heading, $align);
304         $this->bytes = 50;
305         if ($field == 'content') {
306             $this->_heading .= sprintf(_(" ... first %d bytes"),
307                                        $this->bytes);
308         } elseif ($field == 'hi_content') {
309             if (!empty($_POST['admin_replace'])) {
310               $search = $_POST['admin_replace']['from'];
311               $this->_heading .= sprintf(_(" ... around %s"),
312                                        '»'.$search.'«');
313             }
314         }
315     }
316     function _getValue ($page_handle, &$revision_handle) {
317         if (!$revision_handle)
318             $revision_handle = $page_handle->getCurrentRevision();
319         // Not sure why implode is needed here, I thought
320         // getContent() already did this, but it seems necessary.
321         $c = implode("\n", $revision_handle->getContent());
322         if ($this->_field == 'hi_content') {
323             $search = $_POST['admin_replace']['from'];
324             if ($search and ($i = strpos($c,$search))) {
325                 $l = strlen($search);
326                 $j = max(0,$i - ($this->bytes / 2));
327                 return HTML::div(array('style' => 'font-size:x-small'),
328                                  HTML::div(array('class' => 'transclusion'),
329                                            HTML::span(substr($c, $j, ($this->bytes / 2))),
330                                            HTML::span(array("style"=>"background:yellow"),$search),
331                                            HTML::span(substr($c, $i+$l, ($this->bytes / 2))))
332                                  );
333             } else {
334                 $c = sprintf(_("%s not found"),
335                              '»'.$search.'«');
336                 return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
337                                  $c);
338             }
339         } elseif (($len = strlen($c)) > $this->bytes) {
340             $c = substr($c, 0, $this->bytes);
341         }
342         include_once('lib/BlockParser.php');
343         // false --> don't bother processing hrefs for embedded WikiLinks
344         $ct = TransformText($c, $revision_handle->get('markup'), false);
345         return HTML::div(array('style' => 'font-size:x-small'),
346                          HTML::div(array('class' => 'transclusion'), $ct),
347                          // Don't show bytes here if size column present too
348                          ($this->parent->_columns_seen['size'] or !$len) ? "" :
349                            ByteFormatter($len, /*$longformat = */true));
350     }
351 };
352
353 class _PageList_Column_author extends _PageList_Column {
354     function _PageList_Column_author ($field, $default_heading, $align = false) {
355         _PageList_Column::_PageList_Column($field, $default_heading, $align);
356         global $WikiNameRegexp, $request;
357         $this->WikiNameRegexp = $WikiNameRegexp;
358         $this->dbi = &$request->getDbh();
359     }
360
361     function _getValue ($page_handle, &$revision_handle) {
362         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
363         if (preg_match("/^$this->WikiNameRegexp\$/", $author) && $this->dbi->isWikiPage($author))
364             return WikiLink($author);
365         else
366             return $author;
367     }
368 };
369
370 class _PageList_Column_perm extends _PageList_Column {
371     function _getValue ($page_handle, &$revision_handle) {
372         $perm_array = pagePermissions($page_handle->_pagename);
373         return pagePermissionsSimpleFormat($perm_array,
374                                            $page_handle->get('author'),$page_handle->get('group'));
375         if (0) {
376             ob_start();
377             var_dump($perm_array);
378             $xml = ob_get_contents();
379             ob_end_clean();
380             return $xml;
381         }
382     }
383 };
384
385 class _PageList_Column_acl extends _PageList_Column {
386     function _getValue ($page_handle, &$revision_handle) {
387         $perm_tree = pagePermissions($page_handle->_pagename);
388         return pagePermissionsAclFormat($perm_tree);
389         if (0) {
390             ob_start();
391             var_dump($perm_array);
392             $xml = ob_get_contents();
393             ob_end_clean();
394             return $xml;
395         }
396     }
397 };
398
399 class _PageList_Column_pagename extends _PageList_Column_base {
400     var $_field = 'pagename';
401
402     function _PageList_Column_pagename () {
403         $this->_PageList_Column_base(_("Page Name"));
404         global $request;
405         $this->dbi = &$request->getDbh();
406     }
407
408     function _getValue ($page_handle, &$revision_handle) {
409         if ($this->dbi->isWikiPage($page_handle->getName()))
410             return WikiLink($page_handle);
411         else
412             return WikiLink($page_handle, 'unknown');
413     }
414 };
415
416
417
418 class PageList {
419     var $_group_rows = 3;
420     var $_columns = array();
421     var $_excluded_pages = array();
422     var $_rows = array();
423     var $_caption = "";
424     var $_pagename_seen = false;
425     var $_types = array();
426     var $_options = array();
427     var $_selected = array();
428     var $_sortby = array();
429
430     function PageList ($columns = false, $exclude = false, $options = false) {
431         $this->_initAvailableColumns();
432         $symbolic_columns = 
433             array(
434                   'all' =>  array_diff(array_keys($this->_types), // all but...
435                                        array('checkbox','remove','renamed_pagename',
436                                              'content','hi_content','perm','acl')),
437                   'most' => array('pagename','mtime','author','size','hits'),
438                   'some' => array('pagename','mtime','author')
439                   );
440         if ($columns) {
441             if (!is_array($columns))
442                 $columns = explode(',', $columns);
443             // expand symbolic columns:
444             foreach ($symbolic_columns as $symbol => $cols) {
445                 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
446                     $columns = array_diff(array_merge($columns,$cols),array($symbol));
447                 }
448             }
449             if (!in_array('pagename',$columns))
450                 $this->_addColumn('pagename');
451             foreach ($columns as $col) {
452                 $this->_addColumn($col);
453             }
454         }
455         $this->_addColumn('pagename');
456
457         $this->_options = $options;
458         if (!empty($options) and !empty($options['sortby'])) {
459             $sortby = $options['sortby'];
460         } else {
461             $sortby = $GLOBALS['request']->getArg('sortby');
462         }
463         $this->sortby($sortby,'init');
464         if (!empty($options) and !empty($options['limit'])) {
465             $limit = $options['limit'];
466         } else {
467             $limit = $GLOBALS['request']->getArg('limit');
468         }
469         if ($exclude) {
470             if (!is_array($exclude))
471                 $exclude = $this->explodePageList($exclude,false,$sortby,$limit);
472             $this->_excluded_pages = $exclude;
473         }
474             
475         $this->_messageIfEmpty = _("<no matches>");
476     }
477
478     // Currently PageList takes these arguments:
479     // 1: info, 2: exclude, 3: hash of options
480     // Here we declare which options are supported, so that 
481     // the calling plugin may simply merge this with its own default arguments 
482     function supportedArgs () {
483         return array(//Currently supported options:
484                      'info'              => 'pagename',
485                      'exclude'           => '',          // also wildcards and comma-seperated lists
486
487                      // for the sort buttons in <th>
488                      'sortby'            => '',   // same as for WikiDB::getAllPages
489
490                      //PageList pager options:
491                      // These options may also be given to _generate(List|Table) later
492                      // But limit and offset might help the query WikiDB::getAllPages()
493                      //cols    => 1,       // side-by-side display of list (1-3)
494                      //limit   => 50,      // length of one column
495                      //offset  => 0,       // needed internally for the pager
496                      //paging  => 'auto',  // '':     normal paging mode
497                      //                    // 'auto': drop 'info' columns and enhance rows 
498                      //                    //         when the list becomes large
499                      //                    // 'none': don't page at all
500                      );
501     }
502
503     function setCaption ($caption_string) {
504         $this->_caption = $caption_string;
505     }
506
507     function getCaption () {
508         // put the total into the caption if needed
509         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
510             return sprintf($this->_caption, $this->getTotal());
511         return $this->_caption;
512     }
513
514     function setMessageIfEmpty ($msg) {
515         $this->_messageIfEmpty = $msg;
516     }
517
518
519     function getTotal () {
520         return count($this->_rows);
521     }
522
523     function isEmpty () {
524         return empty($this->_rows);
525     }
526
527     function addPage ($page_handle) {
528         if (is_string($page_handle)) {
529             if (empty($page_handle)) return;
530             if (in_array($page_handle, $this->_excluded_pages))
531                 return;             // exclude page.
532             $dbi = $GLOBALS['request']->getDbh();
533             $page_handle = $dbi->getPage($page_handle);
534         } elseif (is_object($page_handle)) {
535           if (in_array($page_handle->getName(), $this->_excluded_pages))
536             return;             // exclude page.
537         }
538         //FIXME. only on sf.net
539         if (!is_object($page_handle)) {
540             trigger_error("PageList: Invalid page_handle $page_handle", E_USER_WARNING);
541             return;
542         }
543         // enforce view permission
544         if (!mayAccessPage('view',$page_handle->getName()))
545             return;
546
547         $group = (int)(count($this->_rows) / $this->_group_rows);
548         $class = ($group % 2) ? 'oddrow' : 'evenrow';
549         $revision_handle = false;
550
551         if (count($this->_columns) > 1) {
552             $row = HTML::tr(array('class' => $class));
553             foreach ($this->_columns as $col)
554                 $row->pushContent($col->format($this, $page_handle, $revision_handle));
555         }
556         else {
557             $col = $this->_columns[0];
558             $row = $col->_getValue($page_handle, $revision_handle);
559         }
560
561         $this->_rows[] = $row;
562     }
563
564     function addPages ($page_iter) {
565         while ($page = $page_iter->next())
566             $this->addPage($page);
567     }
568
569     function addPageList (&$list) {
570         reset ($list);
571         while ($page = next($list))
572             $this->addPage($page);
573     }
574
575     function getContent() {
576         // Note that the <caption> element wants inline content.
577         $caption = $this->getCaption();
578
579         if ($this->isEmpty())
580             return $this->_emptyList($caption);
581         elseif (count($this->_columns) == 1)
582             return $this->_generateList($caption);
583         else
584             return $this->_generateTable($caption);
585     }
586
587     function printXML() {
588         PrintXML($this->getContent());
589     }
590
591     function asXML() {
592         return AsXML($this->getContent());
593     }
594
595     function sortable_columns() {
596         return array('pagename','mtime','hits');
597     }
598
599     /** 
600      * Handles sortby requests for the DB iterator and table header links
601      * Prefix the column with + or - like "+pagename","-mtime", ...
602      * supported columns: 'pagename','mtime','hits'
603      * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
604      *                    'db'         "-pagename" => "pagename DESC"
605      */
606     function sortby ($column, $action) {
607         if (empty($column)) return;
608         //support multiple comma-delimited sortby args: "+hits,+pagename"
609         if (strstr($column,',')) {
610             $result = array();
611             foreach (explode(',',$column) as $col) {
612                 $result[] = $this->sortby($col,$action);
613             }
614             return join(",",$result);
615         }
616         if (substr($column,0,1) == '+') {
617             $order = '+'; $column = substr($column,1);
618         } elseif (substr($column,0,1) == '-') {
619             $order = '-'; $column = substr($column,1);
620         }
621         if (in_array($column,PageList::sortable_columns())) {
622             // default order: +pagename, -mtime, -hits
623             if (empty($order))
624                 if (in_array($column,array('mtime','hits')))
625                     $order = '-';
626                 else
627                     $order = '+';
628             if ($action == 'flip_order') {
629                 return ($order == '+' ? '-' : '+') . $column;
630             } elseif ($action == 'init') {
631                 $this->_sortby[$column] = $order;
632                 return $order . $column;
633             } elseif ($action == 'check') {
634                 return (!empty($this->_sortby[$column]) or 
635                         ($GLOBALS['request']->getArg('sortby') and 
636                          strstr($GLOBALS['request']->getArg('sortby'),$column)));
637             } elseif ($action == 'db') {
638                 // asc or desc: +pagename, -pagename
639                 return $column . ($order == '+' ? ' ASC' : ' DESC');
640             }
641         }
642         return '';
643     }
644
645     // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
646     function explodePageList($input, $perm = false, $sortby=false, $limit=false) {
647         // expand wildcards from list of all pages
648         if (preg_match('/[\?\*]/',$input)) {
649             $dbi = $GLOBALS['request']->getDbh();
650             $allPagehandles = $dbi->getAllPages($perm,$sortby,$limit);
651             while ($pagehandle = $allPagehandles->next()) {
652                 $allPages[] = $pagehandle->getName();
653             }
654             return explodeList($input, $allPages);
655         } else {
656             //TODO: do the sorting, normally not needed if used for exclude only
657             return explode(',',$input);
658         }
659     }
660
661
662     ////////////////////
663     // private
664     ////////////////////
665     //Performance Fixme: Initialize only the requested objects
666     function _initAvailableColumns() {
667         if (!empty($this->_types))
668             return;
669
670         $this->_types =
671             array(
672                   'content'
673                   => new _PageList_Column_content('rev:content', _("Content")),
674                   'hi_content' // with highlighted search for SearchReplace
675                   => new _PageList_Column_content('rev:hi_content', _("Content")),
676                   'remove'
677                   => new _PageList_Column_remove('remove', _("Remove")),
678                   'renamed_pagename'
679                   => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
680                   'perm'
681                   => new _PageList_Column_perm('perm', _("Permission")),
682                   'acl'
683                   => new _PageList_Column_acl('acl', _("ACL")),
684                   'checkbox'
685                   => new _PageList_Column_checkbox('p', _("Select")),
686                   'pagename'
687                   => new _PageList_Column_pagename,
688                   'mtime'
689                   => new _PageList_Column_time('rev:mtime', _("Last Modified")),
690                   'hits'
691                   => new _PageList_Column('hits', _("Hits"), 'right'),
692                   'size'
693                   => new _PageList_Column_size('rev:size', _("Size"), 'right'),
694                                               /*array('align' => 'char', 'char' => ' ')*/
695                   'summary'
696                   => new _PageList_Column('rev:summary', _("Last Summary")),
697                   'version'
698                   => new _PageList_Column_version('rev:version', _("Version"),
699                                                  'right'),
700                   'author'
701                   => new _PageList_Column_author('rev:author', _("Last Author")),
702                   'owner'
703                   => new _PageList_Column_author('owner', _("Owner")),
704                   'group'
705                   => new _PageList_Column_author('group', _("Group")),
706                   'locked'
707                   => new _PageList_Column_bool('locked', _("Locked"),
708                                                _("locked")),
709                   'minor'
710                   => new _PageList_Column_bool('rev:is_minor_edit',
711                                                _("Minor Edit"), _("minor")),
712                   'markup'
713                   => new _PageList_Column('rev:markup', _("Markup"))
714                   );
715     }
716
717     function _addColumn ($column) {
718
719         $this->_initAvailableColumns();
720
721         if (isset($this->_columns_seen[$column]))
722             return false;       // Already have this one.
723         $this->_columns_seen[$column] = true;
724
725         if (strstr($column, ':'))
726             list ($column, $heading) = explode(':', $column, 2);
727
728         if (!isset($this->_types[$column])) {
729             trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
730             return false;
731         }
732
733         $col = $this->_types[$column];
734         if (!empty($heading))
735             $col->setHeading($heading);
736
737         $this->_columns[] = $col;
738
739         return true;
740     }
741
742     // make a table given the caption
743     function _generateTable($caption) {
744         $table = HTML::table(array('cellpadding' => 0,
745                                    'cellspacing' => 1,
746                                    'border'      => 0,
747                                    'class'       => 'pagelist'));
748         if ($caption)
749             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
750
751         //Warning: This is quite fragile. It depends solely on a private variable
752         //         in ->_addColumn()
753         if (!empty($this->_columns_seen['checkbox'])) {
754             $table->pushContent($this->_jsFlipAll());
755         }
756         $row = HTML::tr();
757         $table_summary = array();
758         foreach ($this->_columns as $col) {
759             $row->pushContent($col->button_heading());
760             if (is_string($col->_heading))
761                 $table_summary[] = $col->_heading;
762         }
763         // Table summary for non-visual browsers.
764         $table->setAttr('summary', sprintf(_("Columns: %s."), 
765                                            implode(", ", $table_summary)));
766
767         $table->pushContent(HTML::thead($row),
768                             HTML::tbody(false, $this->_rows));
769         return $table;
770     }
771
772     function _jsFlipAll() {
773       return JavaScript("
774 function flipAll(formObj) {
775   var isFirstSet = -1;
776   for (var i=0;i < formObj.length;i++) {
777       fldObj = formObj.elements[i];
778       if (fldObj.type == 'checkbox') { 
779          if (isFirstSet == -1)
780            isFirstSet = (fldObj.checked) ? true : false;
781          fldObj.checked = (isFirstSet) ? false : true;
782        }
783    }
784 }");
785     }
786
787     function _generateList($caption) {
788         $list = HTML::ul(array('class' => 'pagelist'));
789         $i = 0;
790         foreach ($this->_rows as $page) {
791             $group = ($i++ / $this->_group_rows);
792             $class = ($group % 2) ? 'oddrow' : 'evenrow';
793             $list->pushContent(HTML::li(array('class' => $class),$page));
794         }
795         $out = HTML();
796         //Warning: This is quite fragile. It depends solely on a private variable
797         //         in ->_addColumn()
798         // Questionable if its of use here anyway. This is a one-col pagename list only.
799         //if (!empty($this->_columns_seen['checkbox'])) $out->pushContent($this->_jsFlipAll());
800         if ($caption)
801             $out->pushContent(HTML::p($caption));
802         $out->pushContent($list);
803         return $out;
804     }
805
806     function _emptyList($caption) {
807         $html = HTML();
808         if ($caption)
809             $html->pushContent(HTML::p($caption));
810         if ($this->_messageIfEmpty)
811             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
812         return $html;
813     }
814
815     // Condense list: "Page1, Page2, ..." 
816     // Alternative $seperator = HTML::Raw(' &middot; ')
817     function _generateCommaList($seperator = ', ') {
818         return HTML(join($seperator, $list));
819     }
820
821 };
822
823 /* List pages with checkboxes to select from.
824  * The [Select] button toggles via _jsFlipAll
825  */
826
827 class PageList_Selectable
828 extends PageList {
829
830     function PageList_Selectable ($columns=false, $exclude=false) {
831         if ($columns) {
832             if (!is_array($columns))
833                 $columns = explode(',', $columns);
834             if (!in_array('checkbox',$columns))
835                 array_unshift($columns,'checkbox');
836         } else {
837             $columns = array('checkbox','pagename');
838         }
839         PageList::PageList($columns,$exclude);
840     }
841
842     function addPageList ($array) {
843         while (list($pagename,$selected) = each($array)) {
844             if ($selected) $this->addPageSelected($pagename);
845             $this->addPage($pagename);
846         }
847     }
848
849     function addPageSelected ($pagename) {
850         $this->_selected[$pagename] = 1;
851     }
852     //Todo:
853     //insert javascript when clicked on Selected Select/Deselect all
854 }
855
856 // (c-file-style: "gnu")
857 // Local Variables:
858 // mode: php
859 // tab-width: 8
860 // c-basic-offset: 4
861 // c-hanging-comment-ender-p: nil
862 // indent-tabs-mode: nil
863 // End:
864 ?>