]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/PageList.php
renamed global $Theme to $WikiTheme (gforge nameclash)
[SourceForge/phpwiki.git] / lib / PageList.php
1 <?php rcs_id('$Id: PageList.php,v 1.88 2004-06-14 11:31:35 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  * 'creator'  _("Creator"),  //todo: implement this again for PagePerm
25  * 'owner'    _("Owner"),    //todo: implement this again 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 $WikiTheme, $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_src = $WikiTheme->getButtonURL('no_order');
114             if ($noimg_src)
115                 $noimg = HTML::img(array('src' => $noimg_src,
116                                          'width' => '7', 
117                                          'height' => '7',
118                                          'border' => 0,
119                                          'alt'    => '.'));
120             else $noimg = HTML::raw('&nbsp;');
121             if ($request->getArg('sortby')) {
122                 if (PageList::sortby($this->_field,'check')) { // show icon?
123                     $sortby = PageList::sortby($request->getArg('sortby'),'flip_order');
124                     $request->setArg('sortby',$sortby);
125                     $desc = (substr($sortby,0,1) == '-');      // asc or desc? (+pagename, -pagename)
126                     $src = $WikiTheme->getButtonURL($desc ? 'asc_order' : 'desc_order');
127                 } else {
128                     $sortby = PageList::sortby($this->_field,'init');
129                 }
130             } else {
131                 $sortby = PageList::sortby($this->_field,'init');
132             }
133             if (!$src) {
134                 $img = $noimg;
135                 //$img->setAttr('alt', _("Click to sort"));
136             } else {
137                 $img = HTML::img(array('src' => $src, 
138                                        'width' => '7', 
139                                        'height' => '7', 
140                                        'border' => 0,
141                                        'alt' => _("Click to reverse sort order")));
142             }
143             $s = HTML::a(array('href' => 
144                                //Fixme: pass all also other GET args along. (limit, p[])
145                                //Fixme: convert to POST submit[sortby]
146                                $request->GetURLtoSelf(array('sortby' => $sortby,
147                                                             'nopurge' => '1')),
148                                'class' => 'gridbutton', 
149                                'title' => sprintf(_("Click to sort by %s"),$this->_field)),
150                          HTML::raw('&nbsp;'),
151                          $noimg,
152                          HTML::raw('&nbsp;'),
153                          $this->_heading,
154                          HTML::raw('&nbsp;'),
155                          $img,
156                          HTML::raw('&nbsp;'));
157         } else {
158             $s = HTML(HTML::raw('&nbsp;'), $this->_heading, HTML::raw('&nbsp;'));
159         }
160         return HTML::th(array('align' => 'center', 'valign' => 'middle', 
161                               'class' => 'gridbutton'), $s);
162     }
163 };
164
165 class _PageList_Column extends _PageList_Column_base {
166     function _PageList_Column ($field, $default_heading, $align = false) {
167         $this->_PageList_Column_base($default_heading, $align);
168
169         $this->_need_rev = substr($field, 0, 4) == 'rev:';
170         $this->_iscustom = substr($field, 0, 7) == 'custom:';
171         if ($this->_iscustom)
172             $this->_field = substr($field, 7);
173         elseif ($this->_need_rev)
174             $this->_field = substr($field, 4);
175         else
176             $this->_field = $field;
177     }
178
179     function _getValue ($page_handle, &$revision_handle) {
180         if ($this->_need_rev) {
181             if (!$revision_handle)
182                 $revision_handle = $page_handle->getCurrentRevision();
183             return $revision_handle->get($this->_field);
184         }
185         else {
186             return $page_handle->get($this->_field);
187         }
188     }
189 };
190
191 class _PageList_Column_size extends _PageList_Column {
192     function _getValue ($page_handle, &$revision_handle) {
193         if (!$revision_handle)
194             $revision_handle = $page_handle->getCurrentRevision();
195         return $this->_getSize($revision_handle);
196     }
197
198     function _getSize($revision_handle) {
199         $bytes = @strlen($revision_handle->_data['%content']);
200         return ByteFormatter($bytes);
201     }
202 }
203
204
205 class _PageList_Column_bool extends _PageList_Column {
206     function _PageList_Column_bool ($field, $default_heading, $text = 'yes') {
207         $this->_PageList_Column($field, $default_heading, 'center');
208         $this->_textIfTrue = $text;
209         $this->_textIfFalse = new RawXml('&#8212;'); //mdash
210     }
211
212     function _getValue ($page_handle, &$revision_handle) {
213         $val = _PageList_Column::_getValue($page_handle, $revision_handle);
214         return $val ? $this->_textIfTrue : $this->_textIfFalse;
215     }
216 };
217
218 class _PageList_Column_checkbox extends _PageList_Column {
219     function _PageList_Column_checkbox ($field, $default_heading, $name='p') {
220         $this->_name = $name;
221         $heading = HTML::input(array('type'  => 'button',
222                                      'title' => _("Click to de-/select all pages"),
223                                      //'width' => '100%',
224                                      'name'  => $default_heading,
225                                      'value' => $default_heading,
226                                      'onclick' => "flipAll(this.form)"
227                                      ));
228         $this->_PageList_Column($field, $heading, 'center');
229     }
230     function _getValue ($pagelist, $page_handle, &$revision_handle) {
231         $pagename = $page_handle->getName();
232         $selected = !empty($pagelist->_selected[$pagename]);
233         if (strstr($pagename,'[') or strstr($pagename,']')) {
234             $pagename = str_replace(array('[',']'),array('%5B','%5D'),$pagename);
235         }
236         if ($selected) {
237             return HTML::input(array('type' => 'checkbox',
238                                      'name' => $this->_name . "[$pagename]",
239                                      'value' => 1,
240                                      'checked' => 'CHECKED'));
241         } else {
242             return HTML::input(array('type' => 'checkbox',
243                                      'name' => $this->_name . "[$pagename]",
244                                      'value' => 1));
245         }
246     }
247     function format ($pagelist, $page_handle, &$revision_handle) {
248         return HTML::td($this->_tdattr,
249                         HTML::raw('&nbsp;'),
250                         $this->_getValue($pagelist, $page_handle, $revision_handle),
251                         HTML::raw('&nbsp;'));
252     }
253 };
254
255 class _PageList_Column_time extends _PageList_Column {
256     function _PageList_Column_time ($field, $default_heading) {
257         $this->_PageList_Column($field, $default_heading, 'right');
258         global $WikiTheme;
259         $this->Theme = &$WikiTheme;
260     }
261
262     function _getValue ($page_handle, &$revision_handle) {
263         $time = _PageList_Column::_getValue($page_handle, $revision_handle);
264         return $this->Theme->formatDateTime($time);
265     }
266 };
267
268 class _PageList_Column_version extends _PageList_Column {
269     function _getValue ($page_handle, &$revision_handle) {
270         if (!$revision_handle)
271             $revision_handle = $page_handle->getCurrentRevision();
272         return $revision_handle->getVersion();
273     }
274 };
275
276 // Output is hardcoded to limit of first 50 bytes. Otherwise
277 // on very large Wikis this will fail if used with AllPages
278 // (PHP memory limit exceeded)
279 // FIXME: old PHP without superglobals
280 class _PageList_Column_content extends _PageList_Column {
281     function _PageList_Column_content ($field, $default_heading, $align = false) {
282         _PageList_Column::_PageList_Column($field, $default_heading, $align);
283         $this->bytes = 50;
284         if ($field == 'content') {
285             $this->_heading .= sprintf(_(" ... first %d bytes"),
286                                        $this->bytes);
287         } elseif ($field == 'hi_content') {
288             if (!empty($_POST['admin_replace'])) {
289                 $search = $_POST['admin_replace']['from'];
290                 $this->_heading .= sprintf(_(" ... around %s"),
291                                            '»'.$search.'«');
292             }
293         }
294     }
295     function _getValue ($page_handle, &$revision_handle) {
296         if (!$revision_handle)
297             $revision_handle = $page_handle->getCurrentRevision();
298         // Not sure why implode is needed here, I thought
299         // getContent() already did this, but it seems necessary.
300         $c = implode("\n", $revision_handle->getContent());
301         if ($this->_field == 'hi_content') {
302             $search = $_POST['admin_replace']['from'];
303             if ($search and ($i = strpos($c,$search))) {
304                 $l = strlen($search);
305                 $j = max(0,$i - ($this->bytes / 2));
306                 return HTML::div(array('style' => 'font-size:x-small'),
307                                  HTML::div(array('class' => 'transclusion'),
308                                            HTML::span(substr($c, $j, ($this->bytes / 2))),
309                                            HTML::span(array("style"=>"background:yellow"),$search),
310                                            HTML::span(substr($c, $i+$l, ($this->bytes / 2))))
311                                  );
312             } else {
313                 $c = sprintf(_("%s not found"),
314                              '»'.$search.'«');
315                 return HTML::div(array('style' => 'font-size:x-small','align'=>'center'),
316                                  $c);
317             }
318         } elseif (($len = strlen($c)) > $this->bytes) {
319             $c = substr($c, 0, $this->bytes);
320         }
321         include_once('lib/BlockParser.php');
322         // false --> don't bother processing hrefs for embedded WikiLinks
323         $ct = TransformText($c, $revision_handle->get('markup'), false);
324         return HTML::div(array('style' => 'font-size:x-small'),
325                          HTML::div(array('class' => 'transclusion'), $ct),
326                          // Don't show bytes here if size column present too
327                          ($this->parent->_columns_seen['size'] or !$len) ? "" :
328                            ByteFormatter($len, /*$longformat = */true));
329     }
330 };
331
332 class _PageList_Column_author extends _PageList_Column {
333     function _PageList_Column_author ($field, $default_heading, $align = false) {
334         _PageList_Column::_PageList_Column($field, $default_heading, $align);
335         $this->dbi =& $GLOBALS['request']->getDbh();
336     }
337
338     function _getValue ($page_handle, &$revision_handle) {
339         $author = _PageList_Column::_getValue($page_handle, $revision_handle);
340         if (isWikiWord($author) && $this->dbi->isWikiPage($author))
341             return WikiLink($author);
342         else
343             return $author;
344     }
345 };
346
347 class _PageList_Column_owner extends _PageList_Column_author {
348     function _getValue ($page_handle, &$revision_handle) {
349         $author = $page_handle->getOwner();
350         if (isWikiWord($author) && $this->dbi->isWikiPage($author))
351             return WikiLink($author);
352         else
353             return $author;
354     }
355 };
356 class _PageList_Column_creator extends _PageList_Column_author {
357     function _getValue ($page_handle, &$revision_handle) {
358         $author = $page_handle->getCreator();
359         if (isWikiWord($author) && $this->dbi->isWikiPage($author))
360             return WikiLink($author);
361         else
362             return $author;
363     }
364 };
365
366 // DONE: only if RateIt is used
367 // class _PageList_Column_rating extends _PageList_Column
368 // moved to theme/wikilens/themeinfo.php
369
370 class _PageList_Column_pagename extends _PageList_Column_base {
371     var $_field = 'pagename';
372
373     function _PageList_Column_pagename () {
374         $this->_PageList_Column_base(_("Page Name"));
375         global $request;
376         $this->dbi = &$request->getDbh();
377     }
378
379     function _getValue ($page_handle, &$revision_handle) {
380         if ($this->dbi->isWikiPage($page_handle->getName()))
381             return WikiLink($page_handle);
382         else
383             return WikiLink($page_handle, 'unknown');
384     }
385 };
386
387
388
389 class PageList {
390     var $_group_rows = 3;
391     var $_columns = array();
392     var $_excluded_pages = array();
393     var $_rows = array();
394     var $_caption = "";
395     var $_pagename_seen = false;
396     var $_types = array();
397     var $_options = array();
398     var $_selected = array();
399     var $_sortby = array();
400     var $_maxlen = 0;
401
402     function PageList ($columns = false, $exclude = false, $options = false) {
403         // let plugins predefine only certain objects, such its own custom pagelist columns
404         if (!empty($options['types'])) {
405             $this->_types = $options['types'];
406             unset($options['types']);
407         }
408         $this->_initAvailableColumns();
409         $symbolic_columns = 
410             array(
411                   'all' =>  array_diff(array_keys($this->_types), // all but...
412                                        array('checkbox','remove','renamed_pagename',
413                                              'content','hi_content','perm','acl')),
414                   'most' => array('pagename','mtime','author','size','hits'),
415                   'some' => array('pagename','mtime','author')
416                   );
417         if ($columns) {
418             if (!is_array($columns))
419                 $columns = explode(',', $columns);
420             // expand symbolic columns:
421             foreach ($symbolic_columns as $symbol => $cols) {
422                 if (in_array($symbol,$columns)) { // e.g. 'checkbox,all'
423                     $columns = array_diff(array_merge($columns,$cols),array($symbol));
424                 }
425             }
426             if (!in_array('pagename',$columns))
427                 $this->_addColumn('pagename');
428             foreach ($columns as $col) {
429                 $this->_addColumn($col);
430             }
431         }
432         $this->_addColumn('pagename');
433
434         $this->_options = $options;
435         foreach (array('sortby','limit','paging','count') as $key) {
436           if (!empty($options) and !empty($options[$key])) {
437             $this->_options[$key] = $options[$key];
438           } else {
439             $this->_options[$key] = $GLOBALS['request']->getArg($key);
440           }
441         }
442         $this->_options['sortby'] = $this->sortby($this->_options['sortby'], 'init');
443         if ($exclude) {
444             if (!is_array($exclude))
445                 $exclude = $this->explodePageList($exclude,false,
446                                                   $this->_options['sortby'],
447                                                   $this->_options['limit']);
448             $this->_excluded_pages = $exclude;
449         }
450         $this->_messageIfEmpty = _("<no matches>");
451     }
452
453     // Currently PageList takes these arguments:
454     // 1: info, 2: exclude, 3: hash of options
455     // Here we declare which options are supported, so that 
456     // the calling plugin may simply merge this with its own default arguments 
457     function supportedArgs () {
458         return array(//Currently supported options:
459                      'info'              => 'pagename',
460                      'exclude'           => '',          // also wildcards and comma-seperated lists
461
462                      /* select pages by meta-data: */
463                      'author'   => false, // current user by []
464                      'owner'    => false, // current user by []
465                      'creator'  => false, // current user by []
466
467                      // for the sort buttons in <th>
468                      'sortby'            => 'pagename', // same as for WikiDB::getAllPages
469
470                      //PageList pager options:
471                      // These options may also be given to _generate(List|Table) later
472                      // But limit and offset might help the query WikiDB::getAllPages()
473                      'cols'     => 1,       // side-by-side display of list (1-3)
474                      'limit'    => 0,       // number of rows (pagesize)
475                      'paging'   => 'auto',  // 'auto'  normal paging mode
476                      //                     // 'smart' drop 'info' columns and enhance rows 
477                      //                     //         when the list becomes large
478                      //                     // 'none'  don't page at all
479                      //'azhead' => 0        // provide shortcut links to pages starting with different letters
480                      );
481     }
482
483     function setCaption ($caption_string) {
484         $this->_caption = $caption_string;
485     }
486
487     function getCaption () {
488         // put the total into the caption if needed
489         if (is_string($this->_caption) && strstr($this->_caption, '%d'))
490             return sprintf($this->_caption, $this->getTotal());
491         return $this->_caption;
492     }
493
494     function setMessageIfEmpty ($msg) {
495         $this->_messageIfEmpty = $msg;
496     }
497
498
499     function getTotal () {
500         return !empty($this->_options['count'])
501                ? (integer) $this->_options['count'] : count($this->_rows);
502     }
503
504     function isEmpty () {
505         return empty($this->_rows);
506     }
507
508     function addPage ($page_handle) {
509         if (is_string($page_handle)) {
510             if ($page_handle == '') return;
511             if (in_array($page_handle, $this->_excluded_pages))
512                 return;             // exclude page.
513             $dbi = $GLOBALS['request']->getDbh();
514             $page_handle = $dbi->getPage($page_handle);
515         } elseif (is_object($page_handle)) {
516           if (in_array($page_handle->getName(), $this->_excluded_pages))
517             return;             // exclude page.
518         }
519         //FIXME. only on sf.net
520         if (!is_object($page_handle)) {
521             trigger_error("PageList: Invalid page_handle $page_handle", E_USER_WARNING);
522             return;
523         }
524         // enforce view permission
525         if (!mayAccessPage('view',$page_handle->getName()))
526             return;
527
528         $group = (int)(count($this->_rows) / $this->_group_rows);
529         $class = ($group % 2) ? 'oddrow' : 'evenrow';
530         $revision_handle = false;
531         $this->_maxlen = max($this->_maxlen,strlen($page_handle->getName()));
532
533         if (count($this->_columns) > 1) {
534             $row = HTML::tr(array('class' => $class));
535             foreach ($this->_columns as $col)
536                 $row->pushContent($col->format($this, $page_handle, $revision_handle));
537         }
538         else {
539             $col = $this->_columns[0];
540             $row = $col->_getValue($page_handle, $revision_handle);
541         }
542
543         $this->_rows[] = $row;
544     }
545
546     function addPages ($page_iter) {
547         //Todo: if limit check max(strlen(pagename))
548         while ($page = $page_iter->next())
549             $this->addPage($page);
550     }
551
552     function addPageList (&$list) {
553         reset ($list);
554         while ($page = next($list))
555             $this->addPage((string)$page);
556     }
557
558     function maxLen() {
559         global $DBParams, $request;
560         if ($DBParams['dbtype'] == 'SQL' or $DBParams['dbtype'] == 'ADODB') {
561             $dbi =& $request->getDbh();
562             extract($dbi->_backend->_table_names);
563             if ($DBParams['dbtype'] == 'SQL') {
564                 $res = $dbi->_backend->_dbh->getOne("SELECT max(length(pagename)) FROM $page_tbl LIMIT 1");
565                 if (DB::isError($res) || empty($res)) return false;
566                 else return $res;
567             } elseif ($DBParams['dbtype'] == 'ADODB') {
568                 $row = $dbi->_backend->_dbh->getRow("SELECT max(length(pagename)) FROM $page_tbl LIMIT 1");
569                 return $row ? $row[0] : false;
570             }
571         } else 
572             return false;
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      * Handle sortby requests for the DB iterator and table header links.
601      * Prefix the column with + or - like "+pagename","-mtime", ...
602      * db supported columns: 'pagename','mtime','hits'
603      * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
604      *                    'db'         "-pagename" => "pagename DESC"
605      * which other column types should be sortable?
606      */
607     function sortby ($column, $action) {
608         if (empty($column)) return;
609         //support multiple comma-delimited sortby args: "+hits,+pagename"
610         if (strstr($column,',')) {
611             $result = array();
612             foreach (explode(',',$column) as $col) {
613                 $result[] = $this->sortby($col,$action);
614             }
615             return join(",",$result);
616         }
617         if (substr($column,0,1) == '+') {
618             $order = '+'; $column = substr($column,1);
619         } elseif (substr($column,0,1) == '-') {
620             $order = '-'; $column = substr($column,1);
621         }
622         if (in_array($column,PageList::sortable_columns())) {
623             // default order: +pagename, -mtime, -hits
624             if (empty($order))
625                 if (in_array($column,array('mtime','hits')))
626                     $order = '-';
627                 else
628                     $order = '+';
629             if ($action == 'flip_order') {
630                 return ($order == '+' ? '-' : '+') . $column;
631             } elseif ($action == 'init') {
632                 $this->_sortby[$column] = $order;
633                 return $order . $column;
634             } elseif ($action == 'check') {
635                 return (!empty($this->_sortby[$column]) or 
636                         ($GLOBALS['request']->getArg('sortby') and 
637                          strstr($GLOBALS['request']->getArg('sortby'),$column)));
638             } elseif ($action == 'db') {
639                 // asc or desc: +pagename, -pagename
640                 return $column . ($order == '+' ? ' ASC' : ' DESC');
641             }
642         }
643         return '';
644     }
645
646     // echo implode(":",explodeList("Test*",array("xx","Test1","Test2")));
647     function explodePageList($input, $perm = false, $sortby=false, $limit=false) {
648         // expand wildcards from list of all pages
649         if (preg_match('/[\?\*]/',$input)) {
650             $dbi = $GLOBALS['request']->getDbh();
651             $allPagehandles = $dbi->getAllPages($perm,$sortby,$limit);
652             while ($pagehandle = $allPagehandles->next()) {
653                 $allPages[] = $pagehandle->getName();
654             }
655             return explodeList($input, $allPages);
656         } else {
657             //TODO: do the sorting, normally not needed if used for exclude only
658             return explode(',',$input);
659         }
660     }
661
662     function allPagesByAuthor($wildcard, $perm=false, $sortby=false, $limit=false) {
663         $dbi = $GLOBALS['request']->getDbh();
664         $allPagehandles = $dbi->getAllPages($perm, $sortby, $limit);
665         $allPages = array();
666         if ($wildcard === '[]') {
667             $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
668             if (!$wildcard) return $allPages;
669         }
670         while ($pagehandle = $allPagehandles->next()) {
671             $name = $pagehandle->getName();
672             $author = $pagehandle->getAuthor();
673             if ($author) {
674                 if (preg_match('/[\?\*]/', $wildcard)) {
675                     if (glob_match($wildcard, $author))
676                         $allPages[] = $name;
677                 } elseif ($wildcard == $author) {
678                       $allPages[] = $name;
679                 }
680             }
681         }
682         return $allPages;
683     }
684
685     function allPagesByOwner($wildcard, $perm=false, $sortby=false, $limit=false) {
686         $dbi = $GLOBALS['request']->getDbh();
687         $allPagehandles = $dbi->getAllPages($perm, $sortby, $limit);
688         $allPages = array();
689         if ($wildcard === '[]') {
690             $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
691             if (!$wildcard) return $allPages;
692         }
693         while ($pagehandle = $allPagehandles->next()) {
694             $name = $pagehandle->getName();
695             $owner = $pagehandle->getOwner();
696             if ($owner) {
697                 if (preg_match('/[\?\*]/', $wildcard)) {
698                     if (glob_match($wildcard, $owner))
699                         $allPages[] = $name;
700                 } elseif ($wildcard == $owner) {
701                       $allPages[] = $name;
702                 }
703             }
704         }
705         return $allPages;
706     }
707
708     function allPagesByCreator($wildcard, $perm=false, $sortby=false, $limit=false) {
709         $dbi = $GLOBALS['request']->getDbh();
710         $allPagehandles = $dbi->getAllPages($perm, $sortby, $limit);
711         $allPages = array();
712         if ($wildcard === '[]') {
713             $wildcard = $GLOBALS['request']->_user->getAuthenticatedId();
714             if (!$wildcard) return $allPages;
715         }
716         while ($pagehandle = $allPagehandles->next()) {
717             $name = $pagehandle->getName();
718             $creator = $pagehandle->getCreator();
719             if ($creator) {
720                 if (preg_match('/[\?\*]/', $wildcard)) {
721                     if (glob_match($wildcard, $creator))
722                         $allPages[] = $name;
723                 } elseif ($wildcard == $creator) {
724                       $allPages[] = $name;
725                 }
726             }
727         }
728         return $allPages;
729     }
730
731     ////////////////////
732     // private
733     ////////////////////
734     /** Plugin and theme hooks: 
735      *  If the pageList is initialized with $options['types'] these types are also initialized, 
736      *  overriding the standard types.
737      */
738     function _initAvailableColumns() {
739         global $customPageListColumns;
740         $standard_types =
741             array(
742                   'content'
743                   => new _PageList_Column_content('rev:content', _("Content")),
744                   // new: plugin specific column types initialised by the relevant plugins
745                   /*
746                   'hi_content' // with highlighted search for SearchReplace
747                   => new _PageList_Column_content('rev:hi_content', _("Content")),
748                   'remove'
749                   => new _PageList_Column_remove('remove', _("Remove")),
750                   // initialised by the plugin
751                   'renamed_pagename'
752                   => new _PageList_Column_renamed_pagename('rename', _("Rename to")),
753                   'perm'
754                   => new _PageList_Column_perm('perm', _("Permission")),
755                   'acl'
756                   => new _PageList_Column_acl('acl', _("ACL")),
757                   */
758                   'checkbox'
759                   => new _PageList_Column_checkbox('p', _("Select")),
760                   'pagename'
761                   => new _PageList_Column_pagename,
762                   'mtime'
763                   => new _PageList_Column_time('rev:mtime', _("Last Modified")),
764                   'hits'
765                   => new _PageList_Column('hits', _("Hits"), 'right'),
766                   'size'
767                   => new _PageList_Column_size('rev:size', _("Size"), 'right'),
768                                               /*array('align' => 'char', 'char' => ' ')*/
769                   'summary'
770                   => new _PageList_Column('rev:summary', _("Last Summary")),
771                   'version'
772                   => new _PageList_Column_version('rev:version', _("Version"),
773                                                  'right'),
774                   'author'
775                   => new _PageList_Column_author('rev:author', _("Last Author")),
776                   'owner'
777                   => new _PageList_Column_owner('author_id', _("Owner")),
778                   'creator'
779                   => new _PageList_Column_creator('author_id', _("Creator")),
780                   /*
781                   'group'
782                   => new _PageList_Column_author('group', _("Group")),
783                   */
784                   'locked'
785                   => new _PageList_Column_bool('locked', _("Locked"),
786                                                _("locked")),
787                   'minor'
788                   => new _PageList_Column_bool('rev:is_minor_edit',
789                                                _("Minor Edit"), _("minor")),
790                   'markup'
791                   => new _PageList_Column('rev:markup', _("Markup")),
792                   // 'rating' initialised by the wikilens theme hook: addPageListColumn
793                   /*
794                   'rating'
795                   => new _PageList_Column_rating('rating', _("Rate")),
796                   */
797                   );
798         if (empty($this->_types))
799             $this->_types = array();
800         // add plugin specific pageList columns, initialized by $options['types']
801         $this->_types = array_merge($standard_types, $this->_types);
802         // add theme specific pageList columns
803         if (!empty($customPageListColumns))
804             $this->_types = array_merge($this->_types, $customPageListColumns);
805     }
806
807     function _addColumn ($column) {
808         
809         if (isset($this->_columns_seen[$column]))
810             return false;       // Already have this one.
811         if (!isset($this->_types[$column]))
812             $this->_initAvailableColumns();
813         $this->_columns_seen[$column] = true;
814
815         if (strstr($column, ':'))
816             list ($column, $heading) = explode(':', $column, 2);
817
818         if (!isset($this->_types[$column])) {
819             //trigger_error(sprintf("%s: Bad column", $column), E_USER_NOTICE);
820             return false;
821         }
822         if ($column == 'rating' and !$GLOBALS['request']->_user->isSignedIn())
823             return;
824
825         $col = $this->_types[$column];
826         if (!empty($heading))
827             $col->setHeading($heading);
828
829         $this->_columns[] = $col;
830
831         return true;
832     }
833
834     function limit($limit) {
835         if (strstr($limit,','))
836             return split(',',$limit);
837         else
838             return array(0,$limit);
839     }
840     
841     // make a table given the caption
842     function _generateTable($caption) {
843         $table = HTML::table(array('cellpadding' => 0,
844                                    'cellspacing' => 1,
845                                    'border'      => 0,
846                                    'class'       => 'pagelist'));
847         if ($caption)
848             $table->pushContent(HTML::caption(array('align'=>'top'), $caption));
849
850         //Warning: This is quite fragile. It depends solely on a private variable
851         //         in ->_addColumn()
852         if (!empty($this->_columns_seen['checkbox'])) {
853             $table->pushContent($this->_jsFlipAll());
854         }
855         $do_paging = ( isset($this->_options['paging']) and 
856                        !empty($this->_options['limit']) and $this->getTotal() and
857                        $this->_options['paging'] != 'none' );
858         $row = HTML::tr();
859         $table_summary = array();
860         foreach ($this->_columns as $col) {
861             $heading = $col->button_heading();
862             if ($do_paging and 
863                 isset($col->_field) and $col->_field == 'pagename' and 
864                 ($maxlen = $this->maxLen()))
865                 $heading->setAttr('width',$maxlen * 7);
866             $row->pushContent($heading);
867             if (is_string($col->_heading))
868                 $table_summary[] = $col->_heading;
869         }
870         // Table summary for non-visual browsers.
871         $table->setAttr('summary', sprintf(_("Columns: %s."), 
872                                            implode(", ", $table_summary)));
873
874         if ( $do_paging ) {
875             // if there are more pages than the limit, show a table-header, -footer
876             list($offset,$pagesize) = $this->limit($this->_options['limit']);
877             $numrows = $this->getTotal();
878             if (!$pagesize or
879                 (!$offset and $numrows <= $pagesize) or
880                 ($offset + $pagesize < 0)) 
881             {
882                 $table->pushContent(HTML::thead($row),
883                                     HTML::tbody(false, $this->_rows));
884                 return $table;
885             }
886             global $request;
887             include_once('lib/Template.php');
888
889             $tokens = array();
890             $pagename = $request->getArg('pagename');
891             $defargs = $request->args;
892             unset($defargs['pagename']); unset($defargs['action']);
893             //$defargs['nocache'] = 1;
894             $prev = $defargs;
895             $tokens['PREV'] = false; $tokens['PREV_LINK'] = "";
896             $tokens['COLS'] = count($this->_columns);
897             $tokens['COUNT'] = $numrows; 
898             $tokens['OFFSET'] = $offset; 
899             $tokens['SIZE'] = $pagesize;
900             $tokens['NUMPAGES'] = (int)($numrows / $pagesize)+1;
901             $tokens['ACTPAGE'] = (int) (($offset+1) / $pagesize)+1;
902             if ($offset > 0) {
903                 $prev['limit'] = min(0,$offset - $pagesize) . ",$pagesize";
904                 $prev['count'] = $numrows;
905                 $tokens['LIMIT'] = $prev['limit'];
906                 $tokens['PREV'] = true;
907                 $tokens['PREV_LINK'] = WikiURL($pagename,$prev);
908                 $prev['limit'] = "0,$pagesize";
909                 $tokens['FIRST_LINK'] = WikiURL($pagename,$prev);
910             }
911             $next = $defargs;
912             $tokens['NEXT'] = false; $tokens['NEXT_LINK'] = "";
913             if ($offset + $pagesize < $numrows) {
914                 $next['limit'] = min($offset + $pagesize,$numrows - $pagesize) . ",$pagesize";
915                 $next['count'] = $numrows;
916                 $tokens['LIMIT'] = $next['limit'];
917                 $tokens['NEXT'] = true;
918                 $tokens['NEXT_LINK'] = WikiURL($pagename,$next);
919                 $next['limit'] = $numrows - $pagesize . ",$pagesize";
920                 $tokens['LAST_LINK'] = WikiURL($pagename,$next);
921             }
922             $paging = new Template("pagelink", $request, $tokens);
923             $table->pushContent(HTML::thead($paging),
924                                 HTML::tbody(false,HTML($row,$this->_rows)),
925                                 HTML::tfoot($paging));
926             return $table;
927         }
928         $table->pushContent(HTML::thead($row),
929                             HTML::tbody(false, $this->_rows));
930         return $table;
931     }
932
933     function _jsFlipAll() {
934       return JavaScript("
935 function flipAll(formObj) {
936   var isFirstSet = -1;
937   for (var i=0;i < formObj.length;i++) {
938       fldObj = formObj.elements[i];
939       if (fldObj.type == 'checkbox') { 
940          if (isFirstSet == -1)
941            isFirstSet = (fldObj.checked) ? true : false;
942          fldObj.checked = (isFirstSet) ? false : true;
943        }
944    }
945 }");
946     }
947
948     function _generateList($caption) {
949         $list = HTML::ul(array('class' => 'pagelist'));
950         $i = 0;
951         foreach ($this->_rows as $page) {
952             $group = ($i++ / $this->_group_rows);
953             $class = ($group % 2) ? 'oddrow' : 'evenrow';
954             $list->pushContent(HTML::li(array('class' => $class),$page));
955         }
956         $out = HTML();
957         //Warning: This is quite fragile. It depends solely on a private variable
958         //         in ->_addColumn()
959         // Questionable if its of use here anyway. This is a one-col pagename list only.
960         //if (!empty($this->_columns_seen['checkbox'])) $out->pushContent($this->_jsFlipAll());
961         if ($caption)
962             $out->pushContent(HTML::p($caption));
963         $out->pushContent($list);
964         return $out;
965     }
966
967     function _emptyList($caption) {
968         $html = HTML();
969         if ($caption)
970             $html->pushContent(HTML::p($caption));
971         if ($this->_messageIfEmpty)
972             $html->pushContent(HTML::blockquote(HTML::p($this->_messageIfEmpty)));
973         return $html;
974     }
975
976     // Condense list: "Page1, Page2, ..." 
977     // Alternative $seperator = HTML::Raw(' &middot; ')
978     function _generateCommaList($seperator = ', ') {
979         return HTML(join($seperator, $list));
980     }
981
982 };
983
984 /* List pages with checkboxes to select from.
985  * The [Select] button toggles via _jsFlipAll
986  */
987
988 class PageList_Selectable
989 extends PageList {
990
991     function PageList_Selectable ($columns=false, $exclude=false, $options = false) {
992         if ($columns) {
993             if (!is_array($columns))
994                 $columns = explode(',', $columns);
995             if (!in_array('checkbox',$columns))
996                 array_unshift($columns,'checkbox');
997         } else {
998             $columns = array('checkbox','pagename');
999         }
1000         PageList::PageList($columns, $exclude, $options);
1001     }
1002
1003     function addPageList ($array) {
1004         while (list($pagename,$selected) = each($array)) {
1005             if ($selected) $this->addPageSelected((string)$pagename);
1006             $this->addPage((string)$pagename);
1007         }
1008     }
1009
1010     function addPageSelected ($pagename) {
1011         $this->_selected[$pagename] = 1;
1012     }
1013 }
1014
1015 // $Log: not supported by cvs2svn $
1016 // Revision 1.87  2004/06/13 16:02:12  rurban
1017 // empty list of pages if user=[] and not authenticated.
1018 //
1019 // Revision 1.86  2004/06/13 15:51:37  rurban
1020 // Support pagelist filter for current author,owner,creator by []
1021 //
1022 // Revision 1.85  2004/06/13 15:33:19  rurban
1023 // new support for arguments owner, author, creator in most relevant
1024 // PageList plugins. in WikiAdmin* via preSelectS()
1025 //
1026 // Revision 1.84  2004/06/08 13:51:56  rurban
1027 // some comments only
1028 //
1029 // Revision 1.83  2004/05/18 13:35:39  rurban
1030 //  improve Pagelist layout by equal pagename width for limited lists
1031 //
1032 // Revision 1.82  2004/05/16 22:07:35  rurban
1033 // check more config-default and predefined constants
1034 // various PagePerm fixes:
1035 //   fix default PagePerms, esp. edit and view for Bogo and Password users
1036 //   implemented Creator and Owner
1037 //   BOGOUSERS renamed to BOGOUSER
1038 // fixed syntax errors in signin.tmpl
1039 //
1040 // Revision 1.81  2004/05/13 12:30:35  rurban
1041 // fix for MacOSX border CSS attr, and if sort buttons are not found
1042 //
1043 // Revision 1.80  2004/04/20 00:56:00  rurban
1044 // more paging support and paging fix for shorter lists
1045 //
1046 // Revision 1.79  2004/04/20 00:34:16  rurban
1047 // more paging support
1048 //
1049 // Revision 1.78  2004/04/20 00:06:03  rurban
1050 // themable paging support
1051 //
1052 // Revision 1.77  2004/04/18 01:11:51  rurban
1053 // more numeric pagename fixes.
1054 // fixed action=upload with merge conflict warnings.
1055 // charset changed from constant to global (dynamic utf-8 switching)
1056 //
1057
1058 // (c-file-style: "gnu")
1059 // Local Variables:
1060 // mode: php
1061 // tab-width: 8
1062 // c-basic-offset: 4
1063 // c-hanging-comment-ender-p: nil
1064 // indent-tabs-mode: nil
1065 // End:
1066 ?>