2 rcs_id('$Id: CachedMarkup.php,v 1.46 2006-12-22 00:11:38 rurban Exp $');
3 /* Copyright (C) 2002 Geoffrey T. Dairiki <dairiki@dairiki.org>
4 * Copyright (C) 2004,2005,2006 $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 = new 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.
80 * @return array of hashes { linkto=>pagename, relation=>pagename }
82 function getWikiPageLinks() {
84 foreach ($this->_content as $item) {
85 if (!isa($item, 'Cached_DynamicContent'))
87 if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
89 $links = array_merge($links, $item_links);
91 // array_unique has a bug with hashes!
92 // set_links checks for duplicates, array_merge does not
93 //return array_unique($links);
99 * This is here to support the XML-RPC listLinks() method.
102 * Returns an array of hashes.
104 function getLinkInfo() {
106 foreach ($this->_content as $link) {
107 if (! isa($link, 'Cached_Link'))
109 $info = $link->getLinkInfo($this->_basepage);
110 $links[$info->href] = $info;
112 return array_values($links);
115 function _append($item) {
116 if (is_array($item)) {
117 foreach ($item as $subitem)
118 $this->_append($subitem);
120 elseif (!is_object($item)) {
121 $this->_buf .= $this->_quote((string) $item);
123 elseif (isa($item, 'Cached_DynamicContent')) {
125 $this->_content[] = $this->_buf;
128 $this->_content[] = $item;
130 elseif (isa($item, 'XmlElement')) {
131 if ($item->isEmpty()) {
132 $this->_buf .= $item->emptyTag();
135 $this->_buf .= $item->startTag();
136 foreach ($item->getContent() as $subitem)
137 $this->_append($subitem);
138 $this->_buf .= "</$item->_tag>";
140 if (!isset($this->_description) and $item->getTag() == 'p')
141 $this->_glean_description($item->asString());
143 if (!$item->isInlineElement())
146 elseif (isa($item, 'XmlContent')) {
147 foreach ($item->getContent() as $item)
148 $this->_append($item);
150 elseif (method_exists($item, 'asXML')) {
151 $this->_buf .= $item->asXML();
153 elseif (method_exists($item, 'asString')) {
154 $this->_buf .= $this->_quote($item->asString());
157 $this->_buf .= sprintf("==Object(%s)==", get_class($item));
161 function _glean_description($text) {
162 static $two_sentences;
163 if (!$two_sentences) {
164 $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
166 . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
169 if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
170 $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
174 * Guess a short description of the page.
178 * This algorithm was suggested on MeatballWiki by
179 * Alex Schroeder <kensanata@yahoo.com>.
181 * Use the first paragraph in the page which contains at least two
184 * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
188 function getDescription () {
189 return isset($this->_description) ? $this->_description : '';
194 $basepage = $this->_basepage;
196 foreach ($this->_content as $item) {
197 if (is_string($item)) {
200 elseif (is_subclass_of($item,
202 ? 'Cached_DynamicContent'
203 : 'cached_dynamiccontent'))
205 $val = $item->expand($basepage, $this);
206 $xml .= $val->asXML();
209 $xml .= $item->asXML();
215 function printXML () {
216 $basepage = $this->_basepage;
217 // _content might be changed from a plugin (CreateToc)
218 for ($i=0; $i < count($this->_content); $i++) {
219 $item = $this->_content[$i];
220 if (is_string($item)) {
223 elseif (is_subclass_of($item,
225 ? 'Cached_DynamicContent'
226 : 'cached_dynamiccontent'))
227 { // give the content the chance to know about itself or even
229 $val = $item->expand($basepage, $this);
240 * The base class for all dynamic content.
242 * Dynamic content is anything that can change even when the original
243 * wiki-text from which it was parsed is unchanged.
245 class Cached_DynamicContent {
247 function cache(&$cache) {
251 function expand($basepage, &$obj) {
252 trigger_error("Pure virtual", E_USER_ERROR);
255 function getWikiPageLinks($basepage) {
260 class XmlRpc_LinkInfo {
261 function XmlRpc_LinkInfo($page, $type, $href, $relation = '') {
265 $this->relation = $relation;
266 //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
270 class Cached_Link extends Cached_DynamicContent {
272 function isInlineElement() {
276 /** Get link info (for XML-RPC support)
278 * This is here to support the XML-RPC listLinks method.
279 * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
281 function getLinkInfo($basepage) {
282 return new XmlRpc_LinkInfo($this->_getName($basepage),
284 $this->_getURL($basepage),
285 $this->_getRelation($basepage));
288 function _getURL($basepage) {
291 function __getRelation($basepage) {
292 return $this->_relation;
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;
304 $this->_basepage = false;
307 function _getType() {
311 function getPagename($basepage) {
312 $page = new WikiPageName($this->_page, $basepage);
313 if ($page->isValid()) return $page->name;
317 function getWikiPageLinks($basepage) {
318 if ($basepage == '') return false;
319 if ($link = $this->getPagename($basepage))
320 return array(array('linkto' => $link, 'relation' => 0));
324 function _getName($basepage) {
325 return $this->getPagename($basepage);
328 function _getURL($basepage) {
329 return WikiURL($this->getPagename($basepage));
330 //return WikiURL($this->getPagename($basepage), false, 'abs_url');
333 function expand($basepage, &$markup) {
334 $this->_basepage = $basepage;
335 $label = isset($this->_label) ? $this->_label : false;
336 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
337 $page = new WikiPageName($this->_page, $basepage, $anchor);
338 if ($page->isValid()) return WikiLink($page, 'auto', $label);
339 else return HTML($label);
343 $label = isset($this->_label) ? $this->_label : false;
344 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
345 //TODO: need basepage for subpages like /Remove (within CreateTOC)
346 $page = new WikiPageName($this->_page, $this->_basepage, $anchor);
347 $link = WikiLink($page, 'auto', $label);
348 return $link->asXML();
351 function asString() {
352 if (isset($this->_label))
353 return $this->_label;
358 class Cached_WikiLinkIfKnown extends Cached_WikiLink
360 function Cached_WikiLinkIfKnown ($moniker) {
361 $this->_page = $moniker;
364 function expand($basepage, &$markup) {
365 return WikiLink($this->_page, 'if_known');
369 class Cached_PhpwikiURL extends Cached_DynamicContent
371 function Cached_PhpwikiURL ($url, $label) {
374 $this->_label = $label;
377 function isInlineElement() {
381 function expand($basepage, &$markup) {
382 $label = isset($this->_label) ? $this->_label : false;
383 return LinkPhpwikiURL($this->_url, $label, $basepage);
387 $label = isset($this->_label) ? $this->_label : false;
388 $link = LinkPhpwikiURL($this->_url, $label);
389 return $link->asXML();
392 function asString() {
393 if (isset($this->_label))
394 return $this->_label;
399 class Cached_SemanticLink extends Cached_WikiLink {
401 function Cached_SemanticLink ($url, $label) {
403 if ($label && $label != $url)
404 $this->_label = $label;
407 function isInlineElement() {
411 function getPagename($basepage) {
412 if (!isset($this->_page)) {
413 $this->_expandurl($this->_url);
415 $page = new WikiPageName($this->_page, $basepage);
416 if ($page->isValid()) return $page->name;
420 /* add relation to the link table */
421 function getWikiPageLinks($basepage) {
422 if ($basepage == '') return false;
423 if ($link = $this->getPagename($basepage))
424 return array(array('linkto' => $link, 'relation' => $this->_relation));
428 function _expandurl($url) {
430 if (!preg_match('/^ ([^:]*) (:[:-]) (.*) $/x', $url, $m)) {
431 return HTML::strong(array('class' => 'rawurl'),
432 HTML::u(array('class' => 'baduri'),
433 _("BAD semantic relation link")));
435 $this->_relation = urldecode($m[1]);
436 $this->_page = urldecode($m[3]);
440 function _expand($url, $label = false) {
441 $m = $this->_expandurl($url);
443 // do not link to the attribute value, but to the attribute
444 $is_attribute = ($m[2] == ':-');
447 HTML::a(array('href' => WikiURL($is_attribute ? $this->_relation : $page),
451 } elseif ($is_attribute) {
453 HTML::a(array('href' => WikiURL($this->_relation),
459 HTML::a(array('href' => WikiURL($this->_relation),
463 HTML::a(array('href' => WikiURL($this->_page),
470 function expand($basepage, &$markup) {
471 $label = isset($this->_label) ? $this->_label : false;
472 return $this->_expand($this->_url, $label);
476 $label = isset($this->_label) ? $this->_label : false;
477 $link = $this->_expand($this->_url, $label);
478 return $link->asXML();
481 function asString() {
482 if (isset($this->_label))
483 return $this->_label;
488 class Cached_ExternalLink extends Cached_Link {
490 function Cached_ExternalLink($url, $label=false) {
492 if ($label && $label != $url)
493 $this->_label = $label;
496 function _getType() {
500 function _getName($basepage) {
501 $label = isset($this->_label) ? $this->_label : false;
502 return ($label and is_string($label)) ? $label : $this->_url;
505 function expand($basepage, &$markup) {
508 $label = isset($this->_label) ? $this->_label : false;
509 $link = LinkURL($this->_url, $label);
511 if (GOOGLE_LINKS_NOFOLLOW) {
512 // Ignores nofollow when the user who saved the page was authenticated.
513 $page = $request->getPage($basepage);
514 $current = $page->getCurrentRevision();
515 if (!$current->get('author_id'))
516 $link->setAttr('rel', 'nofollow');
521 function asString() {
522 if (isset($this->_label))
523 return $this->_label;
528 class Cached_InterwikiLink extends Cached_ExternalLink {
530 function Cached_InterwikiLink($link, $label=false) {
531 $this->_link = $link;
533 $this->_label = $label;
536 function getPagename($basepage) {
537 list ($moniker, $page) = split (":", $this->_link, 2);
538 $page = new WikiPageName($page, $basepage);
539 if ($page->isValid()) return $page->name;
543 function getWikiPageLinks($basepage) {
544 if ($basepage == '') return false;
545 /* ":DontStoreLink" */
546 if (substr($this->_link,0,1) == ':') return false;
547 /* store only links to valid pagenames */
548 if ($link = $this->getPagename($basepage))
549 return array(array('linkto' => $link, 'relation' => 0));
550 else return false; // dont store external links
553 function _getName($basepage) {
554 $label = isset($this->_label) ? $this->_label : false;
555 return ($label and is_string($label)) ? $label : $this->_link;
558 /* there may be internal interwiki links also */
559 function _getType() {
560 return $this->getPagename(false) ? 'internal' : 'external';
563 function _getURL($basepage) {
564 $link = $this->expand($basepage, $this);
565 return $link->getAttr('href');
568 function expand($basepage, &$markup) {
569 $intermap = getInterwikiMap();
570 $label = isset($this->_label) ? $this->_label : false;
571 return $intermap->link($this->_link, $label);
574 function asString() {
575 if (isset($this->_label))
576 return $this->_label;
581 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
582 // Thanks to PhpWiki:DanFr for finding this bug.
583 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
584 class Cached_UserLink extends Cached_WikiLink {
585 function expand($basepage, &$markup) {
586 $label = isset($this->_label) ? $this->_label : false;
587 $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
588 $page = new WikiPageName($this->_page, $basepage, $anchor);
589 $link = WikiLink($page, 'auto', $label);
590 // $link = HTML::a(array('href' => $PageName));
591 $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
592 $link->setAttr('class', 'wikiuser');
597 class Cached_PluginInvocation extends Cached_DynamicContent {
599 function Cached_PluginInvocation ($pi) {
603 function setTightness($top, $bottom) {
604 $this->_tightenable = 0;
605 if ($top) $this->_tightenable |= 1;
606 if ($bottom) $this->_tightenable |= 2;
609 function isInlineElement() {
613 function expand($basepage, &$markup) {
614 $loader = $this->_getLoader();
616 $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
617 $div = HTML::div(array('class' => 'plugin'));
618 if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
619 $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
621 if (isset($this->_tightenable)) {
622 if ($this->_tightenable == 3) {
623 $span = HTML::span(array('class' => 'plugin'), $xml);
625 $span->setAttr('id', $id);
628 $div->setInClass('tightenable');
629 $div->setInClass('top', ($this->_tightenable & 1) != 0);
630 $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
633 $div->setAttr('id', $id);
634 $div->pushContent($xml);
638 function asString() {
643 function getWikiPageLinks($basepage) {
644 $loader = $this->_getLoader();
646 return $loader->getWikiPageLinks($this->_pi, $basepage);
649 function & _getLoader() {
650 static $loader = false;
653 include_once('lib/WikiPlugin.php');
654 $loader = new WikiPluginLoader;
660 // $Log: not supported by cvs2svn $
661 // Revision 1.45 2006/10/12 06:33:50 rurban
662 // decide later with which class to render this link (fixes interwiki link layout)
664 // (c-file-style: "gnu")
669 // c-hanging-comment-ender-p: nil
670 // indent-tabs-mode: nil