2 rcs_id('$Id: CachedMarkup.php,v 1.39 2005-12-11 11:13:06 rurban Exp $');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004, 2005 $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 class CacheableMarkup extends XmlContent {
25 function CacheableMarkup($content, $basepage) {
26 $this->_basepage = $basepage;
28 $this->_content = array();
29 $this->_append($content);
30 if ($this->_buf != '')
31 $this->_content[] = $this->_buf;
36 if (function_exists('gzcompress'))
37 return gzcompress(serialize($this), 9);
38 return serialize($this);
40 // FIXME: probably should implement some sort of "compression"
41 // when no gzcompress is available.
44 function unpack($packed) {
48 // ZLIB format has a five bit checksum in it's header.
49 // Lets check for sanity.
50 if (((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0)
51 and (substr($packed,0,2) == "\037\213")
52 or (substr($packed,0,2) == "x\332")) // 120, 218
54 if (function_exists('gzuncompress')) {
56 $data = gzuncompress($packed);
57 return unserialize($data);
59 // user our php lib. TESTME
60 include_once("ziplib.php");
61 $zip = ZipReader($packed);
62 list(,$data,$§attrib) = $zip->readFile();
63 return unserialize($data);
66 if (substr($packed,0,2) == "O:") {
67 // Looks like a serialized object
68 return unserialize($packed);
70 if (preg_match("/^\w+$/", $packed))
72 // happened with _BackendInfo problem also.
73 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
78 /** Get names of wikipages linked to.
81 * A hash of linkto=>pagenames, relation=>pagenames (strings).
83 function getWikiPageLinks() {
84 // include_once('lib/WikiPlugin.php');
85 // $ploader = new WikiPluginLoader();
88 foreach ($this->_content as $item) {
89 if (!isa($item, 'Cached_DynamicContent'))
91 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
93 $links = array_merge($links, $item_links);
95 return array_unique($links);
100 * This is here to support the XML-RPC listLinks() method.
103 * Returns an array of hashes.
105 function getLinkInfo() {
107 foreach ($this->_content as $link) {
108 if (! isa($link, 'Cached_Link'))
110 $info = $link->getLinkInfo($this->_basepage);
111 $links[$info->href] = $info;
113 return array_values($links);
116 function _append($item) {
117 if (is_array($item)) {
118 foreach ($item as $subitem)
119 $this->_append($subitem);
121 elseif (!is_object($item)) {
122 $this->_buf .= $this->_quote((string) $item);
124 elseif (isa($item, 'Cached_DynamicContent')) {
126 $this->_content[] = $this->_buf;
129 $this->_content[] = $item;
131 elseif (isa($item, 'XmlElement')) {
132 if ($item->isEmpty()) {
133 $this->_buf .= $item->emptyTag();
136 $this->_buf .= $item->startTag();
137 foreach ($item->getContent() as $subitem)
138 $this->_append($subitem);
139 $this->_buf .= "</$item->_tag>";
141 if (!isset($this->_description) and $item->getTag() == 'p')
142 $this->_glean_description($item->asString());
144 if (!$item->isInlineElement())
147 elseif (isa($item, 'XmlContent')) {
148 foreach ($item->getContent() as $item)
149 $this->_append($item);
151 elseif (method_exists($item, 'asXML')) {
152 $this->_buf .= $item->asXML();
154 elseif (method_exists($item, 'asString')) {
155 $this->_buf .= $this->_quote($item->asString());
158 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
162 function _glean_description($text) {
163 static $two_sentences;
164 if (!$two_sentences) {
165 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
167 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
170 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
171 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
175 * Guess a short description of the page.
179 * This algorithm was suggested on MeatballWiki by
180 * Alex Schroeder <kensanata@yahoo.com>.
182 * Use the first paragraph in the page which contains at least two
185 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
189 function getDescription () {
190 return isset($this->_description) ? $this->_description : '';
195 $basepage = $this->_basepage;
197 foreach ($this->_content as $item) {
198 if (is_string($item)) {
201 elseif (is_subclass_of($item,
203 ? 'Cached_DynamicContent'
204 : 'cached_dynamiccontent'))
206 $val = $item->expand($basepage, $this);
207 $xml .= $val->asXML();
210 $xml .= $item->asXML();
216 function printXML () {
217 $basepage = $this->_basepage;
218 // _content might be changed from a plugin (CreateToc)
219 for ($i=0; $i < count($this->_content); $i++) {
220 $item = $this->_content[$i];
221 if (is_string($item)) {
224 elseif (is_subclass_of($item,
226 ? 'Cached_DynamicContent'
227 : 'cached_dynamiccontent'))
228 { // give the content the chance to know about itself or even
230 $val = $item->expand($basepage, $this);
241 * The base class for all dynamic content.
243 * Dynamic content is anything that can change even when the original
244 * wiki-text from which it was parsed is unchanged.
246 class Cached_DynamicContent {
248 function cache(&$cache) {
252 function expand($basepage, &$obj) {
253 trigger_error("Pure virtual", E_USER_ERROR);
256 function getWikiPageLinks($basepage) {
261 class XmlRpc_LinkInfo {
262 function XmlRpc_LinkInfo($page, $type, $href, $relation = '') {
266 $this->relation = $relation;
267 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
271 class Cached_Link extends Cached_DynamicContent {
273 function isInlineElement() {
277 /** Get link info (for XML-RPC support)
279 * This is here to support the XML-RPC listLinks method.
280 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
282 function getLinkInfo($basepage) {
283 return new XmlRpc_LinkInfo($this->_getName($basepage),
285 $this->_getURL($basepage),
286 $this->_getRelation($basepage));
289 function _getURL($basepage) {
292 function __getRelation($basepage) {
293 return $this->_relation;
297 class Cached_WikiLink extends Cached_Link {
299 function Cached_WikiLink ($page, $label = false, $anchor = false) {
300 $this->_page = $page;
302 $this->_anchor = $anchor;
303 if ($label and $label != $page)
304 $this->_label = $label;
307 function _getType() {
311 function getPagename($basepage) {
312 $page = new WikiPageName($this->_page, $basepage);
313 if ($page->isValid()) return $page->name;
317 function getWikiPageLinks($basepage) {
318 if ($basepage == '') return false;
319 if ($link = $this->getPagename($basepage))
320 return array(array('linkto' => $link, 'relation' => 0));
324 function _getName($basepage) {
325 return $this->getPagename($basepage);
328 function _getURL($basepage) {
329 return WikiURL($this->getPagename($basepage));
330 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
333 function expand($basepage, &$markup) {
334 $label = isset($this->_label) ? $this->_label : false;
335 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
336 $page = new WikiPageName($this->_page, $basepage, $anchor);
337 if ($page->isValid()) return WikiLink($page, 'auto', $label);
338 else return HTML($label);
342 $label = isset($this->_label) ? $this->_label : false;
343 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
344 $page = new WikiPageName($this->_page, false, $anchor);
345 $link = WikiLink($page, 'auto', $label);
346 return $link->asXML();
349 function asString() {
350 if (isset($this->_label))
351 return $this->_label;
356 class Cached_WikiLinkIfKnown extends Cached_WikiLink
358 function Cached_WikiLinkIfKnown ($moniker) {
359 $this->_page = $moniker;
362 function expand($basepage, &$markup) {
363 return WikiLink($this->_page, 'if_known');
367 class Cached_PhpwikiURL extends Cached_DynamicContent
369 function Cached_PhpwikiURL ($url, $label) {
372 $this->_label = $label;
375 function isInlineElement() {
379 function expand($basepage, &$markup) {
380 $label = isset($this->_label) ? $this->_label : false;
381 return LinkPhpwikiURL($this->_url, $label, $basepage);
385 $label = isset($this->_label) ? $this->_label : false;
386 $link = LinkPhpwikiURL($this->_url, $label);
387 return $link->asXML();
390 function asString() {
391 if (isset($this->_label))
392 return $this->_label;
397 class Cached_SemanticLink extends Cached_WikiLink {
399 function Cached_SemanticLink ($url, $label) {
401 if ($label && $label != $url)
402 $this->_label = $label;
405 function isInlineElement() {
409 function getPagename($basepage) {
410 $page = new WikiPageName($this->_page, $basepage);
411 if ($page->isValid()) return $page->name;
415 /* add relation to the link table */
416 function getWikiPageLinks($basepage) {
417 if ($basepage == '') return false;
418 if ($link = $this->getPagename($basepage))
419 return array(array('linkto' => $link, 'relation' => $this->_relation));
423 function _expand($url, $label = false) {
425 if (!preg_match('/^ ([^:]*) (:[:-]) (.*) $/x', $url, $m)) {
426 return HTML::strong(array('class' => 'rawurl'),
427 HTML::u(array('class' => 'baduri'),
428 _("BAD semantic relation link")));
430 $relation = $this->_relation = urldecode($m[1]);
431 $page = $this->_page = urldecode($m[3]);
433 // do not link to the attribute value, but to the attribute
434 $is_attribute = ($m[2] == ':-');
437 HTML::a(array('href' => WikiURL($is_attribute ? $relation : $page),
441 } elseif ($is_attribute) {
443 HTML::a(array('href' => WikiURL($relation),
449 HTML::a(array('href' => WikiURL($relation),
453 HTML::a(array('href' => WikiURL($page),
459 function expand($basepage, &$markup) {
460 $label = isset($this->_label) ? $this->_label : false;
461 return $this->_expand($this->_url, $label);
465 $label = isset($this->_label) ? $this->_label : false;
466 $link = $this->_expand($this->_url, $label);
467 return $link->asXML();
470 function asString() {
471 if (isset($this->_label))
472 return $this->_label;
477 class Cached_ExternalLink extends Cached_Link {
479 function Cached_ExternalLink($url, $label=false) {
481 if ($label && $label != $url)
482 $this->_label = $label;
485 function _getType() {
489 function _getName($basepage) {
490 $label = isset($this->_label) ? $this->_label : false;
491 return ($label and is_string($label)) ? $label : $this->_url;
494 function expand($basepage, &$markup) {
497 $label = isset($this->_label) ? $this->_label : false;
498 $link = LinkURL($this->_url, $label);
500 if (GOOGLE_LINKS_NOFOLLOW) {
501 // Ignores nofollow when the user who saved the page was authenticated.
502 $page = $request->getPage($basepage);
503 $current = $page->getCurrentRevision();
504 if (!$current->get('author_id'))
505 $link->setAttr('rel', 'nofollow');
510 function asString() {
511 if (isset($this->_label))
512 return $this->_label;
517 class Cached_InterwikiLink extends Cached_ExternalLink {
519 function Cached_InterwikiLink($link, $label=false) {
520 $this->_link = $link;
522 $this->_label = $label;
525 function _getName($basepage) {
526 $label = isset($this->_label) ? $this->_label : false;
527 return ($label and is_string($label)) ? $label : $link;
530 function _getURL($basepage) {
531 $link = $this->expand($basepage, $this);
532 return $link->getAttr('href');
535 function expand($basepage, &$markup) {
536 $intermap = getInterwikiMap();
537 $label = isset($this->_label) ? $this->_label : false;
538 return $intermap->link($this->_link, $label);
541 function asString() {
542 if (isset($this->_label))
543 return $this->_label;
548 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
549 // Thanks to PhpWiki:DanFr for finding this bug.
550 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
551 class Cached_UserLink extends Cached_WikiLink {
552 function expand($basepage, &$markup) {
553 $label = isset($this->_label) ? $this->_label : false;
554 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
555 $page = new WikiPageName($this->_page, $basepage, $anchor);
556 $link = WikiLink($page, 'auto', $label);
557 // $link = HTML::a(array('href' => $PageName));
558 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
559 $link->setAttr('class', 'wikiuser');
564 class Cached_PluginInvocation extends Cached_DynamicContent {
566 function Cached_PluginInvocation ($pi) {
570 function setTightness($top, $bottom) {
571 $this->_tightenable = 0;
572 if ($top) $this->_tightenable |= 1;
573 if ($bottom) $this->_tightenable |= 2;
576 function isInlineElement() {
580 function expand($basepage, &$markup) {
581 $loader = $this->_getLoader();
583 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
584 $div = HTML::div(array('class' => 'plugin'));
585 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
586 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
588 if (isset($this->_tightenable)) {
589 if ($this->_tightenable == 3) {
590 $span = HTML::span(array('class' => 'plugin'), $xml);
592 $span->setAttr('id', $id);
595 $div->setInClass('tightenable');
596 $div->setInClass('top', ($this->_tightenable & 1) != 0);
597 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
600 $div->setAttr('id', $id);
601 $div->pushContent($xml);
605 function asString() {
610 function getWikiPageLinks($basepage) {
611 $loader = &$this->_getLoader();
613 return $loader->getWikiPageLinks($this->_pi, $basepage);
616 function & _getLoader() {
617 static $loader = false;
620 include_once('lib/WikiPlugin.php');
621 $loader = new WikiPluginLoader;
627 // (c-file-style: "gnu")
632 // c-hanging-comment-ender-p: nil
633 // indent-tabs-mode: nil