]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Theme.php
fixed \r\r\n with dumping on windows
[SourceForge/phpwiki.git] / lib / Theme.php
1 <?php rcs_id('$Id: Theme.php,v 1.71 2004-02-15 21:34:37 rurban Exp $');
2
3 require_once('lib/HtmlElement.php');
4
5
6 /**
7  * Make a link to a wiki page (in this wiki).
8  *
9  * This is a convenience function.
10  *
11  * @param mixed $page_or_rev
12  * Can be:<dl>
13  * <dt>A string</dt><dd>The page to link to.</dd>
14  * <dt>A WikiDB_Page object</dt><dd>The page to link to.</dd>
15  * <dt>A WikiDB_PageRevision object</dt><dd>A specific version of the page to link to.</dd>
16  * </dl>
17  *
18  * @param string $type
19  * One of:<dl>
20  * <dt>'unknown'</dt><dd>Make link appropriate for a non-existant page.</dd>
21  * <dt>'known'</dt><dd>Make link appropriate for an existing page.</dd>
22  * <dt>'auto'</dt><dd>Either 'unknown' or 'known' as appropriate.</dd>
23  * <dt>'button'</dt><dd>Make a button-style link.</dd>
24  * <dt>'if_known'</dt><dd>Only linkify if page exists.</dd>
25  * </dl>
26  * Unless $type of of the latter form, the link will be of class 'wiki', 'wikiunknown',
27  * 'named-wiki', or 'named-wikiunknown', as appropriate.
28  *
29  * @param mixed $label (string or XmlContent object)
30  * Label for the link.  If not given, defaults to the page name.
31  *
32  * @return XmlContent The link
33  */
34 function WikiLink ($page_or_rev, $type = 'known', $label = false) {
35     global $Theme, $request;
36
37     if ($type == 'button') {
38         return $Theme->makeLinkButton($page_or_rev, $label);
39     }
40
41     $version = false;
42     
43     if (isa($page_or_rev, 'WikiDB_PageRevision')) {
44         $version = $page_or_rev->getVersion();
45         $page = $page_or_rev->getPage();
46         $pagename = $page->getName();
47         $wikipage = $pagename;
48         $exists = true;
49     }
50     elseif (isa($page_or_rev, 'WikiDB_Page')) {
51         $page = $page_or_rev;
52         $pagename = $page->getName();
53         $wikipage = $pagename;
54     }
55     elseif (isa($page_or_rev, 'WikiPageName')) {
56         $wikipage = $page_or_rev;
57         $pagename = $wikipage->name;
58     }
59     else {
60         $wikipage = new WikiPageName($page_or_rev, $request->getPage());
61         $pagename = $wikipage->name;
62     }
63     
64     if (!is_string($wikipage) and !$wikipage->isValid('strict'))
65         return $Theme->linkBadWikiWord($wikipage, $label);
66     
67     if ($type == 'auto' or $type == 'if_known') {
68         if (isset($page)) {
69             $current = $page->getCurrentRevision();
70             $exists = ! $current->hasDefaultContents();
71         }
72         else {
73             $dbi = $request->getDbh();
74             $exists = $dbi->isWikiPage($wikipage->name);
75         }
76     }
77     elseif ($type == 'unknown') {
78         $exists = false;
79     }
80     else {
81         $exists = true;
82     }
83
84     // FIXME: this should be somewhere else, if really needed.
85     // WikiLink makes A link, not a string of fancy ones.
86     // (I think that the fancy split links are just confusing.)
87     // Todo: test external ImageLinks http://some/images/next.gif
88     if (isa($wikipage, 'WikiPageName') and !$label 
89         and strchr(substr($wikipage->shortName,1), SUBPAGE_SEPARATOR)) {
90         $parts = explode(SUBPAGE_SEPARATOR, $wikipage->shortName);
91         $last_part = array_pop($parts);
92         $sep = '';
93         $link = HTML::span();
94         foreach ($parts as $part) {
95             $path[] = $part;
96             $parent = join(SUBPAGE_SEPARATOR, $path);
97             if ($part)
98                 $link->pushContent($Theme->linkExistingWikiWord($parent, $sep . $part));
99             $sep = SUBPAGE_SEPARATOR;
100         }
101         if ($exists)
102             $link->pushContent($Theme->linkExistingWikiWord($wikipage, $sep . $last_part, $version));
103         else
104             $link->pushContent($Theme->linkUnknownWikiWord($wikipage, $last_part));
105         return $link;
106     }
107
108     if ($exists) {
109         return $Theme->linkExistingWikiWord($wikipage, $label, $version);
110     }
111     elseif ($type == 'if_known') {
112         if (!$label && isa($wikipage, 'WikiPageName'))
113             $label = $wikipage->shortName;
114         return HTML($label ? $label : $pagename);
115     }
116     else {
117         return $Theme->linkUnknownWikiWord($wikipage, $label);
118     }
119 }
120
121
122
123 /**
124  * Make a button.
125  *
126  * This is a convenience function.
127  *
128  * @param $action string
129  * One of <dl>
130  * <dt>[action]</dt><dd>Perform action (e.g. 'edit') on the selected page.</dd>
131  * <dt>[ActionPage]</dt><dd>Run the actionpage (e.g. 'BackLinks') on the selected page.</dd>
132  * <dt>'submit:'[name]</dt><dd>Make a form submission button with the given name.
133  *      ([name] can be blank for a nameless submit button.)</dd>
134  * <dt>a hash</dt><dd>Query args for the action. E.g.<pre>
135  *      array('action' => 'diff', 'previous' => 'author')
136  * </pre></dd>
137  * </dl>
138  *
139  * @param $label string
140  * A label for the button.  If ommited, a suitable default (based on the valued of $action)
141  * will be picked.
142  *
143  * @param $page_or_rev mixed
144  * Which page (& version) to perform the action on.
145  * Can be one of:<dl>
146  * <dt>A string</dt><dd>The pagename.</dd>
147  * <dt>A WikiDB_Page object</dt><dd>The page.</dd>
148  * <dt>A WikiDB_PageRevision object</dt><dd>A specific version of the page.</dd>
149  * </dl>
150  * ($Page_or_rev is ignored for submit buttons.)
151  */
152 function Button ($action, $label = false, $page_or_rev = false) {
153     global $Theme;
154
155     if (!is_array($action) && preg_match('/submit:(.*)/A', $action, $m))
156         return $Theme->makeSubmitButton($label, $m[1], $class = $page_or_rev);
157     else
158         return $Theme->makeActionButton($action, $label, $page_or_rev);
159 }
160
161
162 class Theme {
163     var $HTML_DUMP_SUFFIX = '';
164     function Theme ($theme_name = 'default') {
165         $this->_name = $theme_name;
166         $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
167
168         $this->_path  = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/" : "";
169         $this->_theme = "themes/$theme_name";
170
171         if ($theme_name != 'default')
172             $this->_default_theme = new Theme;
173
174         $this->_css = array();
175     }
176
177     function file ($file) {
178         return $this->_path . "$this->_theme/$file";
179     }
180
181     function _findFile ($file, $missing_okay = false) {
182         if (file_exists($this->_path . "$this->_theme/$file"))
183             return "$this->_theme/$file";
184
185         // FIXME: this is a short-term hack.  Delete this after all files
186         // get moved into themes/...
187         if (file_exists($this->_path . $file))
188             return $file;
189
190
191         if (isset($this->_default_theme)) {
192             return $this->_default_theme->_findFile($file, $missing_okay);
193         }
194         else if (!$missing_okay) {
195             trigger_error("$file: not found", E_USER_NOTICE);
196         }
197         return false;
198     }
199
200     function _findData ($file, $missing_okay = false) {
201         $path = $this->_findFile($file, $missing_okay);
202         if (!$path)
203             return false;
204
205         if (defined('DATA_PATH'))
206             return DATA_PATH . "/$path";
207         return $path;
208     }
209
210     ////////////////////////////////////////////////////////////////
211     //
212     // Date and Time formatting
213     //
214     ////////////////////////////////////////////////////////////////
215
216     // Note:  Windows' implemetation of strftime does not include certain
217         // format specifiers, such as %e (for date without leading zeros).  In
218         // general, see:
219         // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp
220         // As a result, we have to use %d, and strip out leading zeros ourselves.
221
222     var $_dateFormat = "%B %d, %Y";
223     var $_timeFormat = "%I:%M %p";
224
225     var $_showModTime = true;
226
227     /**
228      * Set format string used for dates.
229      *
230      * @param $fs string Format string for dates.
231      *
232      * @param $show_mod_time bool If true (default) then times
233      * are included in the messages generated by getLastModifiedMessage(),
234      * otherwise, only the date of last modification will be shown.
235      */
236     function setDateFormat ($fs, $show_mod_time = true) {
237         $this->_dateFormat = $fs;
238         $this->_showModTime = $show_mod_time;
239     }
240
241     /**
242      * Set format string used for times.
243      *
244      * @param $fs string Format string for times.
245      */
246     function setTimeFormat ($fs) {
247         $this->_timeFormat = $fs;
248     }
249
250     /**
251      * Format a date.
252      *
253      * Any time zone offset specified in the users preferences is
254      * taken into account by this method.
255      *
256      * @param $time_t integer Unix-style time.
257      *
258      * @return string The date.
259      */
260     function formatDate ($time_t) {
261         global $request;
262         
263         $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
264         // strip leading zeros from date elements (ie space followed by zero)
265         return preg_replace('/ 0/', ' ', 
266                             strftime($this->_dateFormat, $offset_time));
267     }
268
269     /**
270      * Format a date.
271      *
272      * Any time zone offset specified in the users preferences is
273      * taken into account by this method.
274      *
275      * @param $time_t integer Unix-style time.
276      *
277      * @return string The time.
278      */
279     function formatTime ($time_t) {
280         //FIXME: make 24-hour mode configurable?
281         global $request;
282         $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
283         return preg_replace('/^0/', ' ',
284                             strtolower(strftime($this->_timeFormat, $offset_time)));
285     }
286
287     /**
288      * Format a date and time.
289      *
290      * Any time zone offset specified in the users preferences is
291      * taken into account by this method.
292      *
293      * @param $time_t integer Unix-style time.
294      *
295      * @return string The date and time.
296      */
297     function formatDateTime ($time_t) {
298         return $this->formatDate($time_t) . ' ' . $this->formatTime($time_t);
299     }
300
301     /**
302      * Format a (possibly relative) date.
303      *
304      * If enabled in the users preferences, this method might
305      * return a relative day (e.g. 'Today', 'Yesterday').
306      *
307      * Any time zone offset specified in the users preferences is
308      * taken into account by this method.
309      *
310      * @param $time_t integer Unix-style time.
311      *
312      * @return string The day.
313      */
314     function getDay ($time_t) {
315         global $request;
316         
317         if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($time_t))) {
318             return ucfirst($date);
319         }
320         return $this->formatDate($time_t);
321     }
322     
323     /**
324      * Format the "last modified" message for a page revision.
325      *
326      * @param $revision object A WikiDB_PageRevision object.
327      *
328      * @param $show_version bool Should the page version number
329      * be included in the message.  (If this argument is omitted,
330      * then the version number will be shown only iff the revision
331      * is not the current one.
332      *
333      * @return string The "last modified" message.
334      */
335     function getLastModifiedMessage ($revision, $show_version = 'auto') {
336         global $request;
337
338         // dates >= this are considered invalid.
339         if (! defined('EPOCH'))
340             define('EPOCH', 0); // seconds since ~ January 1 1970
341
342         $mtime = $revision->get('mtime');
343         if ($mtime <= EPOCH)
344             return fmt("Never edited.");
345
346         if ($show_version == 'auto')
347             $show_version = !$revision->isCurrent();
348
349         if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($mtime))) {
350             if ($this->_showModTime)
351                 $date =  sprintf(_("%s at %s"),
352                                  $date, $this->formatTime($mtime));
353             
354             if ($show_version)
355                 return fmt("Version %s, saved %s.", $revision->getVersion(), $date);
356             else
357                 return fmt("Last edited %s.", $date);
358         }
359
360         if ($this->_showModTime)
361             $date = $this->formatDateTime($mtime);
362         else
363             $date = $this->formatDate($mtime);
364         
365         if ($show_version)
366             return fmt("Version %s, saved on %s.", $revision->getVersion(), $date);
367         else
368             return fmt("Last edited on %s.", $date);
369     }
370     
371     function _relativeDay ($time_t) {
372         global $request;
373         
374         if (is_numeric($request->getPref('timeOffset')))
375           $offset = 3600 * $request->getPref('timeOffset');
376         else 
377           $offset = 0;          
378
379         $now = time() + $offset;
380         $today = localtime($now, true);
381         $time = localtime($time_t + $offset, true);
382
383         if ($time['tm_yday'] == $today['tm_yday'] && $time['tm_year'] == $today['tm_year'])
384             return _("today");
385         
386         // Note that due to daylight savings chages (and leap seconds), $now minus
387         // 24 hours is not guaranteed to be yesterday.
388         $yesterday = localtime($now - (12 + $today['tm_hour']) * 3600, true);
389         if ($time['tm_yday'] == $yesterday['tm_yday'] && $time['tm_year'] == $yesterday['tm_year'])
390             return _("yesterday");
391
392         return false;
393     }
394
395     ////////////////////////////////////////////////////////////////
396     //
397     // Hooks for other formatting
398     //
399     ////////////////////////////////////////////////////////////////
400
401     //FIXME: PHP 4.1 Warnings
402     //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
403     //use the call_user_func variety with the array(&$obj, "method") syntax instead
404
405     function getFormatter ($type, $format) {
406         $method = strtolower("get${type}Formatter");
407         if (method_exists($this, $method))
408             return $this->{$method}($format);
409         return false;
410     }
411
412     ////////////////////////////////////////////////////////////////
413     //
414     // Links
415     //
416     ////////////////////////////////////////////////////////////////
417
418     var $_autosplitWikiWords = false;
419
420     function setAutosplitWikiWords($autosplit=false) {
421         $this->_autosplitWikiWords = $autosplit ? true : false;
422     }
423
424     function maybeSplitWikiWord ($wikiword) {
425         if ($this->_autosplitWikiWords)
426             return split_pagename($wikiword);
427         else
428             return $wikiword;
429     }
430
431     function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
432         global $request;
433
434         if ($version !== false)
435             $url = WikiURL($wikiword, array('version' => $version));
436         else
437             $url = WikiURL($wikiword);
438
439         // Extra steps for dumping page to an html file.
440         // FIXME: shouldn't this be in WikiURL?
441         if ($this->HTML_DUMP_SUFFIX) {
442             // urlencode for pagenames with accented letters
443             $url = rawurlencode($url);
444             $url = preg_replace('/^\./', '%2e', $url);
445             $url .= $this->HTML_DUMP_SUFFIX;
446         }
447
448         $link = HTML::a(array('href' => $url));
449
450         if (isa($wikiword, 'WikiPageName'))
451             $default_text = $wikiword->shortName;
452         else
453             $default_text = $wikiword;
454         
455         if (!empty($linktext)) {
456             $link->pushContent($linktext);
457             $link->setAttr('class', 'named-wiki');
458             $link->setAttr('title', $this->maybeSplitWikiWord($default_text));
459         }
460         else {
461             $link->pushContent($this->maybeSplitWikiWord($default_text));
462             $link->setAttr('class', 'wiki');
463         }
464         if ($request->getArg('frame'))
465             $link->setAttr('target', '_top');
466         return $link;
467     }
468
469     function linkUnknownWikiWord($wikiword, $linktext = '') {
470         global $request;
471
472         // Get rid of anchors on unknown wikiwords
473         if (isa($wikiword, 'WikiPageName')) {
474             $default_text = $wikiword->shortName;
475             $wikiword = $wikiword->name;
476         }
477         else {
478             $default_text = $wikiword;
479         }
480         
481         $url = WikiURL($wikiword, array('action' => 'create'));
482
483         $button = $this->makeButton('?', $url);
484         $button->addTooltip(sprintf(_("Create: %s"), $wikiword));
485         $link = HTML::span($button);
486
487
488         if (!empty($linktext)) {
489             $link->pushContent(HTML::u($linktext));
490             $link->setAttr('class', 'named-wikiunknown');
491         }
492         else {
493             $link->pushContent(HTML::u($this->maybeSplitWikiWord($default_text)));
494             $link->setAttr('class', 'wikiunknown');
495         }
496         if ($request->getArg('frame'))
497             $link->setAttr('target', '_top');
498
499         return $link;
500     }
501
502     function linkBadWikiWord($wikiword, $linktext = '') {
503         global $ErrorManager;
504         
505         if ($linktext) {
506             $text = $linktext;
507         }
508         elseif (isa($wikiword, 'WikiPageName')) {
509             $text = $wikiword->shortName;
510         }
511         else {
512             $text = $wikiword;
513         }
514
515         if (isa($wikiword, 'WikiPageName'))
516             $message = $wikiword->getWarnings();
517         else
518             $message = sprintf(_("'%s': Bad page name"), $wikiword);
519         $ErrorManager->warning($message);
520         
521         return HTML::span(array('class' => 'badwikiword'), $text);
522     }
523     
524     ////////////////////////////////////////////////////////////////
525     //
526     // Images and Icons
527     //
528     ////////////////////////////////////////////////////////////////
529     var $_imageAliases = array();
530     var $_imageAlt = array();
531
532     /**
533      *
534      * (To disable an image, alias the image to <code>false</code>.
535      */
536     function addImageAlias ($alias, $image_name) {
537         // fall back to the PhpWiki-supplied image if not found
538         if ($this->_findFile("images/$image_name", true))
539             $this->_imageAliases[$alias] = $image_name;
540     }
541
542     function addImageAlt ($alias, $alt_text) {
543         $this->_imageAlt[$alias] = $alt_text;
544     }
545     function getImageAlt ($alias) {
546         return $this->_imageAlt[$alias];
547     }
548
549     function getImageURL ($image) {
550         $aliases = &$this->_imageAliases;
551
552         if (isset($aliases[$image])) {
553             $image = $aliases[$image];
554             if (!$image)
555                 return false;
556         }
557
558         // If not extension, default to .png.
559         if (!preg_match('/\.\w+$/', $image))
560             $image .= '.png';
561
562         // FIXME: this should probably be made to fall back
563         //        automatically to .gif, .jpg.
564         //        Also try .gif before .png if browser doesn't like png.
565
566         $path = $this->_findData("images/$image", 'missing okay');
567         if (!$path) // search explicit images/ or button/ links also
568             return $this->_findData("$image", 'missing okay');
569         else 
570             return $path;       
571     }
572
573     function setLinkIcon($proto, $image = false) {
574         if (!$image)
575             $image = $proto;
576
577         $this->_linkIcons[$proto] = $image;
578     }
579
580     function getLinkIconURL ($proto) {
581         $icons = &$this->_linkIcons;
582         if (!empty($icons[$proto]))
583             return $this->getImageURL($icons[$proto]);
584         elseif (!empty($icons['*']))
585             return $this->getImageURL($icons['*']);
586         return false;
587     }
588
589     function addButtonAlias ($text, $alias = false) {
590         $aliases = &$this->_buttonAliases;
591
592         if (is_array($text))
593             $aliases = array_merge($aliases, $text);
594         elseif ($alias === false)
595             unset($aliases[$text]);
596         else
597             $aliases[$text] = $alias;
598     }
599
600     function getButtonURL ($text) {
601         $aliases = &$this->_buttonAliases;
602         if (isset($aliases[$text]))
603             $text = $aliases[$text];
604
605         $qtext = urlencode($text);
606         $url = $this->_findButton("$qtext.png");
607         if ($url && strstr($url, '%')) {
608             $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
609         }
610         if (!$url) {// Jeff complained about png not supported everywhere. This is not PC
611             $url = $this->_findButton("$qtext.gif");
612             if ($url && strstr($url, '%')) {
613                 $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
614             }
615         }
616         return $url;
617     }
618
619     function _findButton ($button_file) {
620         if (!isset($this->_button_path))
621             $this->_button_path = $this->_getButtonPath();
622
623         foreach ($this->_button_path as $dir) {
624             $path = "$this->_theme/$dir/$button_file";
625             if (file_exists($this->_path . $path))
626                 return defined('DATA_PATH') ? DATA_PATH . "/$path" : $path;
627         }
628         return false;
629     }
630
631     function _getButtonPath () {
632         $button_dir = $this->file("buttons");
633         if (!file_exists($button_dir) || !is_dir($button_dir))
634             return array();
635
636         $path = array('buttons');
637
638         $dir = dir($button_dir);
639         while (($subdir = $dir->read()) !== false) {
640             if ($subdir[0] == '.')
641                 continue;
642             if (is_dir("$button_dir/$subdir"))
643                 $path[] = "buttons/$subdir";
644         }
645         $dir->close();
646
647         return $path;
648     }
649
650     ////////////////////////////////////////////////////////////////
651     //
652     // Button style
653     //
654     ////////////////////////////////////////////////////////////////
655
656     function makeButton ($text, $url, $class = false) {
657         // FIXME: don't always try for image button?
658
659         // Special case: URLs like 'submit:preview' generate form
660         // submission buttons.
661         if (preg_match('/^submit:(.*)$/', $url, $m))
662             return $this->makeSubmitButton($text, $m[1], $class);
663
664         $imgurl = $this->getButtonURL($text);
665         if ($imgurl)
666             return new ImageButton($text, $url, $class, $imgurl);
667         else
668             return new Button($text, $url, $class);
669     }
670
671     function makeSubmitButton ($text, $name, $class = false) {
672         $imgurl = $this->getButtonURL($text);
673
674         if ($imgurl)
675             return new SubmitImageButton($text, $name, $class, $imgurl);
676         else
677             return new SubmitButton($text, $name, $class);
678     }
679
680     /**
681      * Make button to perform action.
682      *
683      * This constructs a button which performs an action on the
684      * currently selected version of the current page.
685      * (Or anotherpage or version, if you want...)
686      *
687      * @param $action string The action to perform (e.g. 'edit', 'lock').
688      * This can also be the name of an "action page" like 'LikePages'.
689      * Alternatively you can give a hash of query args to be applied
690      * to the page.
691      *
692      * @param $label string Textual label for the button.  If left empty,
693      * a suitable name will be guessed.
694      *
695      * @param $page_or_rev mixed  The page to link to.  This can be
696      * given as a string (the page name), a WikiDB_Page object, or as
697      * WikiDB_PageRevision object.  If given as a WikiDB_PageRevision
698      * object, the button will link to a specific version of the
699      * designated page, otherwise the button links to the most recent
700      * version of the page.
701      *
702      * @return object A Button object.
703      */
704     function makeActionButton ($action, $label = false, $page_or_rev = false) {
705         extract($this->_get_name_and_rev($page_or_rev));
706
707         if (is_array($action)) {
708             $attr = $action;
709             $action = isset($attr['action']) ? $attr['action'] : 'browse';
710         }
711         else
712             $attr['action'] = $action;
713
714         $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
715         if (!$label)
716             $label = $this->_labelForAction($action);
717
718         if ($version)
719             $attr['version'] = $version;
720
721         if ($action == 'browse')
722             unset($attr['action']);
723
724         return $this->makeButton($label, WikiURL($pagename, $attr), $class);
725     }
726
727     /**
728      * Make a "button" which links to a wiki-page.
729      *
730      * These are really just regular WikiLinks, possibly
731      * disguised (e.g. behind an image button) by the theme.
732      *
733      * This method should probably only be used for links
734      * which appear in page navigation bars, or similar places.
735      *
736      * Use linkExistingWikiWord, or LinkWikiWord for normal links.
737      *
738      * @param $page_or_rev mixed The page to link to.  This can be
739      * given as a string (the page name), a WikiDB_Page object, or as
740      * WikiDB_PageRevision object.  If given as a WikiDB_PageRevision
741      * object, the button will link to a specific version of the
742      * designated page, otherwise the button links to the most recent
743      * version of the page.
744      *
745      * @return object A Button object.
746      */
747     function makeLinkButton ($page_or_rev, $label = false) {
748         extract($this->_get_name_and_rev($page_or_rev));
749
750         $args = $version ? array('version' => $version) : false;
751
752         return $this->makeButton($label ? $label : $this->maybeSplitWikiWord($pagename), 
753                                  WikiURL($pagename, $args), 'wiki');
754     }
755
756     function _get_name_and_rev ($page_or_rev) {
757         $version = false;
758
759         if (empty($page_or_rev)) {
760             global $request;
761             $pagename = $request->getArg("pagename");
762             $version = $request->getArg("version");
763         }
764         elseif (is_object($page_or_rev)) {
765             if (isa($page_or_rev, 'WikiDB_PageRevision')) {
766                 $rev = $page_or_rev;
767                 $page = $rev->getPage();
768                 $version = $rev->getVersion();
769             }
770             else {
771                 $page = $page_or_rev;
772             }
773             $pagename = $page->getName();
774         }
775         else {
776             $pagename = (string) $page_or_rev;
777         }
778         return compact('pagename', 'version');
779     }
780
781     function _labelForAction ($action) {
782         switch ($action) {
783             case 'edit':   return _("Edit");
784             case 'diff':   return _("Diff");
785             case 'logout': return _("Sign Out");
786             case 'login':  return _("Sign In");
787             case 'lock':   return _("Lock Page");
788             case 'unlock': return _("Unlock Page");
789             case 'remove': return _("Remove Page");
790             default:
791                 // I don't think the rest of these actually get used.
792                 // 'setprefs'
793                 // 'upload' 'dumpserial' 'loadfile' 'zip'
794                 // 'save' 'browse'
795                 return gettext(ucfirst($action));
796         }
797     }
798
799     //----------------------------------------------------------------
800     var $_buttonSeparator = "\n | ";
801
802     function setButtonSeparator($separator) {
803         $this->_buttonSeparator = $separator;
804     }
805
806     function getButtonSeparator() {
807         return $this->_buttonSeparator;
808     }
809
810
811     ////////////////////////////////////////////////////////////////
812     //
813     // CSS
814     //
815     // Notes:
816     //
817     // Based on testing with Galeon 1.2.7 (Mozilla 1.2):
818     // Automatic media-based style selection (via <link> tags) only
819     // seems to work for the default style, not for alternate styles.
820     //
821     // Doing
822     //
823     //  <link rel="stylesheet" type="text/css" href="phpwiki.css" />
824     //  <link rel="stylesheet" type="text/css" href="phpwiki-printer.css" media="print" />
825     //
826     // works to make it so that the printer style sheet get used
827     // automatically when printing (or print-previewing) a page
828     // (but when only when the default style is selected.)
829     //
830     // Attempts like:
831     //
832     //  <link rel="alternate stylesheet" title="Modern"
833     //        type="text/css" href="phpwiki-modern.css" />
834     //  <link rel="alternate stylesheet" title="Modern"
835     //        type="text/css" href="phpwiki-printer.css" media="print" />
836     //
837     // Result in two "Modern" choices when trying to select alternate style.
838     // If one selects the first of those choices, one gets phpwiki-modern
839     // both when browsing and printing.  If one selects the second "Modern",
840     // one gets no CSS when browsing, and phpwiki-printer when printing.
841     //
842     // The Real Fix?
843     // =============
844     //
845     // We should probably move to doing the media based style
846     // switching in the CSS files themselves using, e.g.:
847     //
848     //  @import url(print.css) print;
849     //
850     ////////////////////////////////////////////////////////////////
851
852     function _CSSlink($title, $css_file, $media, $is_alt = false) {
853         // Don't set title on default style.  This makes it clear to
854         // the user which is the default (i.e. most supported) style.
855         $link = HTML::link(array('rel'     => $is_alt ? 'alternate stylesheet' : 'stylesheet',
856                                  'type'    => 'text/css',
857                                  'charset' => CHARSET,
858                                  'href'    => $this->_findData($css_file)));
859         if ($is_alt)
860             $link->setAttr('title', $title);
861
862         if ($media) 
863             $link->setAttr('media', $media);
864         
865         return $link;
866     }
867
868     /** Set default CSS source for this theme.
869      *
870      * To set styles to be used for different media, pass a
871      * hash for the second argument, e.g.
872      *
873      * $theme->setDefaultCSS('default', array('' => 'normal.css',
874      *                                        'print' => 'printer.css'));
875      *
876      * If you call this more than once, the last one called takes
877      * precedence as the default style.
878      *
879      * @param string $title Name of style (currently ignored, unless
880      * you call this more than once, in which case, some of the style
881      * will become alternate (rather than default) styles, and then their
882      * titles will be used.
883      *
884      * @param mixed $css_files Name of CSS file, or hash containing a mapping
885      * between media types and CSS file names.  Use a key of '' (the empty string)
886      * to set the default CSS for non-specified media.  (See above for an example.)
887      */
888     function setDefaultCSS ($title, $css_files) {
889         if (!is_array($css_files))
890             $css_files = array('' => $css_files);
891         // Add to the front of $this->_css
892         unset($this->_css[$title]);
893         $this->_css = array_merge(array($title => $css_files), $this->_css);
894     }
895
896     /** Set alternate CSS source for this theme.
897      *
898      * @param string $title Name of style.
899      * @param string $css_files Name of CSS file.
900      */
901     function addAlternateCSS ($title, $css_files) {
902         if (!is_array($css_files))
903             $css_files = array('' => $css_files);
904         $this->_css[$title] = $css_files;
905     }
906
907     /**
908         * @return string HTML for CSS.
909      */
910     function getCSS () {
911         $css = array();
912         $is_alt = false;
913         foreach ($this->_css as $title => $css_files) {
914             ksort($css_files); // move $css_files[''] to front.
915             foreach ($css_files as $media => $css_file) {
916                 $css[] = $this->_CSSlink($title, $css_file, $media, $is_alt);
917                 if ($is_alt) break;
918                 
919             }
920             $is_alt = true;
921         }
922         return HTML($css);
923     }
924     
925
926     function findTemplate ($name) {
927         return $this->_path . $this->_findFile("templates/$name.tmpl");
928     }
929
930     var $_MoreHeaders = array();
931     function addMoreHeaders ($element) {
932         array_push($this->_MoreHeaders,$element);
933     }
934     function getMoreHeaders () {
935         if (empty($this->_MoreHeaders))
936             return '';
937         $out = '';
938         //$out = "<!-- More Headers -->\n";
939         foreach ($this->_MoreHeaders as $h) {
940             if (is_object($h))
941                 $out .= printXML($h);
942             else
943                 $out .= "$h\n";
944         }
945         return $out;
946     }
947
948     var $_MoreAttr = array();
949     function addMoreAttr ($id,$element) {
950         if (empty($this->_MoreAttr) or !is_array($this->_MoreAttr[$id]))
951             $this->_MoreAttr[$id] = array($element);
952         else
953             array_push($this->_MoreAttr[$id],$element);
954     }
955     function getMoreAttr ($id) {
956         if (empty($this->_MoreAttr[$id]))
957             return '';
958         $out = '';
959         foreach ($this->_MoreAttr[$id] as $h) {
960             if (is_object($h))
961                 $out .= printXML($h);
962             else
963                 $out .= "$h";
964         }
965         return $out;
966     }
967 };
968
969
970 /**
971  * A class representing a clickable "button".
972  *
973  * In it's simplest (default) form, a "button" is just a link associated
974  * with some sort of wiki-action.
975  */
976 class Button extends HtmlElement {
977     /** Constructor
978      *
979      * @param $text string The text for the button.
980      * @param $url string The url (href) for the button.
981      * @param $class string The CSS class for the button.
982      */
983     function Button ($text, $url, $class = false) {
984         global $request;
985         $this->HtmlElement('a', array('href' => $url));
986         if ($class)
987             $this->setAttr('class', $class);
988         if ($request->getArg('frame'))
989             $this->setAttr('target', '_top');
990         $this->pushContent($GLOBALS['Theme']->maybeSplitWikiWord($text));
991     }
992
993 };
994
995
996 /**
997  * A clickable image button.
998  */
999 class ImageButton extends Button {
1000     /** Constructor
1001      *
1002      * @param $text string The text for the button.
1003      * @param $url string The url (href) for the button.
1004      * @param $class string The CSS class for the button.
1005      * @param $img_url string URL for button's image.
1006      * @param $img_attr array Additional attributes for the &lt;img&gt; tag.
1007      */
1008     function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
1009         $this->HtmlElement('a', array('href' => $url));
1010         if ($class)
1011             $this->setAttr('class', $class);
1012
1013         if (!is_array($img_attr))
1014             $img_attr = array();
1015         $img_attr['src'] = $img_url;
1016         $img_attr['alt'] = $text;
1017         $img_attr['class'] = 'wiki-button';
1018         $img_attr['border'] = 0;
1019         $this->pushContent(HTML::img($img_attr));
1020     }
1021 };
1022
1023 /**
1024  * A class representing a form <samp>submit</samp> button.
1025  */
1026 class SubmitButton extends HtmlElement {
1027     /** Constructor
1028      *
1029      * @param $text string The text for the button.
1030      * @param $name string The name of the form field.
1031      * @param $class string The CSS class for the button.
1032      */
1033     function SubmitButton ($text, $name = false, $class = false) {
1034         $this->HtmlElement('input', array('type' => 'submit',
1035                                           'value' => $text));
1036         if ($name)
1037             $this->setAttr('name', $name);
1038         if ($class)
1039             $this->setAttr('class', $class);
1040     }
1041
1042 };
1043
1044
1045 /**
1046  * A class representing an image form <samp>submit</samp> button.
1047  */
1048 class SubmitImageButton extends SubmitButton {
1049     /** Constructor
1050      *
1051      * @param $text string The text for the button.
1052      * @param $name string The name of the form field.
1053      * @param $class string The CSS class for the button.
1054      * @param $img_url string URL for button's image.
1055      * @param $img_attr array Additional attributes for the &lt;img&gt; tag.
1056      */
1057     function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
1058         $this->HtmlElement('input', array('type'  => 'image',
1059                                           'src'   => $img_url,
1060                                           'value' => $text,
1061                                           'alt'   => $text));
1062         if ($name)
1063             $this->setAttr('name', $name);
1064         if ($class)
1065             $this->setAttr('class', $class);
1066     }
1067
1068 };
1069
1070 // $Log: not supported by cvs2svn $
1071 // Revision 1.70  2004/01/26 09:17:48  rurban
1072 // * changed stored pref representation as before.
1073 //   the array of objects is 1) bigger and 2)
1074 //   less portable. If we would import packed pref
1075 //   objects and the object definition was changed, PHP would fail.
1076 //   This doesn't happen with an simple array of non-default values.
1077 // * use $prefs->retrieve and $prefs->store methods, where retrieve
1078 //   understands the interim format of array of objects also.
1079 // * simplified $prefs->get() and fixed $prefs->set()
1080 // * added $user->_userid and class '_WikiUser' portability functions
1081 // * fixed $user object ->_level upgrading, mostly using sessions.
1082 //   this fixes yesterdays problems with loosing authorization level.
1083 // * fixed WikiUserNew::checkPass to return the _level
1084 // * fixed WikiUserNew::isSignedIn
1085 // * added explodePageList to class PageList, support sortby arg
1086 // * fixed UserPreferences for WikiUserNew
1087 // * fixed WikiPlugin for empty defaults array
1088 // * UnfoldSubpages: added pagename arg, renamed pages arg,
1089 //   removed sort arg, support sortby arg
1090 //
1091 // Revision 1.69  2003/12/05 01:32:28  carstenklapp
1092 // New feature: Easier to run multiple wiks off of one set of code. Name
1093 // your logo and signature image files "YourWikiNameLogo.png" and
1094 // "YourWikiNameSignature.png" and put them all into
1095 // themes/default/images. YourWikiName should match what is defined as
1096 // WIKI_NAME in index.php. In case the image is not found, the default
1097 // shipped with PhpWiki will be used.
1098 //
1099 // Revision 1.68  2003/03/04 01:53:30  dairiki
1100 // Inconsequential decrufting.
1101 //
1102 // Revision 1.67  2003/02/26 03:40:22  dairiki
1103 // New action=create.  Essentially the same as action=edit, except that if the
1104 // page already exists, it falls back to action=browse.
1105 //
1106 // This is for use in the "question mark" links for unknown wiki words
1107 // to avoid problems and confusion when following links from stale pages.
1108 // (If the "unknown page" has been created in the interim, the user probably
1109 // wants to view the page before editing it.)
1110 //
1111 // Revision 1.66  2003/02/26 00:10:26  dairiki
1112 // More/better/different checks for bad page names.
1113 //
1114 // Revision 1.65  2003/02/24 22:41:57  dairiki
1115 // Fix stupid typo.
1116 //
1117 // Revision 1.64  2003/02/24 22:06:14  dairiki
1118 // Attempts to fix auto-selection of printer CSS when printing.
1119 // See new comments lib/Theme.php for more details.
1120 // Also see SF patch #669563.
1121 //
1122 // Revision 1.63  2003/02/23 03:37:05  dairiki
1123 // Stupid typo/bug fix.
1124 //
1125 // Revision 1.62  2003/02/21 04:14:52  dairiki
1126 // New WikiLink type 'if_known'.  This gives linkified name if page
1127 // exists, otherwise, just plain text.
1128 //
1129 // Revision 1.61  2003/02/18 21:52:05  dairiki
1130 // Fix so that one can still link to wiki pages with # in their names.
1131 // (This was made difficult by the introduction of named tags, since
1132 // '[Page #1]' is now a link to anchor '1' in page 'Page'.
1133 //
1134 // Now the ~ escape for page names should work: [Page ~#1].
1135 //
1136 // Revision 1.60  2003/02/15 01:59:47  dairiki
1137 // Theme::getCSS():  Add Default-Style HTTP(-eqiv) header in attempt
1138 // to fix default stylesheet selection on some browsers.
1139 // For details on the Default-Style header, see:
1140 //  http://home.dairiki.org/docs/html4/present/styles.html#h-14.3.2
1141 //
1142 // Revision 1.59  2003/01/04 22:30:16  carstenklapp
1143 // New: display a "Never edited." message instead of an invalid epoch date.
1144 //
1145
1146 // (c-file-style: "gnu")
1147 // Local Variables:
1148 // mode: php
1149 // tab-width: 8
1150 // c-basic-offset: 4
1151 // c-hanging-comment-ender-p: nil
1152 // indent-tabs-mode: nil
1153 // End:
1154 ?>