2 rcs_id('$Id: CachedMarkup.php,v 1.27 2004-12-17 16:12:09 rurban Exp $');
3 /* Copyright (C) 2002, Geoffrey T. Dairiki <dairiki@dairiki.org>
5 * This file is part of PhpWiki.
7 * PhpWiki is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * PhpWiki is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with PhpWiki; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 class CacheableMarkup extends XmlContent {
24 function CacheableMarkup($content, $basepage) {
25 $this->_basepage = $basepage;
27 $this->_content = array();
28 $this->_append($content);
29 if ($this->_buf != '')
30 $this->_content[] = $this->_buf;
35 if (function_exists('gzcompress'))
36 return gzcompress(serialize($this), 9);
37 return serialize($this);
39 // FIXME: probably should implement some sort of "compression"
40 // when no gzcompress is available.
43 function unpack($packed) {
47 if (function_exists('gzcompress')) {
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) {
52 return unserialize(gzuncompress($packed));
55 if (substr($packed,0,2) == "O:") {
56 // Looks like a serialized object
57 return unserialize($packed);
59 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.", E_USER_WARNING);
63 /** Get names of wikipages linked to.
66 * A list of wiki page names (strings).
68 function getWikiPageLinks() {
69 include_once('lib/WikiPlugin.php');
70 $ploader = new WikiPluginLoader();
73 foreach ($this->_content as $item) {
74 if (!isa($item, 'Cached_DynamicContent'))
77 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
79 foreach ($item_links as $pagename)
80 if (is_string($pagename) and $pagename != '')
84 return array_unique($links);
89 * This is here to support the XML-RPC listLinks() method.
92 * Returns an array of hashes.
94 function getLinkInfo() {
96 foreach ($this->_content as $link) {
97 if (! isa($link, 'Cached_Link'))
99 $info = $link->getLinkInfo($this->_basepage);
100 $links[$info->href] = $info;
102 return array_values($links);
105 function _append($item) {
106 if (is_array($item)) {
107 foreach ($item as $subitem)
108 $this->_append($subitem);
110 elseif (!is_object($item)) {
111 $this->_buf .= $this->_quote((string) $item);
113 elseif (isa($item, 'Cached_DynamicContent')) {
115 $this->_content[] = $this->_buf;
118 $this->_content[] = $item;
120 elseif (isa($item, 'XmlElement')) {
121 if ($item->isEmpty()) {
122 $this->_buf .= $item->emptyTag();
125 $this->_buf .= $item->startTag();
126 foreach ($item->getContent() as $subitem)
127 $this->_append($subitem);
128 $this->_buf .= "</$item->_tag>";
130 if (!isset($this->_description) and $item->getTag() == 'p')
131 $this->_glean_description($item->asString());
133 if (!$item->isInlineElement())
136 elseif (isa($item, 'XmlContent')) {
137 foreach ($item->getContent() as $item)
138 $this->_append($item);
140 elseif (method_exists($item, 'asxml')) {
141 $this->_buf .= $item->asXML();
143 elseif (method_exists($item, 'asstring')) {
144 $this->_buf .= $this->_quote($item->asString());
147 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
151 function _glean_description($text) {
152 static $two_sentences;
153 if (!$two_sentences) {
154 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
156 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
159 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
160 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
164 * Guess a short description of the page.
168 * This algorithm was suggested on MeatballWiki by
169 * Alex Schroeder <kensanata@yahoo.com>.
171 * Use the first paragraph in the page which contains at least two
174 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
178 function getDescription () {
179 return isset($this->_description) ? $this->_description : '';
184 $basepage = $this->_basepage;
186 foreach ($this->_content as $item) {
187 if (is_string($item)) {
190 elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
191 $val = $item->expand($basepage, $this);
192 $xml .= $val->asXML();
195 $xml .= $item->asXML();
201 function printXML () {
202 $basepage = $this->_basepage;
203 // _content might be changed from a plugin (CreateToc)
204 for ($i=0; $i < count($this->_content); $i++) {
205 $item = $this->_content[$i];
206 if (is_string($item)) {
209 elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
210 // give the content the chance to know about itself or even
212 $val = $item->expand($basepage, $this);
223 * The base class for all dynamic content.
225 * Dynamic content is anything that can change even when the original
226 * wiki-text from which it was parsed is unchanged.
228 class Cached_DynamicContent {
230 function cache(&$cache) {
234 function expand($basepage, &$obj) {
235 trigger_error("Pure virtual", E_USER_ERROR);
238 function getWikiPageLinks($basepage) {
243 class XmlRpc_LinkInfo {
244 function XmlRpc_LinkInfo($page, $type, $href) {
248 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
252 class Cached_Link extends Cached_DynamicContent {
254 function isInlineElement() {
258 /** Get link info (for XML-RPC support)
260 * This is here to support the XML-RPC listLinks method.
261 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
263 function getLinkInfo($basepage) {
264 return new XmlRpc_LinkInfo($this->_getName($basepage),
266 $this->_getURL($basepage));
269 function _getURL($basepage) {
274 class Cached_WikiLink extends Cached_Link {
276 function Cached_WikiLink ($page, $label = false, $anchor = false) {
277 $this->_page = $page;
279 $this->_anchor = $anchor;
280 if ($label and $label != $page)
281 $this->_label = $label;
284 function _getType() {
288 function getPagename($basepage) {
289 $page = new WikiPageName($this->_page, $basepage);
290 if ($page->isValid()) return $page->name;
294 function getWikiPageLinks($basepage) {
295 if ($basepage == '') return false;
296 if ($link = $this->getPagename($basepage)) return array($link);
300 function _getName($basepage) {
301 return $this->getPagename($basepage);
304 function _getURL($basepage) {
305 return WikiURL($this->getPagename($basepage));
306 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
309 function expand($basepage, &$markup) {
310 $label = isset($this->_label) ? $this->_label : false;
311 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
312 $page = new WikiPageName($this->_page, $basepage, $anchor);
313 if ($page->isValid()) return WikiLink($page, 'auto', $label);
314 else return HTML($label);
318 $label = isset($this->_label) ? $this->_label : false;
319 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
320 $page = new WikiPageName($this->_page, false, $anchor);
321 $link = WikiLink($page, 'auto', $label);
322 return $link->asXml();
325 function asString() {
326 if (isset($this->_label))
327 return $this->_label;
332 class Cached_WikiLinkIfKnown extends Cached_WikiLink
334 function Cached_WikiLinkIfKnown ($moniker) {
335 $this->_page = $moniker;
338 function expand($basepage, &$markup) {
339 return WikiLink($this->_page, 'if_known');
343 class Cached_PhpwikiURL extends Cached_DynamicContent
345 function Cached_PhpwikiURL ($url, $label) {
348 $this->_label = $label;
351 function isInlineElement() {
355 function expand($basepage, &$markup) {
356 $label = isset($this->_label) ? $this->_label : false;
357 return LinkPhpwikiURL($this->_url, $label, $basepage);
361 $label = isset($this->_label) ? $this->_label : false;
362 $link = LinkPhpwikiURL($this->_url, $label);
363 return $link->asXml();
366 function asString() {
367 if (isset($this->_label))
368 return $this->_label;
373 class Cached_ExternalLink extends Cached_Link {
375 function Cached_ExternalLink($url, $label=false) {
377 if ($label && $label != $url)
378 $this->_label = $label;
381 function _getType() {
385 function _getName($basepage) {
386 $label = isset($this->_label) ? $this->_label : false;
387 return ($label and is_string($label)) ? $label : $this->_url;
390 function expand($basepage, &$markup) {
391 $label = isset($this->_label) ? $this->_label : false;
392 return LinkURL($this->_url, $label);
395 function asString() {
396 if (isset($this->_label))
397 return $this->_label;
402 class Cached_InterwikiLink extends Cached_ExternalLink {
404 function Cached_InterwikiLink($link, $label=false) {
405 $this->_link = $link;
407 $this->_label = $label;
410 function _getName($basepage) {
411 $label = isset($this->_label) ? $this->_label : false;
412 return ($label and is_string($label)) ? $label : $link;
415 function _getURL($basepage) {
416 $link = $this->expand($basepage, $this);
417 return $link->getAttr('href');
420 function expand($basepage, &$markup) {
421 $intermap = getInterwikiMap();
422 $label = isset($this->_label) ? $this->_label : false;
423 return $intermap->link($this->_link, $label);
426 function asString() {
427 if (isset($this->_label))
428 return $this->_label;
433 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
434 // Thanks to PhpWiki:DanFr for finding this bug.
435 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
436 class Cached_UserLink extends Cached_WikiLink {
437 function expand($basepage, &$markup) {
438 $label = isset($this->_label) ? $this->_label : false;
439 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
440 $page = new WikiPageName($this->_page, $basepage, $anchor);
441 $link = WikiLink($page, 'auto', $label);
442 // $link = HTML::a(array('href' => $PageName));
443 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
444 $link->setAttr('class', 'wikiuser');
449 class Cached_PluginInvocation extends Cached_DynamicContent {
450 function Cached_PluginInvocation ($pi) {
454 function setTightness($top, $bottom) {
455 $this->_tightenable = 0;
456 if ($top) $this->_tightenable |= 1;
457 if ($bottom) $this->_tightenable |= 2;
460 function isInlineElement() {
464 function expand($basepage, &$markup) {
465 $loader = &$this->_getLoader();
467 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
468 $div = HTML::div(array('class' => 'plugin'));
469 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
470 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
472 if (isset($this->_tightenable)) {
473 if ($this->_tightenable == 3) {
474 $span = HTML::span(array('class' => 'plugin'), $xml);
476 $span->setAttr('id', $id);
479 $div->setInClass('tightenable');
480 $div->setInClass('top', ($this->_tightenable & 1) != 0);
481 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
484 $div->setAttr('id', $id);
485 $div->pushContent($xml);
489 function asString() {
494 function getWikiPageLinks($basepage) {
495 $loader = &$this->_getLoader();
497 return $loader->getWikiPageLinks($this->_pi, $basepage);
500 function _getLoader() {
501 static $loader = false;
504 include_once('lib/WikiPlugin.php');
505 $loader = new WikiPluginLoader;
511 // (c-file-style: "gnu")
516 // c-hanging-comment-ender-p: nil
517 // indent-tabs-mode: nil