2 rcs_id('$Id: CachedMarkup.php,v 1.23 2004-09-14 10:06:42 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
211 // to change itself itself
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) {
251 class Cached_Link extends Cached_DynamicContent {
253 function isInlineElement() {
257 /** Get link info (for XML-RPC support)
259 * This is here to support the XML-RPC listLinks method.
260 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
262 function getLinkInfo($basepage) {
263 return new XmlRpc_LinkInfo($this->_getName($basepage),
265 $this->_getURL($basepage));
268 function _getURL($basepage) {
273 class Cached_WikiLink extends Cached_Link {
275 function Cached_WikiLink ($page, $label = false, $anchor = false) {
276 $this->_page = $page;
278 $this->_anchor = $anchor;
279 if ($label and $label != $page)
280 $this->_label = $label;
283 function _getType() {
287 function getPagename($basepage) {
288 $page = new WikiPageName($this->_page, $basepage);
289 if ($page->isValid()) return $page->name;
293 function getWikiPageLinks($basepage) {
294 if ($basepage == '') return false;
295 if ($link = $this->getPagename($basepage)) return array($link);
299 function _getName($basepage) {
300 return $this->getPagename($basepage);
303 function _getURL($basepage) {
304 return WikiURL($this->getPagename($basepage), false, 'abs_url');
307 function expand($basepage, &$markup) {
308 $label = isset($this->_label) ? $this->_label : false;
309 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
310 $page = new WikiPageName($this->_page, $basepage, $anchor);
311 if ($page->isValid()) return WikiLink($page, 'auto', $label);
312 else return HTML($label);
316 $label = isset($this->_label) ? $this->_label : false;
317 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
318 $page = new WikiPageName($this->_page, false, $anchor);
319 $link = WikiLink($page, 'auto', $label);
320 return $link->asXml();
323 function asString() {
324 if (isset($this->_label))
325 return $this->_label;
330 class Cached_WikiLinkIfKnown extends Cached_WikiLink
332 function Cached_WikiLinkIfKnown ($moniker) {
333 $this->_page = $moniker;
336 function expand($basepage, &$markup) {
337 return WikiLink($this->_page, 'if_known');
341 class Cached_PhpwikiURL extends Cached_DynamicContent
343 function Cached_PhpwikiURL ($url, $label) {
346 $this->_label = $label;
349 function isInlineElement() {
353 function expand($basepage, &$markup) {
354 $label = isset($this->_label) ? $this->_label : false;
355 return LinkPhpwikiURL($this->_url, $label, $basepage);
359 $label = isset($this->_label) ? $this->_label : false;
360 $link = LinkPhpwikiURL($this->_url, $label);
361 return $link->asXml();
364 function asString() {
365 if (isset($this->_label))
366 return $this->_label;
371 class Cached_ExternalLink extends Cached_Link {
373 function Cached_ExternalLink($url, $label=false) {
375 if ($label && $label != $url)
376 $this->_label = $label;
379 function _getType() {
383 function _getName($basepage) {
384 $label = isset($this->_label) ? $this->_label : false;
385 return ($label and is_string($label)) ? $label : $this->_url;
388 function expand($basepage, &$markup) {
389 $label = isset($this->_label) ? $this->_label : false;
390 return LinkURL($this->_url, $label);
393 function asString() {
394 if (isset($this->_label))
395 return $this->_label;
400 class Cached_InterwikiLink extends Cached_ExternalLink {
402 function Cached_InterwikiLink($link, $label=false) {
403 $this->_link = $link;
405 $this->_label = $label;
408 function _getName($basepage) {
409 $label = isset($this->_label) ? $this->_label : false;
410 return ($label and is_string($label)) ? $label : $link;
413 function _getURL($basepage) {
414 $link = $this->expand($basepage, $this);
415 return $link->getAttr('href');
418 function expand($basepage, &$markup) {
419 //include_once('lib/interwiki.php');
420 $intermap = getInterwikiMap($GLOBALS['request']);
421 $label = isset($this->_label) ? $this->_label : false;
422 return $intermap->link($this->_link, $label);
425 function asString() {
426 if (isset($this->_label))
427 return $this->_label;
432 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
433 // Thanks to PhpWiki:DanFr for finding this bug.
434 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
435 class Cached_UserLink extends Cached_WikiLink {
436 function expand($basepage, &$markup) {
437 $label = isset($this->_label) ? $this->_label : false;
438 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
439 $page = new WikiPageName($this->_page, $basepage, $anchor);
440 $link = WikiLink($page, 'auto', $label);
441 // $link = HTML::a(array('href' => $PageName));
442 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
443 $link->setAttr('class', 'wikiuser');
448 class Cached_PluginInvocation extends Cached_DynamicContent {
449 function Cached_PluginInvocation ($pi) {
453 function setTightness($top, $bottom) {
454 $this->_tightenable = 0;
455 if ($top) $this->_tightenable |= 1;
456 if ($bottom) $this->_tightenable |= 2;
459 function isInlineElement() {
463 function expand($basepage, &$markup) {
464 $loader = &$this->_getLoader();
466 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
467 $div = HTML::div(array('class' => 'plugin'));
468 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
469 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
471 if (isset($this->_tightenable)) {
472 if ($this->_tightenable == 3) {
473 $span = HTML::span(array('class' => 'plugin'), $xml);
475 $span->setAttr('id', $id);
478 $div->setInClass('tightenable');
479 $div->setInClass('top', ($this->_tightenable & 1) != 0);
480 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
483 $div->setAttr('id', $id);
484 $div->pushContent($xml);
488 function asString() {
493 function getWikiPageLinks($basepage) {
494 $loader = &$this->_getLoader();
496 return $loader->getWikiPageLinks($this->_pi, $basepage);
499 function _getLoader() {
500 static $loader = false;
503 include_once('lib/WikiPlugin.php');
504 $loader = new WikiPluginLoader;
510 // (c-file-style: "gnu")
515 // c-hanging-comment-ender-p: nil
516 // indent-tabs-mode: nil