1 <?php rcs_id('$Id: CachedMarkup.php,v 1.7 2003-03-25 21:04:41 dairiki 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);
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);
190 $xml .= $val->asXML();
193 $xml .= $item->asXML();
199 function printXML () {
200 $basepage = $this->_basepage;
202 foreach ($this->_content as $item) {
203 if (is_string($item)) {
206 elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
207 $val = $item->expand($basepage);
218 * The base class for all dynamic content.
220 * Dynamic content is anything that can change even when the original
221 * wiki-text from which it was parsed is unchanged.
223 class Cached_DynamicContent {
225 function cache(&$cache) {
229 function expand($basepage) {
230 trigger_error("Pure virtual", E_USER_ERROR);
233 function getWikiPageLinks($basepage) {
238 class XmlRpc_LinkInfo {
239 function XmlRpc_LinkInfo($page, $type, $href) {
246 class Cached_Link extends Cached_DynamicContent {
248 function isInlineElement() {
252 /** Get link info (for XML-RPC support)
254 * This is here to support the XML-RPC listLinks method.
255 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
257 function getLinkInfo($basepage) {
258 return new XmlRpc_LinkInfo($this->_getName($basepage),
260 $this->_getURL($basepage));
263 function _getURL($basepage) {
268 class Cached_WikiLink extends Cached_Link {
270 function Cached_WikiLink ($page, $label = false, $anchor = false) {
271 $this->_page = $page;
273 $this->_anchor = $anchor;
274 if ($label and $label != $page)
275 $this->_label = $label;
278 function _getType() {
282 function getPagename($basepage) {
283 $page = new WikiPageName($this->_page, $basepage);
287 function getWikiPageLinks($basepage) {
288 return array($this->getPagename($basepage));
291 function _getName($basepage) {
292 return $this->getPagename($basepage);
295 function _getURL($basepage) {
296 return WikiURL($this->getPagename($basepage), false, 'abs_url');
299 function expand($basepage) {
300 $label = isset($this->_label) ? $this->_label : false;
301 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
302 $page = new WikiPageName($this->_page, $basepage, $anchor);
303 return WikiLink($page, 'auto', $label);
306 function asString() {
307 if (isset($this->_label))
308 return $this->_label;
313 class Cached_WikiLinkIfKnown extends Cached_WikiLink
315 function Cached_WikiLinkIfKnown ($moniker) {
316 $this->_page = $moniker;
319 function expand($basepage) {
320 return WikiLink($this->_page, 'if_known');
324 class Cached_PhpwikiURL extends Cached_DynamicContent
326 function Cached_PhpwikiURL ($url, $label) {
329 $this->_label = $label;
332 function isInlineElement() {
336 function expand($basepage) {
337 $label = isset($this->_label) ? $this->_label : false;
338 return LinkPhpwikiURL($this->_url, $label, $basepage);
341 function asString() {
342 if (isset($this->_label))
343 return $this->_label;
348 class Cached_ExternalLink extends Cached_Link {
350 function Cached_ExternalLink($url, $label=false) {
352 if ($label && $label != $url)
353 $this->_label = $label;
356 function _getType() {
360 function _getName($basepage) {
361 $label = isset($this->_label) ? $this->_label : false;
362 return ($label and is_string($label)) ? $label : $this->_url;
365 function expand($basepage) {
366 $label = isset($this->_label) ? $this->_label : false;
367 return LinkURL($this->_url, $label);
370 function asString() {
371 if (isset($this->_label))
372 return $this->_label;
377 class Cached_InterwikiLink extends Cached_ExternalLink {
379 function Cached_InterwikiLink($link, $label=false) {
380 $this->_link = $link;
382 $this->_label = $label;
385 function _getName($basepage) {
386 $label = isset($this->_label) ? $this->_label : false;
387 return ($label and is_string($label)) ? $label : $link;
390 function _getURL($basepage) {
391 $link = $this->expand($basepage);
392 return $link->getAttr('href');
395 function expand($basepage) {
396 include_once('lib/interwiki.php');
397 $intermap = InterWikiMap::GetMap($GLOBALS['request']);
398 $label = isset($this->_label) ? $this->_label : false;
399 return $intermap->link($this->_link, $label);
402 function asString() {
403 if (isset($this->_label))
404 return $this->_label;
411 class Cached_PluginInvocation extends Cached_DynamicContent {
412 function Cached_PluginInvocation ($pi) {
416 function setTightness($top, $bottom) {
417 $this->_tightenable = 0;
418 if ($top) $this->_tightenable |= 1;
419 if ($bottom) $this->_tightenable |= 2;
422 function isInlineElement() {
426 function expand($basepage) {
427 $loader = &$this->_getLoader();
429 $xml = HTML::div(array('class' => 'plugin'),
430 $loader->expandPI($this->_pi, $GLOBALS['request'], $basepage));
432 if (isset($this->_tightenable)) {
433 $xml->setInClass('tightenable');
434 $xml->setInClass('top', ($this->_tightenable & 1) != 0);
435 $xml->setInClass('bottom', ($this->_tightenable & 2) != 0);
441 function asString() {
446 function getWikiPageLinks($basepage) {
447 $loader = &$this->_getLoader();
449 return $loader->getWikiPageLinks($this->_pi, $basepage);
452 function _getLoader() {
453 static $loader = false;
456 include_once('lib/WikiPlugin.php');
457 $loader = new WikiPluginLoader;
463 // (c-file-style: "gnu")
468 // c-hanging-comment-ender-p: nil
469 // indent-tabs-mode: nil