1 <?php rcs_id('$Id: Theme.php,v 1.41 2002-02-14 03:39:15 carstenklapp 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 var $_dateFormat = "%B %e, %Y";
175 var $_timeFormat = "%l:%M %p";
176 var $_showModTime = true;
179 * Set format string used for dates.
181 * @param $fs string Format string for dates.
183 * @param $show_mod_time bool If true (default) then times
184 * are included in the messages generated by getLastModifiedMessage(),
185 * otherwise, only the date of last modification will be shown.
187 function setDateFormat ($fs, $show_mod_time = true) {
188 $this->_dateFormat = $fs;
189 $this->_showModTime = $show_mod_time;
193 * Set format string used for times.
195 * @param $fs string Format string for times.
197 function setTimeFormat ($fs) {
198 $this->_timeFormat = $fs;
204 * Any time zone offset specified in the users preferences is
205 * taken into account by this method.
207 * @param $time_t integer Unix-style time.
209 * @return string The date.
211 function formatDate ($time_t) {
214 $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
215 return strftime($this->_dateFormat, $offset_time);
221 * Any time zone offset specified in the users preferences is
222 * taken into account by this method.
224 * @param $time_t integer Unix-style time.
226 * @return string The time.
228 function formatTime ($time_t) {
229 //FIXME: make 24-hour mode configurable?
231 $offset_time = $time_t + 3600 * $request->getPref('timeOffset');
232 return preg_replace('/^0/', ' ',
233 strtolower(strftime($this->_timeFormat, $offset_time)));
237 * Format a date and time.
239 * Any time zone offset specified in the users preferences is
240 * taken into account by this method.
242 * @param $time_t integer Unix-style time.
244 * @return string The date and time.
246 function formatDateTime ($time_t) {
247 return $this->formatDate($time_t) . ' ' . $this->formatTime($time_t);
251 * Format a (possibly relative) date.
253 * If enabled in the users preferences, this method might
254 * return a relative day (e.g. 'Today', 'Yesterday').
256 * Any time zone offset specified in the users preferences is
257 * taken into account by this method.
259 * @param $time_t integer Unix-style time.
261 * @return string The day.
263 function getDay ($time_t) {
266 if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($time_t))) {
267 return ucfirst($date);
269 return $this->formatDate($time_t);
273 * Format the "last modified" message for a page revision.
275 * @param $revision object A WikiDB_PageRevision object.
277 * @param $show_version bool Should the page version number
278 * be included in the message. (If this argument is omitted,
279 * then the version number will be shown only iff the revision
280 * is not the current one.
282 * @return string The "last modified" message.
284 function getLastModifiedMessage ($revision, $show_version = 'auto') {
287 $mtime = $revision->get('mtime');
289 if ($show_version == 'auto')
290 $show_version = !$revision->isCurrent();
292 if ($request->getPref('relativeDates') && ($date = $this->_relativeDay($mtime))) {
293 if ($this->_showModTime)
294 $date = sprintf(_("%s at %s"),
295 $date, $this->formatTime($mtime));
298 return fmt("Version %s, saved %s.", $revision->getVersion(), $date);
300 return fmt("Last edited %s.", $date);
303 if ($this->_showModTime)
304 $date = $this->formatDateTime($mtime);
306 $date = $this->formatDate($mtime);
309 return fmt("Version %s, saved on %s.", $revision->getVersion(), $date);
311 return fmt("Last edited on %s.", $date);
314 function _relativeDay ($time_t) {
316 $offset = 3600 * $request->getPref('timeOffset');
318 $now = time() + $offset;
319 $today = localtime($now, true);
320 $time = localtime($time_t + $offset, true);
322 if ($time['tm_yday'] == $today['tm_yday'] && $time['tm_year'] == $today['tm_year'])
325 // Note that due to daylight savings chages (and leap seconds), $now minus
326 // 24 hours is not guaranteed to be yesterday.
327 $yesterday = localtime($now - (12 + $today['tm_hour']) * 3600, true);
328 if ($time['tm_yday'] == $yesterday['tm_yday'] && $time['tm_year'] == $yesterday['tm_year'])
329 return _("yesterday");
334 ////////////////////////////////////////////////////////////////
336 // Hooks for other formatting
338 ////////////////////////////////////////////////////////////////
340 //FIXME: PHP 4.1 Warnings
341 //lib/Theme.php:84: Notice[8]: The call_user_method() function is deprecated,
342 //use the call_user_func variety with the array(&$obj, "method") syntax instead
344 function getFormatter ($type, $format) {
345 $method = strtolower("get${type}Formatter");
346 if (method_exists($this, $method))
347 return $this->{$method}($format);
351 ////////////////////////////////////////////////////////////////
355 ////////////////////////////////////////////////////////////////
357 var $_autosplitWikiWords = false;
359 function setAutosplitWikiWords($autosplit=false) {
360 $this->_autosplitWikiWords = $autosplit ? true : false;
363 function maybeSplitWikiWord ($wikiword) {
364 if ($this->_autosplitWikiWords)
365 return split_pagename($wikiword);
370 function linkExistingWikiWord($wikiword, $linktext = '', $version = false) {
371 if ($version !== false)
372 $url = WikiURL($wikiword, array('version' => $version));
374 $url = WikiURL($wikiword);
376 $link = HTML::a(array('href' => $url));
378 if (!empty($linktext)) {
379 $link->pushContent($linktext);
380 $link->setAttr('class', 'named-wiki');
381 $link->setAttr('title', $this->maybeSplitWikiWord($wikiword));
384 $link->pushContent($this->maybeSplitWikiWord($wikiword));
385 $link->setAttr('class', 'wiki');
390 function linkUnknownWikiWord($wikiword, $linktext = '') {
391 $url = WikiURL($wikiword, array('action' => 'edit'));
392 //$link = HTML::span(HTML::a(array('href' => $url), '?'));
393 $button = $this->makeButton('?', $url);
394 $button->addTooltip(sprintf(_("Edit: %s"), $wikiword));
395 $link = HTML::span($button);
398 if (!empty($linktext)) {
399 $link->pushContent(HTML::u($linktext));
400 $link->setAttr('class', 'named-wikiunknown');
403 $link->pushContent(HTML::u($this->maybeSplitWikiWord($wikiword)));
404 $link->setAttr('class', 'wikiunknown');
410 ////////////////////////////////////////////////////////////////
414 ////////////////////////////////////////////////////////////////
418 * (To disable an image, alias the image to <code>false</code>.
420 function addImageAlias ($alias, $image_name) {
421 $this->_imageAliases[$alias] = $image_name;
424 function getImageURL ($image) {
425 $aliases = &$this->_imageAliases;
427 if (isset($aliases[$image])) {
428 $image = $aliases[$image];
433 // If not extension, default to .png.
434 if (!preg_match('/\.\w+$/', $image))
437 // FIXME: this should probably be made to fall back
438 // automatically to .gif, .jpg.
439 // Also try .gif before .png if browser doesn't like png.
441 return $this->_findData("images/$image", 'missing okay');
444 function setLinkIcon($proto, $image = false) {
448 $this->_linkIcons[$proto] = $image;
451 function getLinkIconURL ($proto) {
452 $icons = &$this->_linkIcons;
453 if (!empty($icons[$proto]))
454 return $this->getImageURL($icons[$proto]);
455 elseif (!empty($icons['*']))
456 return $this->getImageURL($icons['*']);
460 function addButtonAlias ($text, $alias = false) {
461 $aliases = &$this->_buttonAliases;
464 $aliases = array_merge($aliases, $text);
465 elseif ($alias === false)
466 unset($aliases[$text]);
468 $aliases[$text] = $alias;
471 function getButtonURL ($text) {
472 $aliases = &$this->_buttonAliases;
473 if (isset($aliases[$text]))
474 $text = $aliases[$text];
476 $qtext = urlencode($text);
477 $url = $this->_findButton("$qtext.png");
478 if ($url && strstr($url, '%')) {
479 $url = preg_replace('|([^/]+)$|e', 'urlencode("\\1")', $url);
484 function _findButton ($button_file) {
485 if (!isset($this->_button_path))
486 $this->_button_path = $this->_getButtonPath();
488 foreach ($this->_button_path as $dir) {
489 $path = "$this->_theme/$dir/$button_file";
490 if (file_exists($this->_path . $path))
491 return defined('DATA_PATH') ? DATA_PATH . "/$path" : $path;
496 function _getButtonPath () {
497 $button_dir = $this->file("buttons");
498 if (!file_exists($button_dir) || !is_dir($button_dir))
501 $path = array('buttons');
503 $dir = dir($button_dir);
504 while (($subdir = $dir->read()) !== false) {
505 if ($subdir[0] == '.')
507 if (is_dir("$button_dir/$subdir"))
508 $path[] = "buttons/$subdir";
515 ////////////////////////////////////////////////////////////////
519 ////////////////////////////////////////////////////////////////
521 function makeButton ($text, $url, $class = false) {
522 // FIXME: don't always try for image button?
524 // Special case: URLs like 'submit:preview' generate form
525 // submission buttons.
526 if (preg_match('/^submit:(.*)$/', $url, $m))
527 return $this->makeSubmitButton($text, $m[1], $class);
529 $imgurl = $this->getButtonURL($text);
531 return new ImageButton($text, $url, $class, $imgurl);
533 return new Button($text, $url, $class);
536 function makeSubmitButton ($text, $name, $class = false) {
537 $imgurl = $this->getButtonURL($text);
540 return new SubmitImageButton($text, $name, $class, $imgurl);
542 return new SubmitButton($text, $name, $class);
546 * Make button to perform action.
548 * This constructs a button which performs an action on the
549 * currently selected version of the current page.
550 * (Or anotherpage or version, if you want...)
552 * @param $action string The action to perform (e.g. 'edit', 'lock').
553 * This can also be the name of an "action page" like 'LikePages'.
554 * Alternatively you can give a hash of query args to be applied
557 * @param $label string Textual label for the button. If left empty,
558 * a suitable name will be guessed.
560 * @param $page_or_rev mixed The page to link to. This can be
561 * given as a string (the page name), a WikiDB_Page object, or as
562 * WikiDB_PageRevision object. If given as a WikiDB_PageRevision
563 * object, the button will link to a specific version of the
564 * designated page, otherwise the button links to the most recent
565 * version of the page.
567 * @return object A Button object.
569 function makeActionButton ($action, $label = false, $page_or_rev = false) {
570 extract($this->_get_name_and_rev($page_or_rev));
572 if (is_array($action)) {
574 $action = isset($attr['action']) ? $attr['action'] : 'browse';
577 $attr['action'] = $action;
579 $class = is_safe_action($action) ? 'wikiaction' : 'wikiadmin';
581 $label = $this->_labelForAction($action);
584 $attr['version'] = $version;
586 if ($action == 'browse')
587 unset($attr['action']);
589 return $this->makeButton($label, WikiURL($pagename, $attr), $class);
593 * Make a "button" which links to a wiki-page.
595 * These are really just regular WikiLinks, possibly
596 * disguised (e.g. behind an image button) by the theme.
598 * This method should probably only be used for links
599 * which appear in page navigation bars, or similar places.
601 * Use linkExistingWikiWord, or LinkWikiWord for normal links.
603 * @param $page_or_rev mixed The page to link to. This can be
604 * given as a string (the page name), a WikiDB_Page object, or as
605 * WikiDB_PageRevision object. If given as a WikiDB_PageRevision
606 * object, the button will link to a specific version of the
607 * designated page, otherwise the button links to the most recent
608 * version of the page.
610 * @return object A Button object.
612 function makeLinkButton ($page_or_rev) {
613 extract($this->_get_name_and_rev($page_or_rev));
615 $args = $version ? array('version' => $version) : false;
617 return $this->makeButton($pagename, WikiURL($pagename, $args), 'wiki');
620 function _get_name_and_rev ($page_or_rev) {
623 if (empty($page_or_rev)) {
625 $pagename = $request->getArg("pagename");
626 $version = $request->getArg("version");
628 elseif (is_object($page_or_rev)) {
629 if (isa($page_or_rev, 'WikiDB_PageRevision')) {
631 $page = $rev->getPage();
632 $version = $rev->getVersion();
635 $page = $page_or_rev;
637 $pagename = $page->getName();
640 $pagename = (string) $page_or_rev;
642 return compact('pagename', 'version');
645 function _labelForAction ($action) {
647 case 'edit': return _("Edit");
648 case 'diff': return _("Diff");
649 case 'logout': return _("Sign Out");
650 case 'login': return _("Sign In");
651 case 'lock': return _("Lock Page");
652 case 'unlock': return _("Unlock Page");
653 case 'remove': return _("Remove Page");
655 // I don't think the rest of these actually get used.
657 // 'upload' 'dumpserial' 'loadfile' 'zip'
659 return ucfirst($action);
663 //----------------------------------------------------------------
664 var $_buttonSeparator = ' | ';
666 function setButtonSeparator($separator) {
667 $this->_buttonSeparator = $separator;
670 function getButtonSeparator() {
671 return $this->_buttonSeparator;
675 ////////////////////////////////////////////////////////////////
679 ////////////////////////////////////////////////////////////////
681 function _CSSlink($title, $css_file, $media, $is_alt = false) {
682 $link = HTML::link(array('rel' => $is_alt ? 'alternate stylesheet' : 'stylesheet',
684 'type' => 'text/css',
685 'charset' => CHARSET,
686 'href' => $this->_findData($css_file)));
688 $link->setAttr('media', $media);
692 function setDefaultCSS ($title, $css_file, $media = false) {
693 if (isset($this->_defaultCSS)) {
694 $oldtitle = $this->_defaultCSS->_attr['title'];
695 $error = sprintf("'%s' -> '%s'", $oldtitle, $title);
696 trigger_error(sprintf(_("Redefinition of default CSS: %s"), $error),
699 if (isset($this->_alternateCSS))
700 unset($this->_alternateCSS[$title]);
701 $this->_defaultCSS = $this->_CSSlink($title, $css_file, $media);
704 function addAlternateCSS ($title, $css_file, $media = false) {
705 $this->_alternateCSS[$title] = $this->_CSSlink($title, $css_file, $media, true);
709 * @return string HTML for CSS.
712 $css = HTML($this->_defaultCSS);
713 if (!empty($this->_alternateCSS))
714 $css->pushContent($this->_alternateCSS);
718 function findTemplate ($name) {
719 return $this->_path . $this->_findFile("templates/$name.tmpl");
725 * A class representing a clickable "button".
727 * In it's simplest (default) form, a "button" is just a link associated
728 * with some sort of wiki-action.
730 class Button extends HtmlElement {
733 * @param $text string The text for the button.
734 * @param $url string The url (href) for the button.
735 * @param $class string The CSS class for the button.
737 function Button ($text, $url, $class = false) {
738 $this->HtmlElement('a', array('href' => $url));
740 $this->setAttr('class', $class);
741 $this->pushContent($text);
748 * A clickable image button.
750 class ImageButton extends Button {
753 * @param $text string The text for the button.
754 * @param $url string The url (href) for the button.
755 * @param $class string The CSS class for the button.
756 * @param $img_url string URL for button's image.
757 * @param $img_attr array Additional attributes for the <img> tag.
759 function ImageButton ($text, $url, $class, $img_url, $img_attr = false) {
760 $this->HtmlElement('a', array('href' => $url));
762 $this->setAttr('class', $class);
764 if (!is_array($img_attr))
766 $img_attr['src'] = $img_url;
767 $img_attr['alt'] = $text;
768 $img_attr['class'] = 'wiki-button';
769 $img_attr['border'] = 0;
770 $this->pushContent(HTML::img($img_attr));
775 * A class representing a form <samp>submit</samp> button.
777 class SubmitButton extends HtmlElement {
780 * @param $text string The text for the button.
781 * @param $name string The name of the form field.
782 * @param $class string The CSS class for the button.
784 function SubmitButton ($text, $name = false, $class = false) {
785 $this->HtmlElement('input', array('type' => 'submit',
788 $this->setAttr('name', $name);
790 $this->setAttr('class', $class);
797 * A class representing an image form <samp>submit</samp> button.
799 class SubmitImageButton extends SubmitButton {
802 * @param $text string The text for the button.
803 * @param $name string The name of the form field.
804 * @param $class string The CSS class for the button.
805 * @param $img_url string URL for button's image.
806 * @param $img_attr array Additional attributes for the <img> tag.
808 function SubmitImageButton ($text, $name = false, $class = false, $img_url) {
809 $this->HtmlElement('input', array('type' => 'image',
814 $this->setAttr('name', $name);
816 $this->setAttr('class', $class);
822 // (c-file-style: "gnu")
827 // c-hanging-comment-ender-p: nil
828 // indent-tabs-mode: nil