2 rcs_id('$Id: CachedMarkup.php,v 1.48 2007-01-03 21:22:08 rurban Exp $');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004,2005,2006,2007 $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 include_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 (!isset($this->_description) and $item->getTag() == 'p')
143 $this->_glean_description($item->asString());
145 if (!$item->isInlineElement())
148 elseif (isa($item, 'XmlContent')) {
149 foreach ($item->getContent() as $item)
150 $this->_append($item);
152 elseif (method_exists($item, 'asXML')) {
153 $this->_buf .= $item->asXML();
155 elseif (method_exists($item, 'asString')) {
156 $this->_buf .= $this->_quote($item->asString());
159 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
163 function _glean_description($text) {
164 static $two_sentences;
165 if (!$two_sentences) {
166 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\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 () {
191 return isset($this->_description) ? $this->_description : '';
196 $basepage = $this->_basepage;
198 foreach ($this->_content as $item) {
199 if (is_string($item)) {
202 elseif (is_subclass_of($item,
204 ? 'Cached_DynamicContent'
205 : 'cached_dynamiccontent'))
207 $val = $item->expand($basepage, $this);
208 $xml .= $val->asXML();
211 $xml .= $item->asXML();
217 function printXML () {
218 $basepage = $this->_basepage;
219 // _content might be changed from a plugin (CreateToc)
220 for ($i=0; $i < count($this->_content); $i++) {
221 $item = $this->_content[$i];
222 if (is_string($item)) {
225 elseif (is_subclass_of($item,
227 ? 'Cached_DynamicContent'
228 : 'cached_dynamiccontent'))
229 { // give the content the chance to know about itself or even
231 $val = $item->expand($basepage, $this);
242 * The base class for all dynamic content.
244 * Dynamic content is anything that can change even when the original
245 * wiki-text from which it was parsed is unchanged.
247 class Cached_DynamicContent {
249 function cache(&$cache) {
253 function expand($basepage, &$obj) {
254 trigger_error("Pure virtual", E_USER_ERROR);
257 function getWikiPageLinks($basepage) {
262 class XmlRpc_LinkInfo {
263 function XmlRpc_LinkInfo($page, $type, $href, $relation = '') {
267 $this->relation = $relation;
268 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
272 class Cached_Link extends Cached_DynamicContent {
274 function isInlineElement() {
278 /** Get link info (for XML-RPC support)
280 * This is here to support the XML-RPC listLinks method.
281 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
283 function getLinkInfo($basepage) {
284 return new XmlRpc_LinkInfo($this->_getName($basepage),
286 $this->_getURL($basepage),
287 $this->_getRelation($basepage));
290 function _getURL($basepage) {
293 function __getRelation($basepage) {
294 return $this->_relation;
298 class Cached_WikiLink extends Cached_Link {
300 function Cached_WikiLink ($page, $label = false, $anchor = false) {
301 $this->_page = $page;
303 $this->_anchor = $anchor;
304 if ($label and $label != $page)
305 $this->_label = $label;
306 $this->_basepage = false;
309 function _getType() {
313 function getPagename($basepage) {
314 $page = new WikiPageName($this->_page, $basepage);
315 if ($page->isValid()) return $page->name;
319 function getWikiPageLinks($basepage) {
320 if ($basepage == '') return false;
321 if ($link = $this->getPagename($basepage))
322 return array(array('linkto' => $link, 'relation' => 0));
326 function _getName($basepage) {
327 return $this->getPagename($basepage);
330 function _getURL($basepage) {
331 return WikiURL($this->getPagename($basepage));
332 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
335 function expand($basepage, &$markup) {
336 $this->_basepage = $basepage;
337 $label = isset($this->_label) ? $this->_label : false;
338 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
339 $page = new WikiPageName($this->_page, $basepage, $anchor);
340 if ($page->isValid()) return WikiLink($page, 'auto', $label);
341 else return HTML($label);
345 $label = isset($this->_label) ? $this->_label : false;
346 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
347 //TODO: need basepage for subpages like /Remove (within CreateTOC)
348 $page = new WikiPageName($this->_page, $this->_basepage, $anchor);
349 $link = WikiLink($page, 'auto', $label);
350 return $link->asXML();
353 function asString() {
354 if (isset($this->_label))
355 return $this->_label;
360 class Cached_WikiLinkIfKnown extends Cached_WikiLink
362 function Cached_WikiLinkIfKnown ($moniker) {
363 $this->_page = $moniker;
366 function expand($basepage, &$markup) {
367 return WikiLink($this->_page, 'if_known');
371 class Cached_PhpwikiURL extends Cached_DynamicContent
373 function Cached_PhpwikiURL ($url, $label) {
376 $this->_label = $label;
379 function isInlineElement() {
383 function expand($basepage, &$markup) {
384 $label = isset($this->_label) ? $this->_label : false;
385 return LinkPhpwikiURL($this->_url, $label, $basepage);
389 $label = isset($this->_label) ? $this->_label : false;
390 $link = LinkPhpwikiURL($this->_url, $label);
391 return $link->asXML();
394 function asString() {
395 if (isset($this->_label))
396 return $this->_label;
402 * Relations (::) are named links to pages.
403 * Attributes (:=) are named metadata per page, "named links to numbers".
404 * We don't want to exhaust the linktable with numbers,
405 * since this would create empty pages per each value,
406 * so we don't store the attributes as relationlink
408 class Cached_SemanticLink extends Cached_WikiLink {
410 function Cached_SemanticLink ($url, $label) {
412 if ($label && $label != $url)
413 $this->_label = $label;
414 $this->_expandurl($this->_url);
415 $this->_units = new Units();
418 function isInlineElement() {
422 function getPagename($basepage) {
423 if (!isset($this->_page)) return false;
424 $page = new WikiPageName($this->_page, $basepage);
425 if ($page->isValid()) return $page->name;
429 /* add relation to the link table.
430 * attributes have the _relation, but not the _page set.
432 function getWikiPageLinks($basepage) {
433 if ($basepage == '') return false;
434 if (!isset($this->_page) and isset($this->_attribute)) {
435 // an attribute, we store it in the basepage now.
436 $page = $GLOBALS['request']->getPage($basepage);
437 $page->setAttribute($this->_relation, $this->_attribute);
438 return array(array('linkto' => '', 'relation' => $this->_relation));
441 if ($link = $this->getPagename($basepage))
442 return array(array('linkto' => $link, 'relation' => $this->_relation));
447 function _expandurl($url) {
449 if (!preg_match('/^ ([^:]+) (:[:=]) (.+) $/x', $url, $m)) {
450 return HTML::strong(array('class' => 'rawurl'),
451 HTML::u(array('class' => 'baduri'),
452 _("BAD semantic relation link")));
454 $this->_relation = urldecode($m[1]);
455 $is_attribute = ($m[2] == ':=');
457 $this->_attribute = urldecode($m[3]);
458 // since this stored in the markup cache, we are extra sensible
459 // not to store false empty stuff.
460 if (!DISABLE_UNITS and isset($this->_units)
461 and is_object($this->_units) and !$this->_units->errcode)
463 $this->_attribute_base = $this->_units->Definition($this->_attribute);
464 $this->_unit = $this->_units->baseunit($this->_attribute);
467 $this->_page = urldecode($m[3]);
472 function _expand($url, $label = false) {
473 $m = $this->_expandurl($url);
475 // do not link to the attribute value, but to the attribute
476 $is_attribute = ($m[2] == ':=');
480 HTML::a(array('href' => WikiURL($is_attribute ? $this->_relation : $this->_page),
481 'class' => "wiki ".($is_attribute ? "attribute" : "relation"),
482 'title' => $is_attribute
483 ? (isset($this->_attribute_base) ? ("Attribute base value: ").$this->_attribute_base
484 : ("Attribute value: ").$this->_attribute)
485 : sprintf(_("Relation %s to page %s"), $this->_relation, $this->_page)),
488 } elseif ($is_attribute) {
491 HTML::a(array('href' => WikiURL($this->_relation),
492 'class' => "wiki attribute",
493 'title' => "Attribute base value: " .$this->_attribute_base),
499 HTML::a(array('href' => WikiURL($this->_relation),
500 'class' => "wiki relation"),
503 HTML::a(array('href' => WikiURL($this->_page),
510 function expand($basepage, &$markup) {
511 $label = isset($this->_label) ? $this->_label : false;
512 return $this->_expand($this->_url, $label);
516 $label = isset($this->_label) ? $this->_label : false;
517 $link = $this->_expand($this->_url, $label);
518 return $link->asXML();
521 function asString() {
522 if (isset($this->_label))
523 return $this->_label;
528 class Cached_ExternalLink extends Cached_Link {
530 function Cached_ExternalLink($url, $label=false) {
532 if ($label && $label != $url)
533 $this->_label = $label;
536 function _getType() {
540 function _getName($basepage) {
541 $label = isset($this->_label) ? $this->_label : false;
542 return ($label and is_string($label)) ? $label : $this->_url;
545 function expand($basepage, &$markup) {
548 $label = isset($this->_label) ? $this->_label : false;
549 $link = LinkURL($this->_url, $label);
551 if (GOOGLE_LINKS_NOFOLLOW) {
552 // Ignores nofollow when the user who saved the page was authenticated.
553 $page = $request->getPage($basepage);
554 $current = $page->getCurrentRevision(false);
555 if (!$current->get('author_id'))
556 $link->setAttr('rel', 'nofollow');
561 function asString() {
562 if (isset($this->_label))
563 return $this->_label;
568 class Cached_InterwikiLink extends Cached_ExternalLink {
570 function Cached_InterwikiLink($link, $label=false) {
571 $this->_link = $link;
573 $this->_label = $label;
576 function getPagename($basepage) {
577 list ($moniker, $page) = split (":", $this->_link, 2);
578 $page = new WikiPageName($page, $basepage);
579 if ($page->isValid()) return $page->name;
583 function getWikiPageLinks($basepage) {
584 if ($basepage == '') return false;
585 /* ":DontStoreLink" */
586 if (substr($this->_link,0,1) == ':') return false;
587 /* store only links to valid pagenames */
588 if ($link = $this->getPagename($basepage))
589 return array(array('linkto' => $link, 'relation' => 0));
590 else return false; // dont store external links
593 function _getName($basepage) {
594 $label = isset($this->_label) ? $this->_label : false;
595 return ($label and is_string($label)) ? $label : $this->_link;
598 /* there may be internal interwiki links also */
599 function _getType() {
600 return $this->getPagename(false) ? 'internal' : 'external';
603 function _getURL($basepage) {
604 $link = $this->expand($basepage, $this);
605 return $link->getAttr('href');
608 function expand($basepage, &$markup) {
609 $intermap = getInterwikiMap();
610 $label = isset($this->_label) ? $this->_label : false;
611 return $intermap->link($this->_link, $label);
614 function asString() {
615 if (isset($this->_label))
616 return $this->_label;
621 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
622 // Thanks to PhpWiki:DanFr for finding this bug.
623 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
624 class Cached_UserLink extends Cached_WikiLink {
625 function expand($basepage, &$markup) {
626 $label = isset($this->_label) ? $this->_label : false;
627 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
628 $page = new WikiPageName($this->_page, $basepage, $anchor);
629 $link = WikiLink($page, 'auto', $label);
630 // $link = HTML::a(array('href' => $PageName));
631 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
632 $link->setAttr('class', 'wikiuser');
637 class Cached_PluginInvocation extends Cached_DynamicContent {
639 function Cached_PluginInvocation ($pi) {
643 function setTightness($top, $bottom) {
644 $this->_tightenable = 0;
645 if ($top) $this->_tightenable |= 1;
646 if ($bottom) $this->_tightenable |= 2;
649 function isInlineElement() {
653 function expand($basepage, &$markup) {
654 $loader = $this->_getLoader();
656 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
657 $div = HTML::div(array('class' => 'plugin'));
658 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
659 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
661 if (isset($this->_tightenable)) {
662 if ($this->_tightenable == 3) {
663 $span = HTML::span(array('class' => 'plugin'), $xml);
665 $span->setAttr('id', $id);
668 $div->setInClass('tightenable');
669 $div->setInClass('top', ($this->_tightenable & 1) != 0);
670 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
673 $div->setAttr('id', $id);
674 $div->pushContent($xml);
678 function asString() {
683 function getWikiPageLinks($basepage) {
684 $loader = $this->_getLoader();
686 return $loader->getWikiPageLinks($this->_pi, $basepage);
689 function & _getLoader() {
690 static $loader = false;
693 include_once('lib/WikiPlugin.php');
694 $loader = new WikiPluginLoader;
700 // $Log: not supported by cvs2svn $
701 // Revision 1.47 2007/01/02 13:17:57 rurban
702 // 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
704 // Revision 1.46 2006/12/22 00:11:38 rurban
705 // add seperate expandurl method, to simplify pagename parsing
707 // Revision 1.45 2006/10/12 06:33:50 rurban
708 // decide later with which class to render this link (fixes interwiki link layout)
710 // (c-file-style: "gnu")
715 // c-hanging-comment-ender-p: nil
716 // indent-tabs-mode: nil