1 <?php rcs_id('$Id: Theme.php,v 1.44 2002-02-26 11:08:41 lakka 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;
55 if ($type == 'auto') {
57 $current = $page->getCurrentRevision();
58 $exists = ! $current->hasDefaultContents();
62 $dbi = $request->getDbh();
63 $exists = $dbi->isWikiPage($pagename);
66 elseif ($type == 'unknown') {
75 return $Theme->linkExistingWikiWord($pagename, $label, $version);
77 return $Theme->linkUnknownWikiWord($pagename, $label);
85 * This is a convenience function.
87 * @param $action string
89 * <dt>[action]</dt><dd>Perform action (e.g. 'edit') on the selected page.</dd>
90 * <dt>[ActionPage]</dt><dd>Run the actionpage (e.g. 'BackLinks') on the selected page.</dd>
91 * <dt>'submit:'[name]</dt><dd>Make a form submission button with the given name.
92 * ([name] can be blank for a nameless submit button.)</dd>
93 * <dt>a hash</dt><dd>Query args for the action. E.g.<pre>
94 * array('action' => 'diff', 'previous' => 'author')
98 * @param $label string
99 * A label for the button. If ommited, a suitable default (based on the valued of $action)
102 * @param $page_or_rev mixed
103 * Which page (& version) to perform the action on.
105 * <dt>A string</dt><dd>The pagename.</dd>
106 * <dt>A WikiDB_Page object</dt><dd>The page.</dd>
107 * <dt>A WikiDB_PageRevision object</dt><dd>A specific version of the page.</dd>
109 * ($Page_or_rev is ignored for submit buttons.)
111 function Button ($action, $label = false, $page_or_rev = false) {
114 if (!is_array($action) && preg_match('/submit:(.*)/A', $action, $m))
115 return $Theme->makeSubmitButton($label, $m[1], $class = $page_or_rev);
117 return $Theme->makeActionButton($action, $label, $page_or_rev);
124 function Theme ($theme_name = 'default') {
125 $this->_name = $theme_name;
126 $themes_dir = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/themes" : "themes";
128 $this->_path = defined('PHPWIKI_DIR') ? PHPWIKI_DIR . "/" : "";
129 $this->_theme = "themes/$theme_name";
131 if ($theme_name != 'default')
132 $this->_default_theme = new Theme;
135 function file ($file) {
136 return $this->_path . "$this->_theme/$file";
139 function _findFile ($file, $missing_okay = false) {
140 if (file_exists($this->_path . "$this->_theme/$file"))
141 return "$this->_theme/$file";
143 // FIXME: this is a short-term hack. Delete this after all files
144 // get moved into themes/...
145 if (file_exists($this->_path . $file))
149 if (isset($this->_default_theme)) {
150 return $this->_default_theme->_findFile($file, $missing_okay);
152 else if (!$missing_okay) {
153 trigger_error("$file: not found", E_USER_NOTICE);
158 function _findData ($file, $missing_okay = false) {
159 $path = $this->_findFile($file, $missing_okay);
163 if (defined('DATA_PATH'))
164 return DATA_PATH . "/$path";
168 ////////////////////////////////////////////////////////////////
170 // Date and Time formatting
172 ////////////////////////////////////////////////////////////////
174 // Note: Windows' implemetation of strftime does not include certain
175 // format specifiers, such as %e (for date without leading zeros). In
177 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp
178 // As a result, we have to use %d, and strip out leading zeros ourselves.
180 var $_dateFormat = "%B %d, %Y";
181 var $_timeFormat = "%I:%M %p";
183 var $_showModTime = true;
186 * Set format string used for dates.
188 * @param $fs string Format string for dates.
190 * @param $show_mod_time bool If true (default) then times
191 * are included in the messages generated by getLastModifiedMessage(),
192 * otherwise, only the date of last modification will be shown.
194 function setDateFormat ($fs, $show_mod_time = true) {
195 $this->_dateFormat = $fs;
196 $this->_showModTime = $show_mod_time;
200 * Set format string used for times.
202 * @param $fs string Format string for times.
204 function setTimeFormat ($fs) {
205 $this->_timeFormat = $fs;
211 * Any time zone offset specified in the users preferences is
212 * taken into account by this method.
214 * @param $time_t integer Unix-style time.
216 * @return string The date.
218 function formatDate ($time_t) {
221 $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
222 // strip leading zeros from date elements (ie space followed by zero)
223 return preg_replace('/ 0/', ' ',
224 strftime($this->_dateFormat, $offset_time));
230 * Any time zone offset specified in the users preferences is
231 * taken into account by this method.
233 * @param $time_t integer Unix-style time.
235 * @return string The time.
237 function formatTime ($time_t) {
238 //FIXME: make 24-hour mode configurable?
240 $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
241 return preg_replace('/^0/', ' ',
242 strtolower(strftime($this->_timeFormat, $offset_time)));
246 * Format a date and time.
248 * Any time zone offset specified in the users preferences is
249 * taken into account by this method.
251 * @param $time_t integer Unix-style time.
253 * @return string The date and time.
255 function formatDateTime ($time_t) {
256 return $this->formatDate($time_t) . ' ' . $this->formatTime($time_t);
260 * Format a (possibly relative) date.
262 * If enabled in the users preferences, this method might
263 * return a relative day (e.g. 'Today', 'Yesterday').
265 * Any time zone offset specified in the users preferences is
266 * taken into account by this method.
268 * @param $time_t integer Unix-style time.
270 * @return string The day.
272 function getDay ($time_t) {
275 if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($time_t))) {
276 return ucfirst($date);
278 return $this->formatDate($time_t);
282 * Format the "last modified" message for a page revision.
284 * @param $revision object A WikiDB_PageRevision object.
286 * @param $show_version bool Should the page version number
287 * be included in the message. (If this argument is omitted,
288 * then the version number will be shown only iff the revision
289 * is not the current one.
291 * @return string The "last modified" message.
293 function getLastModifiedMessage ($revision, $show_version = 'auto') {
296 $mtime = $revision->get('mtime');
298 if ($show_version == 'auto')
299 $show_version = !$revision->isCurrent();
301 if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($mtime))) {
302 if ($this->_showModTime)
303 $date = sprintf(_("%s at %s"),
304 $date, $this->formatTime($mtime));
307 return fmt("Version %s, saved %s.", $revision->getVersion(), $date);
309 return fmt("Last edited %s.", $date);
312 if ($this->_showModTime)
313 $date = $this->formatDateTime($mtime);
315 $date = $this->formatDate($mtime);
318 return fmt("Version %s, saved on %s.", $revision->getVersion(), $date);
320 return fmt("Last edited on %s.", $date);
323 function _relativeDay ($time_t) {
325 $offset = 3600 * $request->getPref('timeOffset');
327 $now = time() + $offset;
328 $today = localtime($now, true);
329 $time = localtime($time_t + $offset, true);
331 if ($time['tm_yday'] == $today['tm_yday'] && $time['tm_year'] == $today['tm_year'])
334 // Note that due to daylight savings chages (and leap seconds), $now minus
335 // 24 hours is not guaranteed to be yesterday.
336 $yesterday = localtime($now - (12 + $today['tm_hour']) * 3600, true);
337 if ($time['tm_yday'] == $yesterday['tm_yday'] && $time['tm_year'] == $yesterday['tm_year'])
338 return _("yesterday");
343 ////////////////////////////////////////////////////////////////
345 // Hooks for other formatting
347 ////////////////////////////////////////////////////////////////
349 //FIXME: PHP 4.1 Warnings
350 //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
351 //use the call_user_func variety with the array(&$obj, "method") syntax instead
353 function getFormatter ($type, $format) {
354 $method = strtolower("get${type}Formatter");
355 if (method_exists($this, $method))
356 return $this->{$method}($format);
360 ////////////////////////////////////////////////////////////////
364 ////////////////////////////////////////////////////////////////
366 var $_autosplitWikiWords = false;
368 function setAutosplitWikiWords($autosplit=false) {
369 $this->_autosplitWikiWords = $autosplit ? true : false;
372 function maybeSplitWikiWord ($wikiword) {
373 if ($this->_autosplitWikiWords)
374 return split_pagename($wikiword);
379 function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
380 if ($version !== false)
381 $url = WikiURL($wikiword, array('version' => $version));
383 $url = WikiURL($wikiword);
385 $link = HTML::a(array('href' => $url));
387 if (!empty($linktext)) {
388 $link->pushContent($linktext);
389 $link->setAttr('class', 'named-wiki');
390 $link->setAttr('title', $this->maybeSplitWikiWord($wikiword));
393 $link->pushContent($this->maybeSplitWikiWord($wikiword));
394 $link->setAttr('class', 'wiki');
399 function linkUnknownWikiWord($wikiword, $linktext = '') {
400 $url = WikiURL($wikiword, array('action' => 'edit'));
401 //$link = HTML::span(HTML::a(array('href' => $url), '?'));
402 $button = $this->makeButton('?', $url);
403 $button->addTooltip(sprintf(_("Edit: %s"), $wikiword));
404 $link = HTML::span($button);
407 if (!empty($linktext)) {
408 $link->pushContent(HTML::u($linktext));
409 $link->setAttr('class', 'named-wikiunknown');
412 $link->pushContent(HTML::u($this->maybeSplitWikiWord($wikiword)));
413 $link->setAttr('class', 'wikiunknown');
419 ////////////////////////////////////////////////////////////////
423 ////////////////////////////////////////////////////////////////
427 * (To disable an image, alias the image to <code>false</code>.
429 function addImageAlias ($alias, $image_name) {
430 $this->_imageAliases[$alias] = $image_name;
433 function getImageURL ($image) {
434 $aliases = &$this->_imageAliases;
436 if (isset($aliases[$image])) {
437 $image = $aliases[$image];
442 // If not extension, default to .png.
443 if (!preg_match('/\.\w+$/', $image))
446 // FIXME: this should probably be made to fall back
447 // automatically to .gif, .jpg.
448 // Also try .gif before .png if browser doesn't like png.
450 return $this->_findData("images/$image", 'missing okay');
453 function setLinkIcon($proto, $image = false) {
457 $this->_linkIcons[$proto] = $image;
460 function getLinkIconURL ($proto) {
461 $icons = &$this->_linkIcons;
462 if (!empty($icons[$proto]))
463 return $this->getImageURL($icons[$proto]);
464 elseif (!empty($icons['*']))
465 return $this->getImageURL($icons['*']);
469 function addButtonAlias ($text, $alias = false) {
470 $aliases = &$this->_buttonAliases;
473 $aliases = array_merge($aliases, $text);
474 elseif ($alias === false)
475 unset($aliases[$text]);
477 $aliases[$text] = $alias;
480 function getButtonURL ($text) {
481 $aliases = &$this->_buttonAliases;
482 if (isset($aliases[$text]))
483 $text = $aliases[$text];
485 $qtext = urlencode($text);
486 $url = $this->_findButton("$qtext.png");
487 if ($url && strstr($url, '%')) {
488 $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
493 function _findButton ($button_file) {
494 if (!isset($this->_button_path))
495 $this->_button_path = $this->_getButtonPath();
497 foreach ($this->_button_path as $dir) {
498 $path = "$this->_theme/$dir/$button_file";
499 if (file_exists($this->_path . $path))
500 return defined('DATA_PATH') ? DATA_PATH . "/$path" : $path;
505 function _getButtonPath () {
506 $button_dir = $this->file("buttons");
507 if (!file_exists($button_dir) || !is_dir($button_dir))
510 $path = array('buttons');
512 $dir = dir($button_dir);
513 while (($subdir = $dir->read()) !== false) {
514 if ($subdir[0] == '.')
516 if (is_dir("$button_dir/$subdir"))
517 $path[] = "buttons/$subdir";
524 ////////////////////////////////////////////////////////////////
528 ////////////////////////////////////////////////////////////////
530 function makeButton ($text, $url, $class = false) {
531 // FIXME: don't always try for image button?
533 // Special case: URLs like 'submit:preview' generate form
534 // submission buttons.
535 if (preg_match('/^submit:(.*)$/', $url, $m))
536 return $this->makeSubmitButton($text, $m[1], $class);
538 $imgurl = $this->getButtonURL($text);
540 return new ImageButton($text, $url, $class, $imgurl);
542 return new Button($text, $url, $class);
545 function makeSubmitButton ($text, $name, $class = false) {
546 $imgurl = $this->getButtonURL($text);
549 return new SubmitImageButton($text, $name, $class, $imgurl);
551 return new SubmitButton($text, $name, $class);
555 * Make button to perform action.
557 * This constructs a button which performs an action on the
558 * currently selected version of the current page.
559 * (Or anotherpage or version, if you want...)
561 * @param $action string The action to perform (e.g. 'edit', 'lock').
562 * This can also be the name of an "action page" like 'LikePages'.
563 * Alternatively you can give a hash of query args to be applied
566 * @param $label string Textual label for the button. If left empty,
567 * a suitable name will be guessed.
569 * @param $page_or_rev mixed The page to link to. This can be
570 * given as a string (the page name), a WikiDB_Page object, or as
571 * WikiDB_PageRevision object. If given as a WikiDB_PageRevision
572 * object, the button will link to a specific version of the
573 * designated page, otherwise the button links to the most recent
574 * version of the page.
576 * @return object A Button object.
578 function makeActionButton ($action, $label = false, $page_or_rev = false) {
579 extract($this->_get_name_and_rev($page_or_rev));
581 if (is_array($action)) {
583 $action = isset($attr['action']) ? $attr['action'] : 'browse';
586 $attr['action'] = $action;
588 $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
590 $label = $this->_labelForAction($action);
593 $attr['version'] = $version;
595 if ($action == 'browse')
596 unset($attr['action']);
598 return $this->makeButton($label, WikiURL($pagename, $attr), $class);
602 * Make a "button" which links to a wiki-page.
604 * These are really just regular WikiLinks, possibly
605 * disguised (e.g. behind an image button) by the theme.
607 * This method should probably only be used for links
608 * which appear in page navigation bars, or similar places.
610 * Use linkExistingWikiWord, or LinkWikiWord for normal links.
612 * @param $page_or_rev mixed The page to link to. This can be
613 * given as a string (the page name), a WikiDB_Page object, or as
614 * WikiDB_PageRevision object. If given as a WikiDB_PageRevision
615 * object, the button will link to a specific version of the
616 * designated page, otherwise the button links to the most recent
617 * version of the page.
619 * @return object A Button object.
621 function makeLinkButton ($page_or_rev) {
622 extract($this->_get_name_and_rev($page_or_rev));
624 $args = $version ? array('version' => $version) : false;
626 return $this->makeButton($pagename, WikiURL($pagename, $args), 'wiki');
629 function _get_name_and_rev ($page_or_rev) {
632 if (empty($page_or_rev)) {
634 $pagename = $request->getArg("pagename");
635 $version = $request->getArg("version");
637 elseif (is_object($page_or_rev)) {
638 if (isa($page_or_rev, 'WikiDB_PageRevision')) {
640 $page = $rev->getPage();
641 $version = $rev->getVersion();
644 $page = $page_or_rev;
646 $pagename = $page->getName();
649 $pagename = (string) $page_or_rev;
651 return compact('pagename', 'version');
654 function _labelForAction ($action) {
656 case 'edit': return _("Edit");
657 case 'diff': return _("Diff");
658 case 'logout': return _("Sign Out");
659 case 'login': return _("Sign In");
660 case 'lock': return _("Lock Page");
661 case 'unlock': return _("Unlock Page");
662 case 'remove': return _("Remove Page");
664 // I don't think the rest of these actually get used.
666 // 'upload' 'dumpserial' 'loadfile' 'zip'
668 return ucfirst($action);
672 //----------------------------------------------------------------
673 var $_buttonSeparator = ' | ';
675 function setButtonSeparator($separator) {
676 $this->_buttonSeparator = $separator;
679 function getButtonSeparator() {
680 return $this->_buttonSeparator;
684 ////////////////////////////////////////////////////////////////
688 ////////////////////////////////////////////////////////////////
690 function _CSSlink($title, $css_file, $media, $is_alt = false) {
691 $link = HTML::link(array('rel' => $is_alt ? 'alternate stylesheet' : 'stylesheet',
693 'type' => 'text/css',
694 'charset' => CHARSET,
695 'href' => $this->_findData($css_file)));
697 $link->setAttr('media', $media);
701 function setDefaultCSS ($title, $css_file, $media = false) {
702 if (isset($this->_defaultCSS)) {
703 $oldtitle = $this->_defaultCSS->_attr['title'];
704 $error = sprintf("'%s' -> '%s'", $oldtitle, $title);
705 trigger_error(sprintf(_("Redefinition of %s: %s"), "'default CSS'", $error),
708 if (isset($this->_alternateCSS))
709 unset($this->_alternateCSS[$title]);
710 $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
713 function addAlternateCSS ($title, $css_file, $media = false) {
714 $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
718 * @return string HTML for CSS.
721 $css = HTML($this->_defaultCSS);
722 if (!empty($this->_alternateCSS))
723 $css->pushContent($this->_alternateCSS);
727 function findTemplate ($name) {
728 return $this->_path . $this->_findFile("templates/$name.tmpl");
734 * A class representing a clickable "button".
736 * In it's simplest (default) form, a "button" is just a link associated
737 * with some sort of wiki-action.
739 class Button extends HtmlElement {
742 * @param $text string The text for the button.
743 * @param $url string The url (href) for the button.
744 * @param $class string The CSS class for the button.
746 function Button ($text, $url, $class = false) {
747 $this->HtmlElement('a', array('href' => $url));
749 $this->setAttr('class', $class);
750 $this->pushContent($text);
757 * A clickable image button.
759 class ImageButton extends Button {
762 * @param $text string The text for the button.
763 * @param $url string The url (href) for the button.
764 * @param $class string The CSS class for the button.
765 * @param $img_url string URL for button's image.
766 * @param $img_attr array Additional attributes for the <img> tag.
768 function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
769 $this->HtmlElement('a', array('href' => $url));
771 $this->setAttr('class', $class);
773 if (!is_array($img_attr))
775 $img_attr['src'] = $img_url;
776 $img_attr['alt'] = $text;
777 $img_attr['class'] = 'wiki-button';
778 $img_attr['border'] = 0;
779 $this->pushContent(HTML::img($img_attr));
784 * A class representing a form <samp>submit</samp> button.
786 class SubmitButton extends HtmlElement {
789 * @param $text string The text for the button.
790 * @param $name string The name of the form field.
791 * @param $class string The CSS class for the button.
793 function SubmitButton ($text, $name = false, $class = false) {
794 $this->HtmlElement('input', array('type' => 'submit',
797 $this->setAttr('name', $name);
799 $this->setAttr('class', $class);
806 * A class representing an image form <samp>submit</samp> button.
808 class SubmitImageButton extends SubmitButton {
811 * @param $text string The text for the button.
812 * @param $name string The name of the form field.
813 * @param $class string The CSS class for the button.
814 * @param $img_url string URL for button's image.
815 * @param $img_attr array Additional attributes for the <img> tag.
817 function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
818 $this->HtmlElement('input', array('type' => 'image',
823 $this->setAttr('name', $name);
825 $this->setAttr('class', $class);
831 // (c-file-style: "gnu")
836 // c-hanging-comment-ender-p: nil
837 // indent-tabs-mode: nil