1 <?php rcs_id('$Id: Theme.php,v 1.47 2002-08-17 15:52:51 rurban Exp $');
3 require_once('lib/HtmlElement.php');
7 * Make a link to a wiki page (in this wiki).
9 * This is a convenience function.
11 * @param $page_or_rev mixed
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>
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>
25 * Unless $type of of the latter form, the link will be of class 'wiki', 'wikiunknown',
26 * 'named-wiki', or 'named-wikiunknown', as appropriate.
28 * @param $label mixed (string or XmlContent object)
29 * Label for the link. If not given, defaults to the page name.
30 * (Label is ignored for $type == 'button'.)
32 function WikiLink ($page_or_rev, $type = 'known', $label = false) {
35 if ($type == 'button') {
36 return $Theme->makeLinkButton($page_or_rev);
41 if (isa($page_or_rev, 'WikiDB_PageRevision')) {
42 $version = $page_or_rev->getVersion();
43 $page = $page_or_rev->getPage();
44 $pagename = $page->getName();
47 elseif (isa($page_or_rev, 'WikiDB_Page')) {
49 $pagename = $page->getName();
52 $pagename = $page_or_rev;
53 if (substr($pagename,0,1) == '/') { // relative link to page below
55 $page = $request->getArg('pagename');
56 $label = substr($page_or_rev,1);
57 $pagename = $page . $pagename;
59 $pages = explode('/',$page);
61 array_pop($pages);// delete last element from array, the current subpage
62 $subpage = implode('/',$pages);
63 $label = substr($page_or_rev,1);
64 $pagename = $subpage . $pagename;
70 if ($type == 'auto') {
72 $current = $page->getCurrentRevision();
73 $exists = ! $current->hasDefaultContents();
77 $dbi = $request->getDbh();
78 $exists = $dbi->isWikiPage($pagename);
81 elseif ($type == 'unknown') {
87 // Todo: fix ImageLinks images/next.gif semantics
88 // support "/SubPage" relative link
89 if (is_string($page_or_rev)
91 and strchr(substr($page_or_rev,1), '/')) {
92 $pages = explode('/',$pagename);
93 $last_page = array_pop($pages); // deletes last element from array as side-effect
94 $link = HTML::span($Theme->linkExistingWikiWord($pages[0], $pages[0] . '/'));
95 $first_pages = $pages[0] . '/';
97 foreach ($pages as $page) {
98 $link->pushContent($Theme->linkExistingWikiWord($first_pages . $page, $page . '/'));
99 $first_pages .= $page . '/';
103 $link->pushContent($Theme->linkExistingWikiWord($pagename, $label, $version));
107 $link->pushContent($Theme->linkUnknownWikiWord($pagename, $label));
112 return $Theme->linkExistingWikiWord($pagename, $label, $version);
115 return $Theme->linkUnknownWikiWord($pagename, $label);
124 * This is a convenience function.
126 * @param $action string
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')
137 * @param $label string
138 * A label for the button. If ommited, a suitable default (based on the valued of $action)
141 * @param $page_or_rev mixed
142 * Which page (& version) to perform the action on.
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>
148 * ($Page_or_rev is ignored for submit buttons.)
150 function Button ($action, $label = false, $page_or_rev = false) {
153 if (!is_array($action) && preg_match('/submit:(.*)/A', $action, $m))
154 return $Theme->makeSubmitButton($label, $m[1], $class = $page_or_rev);
156 return $Theme->makeActionButton($action, $label, $page_or_rev);
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";
168 $this->_path = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/" : "";
169 $this->_theme = "themes/$theme_name";
171 if ($theme_name != 'default')
172 $this->_default_theme = new Theme;
175 function file ($file) {
176 return $this->_path . "$this->_theme/$file";
179 function _findFile ($file, $missing_okay = false) {
180 if (file_exists($this->_path . "$this->_theme/$file"))
181 return "$this->_theme/$file";
183 // FIXME: this is a short-term hack. Delete this after all files
184 // get moved into themes/...
185 if (file_exists($this->_path . $file))
189 if (isset($this->_default_theme)) {
190 return $this->_default_theme->_findFile($file, $missing_okay);
192 else if (!$missing_okay) {
193 trigger_error("$file: not found", E_USER_NOTICE);
198 function _findData ($file, $missing_okay = false) {
199 $path = $this->_findFile($file, $missing_okay);
203 if (defined('DATA_PATH'))
204 return DATA_PATH . "/$path";
208 ////////////////////////////////////////////////////////////////
210 // Date and Time formatting
212 ////////////////////////////////////////////////////////////////
214 // Note: Windows' implemetation of strftime does not include certain
215 // format specifiers, such as %e (for date without leading zeros). In
217 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp
218 // As a result, we have to use %d, and strip out leading zeros ourselves.
220 var $_dateFormat = "%B %d, %Y";
221 var $_timeFormat = "%I:%M %p";
223 var $_showModTime = true;
226 * Set format string used for dates.
228 * @param $fs string Format string for dates.
230 * @param $show_mod_time bool If true (default) then times
231 * are included in the messages generated by getLastModifiedMessage(),
232 * otherwise, only the date of last modification will be shown.
234 function setDateFormat ($fs, $show_mod_time = true) {
235 $this->_dateFormat = $fs;
236 $this->_showModTime = $show_mod_time;
240 * Set format string used for times.
242 * @param $fs string Format string for times.
244 function setTimeFormat ($fs) {
245 $this->_timeFormat = $fs;
251 * Any time zone offset specified in the users preferences is
252 * taken into account by this method.
254 * @param $time_t integer Unix-style time.
256 * @return string The date.
258 function formatDate ($time_t) {
261 $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
262 // strip leading zeros from date elements (ie space followed by zero)
263 return preg_replace('/ 0/', ' ',
264 strftime($this->_dateFormat, $offset_time));
270 * Any time zone offset specified in the users preferences is
271 * taken into account by this method.
273 * @param $time_t integer Unix-style time.
275 * @return string The time.
277 function formatTime ($time_t) {
278 //FIXME: make 24-hour mode configurable?
280 $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
281 return preg_replace('/^0/', ' ',
282 strtolower(strftime($this->_timeFormat, $offset_time)));
286 * Format a date and time.
288 * Any time zone offset specified in the users preferences is
289 * taken into account by this method.
291 * @param $time_t integer Unix-style time.
293 * @return string The date and time.
295 function formatDateTime ($time_t) {
296 return $this->formatDate($time_t) . ' ' . $this->formatTime($time_t);
300 * Format a (possibly relative) date.
302 * If enabled in the users preferences, this method might
303 * return a relative day (e.g. 'Today', 'Yesterday').
305 * Any time zone offset specified in the users preferences is
306 * taken into account by this method.
308 * @param $time_t integer Unix-style time.
310 * @return string The day.
312 function getDay ($time_t) {
315 if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($time_t))) {
316 return ucfirst($date);
318 return $this->formatDate($time_t);
322 * Format the "last modified" message for a page revision.
324 * @param $revision object A WikiDB_PageRevision object.
326 * @param $show_version bool Should the page version number
327 * be included in the message. (If this argument is omitted,
328 * then the version number will be shown only iff the revision
329 * is not the current one.
331 * @return string The "last modified" message.
333 function getLastModifiedMessage ($revision, $show_version = 'auto') {
336 $mtime = $revision->get('mtime');
338 if ($show_version == 'auto')
339 $show_version = !$revision->isCurrent();
341 if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($mtime))) {
342 if ($this->_showModTime)
343 $date = sprintf(_("%s at %s"),
344 $date, $this->formatTime($mtime));
347 return fmt("Version %s, saved %s.", $revision->getVersion(), $date);
349 return fmt("Last edited %s.", $date);
352 if ($this->_showModTime)
353 $date = $this->formatDateTime($mtime);
355 $date = $this->formatDate($mtime);
358 return fmt("Version %s, saved on %s.", $revision->getVersion(), $date);
360 return fmt("Last edited on %s.", $date);
363 function _relativeDay ($time_t) {
365 $offset = 3600 * $request->getPref('timeOffset');
367 $now = time() + $offset;
368 $today = localtime($now, true);
369 $time = localtime($time_t + $offset, true);
371 if ($time['tm_yday'] == $today['tm_yday'] && $time['tm_year'] == $today['tm_year'])
374 // Note that due to daylight savings chages (and leap seconds), $now minus
375 // 24 hours is not guaranteed to be yesterday.
376 $yesterday = localtime($now - (12 + $today['tm_hour']) * 3600, true);
377 if ($time['tm_yday'] == $yesterday['tm_yday'] && $time['tm_year'] == $yesterday['tm_year'])
378 return _("yesterday");
383 ////////////////////////////////////////////////////////////////
385 // Hooks for other formatting
387 ////////////////////////////////////////////////////////////////
389 //FIXME: PHP 4.1 Warnings
390 //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
391 //use the call_user_func variety with the array(&$obj, "method") syntax instead
393 function getFormatter ($type, $format) {
394 $method = strtolower("get${type}Formatter");
395 if (method_exists($this, $method))
396 return $this->{$method}($format);
400 ////////////////////////////////////////////////////////////////
404 ////////////////////////////////////////////////////////////////
406 var $_autosplitWikiWords = false;
408 function setAutosplitWikiWords($autosplit=false) {
409 $this->_autosplitWikiWords = $autosplit ? true : false;
412 function maybeSplitWikiWord ($wikiword) {
413 if ($this->_autosplitWikiWords)
414 return split_pagename($wikiword);
419 function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
420 if ($version !== false)
421 $url = WikiURL($wikiword, array('version' => $version));
423 $url = WikiURL($wikiword);
425 // Extra steps for dumping page to an html file.
426 if ($this->HTML_DUMP_SUFFIX) {
427 // urlencode for pagenames with accented letters
428 $url = rawurlencode($url);
429 $url = preg_replace('/^\./', '%2e', $url);
430 $url .= $this->HTML_DUMP_SUFFIX;
432 $link = HTML::a(array('href' => $url));
434 if (!empty($linktext)) {
435 $link->pushContent($linktext);
436 $link->setAttr('class', 'named-wiki');
437 $link->setAttr('title', $this->maybeSplitWikiWord($wikiword));
440 $link->pushContent($this->maybeSplitWikiWord($wikiword));
441 $link->setAttr('class', 'wiki');
446 function linkUnknownWikiWord($wikiword, $linktext = '') {
447 $url = WikiURL($wikiword, array('action' => 'edit'));
448 //$link = HTML::span(HTML::a(array('href' => $url), '?'));
449 $button = $this->makeButton('?', $url);
450 $button->addTooltip(sprintf(_("Edit: %s"), $wikiword));
451 $link = HTML::span($button);
454 if (!empty($linktext)) {
455 $link->pushContent(HTML::u($linktext));
456 $link->setAttr('class', 'named-wikiunknown');
459 $link->pushContent(HTML::u($this->maybeSplitWikiWord($wikiword)));
460 $link->setAttr('class', 'wikiunknown');
466 ////////////////////////////////////////////////////////////////
470 ////////////////////////////////////////////////////////////////
474 * (To disable an image, alias the image to <code>false</code>.
476 function addImageAlias ($alias, $image_name) {
477 $this->_imageAliases[$alias] = $image_name;
480 function getImageURL ($image) {
481 $aliases = &$this->_imageAliases;
483 if (isset($aliases[$image])) {
484 $image = $aliases[$image];
489 // If not extension, default to .png.
490 if (!preg_match('/\.\w+$/', $image))
493 // FIXME: this should probably be made to fall back
494 // automatically to .gif, .jpg.
495 // Also try .gif before .png if browser doesn't like png.
497 $path = $this->_findData("images/$image", 'missing okay');
498 if (!$path) // search explicit images/ or button/ links also
499 return $this->_findData("$image", 'missing okay');
504 function setLinkIcon($proto, $image = false) {
508 $this->_linkIcons[$proto] = $image;
511 function getLinkIconURL ($proto) {
512 $icons = &$this->_linkIcons;
513 if (!empty($icons[$proto]))
514 return $this->getImageURL($icons[$proto]);
515 elseif (!empty($icons['*']))
516 return $this->getImageURL($icons['*']);
520 function addButtonAlias ($text, $alias = false) {
521 $aliases = &$this->_buttonAliases;
524 $aliases = array_merge($aliases, $text);
525 elseif ($alias === false)
526 unset($aliases[$text]);
528 $aliases[$text] = $alias;
531 function getButtonURL ($text) {
532 $aliases = &$this->_buttonAliases;
533 if (isset($aliases[$text]))
534 $text = $aliases[$text];
536 $qtext = urlencode($text);
537 $url = $this->_findButton("$qtext.png");
538 if ($url && strstr($url, '%')) {
539 $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
544 function _findButton ($button_file) {
545 if (!isset($this->_button_path))
546 $this->_button_path = $this->_getButtonPath();
548 foreach ($this->_button_path as $dir) {
549 $path = "$this->_theme/$dir/$button_file";
550 if (file_exists($this->_path . $path))
551 return defined('DATA_PATH') ? DATA_PATH . "/$path" : $path;
556 function _getButtonPath () {
557 $button_dir = $this->file("buttons");
558 if (!file_exists($button_dir) || !is_dir($button_dir))
561 $path = array('buttons');
563 $dir = dir($button_dir);
564 while (($subdir = $dir->read()) !== false) {
565 if ($subdir[0] == '.')
567 if (is_dir("$button_dir/$subdir"))
568 $path[] = "buttons/$subdir";
575 ////////////////////////////////////////////////////////////////
579 ////////////////////////////////////////////////////////////////
581 function makeButton ($text, $url, $class = false) {
582 // FIXME: don't always try for image button?
584 // Special case: URLs like 'submit:preview' generate form
585 // submission buttons.
586 if (preg_match('/^submit:(.*)$/', $url, $m))
587 return $this->makeSubmitButton($text, $m[1], $class);
589 $imgurl = $this->getButtonURL($text);
591 return new ImageButton($text, $url, $class, $imgurl);
593 return new Button($text, $url, $class);
596 function makeSubmitButton ($text, $name, $class = false) {
597 $imgurl = $this->getButtonURL($text);
600 return new SubmitImageButton($text, $name, $class, $imgurl);
602 return new SubmitButton($text, $name, $class);
606 * Make button to perform action.
608 * This constructs a button which performs an action on the
609 * currently selected version of the current page.
610 * (Or anotherpage or version, if you want...)
612 * @param $action string The action to perform (e.g. 'edit', 'lock').
613 * This can also be the name of an "action page" like 'LikePages'.
614 * Alternatively you can give a hash of query args to be applied
617 * @param $label string Textual label for the button. If left empty,
618 * a suitable name will be guessed.
620 * @param $page_or_rev mixed The page to link to. This can be
621 * given as a string (the page name), a WikiDB_Page object, or as
622 * WikiDB_PageRevision object. If given as a WikiDB_PageRevision
623 * object, the button will link to a specific version of the
624 * designated page, otherwise the button links to the most recent
625 * version of the page.
627 * @return object A Button object.
629 function makeActionButton ($action, $label = false, $page_or_rev = false) {
630 extract($this->_get_name_and_rev($page_or_rev));
632 if (is_array($action)) {
634 $action = isset($attr['action']) ? $attr['action'] : 'browse';
637 $attr['action'] = $action;
639 $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
641 $label = $this->_labelForAction($action);
644 $attr['version'] = $version;
646 if ($action == 'browse')
647 unset($attr['action']);
649 return $this->makeButton($label, WikiURL($pagename, $attr), $class);
653 * Make a "button" which links to a wiki-page.
655 * These are really just regular WikiLinks, possibly
656 * disguised (e.g. behind an image button) by the theme.
658 * This method should probably only be used for links
659 * which appear in page navigation bars, or similar places.
661 * Use linkExistingWikiWord, or LinkWikiWord for normal links.
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.
670 * @return object A Button object.
672 function makeLinkButton ($page_or_rev) {
673 extract($this->_get_name_and_rev($page_or_rev));
675 $args = $version ? array('version' => $version) : false;
677 return $this->makeButton($pagename, WikiURL($pagename, $args), 'wiki');
680 function _get_name_and_rev ($page_or_rev) {
683 if (empty($page_or_rev)) {
685 $pagename = $request->getArg("pagename");
686 $version = $request->getArg("version");
688 elseif (is_object($page_or_rev)) {
689 if (isa($page_or_rev, 'WikiDB_PageRevision')) {
691 $page = $rev->getPage();
692 $version = $rev->getVersion();
695 $page = $page_or_rev;
697 $pagename = $page->getName();
700 $pagename = (string) $page_or_rev;
702 return compact('pagename', 'version');
705 function _labelForAction ($action) {
707 case 'edit': return _("Edit");
708 case 'diff': return _("Diff");
709 case 'logout': return _("Sign Out");
710 case 'login': return _("Sign In");
711 case 'lock': return _("Lock Page");
712 case 'unlock': return _("Unlock Page");
713 case 'remove': return _("Remove Page");
715 // I don't think the rest of these actually get used.
717 // 'upload' 'dumpserial' 'loadfile' 'zip'
719 return ucfirst($action);
723 //----------------------------------------------------------------
724 var $_buttonSeparator = ' | ';
726 function setButtonSeparator($separator) {
727 $this->_buttonSeparator = $separator;
730 function getButtonSeparator() {
731 return $this->_buttonSeparator;
735 ////////////////////////////////////////////////////////////////
739 ////////////////////////////////////////////////////////////////
741 function _CSSlink($title, $css_file, $media, $is_alt = false) {
742 $link = HTML::link(array('rel' => $is_alt ? 'alternate stylesheet' : 'stylesheet',
744 'type' => 'text/css',
745 'charset' => CHARSET,
746 'href' => $this->_findData($css_file)));
748 $link->setAttr('media', $media);
752 function setDefaultCSS ($title, $css_file, $media = false) {
753 if (isset($this->_defaultCSS)) {
754 $oldtitle = $this->_defaultCSS->_attr['title'];
755 $error = sprintf("'%s' -> '%s'", $oldtitle, $title);
756 trigger_error(sprintf(_("Redefinition of %s: %s"), "'default CSS'", $error),
759 if (isset($this->_alternateCSS))
760 unset($this->_alternateCSS[$title]);
761 $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
764 function addAlternateCSS ($title, $css_file, $media = false) {
765 $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
769 * @return string HTML for CSS.
772 $css = HTML($this->_defaultCSS);
773 if (!empty($this->_alternateCSS))
774 $css->pushContent($this->_alternateCSS);
778 function findTemplate ($name) {
779 return $this->_path . $this->_findFile("templates/$name.tmpl");
785 * A class representing a clickable "button".
787 * In it's simplest (default) form, a "button" is just a link associated
788 * with some sort of wiki-action.
790 class Button extends HtmlElement {
793 * @param $text string The text for the button.
794 * @param $url string The url (href) for the button.
795 * @param $class string The CSS class for the button.
797 function Button ($text, $url, $class = false) {
798 $this->HtmlElement('a', array('href' => $url));
800 $this->setAttr('class', $class);
801 $this->pushContent($text);
808 * A clickable image button.
810 class ImageButton extends Button {
813 * @param $text string The text for the button.
814 * @param $url string The url (href) for the button.
815 * @param $class string The CSS class for the button.
816 * @param $img_url string URL for button's image.
817 * @param $img_attr array Additional attributes for the <img> tag.
819 function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
820 $this->HtmlElement('a', array('href' => $url));
822 $this->setAttr('class', $class);
824 if (!is_array($img_attr))
826 $img_attr['src'] = $img_url;
827 $img_attr['alt'] = $text;
828 $img_attr['class'] = 'wiki-button';
829 $img_attr['border'] = 0;
830 $this->pushContent(HTML::img($img_attr));
835 * A class representing a form <samp>submit</samp> button.
837 class SubmitButton extends HtmlElement {
840 * @param $text string The text for the button.
841 * @param $name string The name of the form field.
842 * @param $class string The CSS class for the button.
844 function SubmitButton ($text, $name = false, $class = false) {
845 $this->HtmlElement('input', array('type' => 'submit',
848 $this->setAttr('name', $name);
850 $this->setAttr('class', $class);
857 * A class representing an image form <samp>submit</samp> button.
859 class SubmitImageButton extends SubmitButton {
862 * @param $text string The text for the button.
863 * @param $name string The name of the form field.
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 <img> tag.
868 function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
869 $this->HtmlElement('input', array('type' => 'image',
874 $this->setAttr('name', $name);
876 $this->setAttr('class', $class);
882 // (c-file-style: "gnu")
887 // c-hanging-comment-ender-p: nil
888 // indent-tabs-mode: nil