2 rcs_id('$Id: CachedMarkup.php,v 1.29 2004-12-26 17:08:35 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.",
64 /** Get names of wikipages linked to.
67 * A list of wiki page names (strings).
69 function getWikiPageLinks() {
70 include_once('lib/WikiPlugin.php');
71 $ploader = new WikiPluginLoader();
74 foreach ($this->_content as $item) {
75 if (!isa($item, 'Cached_DynamicContent'))
78 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
80 foreach ($item_links as $pagename)
81 if (is_string($pagename) and $pagename != '')
85 return array_unique($links);
90 * This is here to support the XML-RPC listLinks() method.
93 * Returns an array of hashes.
95 function getLinkInfo() {
97 foreach ($this->_content as $link) {
98 if (! isa($link, 'Cached_Link'))
100 $info = $link->getLinkInfo($this->_basepage);
101 $links[$info->href] = $info;
103 return array_values($links);
106 function _append($item) {
107 if (is_array($item)) {
108 foreach ($item as $subitem)
109 $this->_append($subitem);
111 elseif (!is_object($item)) {
112 $this->_buf .= $this->_quote((string) $item);
114 elseif (isa($item, 'Cached_DynamicContent')) {
116 $this->_content[] = $this->_buf;
119 $this->_content[] = $item;
121 elseif (isa($item, 'XmlElement')) {
122 if ($item->isEmpty()) {
123 $this->_buf .= $item->emptyTag();
126 $this->_buf .= $item->startTag();
127 foreach ($item->getContent() as $subitem)
128 $this->_append($subitem);
129 $this->_buf .= "</$item->_tag>";
131 if (!isset($this->_description) and $item->getTag() == 'p')
132 $this->_glean_description($item->asString());
134 if (!$item->isInlineElement())
137 elseif (isa($item, 'XmlContent')) {
138 foreach ($item->getContent() as $item)
139 $this->_append($item);
141 elseif (method_exists($item, 'asXML')) {
142 $this->_buf .= $item->asXML();
144 elseif (method_exists($item, 'asString')) {
145 $this->_buf .= $this->_quote($item->asString());
148 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
152 function _glean_description($text) {
153 static $two_sentences;
154 if (!$two_sentences) {
155 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
157 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
160 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
161 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
165 * Guess a short description of the page.
169 * This algorithm was suggested on MeatballWiki by
170 * Alex Schroeder <kensanata@yahoo.com>.
172 * Use the first paragraph in the page which contains at least two
175 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
179 function getDescription () {
180 return isset($this->_description) ? $this->_description : '';
185 $basepage = $this->_basepage;
187 foreach ($this->_content as $item) {
188 if (is_string($item)) {
191 elseif (is_subclass_of($item, check_php_version(5) ? 'Cached_DynamicContent' : 'cached_dynamiccontent')) {
192 $val = $item->expand($basepage, $this);
193 $xml .= $val->asXML();
196 $xml .= $item->asXML();
202 function printXML () {
203 $basepage = $this->_basepage;
204 // _content might be changed from a plugin (CreateToc)
205 for ($i=0; $i < count($this->_content); $i++) {
206 $item = $this->_content[$i];
207 if (is_string($item)) {
210 elseif (is_subclass_of($item, check_php_version(5) ? 'Cached_DynamicContent' : 'cached_dynamiccontent')) {
211 // give the content the chance to know about itself or even
213 $val = $item->expand($basepage, $this);
224 * The base class for all dynamic content.
226 * Dynamic content is anything that can change even when the original
227 * wiki-text from which it was parsed is unchanged.
229 class Cached_DynamicContent {
231 function cache(&$cache) {
235 function expand($basepage, &$obj) {
236 trigger_error("Pure virtual", E_USER_ERROR);
239 function getWikiPageLinks($basepage) {
244 class XmlRpc_LinkInfo {
245 function XmlRpc_LinkInfo($page, $type, $href) {
249 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
253 class Cached_Link extends Cached_DynamicContent {
255 function isInlineElement() {
259 /** Get link info (for XML-RPC support)
261 * This is here to support the XML-RPC listLinks method.
262 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
264 function getLinkInfo($basepage) {
265 return new XmlRpc_LinkInfo($this->_getName($basepage),
267 $this->_getURL($basepage));
270 function _getURL($basepage) {
275 class Cached_WikiLink extends Cached_Link {
277 function Cached_WikiLink ($page, $label = false, $anchor = false) {
278 $this->_page = $page;
280 $this->_anchor = $anchor;
281 if ($label and $label != $page)
282 $this->_label = $label;
285 function _getType() {
289 function getPagename($basepage) {
290 $page = new WikiPageName($this->_page, $basepage);
291 if ($page->isValid()) return $page->name;
295 function getWikiPageLinks($basepage) {
296 if ($basepage == '') return false;
297 if ($link = $this->getPagename($basepage)) return array($link);
301 function _getName($basepage) {
302 return $this->getPagename($basepage);
305 function _getURL($basepage) {
306 return WikiURL($this->getPagename($basepage));
307 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
310 function expand($basepage, &$markup) {
311 $label = isset($this->_label) ? $this->_label : false;
312 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
313 $page = new WikiPageName($this->_page, $basepage, $anchor);
314 if ($page->isValid()) return WikiLink($page, 'auto', $label);
315 else return HTML($label);
319 $label = isset($this->_label) ? $this->_label : false;
320 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
321 $page = new WikiPageName($this->_page, false, $anchor);
322 $link = WikiLink($page, 'auto', $label);
323 return $link->asXml();
326 function asString() {
327 if (isset($this->_label))
328 return $this->_label;
333 class Cached_WikiLinkIfKnown extends Cached_WikiLink
335 function Cached_WikiLinkIfKnown ($moniker) {
336 $this->_page = $moniker;
339 function expand($basepage, &$markup) {
340 return WikiLink($this->_page, 'if_known');
344 class Cached_PhpwikiURL extends Cached_DynamicContent
346 function Cached_PhpwikiURL ($url, $label) {
349 $this->_label = $label;
352 function isInlineElement() {
356 function expand($basepage, &$markup) {
357 $label = isset($this->_label) ? $this->_label : false;
358 return LinkPhpwikiURL($this->_url, $label, $basepage);
362 $label = isset($this->_label) ? $this->_label : false;
363 $link = LinkPhpwikiURL($this->_url, $label);
364 return $link->asXml();
367 function asString() {
368 if (isset($this->_label))
369 return $this->_label;
374 class Cached_ExternalLink extends Cached_Link {
376 function Cached_ExternalLink($url, $label=false) {
378 if ($label && $label != $url)
379 $this->_label = $label;
382 function _getType() {
386 function _getName($basepage) {
387 $label = isset($this->_label) ? $this->_label : false;
388 return ($label and is_string($label)) ? $label : $this->_url;
391 function expand($basepage, &$markup) {
392 $label = isset($this->_label) ? $this->_label : false;
393 return LinkURL($this->_url, $label);
396 function asString() {
397 if (isset($this->_label))
398 return $this->_label;
403 class Cached_InterwikiLink extends Cached_ExternalLink {
405 function Cached_InterwikiLink($link, $label=false) {
406 $this->_link = $link;
408 $this->_label = $label;
411 function _getName($basepage) {
412 $label = isset($this->_label) ? $this->_label : false;
413 return ($label and is_string($label)) ? $label : $link;
416 function _getURL($basepage) {
417 $link = $this->expand($basepage, $this);
418 return $link->getAttr('href');
421 function expand($basepage, &$markup) {
422 $intermap = getInterwikiMap();
423 $label = isset($this->_label) ? $this->_label : false;
424 return $intermap->link($this->_link, $label);
427 function asString() {
428 if (isset($this->_label))
429 return $this->_label;
434 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
435 // Thanks to PhpWiki:DanFr for finding this bug.
436 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
437 class Cached_UserLink extends Cached_WikiLink {
438 function expand($basepage, &$markup) {
439 $label = isset($this->_label) ? $this->_label : false;
440 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
441 $page = new WikiPageName($this->_page, $basepage, $anchor);
442 $link = WikiLink($page, 'auto', $label);
443 // $link = HTML::a(array('href' => $PageName));
444 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
445 $link->setAttr('class', 'wikiuser');
450 class Cached_PluginInvocation extends Cached_DynamicContent {
451 function Cached_PluginInvocation ($pi) {
455 function setTightness($top, $bottom) {
456 $this->_tightenable = 0;
457 if ($top) $this->_tightenable |= 1;
458 if ($bottom) $this->_tightenable |= 2;
461 function isInlineElement() {
465 function expand($basepage, &$markup) {
466 $loader = &$this->_getLoader();
468 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
469 $div = HTML::div(array('class' => 'plugin'));
470 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
471 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
473 if (isset($this->_tightenable)) {
474 if ($this->_tightenable == 3) {
475 $span = HTML::span(array('class' => 'plugin'), $xml);
477 $span->setAttr('id', $id);
480 $div->setInClass('tightenable');
481 $div->setInClass('top', ($this->_tightenable & 1) != 0);
482 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
485 $div->setAttr('id', $id);
486 $div->pushContent($xml);
490 function asString() {
495 function getWikiPageLinks($basepage) {
496 $loader = &$this->_getLoader();
498 return $loader->getWikiPageLinks($this->_pi, $basepage);
501 function _getLoader() {
502 static $loader = false;
505 include_once('lib/WikiPlugin.php');
506 $loader = new WikiPluginLoader;
512 // (c-file-style: "gnu")
517 // c-hanging-comment-ender-p: nil
518 // indent-tabs-mode: nil