]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/Theme.php
Don't NORETURN from redirect.
[SourceForge/phpwiki.git] / lib / Theme.php
1 <?php rcs_id('$Id: Theme.php,v 1.62 2003-02-21 04:14:52 dairiki 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
65     if ($type == 'auto' or $type == 'if_known') {
66         if (isset($page)) {
67             $current = $page->getCurrentRevision();
68             $exists = ! $current->hasDefaultContents();
69         }
70         else {
71             $dbi = $request->getDbh();
72             $exists = $dbi->isWikiPage($wikipage->name);
73         }
74     }
75     elseif ($type == 'unknown') {
76         $exists = false;
77     }
78     else {
79         $exists = true;
80     }
81
82     // FIXME: this should be somewhere else, if really needed.
83     // WikiLink makes A link, not a string of fancy ones.
84     // (I think that the fancy split links are just confusing.)
85     // Todo: test external ImageLinks http://some/images/next.gif
86     if (isa($wikipage, 'WikiPageName') and !$label 
87         and strchr(substr($wikipage->shortName,1), SUBPAGE_SEPARATOR)) {
88         $parts = explode(SUBPAGE_SEPARATOR, $wikipage->shortName);
89         $last_part = array_pop($parts);
90         $sep = '';
91         $link = HTML::span();
92         foreach ($parts as $part) {
93             $path[] = $part;
94             $parent = join(SUBPAGE_SEPARATOR, $path);
95             if ($part)
96                 $link->pushContent($Theme->linkExistingWikiWord($parent, $sep . $part));
97             $sep = SUBPAGE_SEPARATOR;
98         }
99         if ($exists)
100             $link->pushContent($Theme->linkExistingWikiWord($wikipage, $sep . $last_part, $version));
101         else
102             $link->pushContent($Theme->linkUnknownWikiWord($wikipage, $last_part));
103         return $link;
104     }
105
106     if ($exists) {
107         return $Theme->linkExistingWikiWord($wikipage, $label, $version);
108     }
109     elseif ($type == 'if_known') {
110         if (!$label && isa($wikipage, 'WikiPageName'))
111             $label = $wikipage->shortName;
112         return HTML($label ? $label : $pagename);
113     }
114     else {
115         return $Theme->linkUnknownWikiWord($pagename, $label);
116     }
117 }
118
119
120
121 /**
122  * Make a button.
123  *
124  * This is a convenience function.
125  *
126  * @param $action string
127  * One of <dl>
128  * <dt>[action]</dt><dd>Perform action (e.g. 'edit') on the selected page.</dd>
129  * <dt>[ActionPage]</dt><dd>Run the actionpage (e.g. 'BackLinks') on the selected page.</dd>
130  * <dt>'submit:'[name]</dt><dd>Make a form submission button with the given name.
131  *      ([name] can be blank for a nameless submit button.)</dd>
132  * <dt>a hash</dt><dd>Query args for the action. E.g.<pre>
133  *      array('action' => 'diff', 'previous' => 'author')
134  * </pre></dd>
135  * </dl>
136  *
137  * @param $label string
138  * A label for the button.  If ommited, a suitable default (based on the valued of $action)
139  * will be picked.
140  *
141  * @param $page_or_rev mixed
142  * Which page (& version) to perform the action on.
143  * Can be one of:<dl>
144  * <dt>A string</dt><dd>The pagename.</dd>
145  * <dt>A WikiDB_Page object</dt><dd>The page.</dd>
146  * <dt>A WikiDB_PageRevision object</dt><dd>A specific version of the page.</dd>
147  * </dl>
148  * ($Page_or_rev is ignored for submit buttons.)
149  */
150 function Button ($action, $label = false, $page_or_rev = false) {
151     global $Theme;
152
153     if (!is_array($action) && preg_match('/submit:(.*)/A', $action, $m))
154         return $Theme->makeSubmitButton($label, $m[1], $class = $page_or_rev);
155     else
156         return $Theme->makeActionButton($action, $label, $page_or_rev);
157 }
158
159
160 class Theme {
161     var $HTML_DUMP_SUFFIX = '';
162     function Theme ($theme_name = 'default') {
163         $this->_name = $theme_name;
164         $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
165
166         $this->_path  = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/" : "";
167         $this->_theme = "themes/$theme_name";
168
169         if ($theme_name != 'default')
170             $this->_default_theme = new Theme;
171     }
172
173     function file ($file) {
174         return $this->_path . "$this->_theme/$file";
175     }
176
177     function _findFile ($file, $missing_okay = false) {
178         if (file_exists($this->_path . "$this->_theme/$file"))
179             return "$this->_theme/$file";
180
181         // FIXME: this is a short-term hack.  Delete this after all files
182         // get moved into themes/...
183         if (file_exists($this->_path . $file))
184             return $file;
185
186
187         if (isset($this->_default_theme)) {
188             return $this->_default_theme->_findFile($file, $missing_okay);
189         }
190         else if (!$missing_okay) {
191             trigger_error("$file: not found", E_USER_NOTICE);
192         }
193         return false;
194     }
195
196     function _findData ($file, $missing_okay = false) {
197         $path = $this->_findFile($file, $missing_okay);
198         if (!$path)
199             return false;
200
201         if (defined('DATA_PATH'))
202             return DATA_PATH . "/$path";
203         return $path;
204     }
205
206     ////////////////////////////////////////////////////////////////
207     //
208     // Date and Time formatting
209     //
210     ////////////////////////////////////////////////////////////////
211
212     // Note:  Windows' implemetation of strftime does not include certain
213         // format specifiers, such as %e (for date without leading zeros).  In
214         // general, see:
215         // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp
216         // As a result, we have to use %d, and strip out leading zeros ourselves.
217
218     var $_dateFormat = "%B %d, %Y";
219     var $_timeFormat = "%I:%M %p";
220
221     var $_showModTime = true;
222
223     /**
224      * Set format string used for dates.
225      *
226      * @param $fs string Format string for dates.
227      *
228      * @param $show_mod_time bool If true (default) then times
229      * are included in the messages generated by getLastModifiedMessage(),
230      * otherwise, only the date of last modification will be shown.
231      */
232     function setDateFormat ($fs, $show_mod_time = true) {
233         $this->_dateFormat = $fs;
234         $this->_showModTime = $show_mod_time;
235     }
236
237     /**
238      * Set format string used for times.
239      *
240      * @param $fs string Format string for times.
241      */
242     function setTimeFormat ($fs) {
243         $this->_timeFormat = $fs;
244     }
245
246     /**
247      * Format a date.
248      *
249      * Any time zone offset specified in the users preferences is
250      * taken into account by this method.
251      *
252      * @param $time_t integer Unix-style time.
253      *
254      * @return string The date.
255      */
256     function formatDate ($time_t) {
257         global $request;
258         
259         $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
260         // strip leading zeros from date elements (ie space followed by zero)
261         return preg_replace('/ 0/', ' ', 
262                             strftime($this->_dateFormat, $offset_time));
263     }
264
265     /**
266      * Format a date.
267      *
268      * Any time zone offset specified in the users preferences is
269      * taken into account by this method.
270      *
271      * @param $time_t integer Unix-style time.
272      *
273      * @return string The time.
274      */
275     function formatTime ($time_t) {
276         //FIXME: make 24-hour mode configurable?
277         global $request;
278         $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
279         return preg_replace('/^0/', ' ',
280                             strtolower(strftime($this->_timeFormat, $offset_time)));
281     }
282
283     /**
284      * Format a date and time.
285      *
286      * Any time zone offset specified in the users preferences is
287      * taken into account by this method.
288      *
289      * @param $time_t integer Unix-style time.
290      *
291      * @return string The date and time.
292      */
293     function formatDateTime ($time_t) {
294         return $this->formatDate($time_t) . ' ' . $this->formatTime($time_t);
295     }
296
297     /**
298      * Format a (possibly relative) date.
299      *
300      * If enabled in the users preferences, this method might
301      * return a relative day (e.g. 'Today', 'Yesterday').
302      *
303      * Any time zone offset specified in the users preferences is
304      * taken into account by this method.
305      *
306      * @param $time_t integer Unix-style time.
307      *
308      * @return string The day.
309      */
310     function getDay ($time_t) {
311         global $request;
312         
313         if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($time_t))) {
314             return ucfirst($date);
315         }
316         return $this->formatDate($time_t);
317     }
318     
319     /**
320      * Format the "last modified" message for a page revision.
321      *
322      * @param $revision object A WikiDB_PageRevision object.
323      *
324      * @param $show_version bool Should the page version number
325      * be included in the message.  (If this argument is omitted,
326      * then the version number will be shown only iff the revision
327      * is not the current one.
328      *
329      * @return string The "last modified" message.
330      */
331     function getLastModifiedMessage ($revision, $show_version = 'auto') {
332         global $request;
333
334         // dates >= this are considered invalid.
335         if (! defined('EPOCH'))
336             define('EPOCH', 0); // seconds since ~ January 1 1970
337
338         $mtime = $revision->get('mtime');
339         if ($mtime <= EPOCH)
340             return fmt("Never edited.");
341
342         if ($show_version == 'auto')
343             $show_version = !$revision->isCurrent();
344
345         if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($mtime))) {
346             if ($this->_showModTime)
347                 $date =  sprintf(_("%s at %s"),
348                                  $date, $this->formatTime($mtime));
349             
350             if ($show_version)
351                 return fmt("Version %s, saved %s.", $revision->getVersion(), $date);
352             else
353                 return fmt("Last edited %s.", $date);
354         }
355
356         if ($this->_showModTime)
357             $date = $this->formatDateTime($mtime);
358         else
359             $date = $this->formatDate($mtime);
360         
361         if ($show_version)
362             return fmt("Version %s, saved on %s.", $revision->getVersion(), $date);
363         else
364             return fmt("Last edited on %s.", $date);
365     }
366     
367     function _relativeDay ($time_t) {
368         global $request;
369         $offset = 3600 * $request->getPref('timeOffset');
370
371         $now = time() + $offset;
372         $today = localtime($now, true);
373         $time = localtime($time_t + $offset, true);
374
375         if ($time['tm_yday'] == $today['tm_yday'] && $time['tm_year'] == $today['tm_year'])
376             return _("today");
377         
378         // Note that due to daylight savings chages (and leap seconds), $now minus
379         // 24 hours is not guaranteed to be yesterday.
380         $yesterday = localtime($now - (12 + $today['tm_hour']) * 3600, true);
381         if ($time['tm_yday'] == $yesterday['tm_yday'] && $time['tm_year'] == $yesterday['tm_year'])
382             return _("yesterday");
383
384         return false;
385     }
386
387     ////////////////////////////////////////////////////////////////
388     //
389     // Hooks for other formatting
390     //
391     ////////////////////////////////////////////////////////////////
392
393     //FIXME: PHP 4.1 Warnings
394     //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
395     //use the call_user_func variety with the array(&$obj, "method") syntax instead
396
397     function getFormatter ($type, $format) {
398         $method = strtolower("get${type}Formatter");
399         if (method_exists($this, $method))
400             return $this->{$method}($format);
401         return false;
402     }
403
404     ////////////////////////////////////////////////////////////////
405     //
406     // Links
407     //
408     ////////////////////////////////////////////////////////////////
409
410     var $_autosplitWikiWords = false;
411
412     function setAutosplitWikiWords($autosplit=false) {
413         $this->_autosplitWikiWords = $autosplit ? true : false;
414     }
415
416     function maybeSplitWikiWord ($wikiword) {
417         if ($this->_autosplitWikiWords)
418             return split_pagename($wikiword);
419         else
420             return $wikiword;
421     }
422
423     function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
424         global $request;
425
426         if ($version !== false)
427             $url = WikiURL($wikiword, array('version' => $version));
428         else
429             $url = WikiURL($wikiword);
430
431         // Extra steps for dumping page to an html file.
432         // FIXME: shouldn't this be in WikiURL?
433         if ($this->HTML_DUMP_SUFFIX) {
434             // urlencode for pagenames with accented letters
435             $url = rawurlencode($url);
436             $url = preg_replace('/^\./', '%2e', $url);
437             $url .= $this->HTML_DUMP_SUFFIX;
438         }
439
440         $link = HTML::a(array('href' => $url));
441
442         if (isa($wikiword, 'WikiPageName'))
443             $default_text = $wikiword->shortName;
444         else
445             $default_text = $wikiword;
446         
447         if (!empty($linktext)) {
448             $link->pushContent($linktext);
449             $link->setAttr('class', 'named-wiki');
450             $link->setAttr('title', $this->maybeSplitWikiWord($default_text));
451         }
452         else {
453             $link->pushContent($this->maybeSplitWikiWord($default_text));
454             $link->setAttr('class', 'wiki');
455         }
456         if ($request->getArg('frame'))
457             $link->setAttr('target', '_top');
458         return $link;
459     }
460
461     function linkUnknownWikiWord($wikiword, $linktext = '') {
462         global $request;
463
464         // Get rid of anchors on unknown wikiwords
465         if (isa($wikiword, 'WikiPageName')) {
466             $default_text = $wikiword->shortName;
467             $wikiword = $wikiword->name;
468         }
469         else {
470             $default_text = $wikiword;
471         }
472         
473         $url = WikiURL($wikiword, array('action' => 'edit'));
474         //$link = HTML::span(HTML::a(array('href' => $url), '?'));
475         $button = $this->makeButton('?', $url);
476         $button->addTooltip(sprintf(_("Edit: %s"), $wikiword));
477         $link = HTML::span($button);
478
479
480         if (!empty($linktext)) {
481             $link->pushContent(HTML::u($linktext));
482             $link->setAttr('class', 'named-wikiunknown');
483         }
484         else {
485             $link->pushContent(HTML::u($this->maybeSplitWikiWord($default_text)));
486             $link->setAttr('class', 'wikiunknown');
487         }
488         if ($request->getArg('frame'))
489             $link->setAttr('target', '_top');
490
491         return $link;
492     }
493
494     ////////////////////////////////////////////////////////////////
495     //
496     // Images and Icons
497     //
498     ////////////////////////////////////////////////////////////////
499     var $_imageAliases = array();
500     var $_imageAlt = array();
501
502     /**
503      *
504      * (To disable an image, alias the image to <code>false</code>.
505      */
506     function addImageAlias ($alias, $image_name) {
507         $this->_imageAliases[$alias] = $image_name;
508     }
509
510     function addImageAlt ($alias, $alt_text) {
511         $this->_imageAlt[$alias] = $alt_text;
512     }
513     function getImageAlt ($alias) {
514         return $this->_imageAlt[$alias];
515     }
516
517     function getImageURL ($image) {
518         $aliases = &$this->_imageAliases;
519
520         if (isset($aliases[$image])) {
521             $image = $aliases[$image];
522             if (!$image)
523                 return false;
524         }
525
526         // If not extension, default to .png.
527         if (!preg_match('/\.\w+$/', $image))
528             $image .= '.png';
529
530         // FIXME: this should probably be made to fall back
531         //        automatically to .gif, .jpg.
532         //        Also try .gif before .png if browser doesn't like png.
533
534         $path = $this->_findData("images/$image", 'missing okay');
535         if (!$path) // search explicit images/ or button/ links also
536             return $this->_findData("$image", 'missing okay');
537         else 
538             return $path;       
539     }
540
541     function setLinkIcon($proto, $image = false) {
542         if (!$image)
543             $image = $proto;
544
545         $this->_linkIcons[$proto] = $image;
546     }
547
548     function getLinkIconURL ($proto) {
549         $icons = &$this->_linkIcons;
550         if (!empty($icons[$proto]))
551             return $this->getImageURL($icons[$proto]);
552         elseif (!empty($icons['*']))
553             return $this->getImageURL($icons['*']);
554         return false;
555     }
556
557     function addButtonAlias ($text, $alias = false) {
558         $aliases = &$this->_buttonAliases;
559
560         if (is_array($text))
561             $aliases = array_merge($aliases, $text);
562         elseif ($alias === false)
563             unset($aliases[$text]);
564         else
565             $aliases[$text] = $alias;
566     }
567
568     function getButtonURL ($text) {
569         $aliases = &$this->_buttonAliases;
570         if (isset($aliases[$text]))
571             $text = $aliases[$text];
572
573         $qtext = urlencode($text);
574         $url = $this->_findButton("$qtext.png");
575         if ($url && strstr($url, '%')) {
576             $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
577         }
578         if (!$url) {// Jeff complained about png not supported everywhere. This is not PC
579             $url = $this->_findButton("$qtext.gif");
580             if ($url && strstr($url, '%')) {
581                 $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
582             }
583         }
584         return $url;
585     }
586
587     function _findButton ($button_file) {
588         if (!isset($this->_button_path))
589             $this->_button_path = $this->_getButtonPath();
590
591         foreach ($this->_button_path as $dir) {
592             $path = "$this->_theme/$dir/$button_file";
593             if (file_exists($this->_path . $path))
594                 return defined('DATA_PATH') ? DATA_PATH . "/$path" : $path;
595         }
596         return false;
597     }
598
599     function _getButtonPath () {
600         $button_dir = $this->file("buttons");
601         if (!file_exists($button_dir) || !is_dir($button_dir))
602             return array();
603
604         $path = array('buttons');
605
606         $dir = dir($button_dir);
607         while (($subdir = $dir->read()) !== false) {
608             if ($subdir[0] == '.')
609                 continue;
610             if (is_dir("$button_dir/$subdir"))
611                 $path[] = "buttons/$subdir";
612         }
613         $dir->close();
614
615         return $path;
616     }
617
618     ////////////////////////////////////////////////////////////////
619     //
620     // Button style
621     //
622     ////////////////////////////////////////////////////////////////
623
624     function makeButton ($text, $url, $class = false) {
625         // FIXME: don't always try for image button?
626
627         // Special case: URLs like 'submit:preview' generate form
628         // submission buttons.
629         if (preg_match('/^submit:(.*)$/', $url, $m))
630             return $this->makeSubmitButton($text, $m[1], $class);
631
632         $imgurl = $this->getButtonURL($text);
633         if ($imgurl)
634             return new ImageButton($text, $url, $class, $imgurl);
635         else
636             return new Button($text, $url, $class);
637     }
638
639     function makeSubmitButton ($text, $name, $class = false) {
640         $imgurl = $this->getButtonURL($text);
641
642         if ($imgurl)
643             return new SubmitImageButton($text, $name, $class, $imgurl);
644         else
645             return new SubmitButton($text, $name, $class);
646     }
647
648     /**
649      * Make button to perform action.
650      *
651      * This constructs a button which performs an action on the
652      * currently selected version of the current page.
653      * (Or anotherpage or version, if you want...)
654      *
655      * @param $action string The action to perform (e.g. 'edit', 'lock').
656      * This can also be the name of an "action page" like 'LikePages'.
657      * Alternatively you can give a hash of query args to be applied
658      * to the page.
659      *
660      * @param $label string Textual label for the button.  If left empty,
661      * a suitable name will be guessed.
662      *
663      * @param $page_or_rev mixed  The page to link to.  This can be
664      * given as a string (the page name), a WikiDB_Page object, or as
665      * WikiDB_PageRevision object.  If given as a WikiDB_PageRevision
666      * object, the button will link to a specific version of the
667      * designated page, otherwise the button links to the most recent
668      * version of the page.
669      *
670      * @return object A Button object.
671      */
672     function makeActionButton ($action, $label = false, $page_or_rev = false) {
673         extract($this->_get_name_and_rev($page_or_rev));
674
675         if (is_array($action)) {
676             $attr = $action;
677             $action = isset($attr['action']) ? $attr['action'] : 'browse';
678         }
679         else
680             $attr['action'] = $action;
681
682         $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
683         if (!$label)
684             $label = $this->_labelForAction($action);
685
686         if ($version)
687             $attr['version'] = $version;
688
689         if ($action == 'browse')
690             unset($attr['action']);
691
692         return $this->makeButton($label, WikiURL($pagename, $attr), $class);
693     }
694
695     /**
696      * Make a "button" which links to a wiki-page.
697      *
698      * These are really just regular WikiLinks, possibly
699      * disguised (e.g. behind an image button) by the theme.
700      *
701      * This method should probably only be used for links
702      * which appear in page navigation bars, or similar places.
703      *
704      * Use linkExistingWikiWord, or LinkWikiWord for normal links.
705      *
706      * @param $page_or_rev mixed The page to link to.  This can be
707      * given as a string (the page name), a WikiDB_Page object, or as
708      * WikiDB_PageRevision object.  If given as a WikiDB_PageRevision
709      * object, the button will link to a specific version of the
710      * designated page, otherwise the button links to the most recent
711      * version of the page.
712      *
713      * @return object A Button object.
714      */
715     function makeLinkButton ($page_or_rev, $label = false) {
716         extract($this->_get_name_and_rev($page_or_rev));
717
718         $args = $version ? array('version' => $version) : false;
719
720         return $this->makeButton($label ? $label : $this->maybeSplitWikiWord($pagename), 
721                                  WikiURL($pagename, $args), 'wiki');
722     }
723
724     function _get_name_and_rev ($page_or_rev) {
725         $version = false;
726
727         if (empty($page_or_rev)) {
728             global $request;
729             $pagename = $request->getArg("pagename");
730             $version = $request->getArg("version");
731         }
732         elseif (is_object($page_or_rev)) {
733             if (isa($page_or_rev, 'WikiDB_PageRevision')) {
734                 $rev = $page_or_rev;
735                 $page = $rev->getPage();
736                 $version = $rev->getVersion();
737             }
738             else {
739                 $page = $page_or_rev;
740             }
741             $pagename = $page->getName();
742         }
743         else {
744             $pagename = (string) $page_or_rev;
745         }
746         return compact('pagename', 'version');
747     }
748
749     function _labelForAction ($action) {
750         switch ($action) {
751             case 'edit':   return _("Edit");
752             case 'diff':   return _("Diff");
753             case 'logout': return _("Sign Out");
754             case 'login':  return _("Sign In");
755             case 'lock':   return _("Lock Page");
756             case 'unlock': return _("Unlock Page");
757             case 'remove': return _("Remove Page");
758             default:
759                 // I don't think the rest of these actually get used.
760                 // 'setprefs'
761                 // 'upload' 'dumpserial' 'loadfile' 'zip'
762                 // 'save' 'browse'
763                 return gettext(ucfirst($action));
764         }
765     }
766
767     //----------------------------------------------------------------
768     var $_buttonSeparator = "\n | ";
769
770     function setButtonSeparator($separator) {
771         $this->_buttonSeparator = $separator;
772     }
773
774     function getButtonSeparator() {
775         return $this->_buttonSeparator;
776     }
777
778
779     ////////////////////////////////////////////////////////////////
780     //
781     // CSS
782     //
783     ////////////////////////////////////////////////////////////////
784
785     function _CSSlink($title, $css_file, $media, $is_alt = false) {
786         $link = HTML::link(array('rel'     => $is_alt ? 'alternate stylesheet' : 'stylesheet',
787                                  'title'   => $title,
788                                  'type'    => 'text/css',
789                                  'charset' => CHARSET,
790                                  'href'    => $this->_findData($css_file)));
791         if ($media)
792             $link->setAttr('media', $media);
793         return $link;
794     }
795
796     function setDefaultCSS ($title, $css_file, $media = false) {
797         if (isset($this->_defaultCSS)) {
798             $oldtitle = $this->_defaultCSS->getAttr('title');
799             $error = sprintf("'%s' -> '%s'", $oldtitle, $title);
800             trigger_error(sprintf(_("Redefinition of %s: %s"), "'default CSS'", $error),
801                           E_USER_NOTICE);
802         }
803         if (isset($this->_alternateCSS))
804             unset($this->_alternateCSS[$title]);
805         $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
806     }
807
808     function addAlternateCSS ($title, $css_file, $media = false) {
809         $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
810     }
811
812     /**
813         * @return string HTML for CSS.
814      */
815     function getCSS () {
816         $meta = HTML::meta(array('http-equiv' => 'Default-Style',
817                                  'content' => $this->_defaultCSS->getAttr('title')));
818         $css = HTML($meta, $this->_defaultCSS);
819         if (!empty($this->_alternateCSS))
820             $css->pushContent($this->_alternateCSS);
821         return $css;
822     }
823
824     function findTemplate ($name) {
825         return $this->_path . $this->_findFile("templates/$name.tmpl");
826     }
827 };
828
829
830 /**
831  * A class representing a clickable "button".
832  *
833  * In it's simplest (default) form, a "button" is just a link associated
834  * with some sort of wiki-action.
835  */
836 class Button extends HtmlElement {
837     /** Constructor
838      *
839      * @param $text string The text for the button.
840      * @param $url string The url (href) for the button.
841      * @param $class string The CSS class for the button.
842      */
843     function Button ($text, $url, $class = false) {
844         global $request;
845         $this->HtmlElement('a', array('href' => $url));
846         if ($class)
847             $this->setAttr('class', $class);
848         if ($request->getArg('frame'))
849             $this->setAttr('target', '_top');
850         $this->pushContent($GLOBALS['Theme']->maybeSplitWikiWord($text));
851     }
852
853 };
854
855
856 /**
857  * A clickable image button.
858  */
859 class ImageButton extends Button {
860     /** Constructor
861      *
862      * @param $text string The text for the button.
863      * @param $url string The url (href) for the button.
864      * @param $class string The CSS class for the button.
865      * @param $img_url string URL for button's image.
866      * @param $img_attr array Additional attributes for the &lt;img&gt; tag.
867      */
868     function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
869         $this->HtmlElement('a', array('href' => $url));
870         if ($class)
871             $this->setAttr('class', $class);
872
873         if (!is_array($img_attr))
874             $img_attr = array();
875         $img_attr['src'] = $img_url;
876         $img_attr['alt'] = $text;
877         $img_attr['class'] = 'wiki-button';
878         $img_attr['border'] = 0;
879         $this->pushContent(HTML::img($img_attr));
880     }
881 };
882
883 /**
884  * A class representing a form <samp>submit</samp> button.
885  */
886 class SubmitButton extends HtmlElement {
887     /** Constructor
888      *
889      * @param $text string The text for the button.
890      * @param $name string The name of the form field.
891      * @param $class string The CSS class for the button.
892      */
893     function SubmitButton ($text, $name = false, $class = false) {
894         $this->HtmlElement('input', array('type' => 'submit',
895                                           'value' => $text));
896         if ($name)
897             $this->setAttr('name', $name);
898         if ($class)
899             $this->setAttr('class', $class);
900     }
901
902 };
903
904
905 /**
906  * A class representing an image form <samp>submit</samp> button.
907  */
908 class SubmitImageButton extends SubmitButton {
909     /** Constructor
910      *
911      * @param $text string The text for the button.
912      * @param $name string The name of the form field.
913      * @param $class string The CSS class for the button.
914      * @param $img_url string URL for button's image.
915      * @param $img_attr array Additional attributes for the &lt;img&gt; tag.
916      */
917     function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
918         $this->HtmlElement('input', array('type'  => 'image',
919                                           'src'   => $img_url,
920                                           'value' => $text,
921                                           'alt'   => $text));
922         if ($name)
923             $this->setAttr('name', $name);
924         if ($class)
925             $this->setAttr('class', $class);
926     }
927
928 };
929
930 // $Log: not supported by cvs2svn $
931 // Revision 1.61  2003/02/18 21:52:05  dairiki
932 // Fix so that one can still link to wiki pages with # in their names.
933 // (This was made difficult by the introduction of named tags, since
934 // '[Page #1]' is now a link to anchor '1' in page 'Page'.
935 //
936 // Now the ~ escape for page names should work: [Page ~#1].
937 //
938 // Revision 1.60  2003/02/15 01:59:47  dairiki
939 // Theme::getCSS():  Add Default-Style HTTP(-eqiv) header in attempt
940 // to fix default stylesheet selection on some browsers.
941 // For details on the Default-Style header, see:
942 //  http://home.dairiki.org/docs/html4/present/styles.html#h-14.3.2
943 //
944 // Revision 1.59  2003/01/04 22:30:16  carstenklapp
945 // New: display a "Never edited." message instead of an invalid epoch date.
946 //
947
948 // (c-file-style: "gnu")
949 // Local Variables:
950 // mode: php
951 // tab-width: 8
952 // c-basic-offset: 4
953 // c-hanging-comment-ender-p: nil
954 // indent-tabs-mode: nil
955 // End:
956 ?>