2 rcs_id('$Id: CachedMarkup.php,v 1.31 2005-01-21 11:51:22 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 if (function_exists('gzcompress')) {
49 // ZLIB format has a five bit checksum in it's header.
50 // Lets check for sanity.
51 if ((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0) {
53 return unserialize(gzuncompress($packed));
56 if (substr($packed,0,2) == "O:") {
57 // Looks like a serialized object
58 return unserialize($packed);
60 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
65 /** Get names of wikipages linked to.
68 * A list of wiki page names (strings).
70 function getWikiPageLinks() {
71 include_once('lib/WikiPlugin.php');
72 $ploader = new WikiPluginLoader();
75 foreach ($this->_content as $item) {
76 if (!isa($item, 'Cached_DynamicContent'))
79 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
81 foreach ($item_links as $pagename)
82 if (is_string($pagename) and $pagename != '')
86 return array_unique($links);
91 * This is here to support the XML-RPC listLinks() method.
94 * Returns an array of hashes.
96 function getLinkInfo() {
98 foreach ($this->_content as $link) {
99 if (! isa($link, 'Cached_Link'))
101 $info = $link->getLinkInfo($this->_basepage);
102 $links[$info->href] = $info;
104 return array_values($links);
107 function _append($item) {
108 if (is_array($item)) {
109 foreach ($item as $subitem)
110 $this->_append($subitem);
112 elseif (!is_object($item)) {
113 $this->_buf .= $this->_quote((string) $item);
115 elseif (isa($item, 'Cached_DynamicContent')) {
117 $this->_content[] = $this->_buf;
120 $this->_content[] = $item;
122 elseif (isa($item, 'XmlElement')) {
123 if ($item->isEmpty()) {
124 $this->_buf .= $item->emptyTag();
127 $this->_buf .= $item->startTag();
128 foreach ($item->getContent() as $subitem)
129 $this->_append($subitem);
130 $this->_buf .= "</$item->_tag>";
132 if (!isset($this->_description) and $item->getTag() == 'p')
133 $this->_glean_description($item->asString());
135 if (!$item->isInlineElement())
138 elseif (isa($item, 'XmlContent')) {
139 foreach ($item->getContent() as $item)
140 $this->_append($item);
142 elseif (method_exists($item, 'asXML')) {
143 $this->_buf .= $item->asXML();
145 elseif (method_exists($item, 'asString')) {
146 $this->_buf .= $this->_quote($item->asString());
149 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
153 function _glean_description($text) {
154 static $two_sentences;
155 if (!$two_sentences) {
156 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
158 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
161 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
162 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
166 * Guess a short description of the page.
170 * This algorithm was suggested on MeatballWiki by
171 * Alex Schroeder <kensanata@yahoo.com>.
173 * Use the first paragraph in the page which contains at least two
176 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
180 function getDescription () {
181 return isset($this->_description) ? $this->_description : '';
186 $basepage = $this->_basepage;
188 foreach ($this->_content as $item) {
189 if (is_string($item)) {
192 elseif (is_subclass_of($item, check_php_version(5) ? 'Cached_DynamicContent' : 'cached_dynamiccontent')) {
193 $val = $item->expand($basepage, $this);
194 $xml .= $val->asXML();
197 $xml .= $item->asXML();
203 function printXML () {
204 $basepage = $this->_basepage;
205 // _content might be changed from a plugin (CreateToc)
206 for ($i=0; $i < count($this->_content); $i++) {
207 $item = $this->_content[$i];
208 if (is_string($item)) {
211 elseif (is_subclass_of($item, check_php_version(5) ? 'Cached_DynamicContent' : 'cached_dynamiccontent')) {
212 // give the content the chance to know about itself or even
214 $val = $item->expand($basepage, $this);
225 * The base class for all dynamic content.
227 * Dynamic content is anything that can change even when the original
228 * wiki-text from which it was parsed is unchanged.
230 class Cached_DynamicContent {
232 function cache(&$cache) {
236 function expand($basepage, &$obj) {
237 trigger_error("Pure virtual", E_USER_ERROR);
240 function getWikiPageLinks($basepage) {
245 class XmlRpc_LinkInfo {
246 function XmlRpc_LinkInfo($page, $type, $href) {
250 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
254 class Cached_Link extends Cached_DynamicContent {
256 function isInlineElement() {
260 /** Get link info (for XML-RPC support)
262 * This is here to support the XML-RPC listLinks method.
263 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
265 function getLinkInfo($basepage) {
266 return new XmlRpc_LinkInfo($this->_getName($basepage),
268 $this->_getURL($basepage));
271 function _getURL($basepage) {
276 class Cached_WikiLink extends Cached_Link {
278 function Cached_WikiLink ($page, $label = false, $anchor = false) {
279 $this->_page = $page;
281 $this->_anchor = $anchor;
282 if ($label and $label != $page)
283 $this->_label = $label;
286 function _getType() {
290 function getPagename($basepage) {
291 $page = new WikiPageName($this->_page, $basepage);
292 if ($page->isValid()) return $page->name;
296 function getWikiPageLinks($basepage) {
297 if ($basepage == '') return false;
298 if ($link = $this->getPagename($basepage)) return array($link);
302 function _getName($basepage) {
303 return $this->getPagename($basepage);
306 function _getURL($basepage) {
307 return WikiURL($this->getPagename($basepage));
308 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
311 function expand($basepage, &$markup) {
312 $label = isset($this->_label) ? $this->_label : false;
313 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
314 $page = new WikiPageName($this->_page, $basepage, $anchor);
315 if ($page->isValid()) return WikiLink($page, 'auto', $label);
316 else return HTML($label);
320 $label = isset($this->_label) ? $this->_label : false;
321 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
322 $page = new WikiPageName($this->_page, false, $anchor);
323 $link = WikiLink($page, 'auto', $label);
324 return $link->asXml();
327 function asString() {
328 if (isset($this->_label))
329 return $this->_label;
334 class Cached_WikiLinkIfKnown extends Cached_WikiLink
336 function Cached_WikiLinkIfKnown ($moniker) {
337 $this->_page = $moniker;
340 function expand($basepage, &$markup) {
341 return WikiLink($this->_page, 'if_known');
345 class Cached_PhpwikiURL extends Cached_DynamicContent
347 function Cached_PhpwikiURL ($url, $label) {
350 $this->_label = $label;
353 function isInlineElement() {
357 function expand($basepage, &$markup) {
358 $label = isset($this->_label) ? $this->_label : false;
359 return LinkPhpwikiURL($this->_url, $label, $basepage);
363 $label = isset($this->_label) ? $this->_label : false;
364 $link = LinkPhpwikiURL($this->_url, $label);
365 return $link->asXml();
368 function asString() {
369 if (isset($this->_label))
370 return $this->_label;
375 class Cached_ExternalLink extends Cached_Link {
377 function Cached_ExternalLink($url, $label=false) {
379 if ($label && $label != $url)
380 $this->_label = $label;
383 function _getType() {
387 function _getName($basepage) {
388 $label = isset($this->_label) ? $this->_label : false;
389 return ($label and is_string($label)) ? $label : $this->_url;
392 function expand($basepage, &$markup) {
393 $label = isset($this->_label) ? $this->_label : false;
394 $link = LinkURL($this->_url, $label);
395 if (GOOGLE_LINKS_NOFOLLOW)
396 $link->setAttr('rel', 'nofollow');
400 function asString() {
401 if (isset($this->_label))
402 return $this->_label;
407 class Cached_InterwikiLink extends Cached_ExternalLink {
409 function Cached_InterwikiLink($link, $label=false) {
410 $this->_link = $link;
412 $this->_label = $label;
415 function _getName($basepage) {
416 $label = isset($this->_label) ? $this->_label : false;
417 return ($label and is_string($label)) ? $label : $link;
420 function _getURL($basepage) {
421 $link = $this->expand($basepage, $this);
422 return $link->getAttr('href');
425 function expand($basepage, &$markup) {
426 $intermap = getInterwikiMap();
427 $label = isset($this->_label) ? $this->_label : false;
428 return $intermap->link($this->_link, $label);
431 function asString() {
432 if (isset($this->_label))
433 return $this->_label;
438 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
439 // Thanks to PhpWiki:DanFr for finding this bug.
440 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
441 class Cached_UserLink extends Cached_WikiLink {
442 function expand($basepage, &$markup) {
443 $label = isset($this->_label) ? $this->_label : false;
444 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
445 $page = new WikiPageName($this->_page, $basepage, $anchor);
446 $link = WikiLink($page, 'auto', $label);
447 // $link = HTML::a(array('href' => $PageName));
448 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
449 $link->setAttr('class', 'wikiuser');
454 class Cached_PluginInvocation extends Cached_DynamicContent {
455 function Cached_PluginInvocation ($pi) {
459 function setTightness($top, $bottom) {
460 $this->_tightenable = 0;
461 if ($top) $this->_tightenable |= 1;
462 if ($bottom) $this->_tightenable |= 2;
465 function isInlineElement() {
469 function expand($basepage, &$markup) {
470 $loader = &$this->_getLoader();
472 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
473 $div = HTML::div(array('class' => 'plugin'));
474 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
475 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
477 if (isset($this->_tightenable)) {
478 if ($this->_tightenable == 3) {
479 $span = HTML::span(array('class' => 'plugin'), $xml);
481 $span->setAttr('id', $id);
484 $div->setInClass('tightenable');
485 $div->setInClass('top', ($this->_tightenable & 1) != 0);
486 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
489 $div->setAttr('id', $id);
490 $div->pushContent($xml);
494 function asString() {
499 function getWikiPageLinks($basepage) {
500 $loader = &$this->_getLoader();
502 return $loader->getWikiPageLinks($this->_pi, $basepage);
505 function _getLoader() {
506 static $loader = false;
509 include_once('lib/WikiPlugin.php');
510 $loader = new WikiPluginLoader;
516 // (c-file-style: "gnu")
521 // c-hanging-comment-ender-p: nil
522 // indent-tabs-mode: nil