3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004-2010 $ThePhpWikiProgrammingTeam
5 * Copyright (C) 2008-2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 require_once 'lib/Units.php';
26 class CacheableMarkup extends XmlContent
29 function __construct($content, $basepage)
31 $this->_basepage = $basepage;
33 $this->_content = array();
34 $this->_append($content);
35 if ($this->_buf != '')
36 $this->_content[] = $this->_buf;
43 // This causes a strange bug when a comment containing
44 // a single quote is entered in the Summary box:
45 // - the history is wrong (user and comment missing)
46 // - the table of contents plugin no longer works
48 if (isa($WikiTheme, 'WikiTheme_fusionforge')) {
49 return serialize($this);
52 return gzcompress(serialize($this), 9);
55 function unpack($packed)
60 // ZLIB format has a five bit checksum in it's header.
61 // Lets check for sanity.
62 if (((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0)
63 and (substr($packed, 0, 2) == "\037\213")
64 or (substr($packed, 0, 2) == "x\332")
68 $data = gzuncompress($packed);
69 return unserialize($data);
71 if (substr($packed, 0, 2) == "O:") {
72 // Looks like a serialized object
73 return unserialize($packed);
75 if (preg_match("/^\w+$/", $packed))
77 // happened with DebugBackendInfo problem also.
78 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
83 /** Get names of wikipages linked to.
85 * @return array of hashes { linkto=>pagename, relation=>pagename }
87 function getWikiPageLinks()
90 foreach ($this->_content as $item) {
91 if (!isa($item, 'Cached_DynamicContent'))
93 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
95 $links = array_merge($links, $item_links);
97 // array_unique has a bug with hashes!
98 // set_links checks for duplicates, array_merge does not
99 //return array_unique($links);
105 * This is here to support the XML-RPC listLinks() method.
108 * Returns an array of hashes.
110 function getLinkInfo()
112 foreach ($this->_content as $link) {
113 if (!isa($link, 'Cached_Link'))
115 $info = $link->getLinkInfo($this->_basepage);
116 $links[$info->href] = $info;
118 return array_values($links);
121 function _append($item)
123 if (is_array($item)) {
124 foreach ($item as $subitem)
125 $this->_append($subitem);
126 } elseif (!is_object($item)) {
127 $this->_buf .= $this->_quote((string)$item);
128 } elseif (isa($item, 'Cached_DynamicContent')) {
130 $this->_content[] = $this->_buf;
133 $this->_content[] = $item;
134 } elseif (isa($item, 'XmlElement')) {
135 if ($item->isEmpty()) {
136 $this->_buf .= $item->emptyTag();
138 $this->_buf .= $item->startTag();
139 foreach ($item->getContent() as $subitem)
140 $this->_append($subitem);
141 $this->_buf .= "</$item->_tag>";
143 if (!$this->getDescription() and $item->getTag() == 'p') {
144 // performance: when is this really needed?
145 $this->_glean_description($item->asString());
148 if (!$item->isInlineElement())
150 } elseif (isa($item, 'XmlContent')) {
151 foreach ($item->getContent() as $item)
152 $this->_append($item);
153 } elseif (method_exists($item, 'asXML')) {
154 $this->_buf .= $item->asXML();
155 } elseif (method_exists($item, 'asString')) {
156 $this->_buf .= $this->_quote($item->asString());
158 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
162 function _glean_description($text)
164 static $two_sentences;
165 if (!$two_sentences) {
166 $two_sentences = "[.?!][\")]*\s+[\"(]*[[:upper:])]"
168 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)";
171 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
172 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
176 * Guess a short description of the page.
180 * This algorithm was suggested on MeatballWiki by
181 * Alex Schroeder <kensanata@yahoo.com>.
183 * Use the first paragraph in the page which contains at least two
186 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
190 function getDescription()
192 return isset($this->_description) ? $this->_description : '';
198 $basepage = $this->_basepage;
200 foreach ($this->_content as $item) {
201 if (is_string($item)) {
203 } elseif (is_subclass_of($item, 'Cached_DynamicContent')
205 $val = $item->expand($basepage, $this);
206 $xml .= $val->asXML();
208 $xml .= $item->asXML();
216 $basepage = $this->_basepage;
217 // _content might be changed from a plugin (CreateToc)
218 for ($i = 0; $i < count($this->_content); $i++) {
219 $item = $this->_content[$i];
220 if (is_string($item)) {
222 } elseif (is_subclass_of($item, 'Cached_DynamicContent')
223 ) { // give the content the chance to know about itself or even
225 $val = $item->expand($basepage, $this);
230 trigger_error('empty item ' . print_r($item, true));
241 * The base class for all dynamic content.
243 * Dynamic content is anything that can change even when the original
244 * wiki-text from which it was parsed is unchanged.
246 class Cached_DynamicContent
249 function cache(&$cache)
254 function expand($basepage, &$obj)
256 trigger_error("Pure virtual", E_USER_ERROR);
259 function getWikiPageLinks($basepage)
265 class XmlRpc_LinkInfo
267 function XmlRpc_LinkInfo($page, $type, $href, $relation = '')
272 $this->relation = $relation;
273 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
277 class Cached_Link extends Cached_DynamicContent
280 function isInlineElement()
285 /** Get link info (for XML-RPC support)
287 * This is here to support the XML-RPC listLinks method.
288 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
290 function getLinkInfo($basepage)
292 return new XmlRpc_LinkInfo($this->_getName($basepage),
294 $this->_getURL($basepage),
295 $this->_getRelation($basepage));
298 function _getURL($basepage)
303 function __getRelation($basepage)
305 return $this->_relation;
310 * Defer interwiki inline links. img src=upload:xx.png
311 * LinkImage($url, $alt = false)
313 class Cached_InlinedImage extends Cached_DynamicContent
315 function isInlineElement()
320 function _getURL($basepage)
325 // TODO: fix interwiki inline links in case of static dumps
326 function expand($basepage, &$markup)
329 $this->_basepage = $basepage;
330 $label = isset($this->_label) ? $this->_label : false;
331 if ($WikiTheme->DUMP_MODE) {
332 // In case of static dumps we need to check if we should
333 // inline the image or not: external: keep link, internal: copy locally
334 return LinkImage($label);
336 return LinkImage($label);
341 class Cached_WikiLink extends Cached_Link
344 function __construct($page, $label = false, $anchor = false)
346 $this->_page = $page;
347 /* ":DontStoreLink" */
348 if (substr($this->_page, 0, 1) == ':') {
349 $this->_page = substr($this->_page, 1);
350 $this->_nolink = true;
353 $this->_anchor = $anchor;
354 if ($label and $label != $page)
355 $this->_label = $label;
356 $this->_basepage = false;
364 function getPagename($basepage)
366 $page = new WikiPageName($this->_page, $basepage);
367 if ($page->isValid()) return $page->name;
371 function getWikiPageLinks($basepage)
373 if ($basepage == '') return false;
374 if (isset($this->_nolink)) return false;
375 if ($link = $this->getPagename($basepage))
376 return array(array('linkto' => $link));
381 function _getName($basepage)
383 return $this->getPagename($basepage);
386 function _getURL($basepage)
388 return WikiURL($this->getPagename($basepage));
389 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
392 function expand($basepage, &$markup)
395 $this->_basepage = $basepage;
396 $label = isset($this->_label) ? $this->_label : false;
397 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
398 $page = new WikiPageName($this->_page, $basepage, $anchor);
399 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
400 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
401 return HTML($label ? $label : $page->getName());
403 if ($page->isValid()) return WikiLink($page, 'auto', $label);
404 else return HTML($label);
410 $label = isset($this->_label) ? $this->_label : false;
411 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
412 //TODO: need basepage for subpages like /Remove (within CreateTOC)
413 $page = new WikiPageName($this->_page, $this->_basepage, $anchor);
414 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
415 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
416 return $label ? $label : $page->getName();
418 $link = WikiLink($page, 'auto', $label);
419 return $link->asXML();
424 if (isset($this->_label))
425 return $this->_label;
430 class Cached_WikiLinkIfKnown extends Cached_WikiLink
432 function __construct($moniker)
434 $this->_page = $moniker;
437 function expand($basepage, &$markup)
440 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
441 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
442 return HTML($label ? $label : $page->getName());
444 return WikiLink($this->_page, 'if_known');
448 class Cached_SpellCheck extends Cached_WikiLink
450 function __construct($word, $suggs)
452 $this->_page = $word;
453 $this->suggestions = $suggs;
456 function expand($basepage, &$markup)
458 $link = HTML::a(array('class' => 'spell-wrong',
459 'title' => 'SpellCheck: ' . join(', ', $this->suggestions),
460 'name' => $this->_page),
466 class Cached_PhpwikiURL extends Cached_DynamicContent
468 function __construct($url, $label)
472 $this->_label = $label;
475 function isInlineElement()
480 function expand($basepage, &$markup)
483 $label = isset($this->_label) ? $this->_label : false;
484 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
485 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
486 return HTML($label ? $label : $page->getName());
488 return LinkPhpwikiURL($this->_url, $label, $basepage);
493 $label = isset($this->_label) ? $this->_label : false;
494 $link = LinkPhpwikiURL($this->_url, $label);
495 return $link->asXML();
500 if (isset($this->_label))
501 return $this->_label;
507 * Relations (::) are named links to pages.
508 * Attributes (:=) are named metadata per page, "named links to numbers with units".
509 * We don't want to exhaust the linktable with numbers,
510 * since this would create empty pages per each value,
511 * so we don't store the attributes as full relationlink.
512 * But we do store the attribute name as relation with an empty pagename
513 * to denote that this is an attribute,
514 * and to enable a fast listRelations mode=attributes
516 class Cached_SemanticLink extends Cached_WikiLink
519 function __construct($url, $label = false)
522 if ($label && $label != $url)
523 $this->_label = $label;
524 $this->_expandurl($this->_url);
527 function isInlineElement()
532 function getPagename($basepage)
534 if (!isset($this->_page)) return false;
535 $page = new WikiPageName($this->_page, $basepage);
536 if ($page->isValid()) return $page->name;
540 /* Add relation to the link table.
541 * attributes have the _relation, but not the _page set.
543 function getWikiPageLinks($basepage)
545 if ($basepage == '') return false;
546 if (!isset($this->_page) and isset($this->_attribute)) {
547 // An attribute: we store it in the basepage now, to fill the cache for page->save
548 // TODO: side-effect free query
549 $page = $GLOBALS['request']->getPage($basepage);
550 $page->setAttribute($this->_relation, $this->_attribute);
551 $this->_page = $basepage;
552 return array(array('linkto' => '', 'relation' => $this->_relation));
554 if ($link = $this->getPagename($basepage))
555 return array(array('linkto' => $link, 'relation' => $this->_relation));
560 function _expandurl($url)
563 if (!preg_match('/^ ([^:]+) (:[:=]) (.+) $/x', $url, $m)) {
564 return HTML::span(array('class' => 'error'), _("BAD semantic relation link"));
566 $this->_relation = urldecode($m[1]);
567 $is_attribute = ($m[2] == ':=');
569 $this->_attribute = urldecode($m[3]);
570 // since this stored in the markup cache, we are extra sensible
571 // not to store false empty stuff.
572 $units = new Units();
573 if (!DISABLE_UNITS and !$units->errcode) {
574 $this->_attribute_base = $units->Definition($this->_attribute);
575 $this->_unit = $units->baseunit($this->_attribute);
578 $this->_page = urldecode($m[3]);
583 function _expand($url, $label = false)
586 $m = $this->_expandurl($url);
588 // do not link to the attribute value, but to the attribute
589 $is_attribute = ($m[2] == ':=');
590 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
591 if (isset($this->_page) and !in_array($this->_page, $WikiTheme->VALID_LINKS))
592 return HTML($label ? $label : ($is_attribute ? $this->_relation : $this->_page));
595 $title = isset($this->_attribute_base)
596 ? sprintf(_("Attribute %s, base value: %s"), $this->_relation, $this->_attribute_base)
597 : sprintf(_("Attribute %s, value: %s"), $this->_relation, $this->_attribute);
600 HTML::a(array('href' => WikiURL($is_attribute ? $this->_relation : $this->_page),
601 'class' => "wiki " . ($is_attribute ? "attribute" : "relation"),
602 'title' => $is_attribute
604 : sprintf(_("Relation %s to page %s"), $this->_relation, $this->_page)),
607 } elseif ($is_attribute) {
610 HTML::a(array('href' => WikiURL($this->_relation),
611 'class' => "wiki attribute",
618 HTML::a(array('href' => WikiURL($this->_relation),
619 'class' => "wiki relation"),
621 HTML::span(array('class' => 'relation-symbol'), $m[2]),
622 HTML::a(array('href' => WikiURL($this->_page),
629 function expand($basepage, &$markup)
631 $label = isset($this->_label) ? $this->_label : false;
632 return $this->_expand($this->_url, $label);
637 $label = isset($this->_label) ? $this->_label : false;
638 $link = $this->_expand($this->_url, $label);
639 return $link->asXML();
644 if (isset($this->_label))
645 return $this->_label;
651 * Highlight found search engine terms
653 class Cached_SearchHighlight extends Cached_DynamicContent
655 function __construct($word, $engine)
657 $this->_word = $word;
658 $this->engine = $engine;
661 function expand($basepage, &$markup)
663 return HTML::span(array('class' => 'search-term',
664 'title' => _("Found by ") . $this->engine),
669 class Cached_ExternalLink extends Cached_Link
672 function __construct($url, $label = false)
675 if ($label && $label != $url)
676 $this->_label = $label;
684 function _getName($basepage)
686 $label = isset($this->_label) ? $this->_label : false;
687 return ($label and is_string($label)) ? $label : $this->_url;
690 function expand($basepage, &$markup)
694 $label = isset($this->_label) ? $this->_label : false;
695 $link = LinkURL($this->_url, $label);
697 if (GOOGLE_LINKS_NOFOLLOW) {
698 // Ignores nofollow when the user who saved the page was authenticated.
699 $page = $request->getPage($basepage);
700 $current = $page->getCurrentRevision(false);
701 if (!$current->get('author_id'))
702 $link->setAttr('rel', 'nofollow');
709 if (isset($this->_label) and is_string($this->_label))
710 return $this->_label;
715 class Cached_InterwikiLink extends Cached_ExternalLink
718 function __construct($link, $label = false)
720 $this->_link = $link;
722 $this->_label = $label;
725 function getPagename($basepage)
727 list ($moniker, $page) = explode(":", $this->_link, 2);
728 $page = new WikiPageName($page, $basepage);
729 if ($page->isValid()) return $page->name;
733 function getWikiPageLinks($basepage)
735 if ($basepage == '') return false;
736 /* ":DontStoreLink" */
737 if (substr($this->_link, 0, 1) == ':') return false;
738 /* store only links to valid pagenames */
739 $dbi = $GLOBALS['request']->getDbh();
740 if ($link = $this->getPagename($basepage) and $dbi->isWikiPage($link)) {
741 return array(array('linkto' => $link));
743 return false; // dont store external links
747 function _getName($basepage)
749 $label = isset($this->_label) ? $this->_label : false;
750 return ($label and is_string($label)) ? $label : $this->_link;
753 /* there may be internal interwiki links also */
756 return $this->getPagename(false) ? 'internal' : 'external';
759 function _getURL($basepage)
761 $link = $this->expand($basepage, $this);
762 return $link->getAttr('href');
765 function expand($basepage, &$markup)
768 $intermap = getInterwikiMap();
769 $label = isset($this->_label) ? $this->_label : false;
770 //FIXME: check Upload: inlined images
771 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
772 if (!in_array($this->_link, $WikiTheme->VALID_LINKS))
773 return HTML($label ? $label : $this->_link);
775 return $intermap->link($this->_link, $label);
780 if (isset($this->_label))
781 return $this->_label;
786 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
787 // Thanks to PhpWiki:DanFr for finding this bug.
788 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
789 class Cached_UserLink extends Cached_WikiLink
791 function expand($basepage, &$markup)
793 $label = isset($this->_label) ? $this->_label : false;
794 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
795 $page = new WikiPageName($this->_page, $basepage, $anchor);
796 $link = WikiLink($page, 'auto', $label);
797 // $link = HTML::a(array('href' => $PageName));
798 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
799 $link->setAttr('class', 'wikiuser');
805 * 1.3.13: Previously stored was only _pi.
806 * A fresh generated cache has now ->name and ->args also.
807 * main::isActionPage only checks the raw content.
809 class Cached_PluginInvocation extends Cached_DynamicContent
812 function __construct($pi)
815 $loader = $this->_getLoader();
816 if (is_array($plugin_cmdline = $loader->parsePI($pi)) and $plugin_cmdline[1]) {
817 $this->pi_name = $plugin_cmdline[0]; // plugin, plugin-form, plugin-list
818 $this->name = $plugin_cmdline[1]->getName();
819 $this->args = $plugin_cmdline[2];
823 function setTightness($top, $bottom)
827 function isInlineElement()
832 function expand($basepage, &$markup)
834 $loader = $this->_getLoader();
835 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
844 function getWikiPageLinks($basepage)
846 $loader = $this->_getLoader();
848 return $loader->getWikiPageLinks($this->_pi, $basepage);
851 function & _getLoader()
853 static $loader = false;
856 include_once 'lib/WikiPlugin.php';
857 $loader = new WikiPluginLoader();
867 // c-hanging-comment-ender-p: nil
868 // indent-tabs-mode: nil