3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004-2008 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 require_once("lib/Units.php");
25 class CacheableMarkup extends XmlContent {
27 function CacheableMarkup($content, $basepage) {
28 $this->_basepage = $basepage;
30 $this->_content = array();
31 $this->_append($content);
32 if ($this->_buf != '')
33 $this->_content[] = $this->_buf;
38 if (function_exists('gzcompress'))
39 return gzcompress(serialize($this), 9);
40 return serialize($this);
42 // FIXME: probably should implement some sort of "compression"
43 // when no gzcompress is available.
46 function unpack($packed) {
50 // ZLIB format has a five bit checksum in it's header.
51 // Lets check for sanity.
52 if (((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0)
53 and (substr($packed,0,2) == "\037\213")
54 or (substr($packed,0,2) == "x\332")) // 120, 218
56 if (function_exists('gzuncompress')) {
58 $data = gzuncompress($packed);
59 return unserialize($data);
61 // user our php lib. TESTME
62 include_once("ziplib.php");
63 $zip = new ZipReader($packed);
64 list(,$data,$attrib) = $zip->readFile();
65 return unserialize($data);
68 if (substr($packed,0,2) == "O:") {
69 // Looks like a serialized object
70 return unserialize($packed);
72 if (preg_match("/^\w+$/", $packed))
74 // happened with _BackendInfo problem also.
75 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
80 /** Get names of wikipages linked to.
82 * @return array of hashes { linkto=>pagename, relation=>pagename }
84 function getWikiPageLinks() {
86 foreach ($this->_content as $item) {
87 if (!isa($item, 'Cached_DynamicContent'))
89 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
91 $links = array_merge($links, $item_links);
93 // array_unique has a bug with hashes!
94 // set_links checks for duplicates, array_merge does not
95 //return array_unique($links);
101 * This is here to support the XML-RPC listLinks() method.
104 * Returns an array of hashes.
106 function getLinkInfo() {
108 foreach ($this->_content as $link) {
109 if (! isa($link, 'Cached_Link'))
111 $info = $link->getLinkInfo($this->_basepage);
112 $links[$info->href] = $info;
114 return array_values($links);
117 function _append($item) {
118 if (is_array($item)) {
119 foreach ($item as $subitem)
120 $this->_append($subitem);
122 elseif (!is_object($item)) {
123 $this->_buf .= $this->_quote((string) $item);
125 elseif (isa($item, 'Cached_DynamicContent')) {
127 $this->_content[] = $this->_buf;
130 $this->_content[] = $item;
132 elseif (isa($item, 'XmlElement')) {
133 if ($item->isEmpty()) {
134 $this->_buf .= $item->emptyTag();
137 $this->_buf .= $item->startTag();
138 foreach ($item->getContent() as $subitem)
139 $this->_append($subitem);
140 $this->_buf .= "</$item->_tag>";
142 if (!$this->getDescription() and $item->getTag() == 'p') {
143 // performance: when is this really needed?
144 $this->_glean_description($item->asString());
147 if (!$item->isInlineElement())
150 elseif (isa($item, 'XmlContent')) {
151 foreach ($item->getContent() as $item)
152 $this->_append($item);
154 elseif (method_exists($item, 'asXML')) {
155 $this->_buf .= $item->asXML();
157 elseif (method_exists($item, 'asString')) {
158 $this->_buf .= $this->_quote($item->asString());
161 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
165 function _glean_description($text) {
166 static $two_sentences;
167 if (!$two_sentences) {
168 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
170 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
173 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
174 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
178 * Guess a short description of the page.
182 * This algorithm was suggested on MeatballWiki by
183 * Alex Schroeder <kensanata@yahoo.com>.
185 * Use the first paragraph in the page which contains at least two
188 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
192 function getDescription () {
193 return isset($this->_description) ? $this->_description : '';
198 $basepage = $this->_basepage;
200 foreach ($this->_content as $item) {
201 if (is_string($item)) {
204 elseif (is_subclass_of($item,
206 ? 'Cached_DynamicContent'
207 : 'cached_dynamiccontent'))
209 $val = $item->expand($basepage, $this);
210 $xml .= $val->asXML();
213 $xml .= $item->asXML();
219 function printXML () {
220 $basepage = $this->_basepage;
221 // _content might be changed from a plugin (CreateToc)
222 for ($i=0; $i < count($this->_content); $i++) {
223 $item = $this->_content[$i];
224 if (is_string($item)) {
227 elseif (is_subclass_of($item,
229 ? 'Cached_DynamicContent'
230 : 'cached_dynamiccontent'))
231 { // give the content the chance to know about itself or even
233 $val = $item->expand($basepage, $this);
244 * The base class for all dynamic content.
246 * Dynamic content is anything that can change even when the original
247 * wiki-text from which it was parsed is unchanged.
249 class Cached_DynamicContent {
251 function cache(&$cache) {
255 function expand($basepage, &$obj) {
256 trigger_error("Pure virtual", E_USER_ERROR);
259 function getWikiPageLinks($basepage) {
264 class XmlRpc_LinkInfo {
265 function XmlRpc_LinkInfo($page, $type, $href, $relation = '') {
269 $this->relation = $relation;
270 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
274 class Cached_Link extends Cached_DynamicContent {
276 function isInlineElement() {
280 /** Get link info (for XML-RPC support)
282 * This is here to support the XML-RPC listLinks method.
283 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
285 function getLinkInfo($basepage) {
286 return new XmlRpc_LinkInfo($this->_getName($basepage),
288 $this->_getURL($basepage),
289 $this->_getRelation($basepage));
292 function _getURL($basepage) {
295 function __getRelation($basepage) {
296 return $this->_relation;
300 * Defer interwiki inline links. img src=upload:xx.png
301 * LinkImage($url, $alt = false)
303 class Cached_InlinedImage extends Cached_DynamicContent {
304 function isInlineElement() {
307 function _getURL($basepage) {
310 // TODO: fix interwiki inline links in case of static dumps
311 function expand($basepage, &$markup) {
313 $this->_basepage = $basepage;
314 $label = isset($this->_label) ? $this->_label : false;
315 if ($WikiTheme->DUMP_MODE) {
316 // In case of static dumps we need to check if we should
317 // inline the image or not: external: keep link, internal: copy locally
318 return LinkImage($label);
320 return LinkImage($label);
325 class Cached_WikiLink extends Cached_Link {
327 function Cached_WikiLink ($page, $label = false, $anchor = false) {
328 $this->_page = $page;
329 /* ":DontStoreLink" */
330 if (substr($this->_page,0,1) == ':') {
331 $this->_page = substr($this->_page,1);
332 $this->_nolink = true;
335 $this->_anchor = $anchor;
336 if ($label and $label != $page)
337 $this->_label = $label;
338 $this->_basepage = false;
341 function _getType() {
345 function getPagename($basepage) {
346 $page = new WikiPageName($this->_page, $basepage);
347 if ($page->isValid()) return $page->name;
351 function getWikiPageLinks($basepage) {
352 if ($basepage == '') return false;
353 if (isset($this->_nolink)) return false;
354 if ($link = $this->getPagename($basepage))
355 return array(array('linkto' => $link, 'relation' => 0));
360 function _getName($basepage) {
361 return $this->getPagename($basepage);
364 function _getURL($basepage) {
365 return WikiURL($this->getPagename($basepage));
366 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
369 function expand($basepage, &$markup) {
371 $this->_basepage = $basepage;
372 $label = isset($this->_label) ? $this->_label : false;
373 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
374 $page = new WikiPageName($this->_page, $basepage, $anchor);
375 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
376 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
377 return HTML($label ? $label : $page->getName());
379 if ($page->isValid()) return WikiLink($page, 'auto', $label);
380 else return HTML($label);
385 $label = isset($this->_label) ? $this->_label : false;
386 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
387 //TODO: need basepage for subpages like /Remove (within CreateTOC)
388 $page = new WikiPageName($this->_page, $this->_basepage, $anchor);
389 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
390 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
391 return $label ? $label : $page->getName();
393 $link = WikiLink($page, 'auto', $label);
394 return $link->asXML();
397 function asString() {
398 if (isset($this->_label))
399 return $this->_label;
404 class Cached_WikiLinkIfKnown extends Cached_WikiLink
406 function Cached_WikiLinkIfKnown ($moniker) {
407 $this->_page = $moniker;
410 function expand($basepage, &$markup) {
412 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
413 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
414 return HTML($label ? $label : $page->getName());
416 return WikiLink($this->_page, 'if_known');
420 class Cached_SpellCheck extends Cached_WikiLink
422 function Cached_SpellCheck ($word, $suggs) {
423 $this->_page = $word;
424 $this->suggestions = $suggs;
427 function expand($basepage, &$markup) {
428 $link = HTML::a(array('class' => 'spell-wrong',
429 'title' => 'SpellCheck: '.join(', ', $this->suggestions),
430 'name' => $this->_page),
436 class Cached_PhpwikiURL extends Cached_DynamicContent
438 function Cached_PhpwikiURL ($url, $label) {
441 $this->_label = $label;
444 function isInlineElement() {
448 function expand($basepage, &$markup) {
450 $label = isset($this->_label) ? $this->_label : false;
451 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
452 if (!in_array($this->_page, $WikiTheme->VALID_LINKS))
453 return HTML($label ? $label : $page->getName());
455 return LinkPhpwikiURL($this->_url, $label, $basepage);
459 $label = isset($this->_label) ? $this->_label : false;
460 $link = LinkPhpwikiURL($this->_url, $label);
461 return $link->asXML();
464 function asString() {
465 if (isset($this->_label))
466 return $this->_label;
472 * Relations (::) are named links to pages.
473 * Attributes (:=) are named metadata per page, "named links to numbers with units".
474 * We don't want to exhaust the linktable with numbers,
475 * since this would create empty pages per each value,
476 * so we don't store the attributes as full relationlink.
477 * But we do store the attribute name as relation with an empty pagename
478 * to denote that this is an attribute,
479 * and to enable a fast listRelations mode=attributes
481 class Cached_SemanticLink extends Cached_WikiLink {
483 function Cached_SemanticLink ($url, $label=false) {
485 if ($label && $label != $url)
486 $this->_label = $label;
487 $this->_expandurl($this->_url);
490 function isInlineElement() {
494 function getPagename($basepage) {
495 if (!isset($this->_page)) return false;
496 $page = new WikiPageName($this->_page, $basepage);
497 if ($page->isValid()) return $page->name;
501 /* Add relation to the link table.
502 * attributes have the _relation, but not the _page set.
504 function getWikiPageLinks($basepage) {
505 if ($basepage == '') return false;
506 if (!isset($this->_page) and isset($this->_attribute)) {
507 // An attribute: we store it in the basepage now, to fill the cache for page->save
508 // TODO: side-effect free query
509 $page = $GLOBALS['request']->getPage($basepage);
510 $page->setAttribute($this->_relation, $this->_attribute);
511 $this->_page = $basepage;
512 return array(array('linkto' => '', 'relation' => $this->_relation));
514 if ($link = $this->getPagename($basepage))
515 return array(array('linkto' => $link, 'relation' => $this->_relation));
520 function _expandurl($url) {
522 if (!preg_match('/^ ([^:]+) (:[:=]) (.+) $/x', $url, $m)) {
523 return HTML::strong(array('class' => 'rawurl'),
524 HTML::u(array('class' => 'baduri'),
525 _("BAD semantic relation link")));
527 $this->_relation = urldecode($m[1]);
528 $is_attribute = ($m[2] == ':=');
530 $this->_attribute = urldecode($m[3]);
531 // since this stored in the markup cache, we are extra sensible
532 // not to store false empty stuff.
533 $units = new Units();
534 if (!DISABLE_UNITS and !$units->errcode)
536 $this->_attribute_base = $units->Definition($this->_attribute);
537 $this->_unit = $units->baseunit($this->_attribute);
540 $this->_page = urldecode($m[3]);
545 function _expand($url, $label = false) {
547 $m = $this->_expandurl($url);
549 // do not link to the attribute value, but to the attribute
550 $is_attribute = ($m[2] == ':=');
551 if ($WikiTheme->DUMP_MODE and $WikiTheme->VALID_LINKS) {
552 if (isset($this->_page) and !in_array($this->_page, $WikiTheme->VALID_LINKS))
553 return HTML($label ? $label : ($is_attribute ? $this->_relation : $this->_page));
556 $title = isset($this->_attribute_base)
557 ? sprintf(_("Attribute %s, base value: %s"), $this->_relation, $this->_attribute_base)
558 : sprintf(_("Attribute %s, value: %s"), $this->_relation, $this->_attribute);
562 HTML::a(array('href' => WikiURL($is_attribute ? $this->_relation : $this->_page),
563 'class' => "wiki ".($is_attribute ? "attribute" : "relation"),
564 'title' => $is_attribute
566 : sprintf(_("Relation %s to page %s"), $this->_relation, $this->_page)),
569 } elseif ($is_attribute) {
572 HTML::a(array('href' => WikiURL($this->_relation),
573 'class' => "wiki attribute",
580 HTML::a(array('href' => WikiURL($this->_relation),
581 'class' => "wiki relation"),
583 HTML::span(array('class'=>'relation-symbol'), $m[2]),
584 HTML::a(array('href' => WikiURL($this->_page),
591 function expand($basepage, &$markup) {
592 $label = isset($this->_label) ? $this->_label : false;
593 return $this->_expand($this->_url, $label);
597 $label = isset($this->_label) ? $this->_label : false;
598 $link = $this->_expand($this->_url, $label);
599 return $link->asXML();
602 function asString() {
603 if (isset($this->_label))
604 return $this->_label;
610 * Highlight found search engine terms
612 class Cached_SearchHighlight extends Cached_DynamicContent
614 function Cached_SearchHighlight ($word, $engine) {
615 $this->_word = $word;
616 $this->engine = $engine;
619 function expand($basepage, &$markup) {
620 return HTML::span(array('class' => 'search-term',
621 'title' => _("Found by ") . $this->engine),
626 class Cached_ExternalLink extends Cached_Link {
628 function Cached_ExternalLink($url, $label=false) {
630 if ($label && $label != $url)
631 $this->_label = $label;
634 function _getType() {
638 function _getName($basepage) {
639 $label = isset($this->_label) ? $this->_label : false;
640 return ($label and is_string($label)) ? $label : $this->_url;
643 function expand($basepage, &$markup) {
646 $label = isset($this->_label) ? $this->_label : false;
647 $link = LinkURL($this->_url, $label);
649 if (GOOGLE_LINKS_NOFOLLOW) {
650 // Ignores nofollow when the user who saved the page was authenticated.
651 $page = $request->getPage($basepage);
652 $current = $page->getCurrentRevision(false);
653 if (!$current->get('author_id'))
654 $link->setAttr('rel', 'nofollow');
659 function asString() {
660 if (isset($this->_label) and is_string($this->_label))
661 return $this->_label;
666 class Cached_InterwikiLink extends Cached_ExternalLink {
668 function Cached_InterwikiLink($link, $label=false) {
669 $this->_link = $link;
671 $this->_label = $label;
674 function getPagename($basepage) {
675 list ($moniker, $page) = split (":", $this->_link, 2);
676 $page = new WikiPageName($page, $basepage);
677 if ($page->isValid()) return $page->name;
681 function getWikiPageLinks($basepage) {
682 if ($basepage == '') return false;
683 /* ":DontStoreLink" */
684 if (substr($this->_link,0,1) == ':') return false;
685 /* store only links to valid pagenames */
686 if ($link = $this->getPagename($basepage))
687 return array(array('linkto' => $link, 'relation' => 0));
688 else return false; // dont store external links
691 function _getName($basepage) {
692 $label = isset($this->_label) ? $this->_label : false;
693 return ($label and is_string($label)) ? $label : $this->_link;
696 /* there may be internal interwiki links also */
697 function _getType() {
698 return $this->getPagename(false) ? 'internal' : 'external';
701 function _getURL($basepage) {
702 $link = $this->expand($basepage, $this);
703 return $link->getAttr('href');
706 function expand($basepage, &$markup) {
708 $intermap = getInterwikiMap();
709 $label = isset($this->_label) ? $this->_label : false;
710 //FIXME: check Upload: inlined images
711 if ($WikiTheme->DUMP_MODE and !empty($WikiTheme->VALID_LINKS)) {
712 if (!in_array($this->_link, $WikiTheme->VALID_LINKS))
713 return HTML($label ? $label : $this->_link);
715 return $intermap->link($this->_link, $label);
718 function asString() {
719 if (isset($this->_label))
720 return $this->_label;
725 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
726 // Thanks to PhpWiki:DanFr for finding this bug.
727 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
728 class Cached_UserLink extends Cached_WikiLink {
729 function expand($basepage, &$markup) {
730 $label = isset($this->_label) ? $this->_label : false;
731 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
732 $page = new WikiPageName($this->_page, $basepage, $anchor);
733 $link = WikiLink($page, 'auto', $label);
734 // $link = HTML::a(array('href' => $PageName));
735 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
736 $link->setAttr('class', 'wikiuser');
742 * 1.3.13: Previously stored was only _pi.
743 * A fresh generated cache has now ->name and ->args also.
744 * main::isActionPage only checks the raw content.
746 class Cached_PluginInvocation extends Cached_DynamicContent {
748 function Cached_PluginInvocation ($pi) {
750 $loader = $this->_getLoader();
751 if (is_array($plugin_cmdline = $loader->parsePI($pi)) and $plugin_cmdline[1]) {
752 $this->pi_name = $plugin_cmdline[0]; // plugin, plugin-form, plugin-list, plugin-link
753 $this->name = $plugin_cmdline[1]->getName();
754 $this->args = $plugin_cmdline[2];
758 function setTightness($top, $bottom) {
759 $this->_tightenable = 0;
760 if ($top) $this->_tightenable |= 1;
761 if ($bottom) $this->_tightenable |= 2;
764 function isInlineElement() {
768 function expand($basepage, &$markup) {
769 $loader = $this->_getLoader();
770 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
774 function asString() {
778 function getWikiPageLinks($basepage) {
779 $loader = $this->_getLoader();
781 return $loader->getWikiPageLinks($this->_pi, $basepage);
784 function & _getLoader() {
785 static $loader = false;
788 include_once('lib/WikiPlugin.php');
789 $loader = new WikiPluginLoader;
795 // $Log: not supported by cvs2svn $
796 // Revision 1.64 2008/03/21 20:35:52 rurban
797 // Improve upon embedded ImgObject, such as [ *.mp3 ], objects.
798 // Object tags now render as label correctly and param tags are also added.
800 // Revision 1.63 2008/03/17 19:03:08 rurban
801 // protect $WikiTheme->VALID_LINKS
803 // Revision 1.62 2008/02/14 18:40:32 rurban
804 // fix DUMP_MODE with LINKS
806 // Revision 1.61 2008/01/30 19:08:59 vargenau
807 // Valid HTML code: we need a div, it might contain a table
809 // Revision 1.60 2007/09/15 12:28:46 rurban
810 // Improve multi-page format handling: abstract _DumpHtmlToDir. get rid of non-external pdf, non-global VALID_LINKS
812 // Revision 1.59 2007/09/12 19:32:29 rurban
813 // link only VALID_LINKS with pagelist HTML_DUMP
815 // Revision 1.58 2007/07/14 12:30:53 rurban
816 // include => require
818 // Revision 1.57 2007/05/28 20:13:46 rurban
819 // Overwrite all attributes at once at page->save to delete dangling meta
821 // Revision 1.56 2007/04/08 16:39:40 rurban
822 // fix when DISABLE_UNITS = true (thanks to Walter Rafelsberger)
823 // simplify title calculation
825 // Revision 1.55 2007/03/18 17:35:14 rurban
826 // Fix :DontStoreLink
828 // Revision 1.54 2007/01/25 07:41:41 rurban
829 // Print attribute in title. Use CSS formatting for ::=
831 // Revision 1.53 2007/01/21 23:26:52 rurban
832 // Translate Found by
834 // Revision 1.52 2007/01/20 15:53:51 rurban
835 // Rewrite of SearchHighlight: through ActionPage and InlineParser
837 // Revision 1.51 2007/01/20 11:24:53 rurban
838 // add SpellCheck support
840 // Revision 1.50 2007/01/07 18:41:51 rurban
841 // Fix fallback ZipReader syntax error. Use label=false. Add parsed plugin names to the stored tree.
843 // Revision 1.49 2007/01/04 16:40:35 rurban
844 // Remove units object from CachedMarkup links, Store parsed linkinfo only: basevalue, baseunit.
846 // Revision 1.48 2007/01/03 21:22:08 rurban
847 // Use Units for attributes. Store the unified base value as Cached_SemanticLink->_attribute_base in the wikimarkup and display it as title.
849 // Revision 1.47 2007/01/02 13:17:57 rurban
850 // fix semantic page links and attributes, esp. attributes. they get stored as link to empty page also. tighten semantic url expander regex, omit want_content if not necessary
852 // Revision 1.46 2006/12/22 00:11:38 rurban
853 // add seperate expandurl method, to simplify pagename parsing
855 // Revision 1.45 2006/10/12 06:33:50 rurban
856 // decide later with which class to render this link (fixes interwiki link layout)
858 // (c-file-style: "gnu")
863 // c-hanging-comment-ender-p: nil
864 // indent-tabs-mode: nil