3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004-2008 $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
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 require_once("lib/Units.php");
26 class CacheableMarkup extends XmlContent {
28 function CacheableMarkup($content, $basepage) {
29 $this->_basepage = $basepage;
31 $this->_content = array();
32 $this->_append($content);
33 if ($this->_buf != '')
34 $this->_content[] = $this->_buf;
39 if (function_exists('gzcompress'))
40 return gzcompress(serialize($this), 9);
41 return serialize($this);
43 // FIXME: probably should implement some sort of "compression"
44 // when no gzcompress is available.
47 function unpack($packed) {
51 // ZLIB format has a five bit checksum in it's header.
52 // Lets check for sanity.
53 if (((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0)
54 and (substr($packed,0,2) == "\037\213")
55 or (substr($packed,0,2) == "x\332")) // 120, 218
57 if (function_exists('gzuncompress')) {
59 $data = gzuncompress($packed);
60 return unserialize($data);
62 // user our php lib. TESTME
63 include_once("ziplib.php");
64 $zip = new ZipReader($packed);
65 list(,$data,$attrib) = $zip->readFile();
66 return unserialize($data);
69 if (substr($packed,0,2) == "O:") {
70 // Looks like a serialized object
71 return unserialize($packed);
73 if (preg_match("/^\w+$/", $packed))
75 // happened with _BackendInfo problem also.
76 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
81 /** Get names of wikipages linked to.
83 * @return array of hashes { linkto=>pagename, relation=>pagename }
85 function getWikiPageLinks() {
87 foreach ($this->_content as $item) {
88 if (!isa($item, 'Cached_DynamicContent'))
90 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
92 $links = array_merge($links, $item_links);
94 // array_unique has a bug with hashes!
95 // set_links checks for duplicates, array_merge does not
96 //return array_unique($links);
102 * This is here to support the XML-RPC listLinks() method.
105 * Returns an array of hashes.
107 function getLinkInfo() {
109 foreach ($this->_content as $link) {
110 if (! isa($link, 'Cached_Link'))
112 $info = $link->getLinkInfo($this->_basepage);
113 $links[$info->href] = $info;
115 return array_values($links);
118 function _append($item) {
119 if (is_array($item)) {
120 foreach ($item as $subitem)
121 $this->_append($subitem);
123 elseif (!is_object($item)) {
124 $this->_buf .= $this->_quote((string) $item);
126 elseif (isa($item, 'Cached_DynamicContent')) {
128 $this->_content[] = $this->_buf;
131 $this->_content[] = $item;
133 elseif (isa($item, 'XmlElement')) {
134 if ($item->isEmpty()) {
135 $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())
151 elseif (isa($item, 'XmlContent')) {
152 foreach ($item->getContent() as $item)
153 $this->_append($item);
155 elseif (method_exists($item, 'asXML')) {
156 $this->_buf .= $item->asXML();
158 elseif (method_exists($item, 'asString')) {
159 $this->_buf .= $this->_quote($item->asString());
162 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
166 function _glean_description($text) {
167 static $two_sentences;
168 if (!$two_sentences) {
169 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
171 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
174 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
175 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
179 * Guess a short description of the page.
183 * This algorithm was suggested on MeatballWiki by
184 * Alex Schroeder <kensanata@yahoo.com>.
186 * Use the first paragraph in the page which contains at least two
189 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
193 function getDescription () {
194 return isset($this->_description) ? $this->_description : '';
199 $basepage = $this->_basepage;
201 foreach ($this->_content as $item) {
202 if (is_string($item)) {
205 elseif (is_subclass_of($item,
207 ? 'Cached_DynamicContent'
208 : 'cached_dynamiccontent'))
210 $val = $item->expand($basepage, $this);
211 $xml .= $val->asXML();
214 $xml .= $item->asXML();
220 function printXML () {
221 $basepage = $this->_basepage;
222 // _content might be changed from a plugin (CreateToc)
223 for ($i=0; $i < count($this->_content); $i++) {
224 $item = $this->_content[$i];
225 if (is_string($item)) {
228 elseif (is_subclass_of($item,
230 ? 'Cached_DynamicContent'
231 : 'cached_dynamiccontent'))
232 { // give the content the chance to know about itself or even
234 $val = $item->expand($basepage, $this);
245 * The base class for all dynamic content.
247 * Dynamic content is anything that can change even when the original
248 * wiki-text from which it was parsed is unchanged.
250 class Cached_DynamicContent {
252 function cache(&$cache) {
256 function expand($basepage, &$obj) {
257 trigger_error("Pure virtual", E_USER_ERROR);
260 function getWikiPageLinks($basepage) {
265 class XmlRpc_LinkInfo {
266 function XmlRpc_LinkInfo($page, $type, $href, $relation = '') {
270 $this->relation = $relation;
271 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
275 class Cached_Link extends Cached_DynamicContent {
277 function isInlineElement() {
281 /** Get link info (for XML-RPC support)
283 * This is here to support the XML-RPC listLinks method.
284 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
286 function getLinkInfo($basepage) {
287 return new XmlRpc_LinkInfo($this->_getName($basepage),
289 $this->_getURL($basepage),
290 $this->_getRelation($basepage));
293 function _getURL($basepage) {
296 function __getRelation($basepage) {
297 return $this->_relation;
301 * Defer interwiki inline links. img src=upload:xx.png
302 * LinkImage($url, $alt = false)
304 class Cached_InlinedImage extends Cached_DynamicContent {
305 function isInlineElement() {
308 function _getURL($basepage) {
311 // TODO: fix interwiki inline links in case of static dumps
312 function expand($basepage, &$markup) {
314 $this->_basepage = $basepage;
315 $label = isset($this->_label) ? $this->_label : false;
316 if ($WikiTheme->DUMP_MODE) {
317 // In case of static dumps we need to check if we should
318 // inline the image or not: external: keep link, internal: copy locally
319 return LinkImage($label);
321 return LinkImage($label);
326 class Cached_WikiLink extends Cached_Link {
328 function Cached_WikiLink ($page, $label = false, $anchor = false) {
329 $this->_page = $page;
330 /* ":DontStoreLink" */
331 if (substr($this->_page,0,1) == ':') {
332 $this->_page = substr($this->_page,1);
333 $this->_nolink = true;
336 $this->_anchor = $anchor;
337 if ($label and $label != $page)
338 $this->_label = $label;
339 $this->_basepage = false;
342 function _getType() {
346 function getPagename($basepage) {
347 $page = new WikiPageName($this->_page, $basepage);
348 if ($page->isValid()) return $page->name;
352 function getWikiPageLinks($basepage) {
353 if ($basepage == '') return false;
354 if (isset($this->_nolink)) return false;
355 if ($link = $this->getPagename($basepage))
356 return array(array('linkto' => $link, 'relation' => 0));
361 function _getName($basepage) {
362 return $this->getPagename($basepage);
365 function _getURL($basepage) {
366 return WikiURL($this->getPagename($basepage));
367 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
370 function expand($basepage, &$markup) {
372 $this->_basepage = $basepage;
373 $label = isset($this->_label) ? $this->_label : false;
374 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
375 $page = new WikiPageName($this->_page, $basepage, $anchor);
376 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
377 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
378 return HTML($label ? $label : $page->getName());
380 if ($page->isValid()) return WikiLink($page, 'auto', $label);
381 else return HTML($label);
386 $label = isset($this->_label) ? $this->_label : false;
387 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
388 //TODO: need basepage for subpages like /Remove (within CreateTOC)
389 $page = new WikiPageName($this->_page, $this->_basepage, $anchor);
390 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
391 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
392 return $label ? $label : $page->getName();
394 $link = WikiLink($page, 'auto', $label);
395 return $link->asXML();
398 function asString() {
399 if (isset($this->_label))
400 return $this->_label;
405 class Cached_WikiLinkIfKnown extends Cached_WikiLink
407 function Cached_WikiLinkIfKnown ($moniker) {
408 $this->_page = $moniker;
411 function expand($basepage, &$markup) {
413 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
414 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
415 return HTML($label ? $label : $page->getName());
417 return WikiLink($this->_page, 'if_known');
421 class Cached_SpellCheck extends Cached_WikiLink
423 function Cached_SpellCheck ($word, $suggs) {
424 $this->_page = $word;
425 $this->suggestions = $suggs;
428 function expand($basepage, &$markup) {
429 $link = HTML::a(array('class' => 'spell-wrong',
430 'title' => 'SpellCheck: '.join(', ', $this->suggestions),
431 'name' => $this->_page),
437 class Cached_PhpwikiURL extends Cached_DynamicContent
439 function Cached_PhpwikiURL ($url, $label) {
442 $this->_label = $label;
445 function isInlineElement() {
449 function expand($basepage, &$markup) {
451 $label = isset($this->_label) ? $this->_label : false;
452 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
453 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
454 return HTML($label ? $label : $page->getName());
456 return LinkPhpwikiURL($this->_url, $label, $basepage);
460 $label = isset($this->_label) ? $this->_label : false;
461 $link = LinkPhpwikiURL($this->_url, $label);
462 return $link->asXML();
465 function asString() {
466 if (isset($this->_label))
467 return $this->_label;
473 * Relations (::) are named links to pages.
474 * Attributes (:=) are named metadata per page, "named links to numbers with units".
475 * We don't want to exhaust the linktable with numbers,
476 * since this would create empty pages per each value,
477 * so we don't store the attributes as full relationlink.
478 * But we do store the attribute name as relation with an empty pagename
479 * to denote that this is an attribute,
480 * and to enable a fast listRelations mode=attributes
482 class Cached_SemanticLink extends Cached_WikiLink {
484 function Cached_SemanticLink ($url, $label=false) {
486 if ($label && $label != $url)
487 $this->_label = $label;
488 $this->_expandurl($this->_url);
491 function isInlineElement() {
495 function getPagename($basepage) {
496 if (!isset($this->_page)) return false;
497 $page = new WikiPageName($this->_page, $basepage);
498 if ($page->isValid()) return $page->name;
502 /* Add relation to the link table.
503 * attributes have the _relation, but not the _page set.
505 function getWikiPageLinks($basepage) {
506 if ($basepage == '') return false;
507 if (!isset($this->_page) and isset($this->_attribute)) {
508 // An attribute: we store it in the basepage now, to fill the cache for page->save
509 // TODO: side-effect free query
510 $page = $GLOBALS['request']->getPage($basepage);
511 $page->setAttribute($this->_relation, $this->_attribute);
512 $this->_page = $basepage;
513 return array(array('linkto' => '', 'relation' => $this->_relation));
515 if ($link = $this->getPagename($basepage))
516 return array(array('linkto' => $link, 'relation' => $this->_relation));
521 function _expandurl($url) {
523 if (!preg_match('/^ ([^:]+) (:[:=]) (.+) $/x', $url, $m)) {
524 return HTML::strong(array('class' => 'rawurl'),
525 HTML::u(array('class' => 'baduri'),
526 _("BAD semantic relation link")));
528 $this->_relation = urldecode($m[1]);
529 $is_attribute = ($m[2] == ':=');
531 $this->_attribute = urldecode($m[3]);
532 // since this stored in the markup cache, we are extra sensible
533 // not to store false empty stuff.
534 $units = new Units();
535 if (!DISABLE_UNITS and !$units->errcode)
537 $this->_attribute_base = $units->Definition($this->_attribute);
538 $this->_unit = $units->baseunit($this->_attribute);
541 $this->_page = urldecode($m[3]);
546 function _expand($url, $label = false) {
548 $m = $this->_expandurl($url);
550 // do not link to the attribute value, but to the attribute
551 $is_attribute = ($m[2] == ':=');
552 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
553 if (isset($this->_page) and !in_array($this->_page, $WikiTheme->VALID_LINKS))
554 return HTML($label ? $label : ($is_attribute ? $this->_relation : $this->_page));
557 $title = isset($this->_attribute_base)
558 ? sprintf(_("Attribute %s, base value: %s"), $this->_relation, $this->_attribute_base)
559 : sprintf(_("Attribute %s, value: %s"), $this->_relation, $this->_attribute);
563 HTML::a(array('href' => WikiURL($is_attribute ? $this->_relation : $this->_page),
564 'class' => "wiki ".($is_attribute ? "attribute" : "relation"),
565 'title' => $is_attribute
567 : sprintf(_("Relation %s to page %s"), $this->_relation, $this->_page)),
570 } elseif ($is_attribute) {
573 HTML::a(array('href' => WikiURL($this->_relation),
574 'class' => "wiki attribute",
581 HTML::a(array('href' => WikiURL($this->_relation),
582 'class' => "wiki relation"),
584 HTML::span(array('class'=>'relation-symbol'), $m[2]),
585 HTML::a(array('href' => WikiURL($this->_page),
592 function expand($basepage, &$markup) {
593 $label = isset($this->_label) ? $this->_label : false;
594 return $this->_expand($this->_url, $label);
598 $label = isset($this->_label) ? $this->_label : false;
599 $link = $this->_expand($this->_url, $label);
600 return $link->asXML();
603 function asString() {
604 if (isset($this->_label))
605 return $this->_label;
611 * Highlight found search engine terms
613 class Cached_SearchHighlight extends Cached_DynamicContent
615 function Cached_SearchHighlight ($word, $engine) {
616 $this->_word = $word;
617 $this->engine = $engine;
620 function expand($basepage, &$markup) {
621 return HTML::span(array('class' => 'search-term',
622 'title' => _("Found by ") . $this->engine),
627 class Cached_ExternalLink extends Cached_Link {
629 function Cached_ExternalLink($url, $label=false) {
631 if ($label && $label != $url)
632 $this->_label = $label;
635 function _getType() {
639 function _getName($basepage) {
640 $label = isset($this->_label) ? $this->_label : false;
641 return ($label and is_string($label)) ? $label : $this->_url;
644 function expand($basepage, &$markup) {
647 $label = isset($this->_label) ? $this->_label : false;
648 $link = LinkURL($this->_url, $label);
650 if (GOOGLE_LINKS_NOFOLLOW) {
651 // Ignores nofollow when the user who saved the page was authenticated.
652 $page = $request->getPage($basepage);
653 $current = $page->getCurrentRevision(false);
654 if (!$current->get('author_id'))
655 $link->setAttr('rel', 'nofollow');
660 function asString() {
661 if (isset($this->_label) and is_string($this->_label))
662 return $this->_label;
667 class Cached_InterwikiLink extends Cached_ExternalLink {
669 function Cached_InterwikiLink($link, $label=false) {
670 $this->_link = $link;
672 $this->_label = $label;
675 function getPagename($basepage) {
676 list ($moniker, $page) = split (":", $this->_link, 2);
677 $page = new WikiPageName($page, $basepage);
678 if ($page->isValid()) return $page->name;
682 function getWikiPageLinks($basepage) {
683 if ($basepage == '') return false;
684 /* ":DontStoreLink" */
685 if (substr($this->_link,0,1) == ':') return false;
686 /* store only links to valid pagenames */
687 if ($link = $this->getPagename($basepage))
688 return array(array('linkto' => $link, 'relation' => 0));
689 else return false; // dont store external links
692 function _getName($basepage) {
693 $label = isset($this->_label) ? $this->_label : false;
694 return ($label and is_string($label)) ? $label : $this->_link;
697 /* there may be internal interwiki links also */
698 function _getType() {
699 return $this->getPagename(false) ? 'internal' : 'external';
702 function _getURL($basepage) {
703 $link = $this->expand($basepage, $this);
704 return $link->getAttr('href');
707 function expand($basepage, &$markup) {
709 $intermap = getInterwikiMap();
710 $label = isset($this->_label) ? $this->_label : false;
711 //FIXME: check Upload: inlined images
712 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
713 if (!in_array($this->_link, $WikiTheme->VALID_LINKS))
714 return HTML($label ? $label : $this->_link);
716 return $intermap->link($this->_link, $label);
719 function asString() {
720 if (isset($this->_label))
721 return $this->_label;
726 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
727 // Thanks to PhpWiki:DanFr for finding this bug.
728 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
729 class Cached_UserLink extends Cached_WikiLink {
730 function expand($basepage, &$markup) {
731 $label = isset($this->_label) ? $this->_label : false;
732 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
733 $page = new WikiPageName($this->_page, $basepage, $anchor);
734 $link = WikiLink($page, 'auto', $label);
735 // $link = HTML::a(array('href' => $PageName));
736 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
737 $link->setAttr('class', 'wikiuser');
743 * 1.3.13: Previously stored was only _pi.
744 * A fresh generated cache has now ->name and ->args also.
745 * main::isActionPage only checks the raw content.
747 class Cached_PluginInvocation extends Cached_DynamicContent {
749 function Cached_PluginInvocation ($pi) {
751 $loader = $this->_getLoader();
752 if (is_array($plugin_cmdline = $loader->parsePI($pi)) and $plugin_cmdline[1]) {
753 $this->pi_name = $plugin_cmdline[0]; // plugin, plugin-form, plugin-list, plugin-link
754 $this->name = $plugin_cmdline[1]->getName();
755 $this->args = $plugin_cmdline[2];
759 function isInlineElement() {
763 function expand($basepage, &$markup) {
764 $loader = $this->_getLoader();
765 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
769 function asString() {
773 function getWikiPageLinks($basepage) {
774 $loader = $this->_getLoader();
776 return $loader->getWikiPageLinks($this->_pi, $basepage);
779 function & _getLoader() {
780 static $loader = false;
783 include_once('lib/WikiPlugin.php');
784 $loader = new WikiPluginLoader;
790 // (c-file-style: "gnu")
795 // c-hanging-comment-ender-p: nil
796 // indent-tabs-mode: nil