1 <?php rcs_id('$Id: CachedMarkup.php,v 1.2 2003-02-21 23:01:09 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() {
69 foreach ($this->_content as $link) {
70 if (! isa($link, 'Cached_WikiLink'))
72 $pagename = $link->getPagename($this->_basepage);
73 $links[$pagename] = 1;
75 return array_keys($links);
80 * This is here to support the XML-RPC listLinks() method.
83 * Returns an array of hashes.
85 function getLinkInfo() {
87 foreach ($this->_content as $link) {
88 if (! isa($link, 'Cached_Link'))
90 $info = $link->getLinkInfo($this->_basepage);
91 $links[$info->href] = $info;
93 return array_values($links);
96 function _append($item) {
97 if (is_array($item)) {
98 foreach ($item as $subitem)
99 $this->_append($subitem);
101 elseif (!is_object($item)) {
102 $this->_buf .= $this->_quote((string) $item);
104 elseif (isa($item, 'Cached_DynamicContent')) {
106 $this->_content[] = $this->_buf;
109 $this->_content[] = $item;
111 elseif (isa($item, 'XmlElement')) {
112 if ($item->isEmpty()) {
113 $this->_buf .= $item->emptyTag();
116 $this->_buf .= $item->startTag();
117 foreach ($item->getContent() as $subitem)
118 $this->_append($subitem);
119 $this->_buf .= "</$item->_tag>";
121 if (!$item->isInlineElement())
124 elseif (isa($item, 'XmlContent')) {
125 foreach ($item->getContent() as $item)
126 $this->_append($item);
128 elseif (method_exists($item, 'asxml')) {
129 $this->_buf .= $item->asXML();
131 elseif (method_exists($item, 'asstring')) {
132 $this->_buf .= $this->_quote($item->asString());
135 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
141 $basepage = $this->_basepage;
143 foreach ($this->_content as $item) {
144 if (is_string($item)) {
147 elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
148 $val = $item->expand($basepage);
149 $xml .= $val->asXML();
152 $xml .= $item->asXML();
158 function printXML () {
159 $basepage = $this->_basepage;
161 foreach ($this->_content as $item) {
162 if (is_string($item)) {
165 elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
166 $val = $item->expand($basepage);
177 * The base class for all dynamic content.
179 * Dynamic content is anything that can change even when the original
180 * wiki-text from which it was parsed is unchanged.
182 class Cached_DynamicContent {
184 function cache(&$cache) {
188 function expand($basepage) {
189 trigger_error("Pure virtual", E_USER_ERROR);
193 class XmlRpc_LinkInfo {
194 function XmlRpc_LinkInfo($page, $type, $href) {
201 class Cached_Link extends Cached_DynamicContent {
203 function isInlineElement() {
207 /** Get link info (for XML-RPC support)
209 * This is here to support the XML-RPC listLinks method.
210 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
212 function getLinkInfo($basepage) {
213 return new XmlRpc_LinkInfo($this->_getName($basepage),
215 $this->_getURL($basepage));
218 function _getURL($basepage) {
223 class Cached_WikiLink extends Cached_Link {
225 function Cached_WikiLink ($page, $label = false, $anchor = false) {
226 $this->_page = $page;
228 $this->_anchor = $anchor;
229 if ($label and $label != $page)
230 $this->_label = $label;
233 function _getType() {
237 function getPagename($basepage) {
238 $page = $this->_page;
239 if (empty($page) or $page[0] == SUBPAGE_SEPARATOR)
240 $page = $basepage . $page;
244 function _getName($basepage) {
245 return $this->getPagename($basepage);
248 function _getURL($basepage) {
249 return WikiURL($this->getPagename($basepage), false, 'abs_url');
252 function expand($basepage) {
253 $label = isset($this->_label) ? $this->_label : false;
254 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
255 $page = new WikiPageName($this->_page, $basepage, $anchor);
256 return WikiLink($page, 'auto', $label);
260 class Cached_WikiLinkIfKnown extends Cached_WikiLink
262 function Cached_WikiLinkIfKnown ($moniker) {
263 $this->_page = $moniker;
266 function expand($basepage) {
267 return WikiLink($this->_page, 'if_known');
271 class Cached_ExternalLink extends Cached_Link {
273 function Cached_ExternalLink($url, $label=false) {
275 if ($label && $label != $url)
276 $this->_label = $label;
279 function _getType() {
283 function _getName($basepage) {
284 $label = isset($this->_label) ? $this->_label : false;
285 return ($label and is_string($label)) ? $label : $this->_url;
288 function expand($basepage) {
289 $label = isset($this->_label) ? $this->_label : false;
290 return LinkURL($this->_url, $label);
294 class Cached_InterwikiLink extends Cached_ExternalLink {
296 function Cached_InterwikiLink($link, $label=false) {
297 $this->_link = $link;
299 $this->_label = $label;
302 function _getName($basepage) {
303 $label = isset($this->_label) ? $this->_label : false;
304 return ($label and is_string($label)) ? $label : $link;
307 function _getURL($basepage) {
308 $link = $this->expand($basepage);
309 return $link->getAttr('href');
312 function expand($basepage) {
313 include_once('lib/interwiki.php');
314 $intermap = InterWikiMap::GetMap($GLOBALS['request']);
315 $label = isset($this->_label) ? $this->_label : false;
316 return $intermap->link($this->_link, $label);
322 class Cached_PluginInvocation extends Cached_DynamicContent {
323 function Cached_PluginInvocation ($pi) {
327 function setTightness($top, $bottom) {
328 $this->_tightenable = 0;
329 if ($top) $this->_tightenable |= 1;
330 if ($bottom) $this->_tightenable |= 2;
333 function isInlineElement() {
337 function expand($basepage) {
338 static $loader = false;
341 include_once('lib/WikiPlugin.php');
342 $loader = new WikiPluginLoader;
345 $xml = HTML::div(array('class' => 'plugin'),
346 $loader->expandPI($this->_pi, $GLOBALS['request'], $basepage));
348 if (isset($this->_tightenable)) {
349 $xml->setInClass('tightenable');
350 $xml->setInClass('top', ($this->_tightenable & 1) != 0);
351 $xml->setInClass('bottom', ($this->_tightenable & 2) != 0);
358 // (c-file-style: "gnu")
363 // c-hanging-comment-ender-p: nil
364 // indent-tabs-mode: nil