1 <?php rcs_id('$Id: CachedMarkup.php,v 1.14 2004-05-01 15:59:29 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. Probably php_zlib extension not loaded.", 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);
310 $label = isset($this->_label) ? $this->_label : false;
311 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
312 $page = new WikiPageName($this->_page, false, $anchor);
313 $link = WikiLink($page, 'auto', $label);
314 return $link->asXml();
317 function asString() {
318 if (isset($this->_label))
319 return $this->_label;
324 class Cached_WikiLinkIfKnown extends Cached_WikiLink
326 function Cached_WikiLinkIfKnown ($moniker) {
327 $this->_page = $moniker;
330 function expand($basepage, &$markup) {
331 return WikiLink($this->_page, 'if_known');
335 class Cached_PhpwikiURL extends Cached_DynamicContent
337 function Cached_PhpwikiURL ($url, $label) {
340 $this->_label = $label;
343 function isInlineElement() {
347 function expand($basepage, &$markup) {
348 $label = isset($this->_label) ? $this->_label : false;
349 return LinkPhpwikiURL($this->_url, $label, $basepage);
353 $label = isset($this->_label) ? $this->_label : false;
354 $link = LinkPhpwikiURL($this->_url, $label);
355 return $link->asXml();
358 function asString() {
359 if (isset($this->_label))
360 return $this->_label;
365 class Cached_ExternalLink extends Cached_Link {
367 function Cached_ExternalLink($url, $label=false) {
369 if ($label && $label != $url)
370 $this->_label = $label;
373 function _getType() {
377 function _getName($basepage) {
378 $label = isset($this->_label) ? $this->_label : false;
379 return ($label and is_string($label)) ? $label : $this->_url;
382 function expand($basepage, &$markup) {
383 $label = isset($this->_label) ? $this->_label : false;
384 return LinkURL($this->_url, $label);
387 function asString() {
388 if (isset($this->_label))
389 return $this->_label;
394 class Cached_InterwikiLink extends Cached_ExternalLink {
396 function Cached_InterwikiLink($link, $label=false) {
397 $this->_link = $link;
399 $this->_label = $label;
402 function _getName($basepage) {
403 $label = isset($this->_label) ? $this->_label : false;
404 return ($label and is_string($label)) ? $label : $link;
407 function _getURL($basepage) {
408 $link = $this->expand($basepage, &$this);
409 return $link->getAttr('href');
412 function expand($basepage, &$markup) {
413 //include_once('lib/interwiki.php');
414 $intermap = getInterwikiMap($GLOBALS['request']);
415 $label = isset($this->_label) ? $this->_label : false;
416 return $intermap->link($this->_link, $label);
419 function asString() {
420 if (isset($this->_label))
421 return $this->_label;
426 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
427 // Thanks to PhpWiki:DanFr for finding this bug.
428 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
429 class Cached_UserLink extends Cached_WikiLink {
430 function expand($basepage, &$markup) {
431 $label = isset($this->_label) ? $this->_label : false;
432 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
433 $page = new WikiPageName($this->_page, $basepage, $anchor);
434 $link = WikiLink($page, 'auto', $label);
435 // $link = HTML::a(array('href' => $PageName));
436 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
437 $link->setAttr('class', 'wikiuser');
442 class Cached_PluginInvocation extends Cached_DynamicContent {
443 function Cached_PluginInvocation ($pi) {
447 function setTightness($top, $bottom) {
448 $this->_tightenable = 0;
449 if ($top) $this->_tightenable |= 1;
450 if ($bottom) $this->_tightenable |= 2;
453 function isInlineElement() {
457 function expand($basepage, &$markup) {
458 $loader = &$this->_getLoader();
460 $xml = HTML::div(array('class' => 'plugin'),
461 $loader->expandPI($this->_pi, $GLOBALS['request'], &$markup, $basepage));
463 if (isset($this->_tightenable)) {
464 $xml->setInClass('tightenable');
465 $xml->setInClass('top', ($this->_tightenable & 1) != 0);
466 $xml->setInClass('bottom', ($this->_tightenable & 2) != 0);
472 function asString() {
477 function getWikiPageLinks($basepage) {
478 $loader = &$this->_getLoader();
480 return $loader->getWikiPageLinks($this->_pi, $basepage);
483 function _getLoader() {
484 static $loader = false;
487 include_once('lib/WikiPlugin.php');
488 $loader = new WikiPluginLoader;
494 // (c-file-style: "gnu")
499 // c-hanging-comment-ender-p: nil
500 // indent-tabs-mode: nil