2 rcs_id('$Id: CachedMarkup.php,v 1.37 2005-10-12 06:15:44 rurban Exp $');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004, 2005 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 class CacheableMarkup extends XmlContent {
25 function CacheableMarkup($content, $basepage) {
26 $this->_basepage = $basepage;
28 $this->_content = array();
29 $this->_append($content);
30 if ($this->_buf != '')
31 $this->_content[] = $this->_buf;
36 if (function_exists('gzcompress'))
37 return gzcompress(serialize($this), 9);
38 return serialize($this);
40 // FIXME: probably should implement some sort of "compression"
41 // when no gzcompress is available.
44 function unpack($packed) {
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)
51 and (substr($packed,0,2) == "\037\213")
52 or (substr($packed,0,2) == "x\332")) // 120, 218
54 if (function_exists('gzuncompress')) {
56 $data = gzuncompress($packed);
57 return unserialize($data);
59 // user our php lib. TESTME
60 include_once("ziplib.php");
61 $zip = ZipReader($packed);
62 list(,$data,$§attrib) = $zip->readFile();
63 return unserialize($data);
66 if (substr($packed,0,2) == "O:") {
67 // Looks like a serialized object
68 return unserialize($packed);
70 if (preg_match("/^\w+$/", $packed))
72 // happened with _BackendInfo problem also.
73 trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.",
78 /** Get names of wikipages linked to.
81 * A list of wiki page names (strings).
83 function getWikiPageLinks() {
84 include_once('lib/WikiPlugin.php');
85 $ploader = new WikiPluginLoader();
88 foreach ($this->_content as $item) {
89 if (!isa($item, 'Cached_DynamicContent'))
92 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
94 foreach ($item_links as $pagename)
95 if (is_string($pagename) and $pagename != '')
99 return array_unique($links);
104 * This is here to support the XML-RPC listLinks() method.
107 * Returns an array of hashes.
109 function getLinkInfo() {
111 foreach ($this->_content as $link) {
112 if (! isa($link, 'Cached_Link'))
114 $info = $link->getLinkInfo($this->_basepage);
115 $links[$info->href] = $info;
117 return array_values($links);
120 function _append($item) {
121 if (is_array($item)) {
122 foreach ($item as $subitem)
123 $this->_append($subitem);
125 elseif (!is_object($item)) {
126 $this->_buf .= $this->_quote((string) $item);
128 elseif (isa($item, 'Cached_DynamicContent')) {
130 $this->_content[] = $this->_buf;
133 $this->_content[] = $item;
135 elseif (isa($item, 'XmlElement')) {
136 if ($item->isEmpty()) {
137 $this->_buf .= $item->emptyTag();
140 $this->_buf .= $item->startTag();
141 foreach ($item->getContent() as $subitem)
142 $this->_append($subitem);
143 $this->_buf .= "</$item->_tag>";
145 if (!isset($this->_description) and $item->getTag() == 'p')
146 $this->_glean_description($item->asString());
148 if (!$item->isInlineElement())
151 elseif (isa($item, 'XmlContent')) {
152 foreach ($item->getContent() as $item)
153 $this->_append($item);
155 elseif (method_exists($item, 'asXML')) {
156 $this->_buf .= $item->asXML();
158 elseif (method_exists($item, 'asString')) {
159 $this->_buf .= $this->_quote($item->asString());
162 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
166 function _glean_description($text) {
167 static $two_sentences;
168 if (!$two_sentences) {
169 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
171 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
174 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
175 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
179 * Guess a short description of the page.
183 * This algorithm was suggested on MeatballWiki by
184 * Alex Schroeder <kensanata@yahoo.com>.
186 * Use the first paragraph in the page which contains at least two
189 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
193 function getDescription () {
194 return isset($this->_description) ? $this->_description : '';
199 $basepage = $this->_basepage;
201 foreach ($this->_content as $item) {
202 if (is_string($item)) {
205 elseif (is_subclass_of($item,
207 ? 'Cached_DynamicContent'
208 : 'cached_dynamiccontent'))
210 $val = $item->expand($basepage, $this);
211 $xml .= $val->asXML();
214 $xml .= $item->asXML();
220 function printXML () {
221 $basepage = $this->_basepage;
222 // _content might be changed from a plugin (CreateToc)
223 for ($i=0; $i < count($this->_content); $i++) {
224 $item = $this->_content[$i];
225 if (is_string($item)) {
228 elseif (is_subclass_of($item,
230 ? 'Cached_DynamicContent'
231 : 'cached_dynamiccontent'))
232 { // give the content the chance to know about itself or even
234 $val = $item->expand($basepage, $this);
245 * The base class for all dynamic content.
247 * Dynamic content is anything that can change even when the original
248 * wiki-text from which it was parsed is unchanged.
250 class Cached_DynamicContent {
252 function cache(&$cache) {
256 function expand($basepage, &$obj) {
257 trigger_error("Pure virtual", E_USER_ERROR);
260 function getWikiPageLinks($basepage) {
265 class XmlRpc_LinkInfo {
266 function XmlRpc_LinkInfo($page, $type, $href) {
270 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
274 class Cached_Link extends Cached_DynamicContent {
276 function isInlineElement() {
280 /** Get link info (for XML-RPC support)
282 * This is here to support the XML-RPC listLinks method.
283 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
285 function getLinkInfo($basepage) {
286 return new XmlRpc_LinkInfo($this->_getName($basepage),
288 $this->_getURL($basepage));
291 function _getURL($basepage) {
296 class Cached_WikiLink extends Cached_Link {
298 function Cached_WikiLink ($page, $label = false, $anchor = false) {
299 $this->_page = $page;
301 $this->_anchor = $anchor;
302 if ($label and $label != $page)
303 $this->_label = $label;
306 function _getType() {
310 function getPagename($basepage) {
311 $page = new WikiPageName($this->_page, $basepage);
312 if ($page->isValid()) return $page->name;
316 function getWikiPageLinks($basepage) {
317 if ($basepage == '') return false;
318 if ($link = $this->getPagename($basepage)) return array($link);
322 function _getName($basepage) {
323 return $this->getPagename($basepage);
326 function _getURL($basepage) {
327 return WikiURL($this->getPagename($basepage));
328 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
331 function expand($basepage, &$markup) {
332 $label = isset($this->_label) ? $this->_label : false;
333 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
334 $page = new WikiPageName($this->_page, $basepage, $anchor);
335 if ($page->isValid()) return WikiLink($page, 'auto', $label);
336 else return HTML($label);
340 $label = isset($this->_label) ? $this->_label : false;
341 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
342 $page = new WikiPageName($this->_page, false, $anchor);
343 $link = WikiLink($page, 'auto', $label);
344 return $link->asXML();
347 function asString() {
348 if (isset($this->_label))
349 return $this->_label;
354 class Cached_WikiLinkIfKnown extends Cached_WikiLink
356 function Cached_WikiLinkIfKnown ($moniker) {
357 $this->_page = $moniker;
360 function expand($basepage, &$markup) {
361 return WikiLink($this->_page, 'if_known');
365 class Cached_PhpwikiURL extends Cached_DynamicContent
367 function Cached_PhpwikiURL ($url, $label) {
370 $this->_label = $label;
373 function isInlineElement() {
377 function expand($basepage, &$markup) {
378 $label = isset($this->_label) ? $this->_label : false;
379 return LinkPhpwikiURL($this->_url, $label, $basepage);
383 $label = isset($this->_label) ? $this->_label : false;
384 $link = LinkPhpwikiURL($this->_url, $label);
385 return $link->asXML();
388 function asString() {
389 if (isset($this->_label))
390 return $this->_label;
395 class Cached_ExternalLink extends Cached_Link {
397 function Cached_ExternalLink($url, $label=false) {
399 if ($label && $label != $url)
400 $this->_label = $label;
403 function _getType() {
407 function _getName($basepage) {
408 $label = isset($this->_label) ? $this->_label : false;
409 return ($label and is_string($label)) ? $label : $this->_url;
412 function expand($basepage, &$markup) {
415 $label = isset($this->_label) ? $this->_label : false;
416 $link = LinkURL($this->_url, $label);
418 if (GOOGLE_LINKS_NOFOLLOW) {
419 // Ignores nofollow when the user who saved the page was authenticated.
420 $page = $request->getPage($basepage);
421 $current = $page->getCurrentRevision();
422 if (!$current->get('author_id'))
423 $link->setAttr('rel', 'nofollow');
428 function asString() {
429 if (isset($this->_label))
430 return $this->_label;
435 class Cached_InterwikiLink extends Cached_ExternalLink {
437 function Cached_InterwikiLink($link, $label=false) {
438 $this->_link = $link;
440 $this->_label = $label;
443 function _getName($basepage) {
444 $label = isset($this->_label) ? $this->_label : false;
445 return ($label and is_string($label)) ? $label : $link;
448 function _getURL($basepage) {
449 $link = $this->expand($basepage, $this);
450 return $link->getAttr('href');
453 function expand($basepage, &$markup) {
454 $intermap = getInterwikiMap();
455 $label = isset($this->_label) ? $this->_label : false;
456 return $intermap->link($this->_link, $label);
459 function asString() {
460 if (isset($this->_label))
461 return $this->_label;
466 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
467 // Thanks to PhpWiki:DanFr for finding this bug.
468 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
469 class Cached_UserLink extends Cached_WikiLink {
470 function expand($basepage, &$markup) {
471 $label = isset($this->_label) ? $this->_label : false;
472 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
473 $page = new WikiPageName($this->_page, $basepage, $anchor);
474 $link = WikiLink($page, 'auto', $label);
475 // $link = HTML::a(array('href' => $PageName));
476 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
477 $link->setAttr('class', 'wikiuser');
482 class Cached_PluginInvocation extends Cached_DynamicContent {
484 function Cached_PluginInvocation ($pi) {
488 function setTightness($top, $bottom) {
489 $this->_tightenable = 0;
490 if ($top) $this->_tightenable |= 1;
491 if ($bottom) $this->_tightenable |= 2;
494 function isInlineElement() {
498 function expand($basepage, &$markup) {
499 $loader = &$this->_getLoader();
501 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
502 $div = HTML::div(array('class' => 'plugin'));
503 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
504 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
506 if (isset($this->_tightenable)) {
507 if ($this->_tightenable == 3) {
508 $span = HTML::span(array('class' => 'plugin'), $xml);
510 $span->setAttr('id', $id);
513 $div->setInClass('tightenable');
514 $div->setInClass('top', ($this->_tightenable & 1) != 0);
515 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
518 $div->setAttr('id', $id);
519 $div->pushContent($xml);
523 function asString() {
528 function getWikiPageLinks($basepage) {
529 $loader = &$this->_getLoader();
531 return $loader->getWikiPageLinks($this->_pi, $basepage);
534 function _getLoader() {
535 static $loader = false;
538 include_once('lib/WikiPlugin.php');
539 $loader = new WikiPluginLoader;
545 // (c-file-style: "gnu")
550 // c-hanging-comment-ender-p: nil
551 // indent-tabs-mode: nil