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