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
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;
41 // This causes a strange bug when a comment containing
42 // a single quote is entered in the Summary box:
43 // - the history is wrong (user and comment missing)
44 // - the table of contents plugin no longer works
46 if (isa($WikiTheme, 'WikiTheme_fusionforge')) {
47 return serialize($this);
50 if (function_exists('gzcompress'))
51 return gzcompress(serialize($this), 9);
52 return serialize($this);
54 // FIXME: probably should implement some sort of "compression"
55 // when no gzcompress is available.
58 function unpack($packed) {
62 // ZLIB format has a five bit checksum in it's header.
63 // Lets check for sanity.
64 if (((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0)
65 and (substr($packed,0,2) == "\037\213")
66 or (substr($packed,0,2) == "x\332")) // 120, 218
68 if (function_exists('gzuncompress')) {
70 $data = gzuncompress($packed);
71 return unserialize($data);
73 // user our php lib. TESTME
74 include_once("ziplib.php");
75 $zip = new ZipReader($packed);
76 list(,$data,$attrib) = $zip->readFile();
77 return unserialize($data);
80 if (substr($packed,0,2) == "O:") {
81 // Looks like a serialized object
82 return unserialize($packed);
84 if (preg_match("/^\w+$/", $packed))
86 // happened with _BackendInfo problem also.
87 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
92 /** Get names of wikipages linked to.
94 * @return array of hashes { linkto=>pagename, relation=>pagename }
96 function getWikiPageLinks() {
98 foreach ($this->_content as $item) {
99 if (!isa($item, 'Cached_DynamicContent'))
101 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
103 $links = array_merge($links, $item_links);
105 // array_unique has a bug with hashes!
106 // set_links checks for duplicates, array_merge does not
107 //return array_unique($links);
113 * This is here to support the XML-RPC listLinks() method.
116 * Returns an array of hashes.
118 function getLinkInfo() {
120 foreach ($this->_content as $link) {
121 if (! isa($link, 'Cached_Link'))
123 $info = $link->getLinkInfo($this->_basepage);
124 $links[$info->href] = $info;
126 return array_values($links);
129 function _append($item) {
130 if (is_array($item)) {
131 foreach ($item as $subitem)
132 $this->_append($subitem);
134 elseif (!is_object($item)) {
135 $this->_buf .= $this->_quote((string) $item);
137 elseif (isa($item, 'Cached_DynamicContent')) {
139 $this->_content[] = $this->_buf;
142 $this->_content[] = $item;
144 elseif (isa($item, 'XmlElement')) {
145 if ($item->isEmpty()) {
146 $this->_buf .= $item->emptyTag();
149 $this->_buf .= $item->startTag();
150 foreach ($item->getContent() as $subitem)
151 $this->_append($subitem);
152 $this->_buf .= "</$item->_tag>";
154 if (!$this->getDescription() and $item->getTag() == 'p') {
155 // performance: when is this really needed?
156 $this->_glean_description($item->asString());
159 if (!$item->isInlineElement())
162 elseif (isa($item, 'XmlContent')) {
163 foreach ($item->getContent() as $item)
164 $this->_append($item);
166 elseif (method_exists($item, 'asXML')) {
167 $this->_buf .= $item->asXML();
169 elseif (method_exists($item, 'asString')) {
170 $this->_buf .= $this->_quote($item->asString());
173 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
177 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 () {
205 return isset($this->_description) ? $this->_description : '';
210 $basepage = $this->_basepage;
212 foreach ($this->_content as $item) {
213 if (is_string($item)) {
216 elseif (is_subclass_of($item,
218 ? 'Cached_DynamicContent'
219 : 'cached_dynamiccontent'))
221 $val = $item->expand($basepage, $this);
222 $xml .= $val->asXML();
225 $xml .= $item->asXML();
231 function printXML () {
232 $basepage = $this->_basepage;
233 // _content might be changed from a plugin (CreateToc)
234 for ($i=0; $i < count($this->_content); $i++) {
235 $item = $this->_content[$i];
236 if (is_string($item)) {
239 elseif (is_subclass_of($item,
241 ? 'Cached_DynamicContent'
242 : 'cached_dynamiccontent'))
243 { // give the content the chance to know about itself or even
245 $val = $item->expand($basepage, $this);
246 if ($val) $val->printXML();
247 else trigger_error('empty item ' . print_r($item, true));
257 * The base class for all dynamic content.
259 * Dynamic content is anything that can change even when the original
260 * wiki-text from which it was parsed is unchanged.
262 class Cached_DynamicContent {
264 function cache(&$cache) {
268 function expand($basepage, &$obj) {
269 trigger_error("Pure virtual", E_USER_ERROR);
272 function getWikiPageLinks($basepage) {
277 class XmlRpc_LinkInfo {
278 function XmlRpc_LinkInfo($page, $type, $href, $relation = '') {
282 $this->relation = $relation;
283 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
287 class Cached_Link extends Cached_DynamicContent {
289 function isInlineElement() {
293 /** Get link info (for XML-RPC support)
295 * This is here to support the XML-RPC listLinks method.
296 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
298 function getLinkInfo($basepage) {
299 return new XmlRpc_LinkInfo($this->_getName($basepage),
301 $this->_getURL($basepage),
302 $this->_getRelation($basepage));
305 function _getURL($basepage) {
308 function __getRelation($basepage) {
309 return $this->_relation;
313 * Defer interwiki inline links. img src=upload:xx.png
314 * LinkImage($url, $alt = false)
316 class Cached_InlinedImage extends Cached_DynamicContent {
317 function isInlineElement() {
320 function _getURL($basepage) {
323 // TODO: fix interwiki inline links in case of static dumps
324 function expand($basepage, &$markup) {
326 $this->_basepage = $basepage;
327 $label = isset($this->_label) ? $this->_label : false;
328 if ($WikiTheme->DUMP_MODE) {
329 // In case of static dumps we need to check if we should
330 // inline the image or not: external: keep link, internal: copy locally
331 return LinkImage($label);
333 return LinkImage($label);
338 class Cached_WikiLink extends Cached_Link {
340 function Cached_WikiLink ($page, $label = false, $anchor = false) {
341 $this->_page = $page;
342 /* ":DontStoreLink" */
343 if (substr($this->_page,0,1) == ':') {
344 $this->_page = substr($this->_page,1);
345 $this->_nolink = true;
348 $this->_anchor = $anchor;
349 if ($label and $label != $page)
350 $this->_label = $label;
351 $this->_basepage = false;
354 function _getType() {
358 function getPagename($basepage) {
359 $page = new WikiPageName($this->_page, $basepage);
360 if ($page->isValid()) return $page->name;
364 function getWikiPageLinks($basepage) {
365 if ($basepage == '') return false;
366 if (isset($this->_nolink)) return false;
367 if ($link = $this->getPagename($basepage))
368 return array(array('linkto' => $link));
373 function _getName($basepage) {
374 return $this->getPagename($basepage);
377 function _getURL($basepage) {
378 return WikiURL($this->getPagename($basepage));
379 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
382 function expand($basepage, &$markup) {
384 $this->_basepage = $basepage;
385 $label = isset($this->_label) ? $this->_label : false;
386 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
387 $page = new WikiPageName($this->_page, $basepage, $anchor);
388 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
389 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
390 return HTML($label ? $label : $page->getName());
392 if ($page->isValid()) return WikiLink($page, 'auto', $label);
393 else return HTML($label);
398 $label = isset($this->_label) ? $this->_label : false;
399 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
400 //TODO: need basepage for subpages like /Remove (within CreateTOC)
401 $page = new WikiPageName($this->_page, $this->_basepage, $anchor);
402 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
403 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
404 return $label ? $label : $page->getName();
406 $link = WikiLink($page, 'auto', $label);
407 return $link->asXML();
410 function asString() {
411 if (isset($this->_label))
412 return $this->_label;
417 class Cached_WikiLinkIfKnown extends Cached_WikiLink
419 function Cached_WikiLinkIfKnown ($moniker) {
420 $this->_page = $moniker;
423 function expand($basepage, &$markup) {
425 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
426 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
427 return HTML($label ? $label : $page->getName());
429 return WikiLink($this->_page, 'if_known');
433 class Cached_SpellCheck extends Cached_WikiLink
435 function Cached_SpellCheck ($word, $suggs) {
436 $this->_page = $word;
437 $this->suggestions = $suggs;
440 function expand($basepage, &$markup) {
441 $link = HTML::a(array('class' => 'spell-wrong',
442 'title' => 'SpellCheck: '.join(', ', $this->suggestions),
443 'name' => $this->_page),
449 class Cached_PhpwikiURL extends Cached_DynamicContent
451 function Cached_PhpwikiURL ($url, $label) {
454 $this->_label = $label;
457 function isInlineElement() {
461 function expand($basepage, &$markup) {
463 $label = isset($this->_label) ? $this->_label : false;
464 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
465 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
466 return HTML($label ? $label : $page->getName());
468 return LinkPhpwikiURL($this->_url, $label, $basepage);
472 $label = isset($this->_label) ? $this->_label : false;
473 $link = LinkPhpwikiURL($this->_url, $label);
474 return $link->asXML();
477 function asString() {
478 if (isset($this->_label))
479 return $this->_label;
485 * Relations (::) are named links to pages.
486 * Attributes (:=) are named metadata per page, "named links to numbers with units".
487 * We don't want to exhaust the linktable with numbers,
488 * since this would create empty pages per each value,
489 * so we don't store the attributes as full relationlink.
490 * But we do store the attribute name as relation with an empty pagename
491 * to denote that this is an attribute,
492 * and to enable a fast listRelations mode=attributes
494 class Cached_SemanticLink extends Cached_WikiLink {
496 function Cached_SemanticLink ($url, $label=false) {
498 if ($label && $label != $url)
499 $this->_label = $label;
500 $this->_expandurl($this->_url);
503 function isInlineElement() {
507 function getPagename($basepage) {
508 if (!isset($this->_page)) return false;
509 $page = new WikiPageName($this->_page, $basepage);
510 if ($page->isValid()) return $page->name;
514 /* Add relation to the link table.
515 * attributes have the _relation, but not the _page set.
517 function getWikiPageLinks($basepage) {
518 if ($basepage == '') return false;
519 if (!isset($this->_page) and isset($this->_attribute)) {
520 // An attribute: we store it in the basepage now, to fill the cache for page->save
521 // TODO: side-effect free query
522 $page = $GLOBALS['request']->getPage($basepage);
523 $page->setAttribute($this->_relation, $this->_attribute);
524 $this->_page = $basepage;
525 return array(array('linkto' => '', 'relation' => $this->_relation));
527 if ($link = $this->getPagename($basepage))
528 return array(array('linkto' => $link, 'relation' => $this->_relation));
533 function _expandurl($url) {
535 if (!preg_match('/^ ([^:]+) (:[:=]) (.+) $/x', $url, $m)) {
536 return HTML::span(array('class' => 'error'), _("BAD semantic relation link"));
538 $this->_relation = urldecode($m[1]);
539 $is_attribute = ($m[2] == ':=');
541 $this->_attribute = urldecode($m[3]);
542 // since this stored in the markup cache, we are extra sensible
543 // not to store false empty stuff.
544 $units = new Units();
545 if (!DISABLE_UNITS and !$units->errcode)
547 $this->_attribute_base = $units->Definition($this->_attribute);
548 $this->_unit = $units->baseunit($this->_attribute);
551 $this->_page = urldecode($m[3]);
556 function _expand($url, $label = false) {
558 $m = $this->_expandurl($url);
560 // do not link to the attribute value, but to the attribute
561 $is_attribute = ($m[2] == ':=');
562 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
563 if (isset($this->_page) and !in_array($this->_page, $WikiTheme->VALID_LINKS))
564 return HTML($label ? $label : ($is_attribute ? $this->_relation : $this->_page));
567 $title = isset($this->_attribute_base)
568 ? sprintf(_("Attribute %s, base value: %s"), $this->_relation, $this->_attribute_base)
569 : sprintf(_("Attribute %s, value: %s"), $this->_relation, $this->_attribute);
573 HTML::a(array('href' => WikiURL($is_attribute ? $this->_relation : $this->_page),
574 'class' => "wiki ".($is_attribute ? "attribute" : "relation"),
575 'title' => $is_attribute
577 : sprintf(_("Relation %s to page %s"), $this->_relation, $this->_page)),
580 } elseif ($is_attribute) {
583 HTML::a(array('href' => WikiURL($this->_relation),
584 'class' => "wiki attribute",
591 HTML::a(array('href' => WikiURL($this->_relation),
592 'class' => "wiki relation"),
594 HTML::span(array('class'=>'relation-symbol'), $m[2]),
595 HTML::a(array('href' => WikiURL($this->_page),
602 function expand($basepage, &$markup) {
603 $label = isset($this->_label) ? $this->_label : false;
604 return $this->_expand($this->_url, $label);
608 $label = isset($this->_label) ? $this->_label : false;
609 $link = $this->_expand($this->_url, $label);
610 return $link->asXML();
613 function asString() {
614 if (isset($this->_label))
615 return $this->_label;
621 * Highlight found search engine terms
623 class Cached_SearchHighlight extends Cached_DynamicContent
625 function Cached_SearchHighlight ($word, $engine) {
626 $this->_word = $word;
627 $this->engine = $engine;
630 function expand($basepage, &$markup) {
631 return HTML::span(array('class' => 'search-term',
632 'title' => _("Found by ") . $this->engine),
637 class Cached_ExternalLink extends Cached_Link {
639 function Cached_ExternalLink($url, $label=false) {
641 if ($label && $label != $url)
642 $this->_label = $label;
645 function _getType() {
649 function _getName($basepage) {
650 $label = isset($this->_label) ? $this->_label : false;
651 return ($label and is_string($label)) ? $label : $this->_url;
654 function expand($basepage, &$markup) {
657 $label = isset($this->_label) ? $this->_label : false;
658 $link = LinkURL($this->_url, $label);
660 if (GOOGLE_LINKS_NOFOLLOW) {
661 // Ignores nofollow when the user who saved the page was authenticated.
662 $page = $request->getPage($basepage);
663 $current = $page->getCurrentRevision(false);
664 if (!$current->get('author_id'))
665 $link->setAttr('rel', 'nofollow');
670 function asString() {
671 if (isset($this->_label) and is_string($this->_label))
672 return $this->_label;
677 class Cached_InterwikiLink extends Cached_ExternalLink {
679 function Cached_InterwikiLink($link, $label=false) {
680 $this->_link = $link;
682 $this->_label = $label;
685 function getPagename($basepage) {
686 list ($moniker, $page) = explode (":", $this->_link, 2);
687 $page = new WikiPageName($page, $basepage);
688 if ($page->isValid()) return $page->name;
692 function getWikiPageLinks($basepage) {
693 if ($basepage == '') return false;
694 /* ":DontStoreLink" */
695 if (substr($this->_link,0,1) == ':') return false;
696 /* store only links to valid pagenames */
697 $dbi = $GLOBALS['request']->getDbh();
698 if ($link = $this->getPagename($basepage) and $dbi->isWikiPage($link)) {
699 return array(array('linkto' => $link));
701 return false; // dont store external links
705 function _getName($basepage) {
706 $label = isset($this->_label) ? $this->_label : false;
707 return ($label and is_string($label)) ? $label : $this->_link;
710 /* there may be internal interwiki links also */
711 function _getType() {
712 return $this->getPagename(false) ? 'internal' : 'external';
715 function _getURL($basepage) {
716 $link = $this->expand($basepage, $this);
717 return $link->getAttr('href');
720 function expand($basepage, &$markup) {
722 $intermap = getInterwikiMap();
723 $label = isset($this->_label) ? $this->_label : false;
724 //FIXME: check Upload: inlined images
725 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
726 if (!in_array($this->_link, $WikiTheme->VALID_LINKS))
727 return HTML($label ? $label : $this->_link);
729 return $intermap->link($this->_link, $label);
732 function asString() {
733 if (isset($this->_label))
734 return $this->_label;
739 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
740 // Thanks to PhpWiki:DanFr for finding this bug.
741 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
742 class Cached_UserLink extends Cached_WikiLink {
743 function expand($basepage, &$markup) {
744 $label = isset($this->_label) ? $this->_label : false;
745 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
746 $page = new WikiPageName($this->_page, $basepage, $anchor);
747 $link = WikiLink($page, 'auto', $label);
748 // $link = HTML::a(array('href' => $PageName));
749 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
750 $link->setAttr('class', 'wikiuser');
756 * 1.3.13: Previously stored was only _pi.
757 * A fresh generated cache has now ->name and ->args also.
758 * main::isActionPage only checks the raw content.
760 class Cached_PluginInvocation extends Cached_DynamicContent {
762 function Cached_PluginInvocation ($pi) {
764 $loader = $this->_getLoader();
765 if (is_array($plugin_cmdline = $loader->parsePI($pi)) and $plugin_cmdline[1]) {
766 $this->pi_name = $plugin_cmdline[0]; // plugin, plugin-form, plugin-list
767 $this->name = $plugin_cmdline[1]->getName();
768 $this->args = $plugin_cmdline[2];
772 function setTightness($top, $bottom) {
775 function isInlineElement() {
779 function expand($basepage, &$markup) {
780 $loader = $this->_getLoader();
781 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
785 function asString() {
789 function getWikiPageLinks($basepage) {
790 $loader = $this->_getLoader();
792 return $loader->getWikiPageLinks($this->_pi, $basepage);
795 function & _getLoader() {
796 static $loader = false;
799 include_once('lib/WikiPlugin.php');
800 $loader = new WikiPluginLoader;
810 // c-hanging-comment-ender-p: nil
811 // indent-tabs-mode: nil