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 CacheableMarkup($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 if (function_exists('gzcompress'))
53 return gzcompress(serialize($this), 9);
54 return serialize($this);
56 // FIXME: probably should implement some sort of "compression"
57 // when no gzcompress is available.
60 function unpack($packed)
65 // ZLIB format has a five bit checksum in it's header.
66 // Lets check for sanity.
67 if (((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0)
68 and (substr($packed, 0, 2) == "\037\213")
69 or (substr($packed, 0, 2) == "x\332")
72 if (function_exists('gzuncompress')) {
74 $data = gzuncompress($packed);
75 return unserialize($data);
77 // user our php lib. TESTME
78 include_once 'ziplib.php';
79 $zip = new ZipReader($packed);
80 list(, $data, $attrib) = $zip->readFile();
81 return unserialize($data);
84 if (substr($packed, 0, 2) == "O:") {
85 // Looks like a serialized object
86 return unserialize($packed);
88 if (preg_match("/^\w+$/", $packed))
90 // happened with _BackendInfo problem also.
91 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
96 /** Get names of wikipages linked to.
98 * @return array of hashes { linkto=>pagename, relation=>pagename }
100 function getWikiPageLinks()
103 foreach ($this->_content as $item) {
104 if (!isa($item, 'Cached_DynamicContent'))
106 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
108 $links = array_merge($links, $item_links);
110 // array_unique has a bug with hashes!
111 // set_links checks for duplicates, array_merge does not
112 //return array_unique($links);
118 * This is here to support the XML-RPC listLinks() method.
121 * Returns an array of hashes.
123 function getLinkInfo()
126 foreach ($this->_content as $link) {
127 if (!isa($link, 'Cached_Link'))
129 $info = $link->getLinkInfo($this->_basepage);
130 $links[$info->href] = $info;
132 return array_values($links);
135 function _append($item)
137 if (is_array($item)) {
138 foreach ($item as $subitem)
139 $this->_append($subitem);
140 } elseif (!is_object($item)) {
141 $this->_buf .= $this->_quote((string)$item);
142 } elseif (isa($item, 'Cached_DynamicContent')) {
144 $this->_content[] = $this->_buf;
147 $this->_content[] = $item;
148 } elseif (isa($item, 'XmlElement')) {
149 if ($item->isEmpty()) {
150 $this->_buf .= $item->emptyTag();
152 $this->_buf .= $item->startTag();
153 foreach ($item->getContent() as $subitem)
154 $this->_append($subitem);
155 $this->_buf .= "</$item->_tag>";
157 if (!$this->getDescription() and $item->getTag() == 'p') {
158 // performance: when is this really needed?
159 $this->_glean_description($item->asString());
162 if (!$item->isInlineElement())
164 } elseif (isa($item, 'XmlContent')) {
165 foreach ($item->getContent() as $item)
166 $this->_append($item);
167 } elseif (method_exists($item, 'asXML')) {
168 $this->_buf .= $item->asXML();
169 } elseif (method_exists($item, 'asString')) {
170 $this->_buf .= $this->_quote($item->asString());
172 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
176 function _glean_description($text)
178 static $two_sentences;
179 if (!$two_sentences) {
180 $two_sentences = "[.?!][\")]*\s+[\"(]*[[:upper:])]"
182 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)";
185 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
186 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
190 * Guess a short description of the page.
194 * This algorithm was suggested on MeatballWiki by
195 * Alex Schroeder <kensanata@yahoo.com>.
197 * Use the first paragraph in the page which contains at least two
200 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
204 function getDescription()
206 return isset($this->_description) ? $this->_description : '';
212 $basepage = $this->_basepage;
214 foreach ($this->_content as $item) {
215 if (is_string($item)) {
217 } elseif (is_subclass_of($item, 'Cached_DynamicContent')
219 $val = $item->expand($basepage, $this);
220 $xml .= $val->asXML();
222 $xml .= $item->asXML();
230 $basepage = $this->_basepage;
231 // _content might be changed from a plugin (CreateToc)
232 for ($i = 0; $i < count($this->_content); $i++) {
233 $item = $this->_content[$i];
234 if (is_string($item)) {
236 } elseif (is_subclass_of($item, 'Cached_DynamicContent')
237 ) { // give the content the chance to know about itself or even
239 $val = $item->expand($basepage, $this);
240 if ($val) $val->printXML();
241 else trigger_error('empty item ' . print_r($item, true));
250 * The base class for all dynamic content.
252 * Dynamic content is anything that can change even when the original
253 * wiki-text from which it was parsed is unchanged.
255 class Cached_DynamicContent
258 function cache(&$cache)
263 function expand($basepage, &$obj)
265 trigger_error("Pure virtual", E_USER_ERROR);
268 function getWikiPageLinks($basepage)
274 class XmlRpc_LinkInfo
276 function XmlRpc_LinkInfo($page, $type, $href, $relation = '')
281 $this->relation = $relation;
282 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
286 class Cached_Link extends Cached_DynamicContent
289 function isInlineElement()
294 /** Get link info (for XML-RPC support)
296 * This is here to support the XML-RPC listLinks method.
297 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
299 function getLinkInfo($basepage)
301 return new XmlRpc_LinkInfo($this->_getName($basepage),
303 $this->_getURL($basepage),
304 $this->_getRelation($basepage));
307 function _getURL($basepage)
312 function __getRelation($basepage)
314 return $this->_relation;
319 * Defer interwiki inline links. img src=upload:xx.png
320 * LinkImage($url, $alt = false)
322 class Cached_InlinedImage extends Cached_DynamicContent
324 function isInlineElement()
329 function _getURL($basepage)
334 // TODO: fix interwiki inline links in case of static dumps
335 function expand($basepage, &$markup)
338 $this->_basepage = $basepage;
339 $label = isset($this->_label) ? $this->_label : false;
340 if ($WikiTheme->DUMP_MODE) {
341 // In case of static dumps we need to check if we should
342 // inline the image or not: external: keep link, internal: copy locally
343 return LinkImage($label);
345 return LinkImage($label);
350 class Cached_WikiLink extends Cached_Link
353 function Cached_WikiLink($page, $label = false, $anchor = false)
355 $this->_page = $page;
356 /* ":DontStoreLink" */
357 if (substr($this->_page, 0, 1) == ':') {
358 $this->_page = substr($this->_page, 1);
359 $this->_nolink = true;
362 $this->_anchor = $anchor;
363 if ($label and $label != $page)
364 $this->_label = $label;
365 $this->_basepage = false;
373 function getPagename($basepage)
375 $page = new WikiPageName($this->_page, $basepage);
376 if ($page->isValid()) return $page->name;
380 function getWikiPageLinks($basepage)
382 if ($basepage == '') return false;
383 if (isset($this->_nolink)) return false;
384 if ($link = $this->getPagename($basepage))
385 return array(array('linkto' => $link));
390 function _getName($basepage)
392 return $this->getPagename($basepage);
395 function _getURL($basepage)
397 return WikiURL($this->getPagename($basepage));
398 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
401 function expand($basepage, &$markup)
404 $this->_basepage = $basepage;
405 $label = isset($this->_label) ? $this->_label : false;
406 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
407 $page = new WikiPageName($this->_page, $basepage, $anchor);
408 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
409 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
410 return HTML($label ? $label : $page->getName());
412 if ($page->isValid()) return WikiLink($page, 'auto', $label);
413 else return HTML($label);
419 $label = isset($this->_label) ? $this->_label : false;
420 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
421 //TODO: need basepage for subpages like /Remove (within CreateTOC)
422 $page = new WikiPageName($this->_page, $this->_basepage, $anchor);
423 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
424 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
425 return $label ? $label : $page->getName();
427 $link = WikiLink($page, 'auto', $label);
428 return $link->asXML();
433 if (isset($this->_label))
434 return $this->_label;
439 class Cached_WikiLinkIfKnown extends Cached_WikiLink
441 function Cached_WikiLinkIfKnown($moniker)
443 $this->_page = $moniker;
446 function expand($basepage, &$markup)
449 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
450 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
451 return HTML($label ? $label : $page->getName());
453 return WikiLink($this->_page, 'if_known');
457 class Cached_SpellCheck extends Cached_WikiLink
459 function Cached_SpellCheck($word, $suggs)
461 $this->_page = $word;
462 $this->suggestions = $suggs;
465 function expand($basepage, &$markup)
467 $link = HTML::a(array('class' => 'spell-wrong',
468 'title' => 'SpellCheck: ' . join(', ', $this->suggestions),
469 'name' => $this->_page),
475 class Cached_PhpwikiURL extends Cached_DynamicContent
477 function Cached_PhpwikiURL($url, $label)
481 $this->_label = $label;
484 function isInlineElement()
489 function expand($basepage, &$markup)
492 $label = isset($this->_label) ? $this->_label : false;
493 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
494 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
495 return HTML($label ? $label : $page->getName());
497 return LinkPhpwikiURL($this->_url, $label, $basepage);
502 $label = isset($this->_label) ? $this->_label : false;
503 $link = LinkPhpwikiURL($this->_url, $label);
504 return $link->asXML();
509 if (isset($this->_label))
510 return $this->_label;
516 * Relations (::) are named links to pages.
517 * Attributes (:=) are named metadata per page, "named links to numbers with units".
518 * We don't want to exhaust the linktable with numbers,
519 * since this would create empty pages per each value,
520 * so we don't store the attributes as full relationlink.
521 * But we do store the attribute name as relation with an empty pagename
522 * to denote that this is an attribute,
523 * and to enable a fast listRelations mode=attributes
525 class Cached_SemanticLink extends Cached_WikiLink
528 function Cached_SemanticLink($url, $label = false)
531 if ($label && $label != $url)
532 $this->_label = $label;
533 $this->_expandurl($this->_url);
536 function isInlineElement()
541 function getPagename($basepage)
543 if (!isset($this->_page)) return false;
544 $page = new WikiPageName($this->_page, $basepage);
545 if ($page->isValid()) return $page->name;
549 /* Add relation to the link table.
550 * attributes have the _relation, but not the _page set.
552 function getWikiPageLinks($basepage)
554 if ($basepage == '') return false;
555 if (!isset($this->_page) and isset($this->_attribute)) {
556 // An attribute: we store it in the basepage now, to fill the cache for page->save
557 // TODO: side-effect free query
558 $page = $GLOBALS['request']->getPage($basepage);
559 $page->setAttribute($this->_relation, $this->_attribute);
560 $this->_page = $basepage;
561 return array(array('linkto' => '', 'relation' => $this->_relation));
563 if ($link = $this->getPagename($basepage))
564 return array(array('linkto' => $link, 'relation' => $this->_relation));
569 function _expandurl($url)
572 if (!preg_match('/^ ([^:]+) (:[:=]) (.+) $/x', $url, $m)) {
573 return HTML::span(array('class' => 'error'), _("BAD semantic relation link"));
575 $this->_relation = urldecode($m[1]);
576 $is_attribute = ($m[2] == ':=');
578 $this->_attribute = urldecode($m[3]);
579 // since this stored in the markup cache, we are extra sensible
580 // not to store false empty stuff.
581 $units = new Units();
582 if (!DISABLE_UNITS and !$units->errcode) {
583 $this->_attribute_base = $units->Definition($this->_attribute);
584 $this->_unit = $units->baseunit($this->_attribute);
587 $this->_page = urldecode($m[3]);
592 function _expand($url, $label = false)
595 $m = $this->_expandurl($url);
597 // do not link to the attribute value, but to the attribute
598 $is_attribute = ($m[2] == ':=');
599 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
600 if (isset($this->_page) and !in_array($this->_page, $WikiTheme->VALID_LINKS))
601 return HTML($label ? $label : ($is_attribute ? $this->_relation : $this->_page));
604 $title = isset($this->_attribute_base)
605 ? sprintf(_("Attribute %s, base value: %s"), $this->_relation, $this->_attribute_base)
606 : sprintf(_("Attribute %s, value: %s"), $this->_relation, $this->_attribute);
609 HTML::a(array('href' => WikiURL($is_attribute ? $this->_relation : $this->_page),
610 'class' => "wiki " . ($is_attribute ? "attribute" : "relation"),
611 'title' => $is_attribute
613 : sprintf(_("Relation %s to page %s"), $this->_relation, $this->_page)),
616 } elseif ($is_attribute) {
619 HTML::a(array('href' => WikiURL($this->_relation),
620 'class' => "wiki attribute",
627 HTML::a(array('href' => WikiURL($this->_relation),
628 'class' => "wiki relation"),
630 HTML::span(array('class' => 'relation-symbol'), $m[2]),
631 HTML::a(array('href' => WikiURL($this->_page),
638 function expand($basepage, &$markup)
640 $label = isset($this->_label) ? $this->_label : false;
641 return $this->_expand($this->_url, $label);
646 $label = isset($this->_label) ? $this->_label : false;
647 $link = $this->_expand($this->_url, $label);
648 return $link->asXML();
653 if (isset($this->_label))
654 return $this->_label;
660 * Highlight found search engine terms
662 class Cached_SearchHighlight extends Cached_DynamicContent
664 function Cached_SearchHighlight($word, $engine)
666 $this->_word = $word;
667 $this->engine = $engine;
670 function expand($basepage, &$markup)
672 return HTML::span(array('class' => 'search-term',
673 'title' => _("Found by ") . $this->engine),
678 class Cached_ExternalLink extends Cached_Link
681 function Cached_ExternalLink($url, $label = false)
684 if ($label && $label != $url)
685 $this->_label = $label;
693 function _getName($basepage)
695 $label = isset($this->_label) ? $this->_label : false;
696 return ($label and is_string($label)) ? $label : $this->_url;
699 function expand($basepage, &$markup)
703 $label = isset($this->_label) ? $this->_label : false;
704 $link = LinkURL($this->_url, $label);
706 if (GOOGLE_LINKS_NOFOLLOW) {
707 // Ignores nofollow when the user who saved the page was authenticated.
708 $page = $request->getPage($basepage);
709 $current = $page->getCurrentRevision(false);
710 if (!$current->get('author_id'))
711 $link->setAttr('rel', 'nofollow');
718 if (isset($this->_label) and is_string($this->_label))
719 return $this->_label;
724 class Cached_InterwikiLink extends Cached_ExternalLink
727 function Cached_InterwikiLink($link, $label = false)
729 $this->_link = $link;
731 $this->_label = $label;
734 function getPagename($basepage)
736 list ($moniker, $page) = explode(":", $this->_link, 2);
737 $page = new WikiPageName($page, $basepage);
738 if ($page->isValid()) return $page->name;
742 function getWikiPageLinks($basepage)
744 if ($basepage == '') return false;
745 /* ":DontStoreLink" */
746 if (substr($this->_link, 0, 1) == ':') return false;
747 /* store only links to valid pagenames */
748 $dbi = $GLOBALS['request']->getDbh();
749 if ($link = $this->getPagename($basepage) and $dbi->isWikiPage($link)) {
750 return array(array('linkto' => $link));
752 return false; // dont store external links
756 function _getName($basepage)
758 $label = isset($this->_label) ? $this->_label : false;
759 return ($label and is_string($label)) ? $label : $this->_link;
762 /* there may be internal interwiki links also */
765 return $this->getPagename(false) ? 'internal' : 'external';
768 function _getURL($basepage)
770 $link = $this->expand($basepage, $this);
771 return $link->getAttr('href');
774 function expand($basepage, &$markup)
777 $intermap = getInterwikiMap();
778 $label = isset($this->_label) ? $this->_label : false;
779 //FIXME: check Upload: inlined images
780 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
781 if (!in_array($this->_link, $WikiTheme->VALID_LINKS))
782 return HTML($label ? $label : $this->_link);
784 return $intermap->link($this->_link, $label);
789 if (isset($this->_label))
790 return $this->_label;
795 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
796 // Thanks to PhpWiki:DanFr for finding this bug.
797 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
798 class Cached_UserLink extends Cached_WikiLink
800 function expand($basepage, &$markup)
802 $label = isset($this->_label) ? $this->_label : false;
803 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
804 $page = new WikiPageName($this->_page, $basepage, $anchor);
805 $link = WikiLink($page, 'auto', $label);
806 // $link = HTML::a(array('href' => $PageName));
807 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
808 $link->setAttr('class', 'wikiuser');
814 * 1.3.13: Previously stored was only _pi.
815 * A fresh generated cache has now ->name and ->args also.
816 * main::isActionPage only checks the raw content.
818 class Cached_PluginInvocation extends Cached_DynamicContent
821 function Cached_PluginInvocation($pi)
824 $loader = $this->_getLoader();
825 if (is_array($plugin_cmdline = $loader->parsePI($pi)) and $plugin_cmdline[1]) {
826 $this->pi_name = $plugin_cmdline[0]; // plugin, plugin-form, plugin-list
827 $this->name = $plugin_cmdline[1]->getName();
828 $this->args = $plugin_cmdline[2];
832 function setTightness($top, $bottom)
836 function isInlineElement()
841 function expand($basepage, &$markup)
843 $loader = $this->_getLoader();
844 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
853 function getWikiPageLinks($basepage)
855 $loader = $this->_getLoader();
857 return $loader->getWikiPageLinks($this->_pi, $basepage);
860 function & _getLoader()
862 static $loader = false;
865 include_once 'lib/WikiPlugin.php';
866 $loader = new WikiPluginLoader;
876 // c-hanging-comment-ender-p: nil
877 // indent-tabs-mode: nil