1 <?php rcs_id('$Id: CachedMarkup.php,v 1.10 2004-03-02 18:11:39 rurban Exp $');
2 /* Copyright (C) 2002, Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * This file is part of PhpWiki.
6 * PhpWiki is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * PhpWiki is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with PhpWiki; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 class CacheableMarkup extends XmlContent {
23 function CacheableMarkup($content, $basepage) {
24 $this->_basepage = $basepage;
26 $this->_content = array();
27 $this->_append($content);
28 if ($this->_buf != '')
29 $this->_content[] = $this->_buf;
34 if (function_exists('gzcompress'))
35 return gzcompress(serialize($this), 9);
36 return serialize($this);
38 // FIXME: probably should implement some sort of "compression"
39 // when no gzcompress is available.
42 function unpack($packed) {
46 if (function_exists('gzcompress')) {
47 // ZLIB format has a five bit checksum in it's header.
48 // Lets check for sanity.
49 if ((ord($packed[0]) * 256 + ord($packed[1])) % 31 == 0) {
51 return unserialize(gzuncompress($packed));
54 if (substr($packed,0,2) == "O:") {
55 // Looks like a serialized object
56 return unserialize($packed);
58 trigger_error("Can't unpack bad cached markup", E_USER_WARNING);
62 /** Get names of wikipages linked to.
65 * A list of wiki page names (strings).
67 function getWikiPageLinks() {
68 include_once('lib/WikiPlugin.php');
69 $ploader = new WikiPluginLoader();
72 foreach ($this->_content as $item) {
73 if (!isa($item, 'Cached_DynamicContent'))
76 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
78 foreach ($item_links as $pagename)
82 return array_unique($links);
87 * This is here to support the XML-RPC listLinks() method.
90 * Returns an array of hashes.
92 function getLinkInfo() {
94 foreach ($this->_content as $link) {
95 if (! isa($link, 'Cached_Link'))
97 $info = $link->getLinkInfo($this->_basepage);
98 $links[$info->href] = $info;
100 return array_values($links);
103 function _append($item) {
104 if (is_array($item)) {
105 foreach ($item as $subitem)
106 $this->_append($subitem);
108 elseif (!is_object($item)) {
109 $this->_buf .= $this->_quote((string) $item);
111 elseif (isa($item, 'Cached_DynamicContent')) {
113 $this->_content[] = $this->_buf;
116 $this->_content[] = $item;
118 elseif (isa($item, 'XmlElement')) {
119 if ($item->isEmpty()) {
120 $this->_buf .= $item->emptyTag();
123 $this->_buf .= $item->startTag();
124 foreach ($item->getContent() as $subitem)
125 $this->_append($subitem);
126 $this->_buf .= "</$item->_tag>";
128 if (!isset($this->_description) and $item->getTag() == 'p')
129 $this->_glean_description($item->asString());
131 if (!$item->isInlineElement())
134 elseif (isa($item, 'XmlContent')) {
135 foreach ($item->getContent() as $item)
136 $this->_append($item);
138 elseif (method_exists($item, 'asxml')) {
139 $this->_buf .= $item->asXML();
141 elseif (method_exists($item, 'asstring')) {
142 $this->_buf .= $this->_quote($item->asString());
145 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
149 function _glean_description($text) {
150 static $two_sentences;
151 if (!$two_sentences) {
152 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
154 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
157 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
158 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
162 * Guess a short description of the page.
166 * This algorithm was suggested on MeatballWiki by
167 * Alex Schroeder <kensanata@yahoo.com>.
169 * Use the first paragraph in the page which contains at least two
172 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
176 function getDescription () {
177 return isset($this->_description) ? $this->_description : '';
182 $basepage = $this->_basepage;
184 foreach ($this->_content as $item) {
185 if (is_string($item)) {
188 elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
189 $val = $item->expand($basepage, &$this);
190 $xml .= $val->asXML();
193 $xml .= $item->asXML();
199 function printXML () {
200 $basepage = $this->_basepage;
201 // _content might be changed from a plugin (CreateToc)
202 for ($i=0; $i < count($this->_content); $i++) {
203 $item = $this->_content[$i];
204 if (is_string($item)) {
207 elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
208 // give the content the chance to know about itself or even
209 // to change itself itself
210 $val = $item->expand($basepage, &$this);
221 * The base class for all dynamic content.
223 * Dynamic content is anything that can change even when the original
224 * wiki-text from which it was parsed is unchanged.
226 class Cached_DynamicContent {
228 function cache(&$cache) {
232 function expand($basepage, $obj) {
233 trigger_error("Pure virtual", E_USER_ERROR);
236 function getWikiPageLinks($basepage) {
241 class XmlRpc_LinkInfo {
242 function XmlRpc_LinkInfo($page, $type, $href) {
249 class Cached_Link extends Cached_DynamicContent {
251 function isInlineElement() {
255 /** Get link info (for XML-RPC support)
257 * This is here to support the XML-RPC listLinks method.
258 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
260 function getLinkInfo($basepage) {
261 return new XmlRpc_LinkInfo($this->_getName($basepage),
263 $this->_getURL($basepage));
266 function _getURL($basepage) {
271 class Cached_WikiLink extends Cached_Link {
273 function Cached_WikiLink ($page, $label = false, $anchor = false) {
274 $this->_page = $page;
276 $this->_anchor = $anchor;
277 if ($label and $label != $page)
278 $this->_label = $label;
281 function _getType() {
285 function getPagename($basepage) {
286 $page = new WikiPageName($this->_page, $basepage);
290 function getWikiPageLinks($basepage) {
291 return array($this->getPagename($basepage));
294 function _getName($basepage) {
295 return $this->getPagename($basepage);
298 function _getURL($basepage) {
299 return WikiURL($this->getPagename($basepage), false, 'abs_url');
302 function expand($basepage, &$markup) {
303 $label = isset($this->_label) ? $this->_label : false;
304 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
305 $page = new WikiPageName($this->_page, $basepage, $anchor);
306 return WikiLink($page, 'auto', $label);
309 function asString() {
310 if (isset($this->_label))
311 return $this->_label;
316 class Cached_WikiLinkIfKnown extends Cached_WikiLink
318 function Cached_WikiLinkIfKnown ($moniker) {
319 $this->_page = $moniker;
322 function expand($basepage, &$markup) {
323 return WikiLink($this->_page, 'if_known');
327 class Cached_PhpwikiURL extends Cached_DynamicContent
329 function Cached_PhpwikiURL ($url, $label) {
332 $this->_label = $label;
335 function isInlineElement() {
339 function expand($basepage, &$markup) {
340 $label = isset($this->_label) ? $this->_label : false;
341 return LinkPhpwikiURL($this->_url, $label, $basepage);
344 function asString() {
345 if (isset($this->_label))
346 return $this->_label;
351 class Cached_ExternalLink extends Cached_Link {
353 function Cached_ExternalLink($url, $label=false) {
355 if ($label && $label != $url)
356 $this->_label = $label;
359 function _getType() {
363 function _getName($basepage) {
364 $label = isset($this->_label) ? $this->_label : false;
365 return ($label and is_string($label)) ? $label : $this->_url;
368 function expand($basepage, &$markup) {
369 $label = isset($this->_label) ? $this->_label : false;
370 return LinkURL($this->_url, $label);
373 function asString() {
374 if (isset($this->_label))
375 return $this->_label;
380 class Cached_InterwikiLink extends Cached_ExternalLink {
382 function Cached_InterwikiLink($link, $label=false) {
383 $this->_link = $link;
385 $this->_label = $label;
388 function _getName($basepage) {
389 $label = isset($this->_label) ? $this->_label : false;
390 return ($label and is_string($label)) ? $label : $link;
393 function _getURL($basepage) {
394 $link = $this->expand($basepage, &$this);
395 return $link->getAttr('href');
398 function expand($basepage, &$markup) {
399 //include_once('lib/interwiki.php');
400 $intermap = PageType_interwikimap::GetMap($GLOBALS['request']);
401 $label = isset($this->_label) ? $this->_label : false;
402 return $intermap->link($this->_link, $label);
405 function asString() {
406 if (isset($this->_label))
407 return $this->_label;
414 class Cached_PluginInvocation extends Cached_DynamicContent {
415 function Cached_PluginInvocation ($pi) {
419 function setTightness($top, $bottom) {
420 $this->_tightenable = 0;
421 if ($top) $this->_tightenable |= 1;
422 if ($bottom) $this->_tightenable |= 2;
425 function isInlineElement() {
429 function expand($basepage, &$markup) {
430 $loader = &$this->_getLoader();
432 $xml = HTML::div(array('class' => 'plugin'),
433 $loader->expandPI($this->_pi, $GLOBALS['request'], &$markup, $basepage));
435 if (isset($this->_tightenable)) {
436 $xml->setInClass('tightenable');
437 $xml->setInClass('top', ($this->_tightenable & 1) != 0);
438 $xml->setInClass('bottom', ($this->_tightenable & 2) != 0);
444 function asString() {
449 function getWikiPageLinks($basepage) {
450 $loader = &$this->_getLoader();
452 return $loader->getWikiPageLinks($this->_pi, $basepage);
455 function _getLoader() {
456 static $loader = false;
459 include_once('lib/WikiPlugin.php');
460 $loader = new WikiPluginLoader;
466 // (c-file-style: "gnu")
471 // c-hanging-comment-ender-p: nil
472 // indent-tabs-mode: nil