]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/CachedMarkup.php
support <span>inlined plugin-result</span>:
[SourceForge/phpwiki.git] / lib / CachedMarkup.php
1 <?php 
2 rcs_id('$Id: CachedMarkup.php,v 1.17 2004-05-08 19:55:29 rurban Exp $');
3 /* Copyright (C) 2002, Geoffrey T. Dairiki <dairiki@dairiki.org>
4  *
5  * This file is part of PhpWiki.
6  * 
7  * PhpWiki is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * 
12  * PhpWiki is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with PhpWiki; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 class CacheableMarkup extends XmlContent {
23
24     function CacheableMarkup($content, $basepage) {
25         $this->_basepage = $basepage;
26         $this->_buf = '';
27         $this->_content = array();
28         $this->_append($content);
29         if ($this->_buf != '')
30             $this->_content[] = $this->_buf;
31         unset($this->_buf);
32     }
33
34     function pack() {
35         if (function_exists('gzcompress'))
36             return gzcompress(serialize($this), 9);
37         return serialize($this);
38
39         // FIXME: probably should implement some sort of "compression"
40         //   when no gzcompress is available.
41     }
42
43     function unpack($packed) {
44         if (!$packed)
45             return false;
46
47         if (function_exists('gzcompress')) {
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                 // Looks like ZLIB.
52                 return unserialize(gzuncompress($packed));
53             }
54         }
55         if (substr($packed,0,2) == "O:") {
56             // Looks like a serialized object
57             return unserialize($packed);
58         }
59         trigger_error("Can't unpack bad cached markup. Probably php_zlib extension not loaded.", E_USER_WARNING);
60         return false;
61     }
62     
63     /** Get names of wikipages linked to.
64      *
65      * @return array
66      * A list of wiki page names (strings).
67      */
68     function getWikiPageLinks() {
69         include_once('lib/WikiPlugin.php');
70         $ploader = new WikiPluginLoader();
71         
72         $links = array();
73         foreach ($this->_content as $item) {
74             if (!isa($item, 'Cached_DynamicContent'))
75                 continue;
76
77             if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
78                 continue;
79             foreach ($item_links as $pagename)
80                 if (is_string($pagename) and $pagename != '')
81                     $links[] = $pagename;
82         }
83
84         return array_unique($links);
85     }
86
87     /** Get link info.
88      *
89      * This is here to support the XML-RPC listLinks() method.
90      *
91      * @return array
92      * Returns an array of hashes.
93      */
94     function getLinkInfo() {
95         $link = array();
96         foreach ($this->_content as $link) {
97             if (! isa($link, 'Cached_Link'))
98                 continue;
99             $info = $link->getLinkInfo($this->_basepage);
100             $links[$info->href] = $info;
101         }
102         return array_values($links);
103     }
104
105     function _append($item) {
106         if (is_array($item)) {
107             foreach ($item as $subitem)
108                 $this->_append($subitem);
109         }
110         elseif (!is_object($item)) {
111             $this->_buf .= $this->_quote((string) $item);
112         }
113         elseif (isa($item, 'Cached_DynamicContent')) {
114             if ($this->_buf) {
115                 $this->_content[] = $this->_buf;
116                 $this->_buf = '';
117             }
118             $this->_content[] = $item;
119         }
120         elseif (isa($item, 'XmlElement')) {
121             if ($item->isEmpty()) {
122                 $this->_buf .= $item->emptyTag();
123             }
124             else {
125                 $this->_buf .= $item->startTag();
126                 foreach ($item->getContent() as $subitem)
127                     $this->_append($subitem);
128                 $this->_buf .= "</$item->_tag>";
129
130                 if (!isset($this->_description) and $item->getTag() == 'p')
131                     $this->_glean_description($item->asString());
132             }
133             if (!$item->isInlineElement())
134                 $this->_buf .= "\n";
135         }
136         elseif (isa($item, 'XmlContent')) {
137             foreach ($item->getContent() as $item)
138                 $this->_append($item);
139         }
140         elseif (method_exists($item, 'asxml')) {
141             $this->_buf .= $item->asXML();
142         }
143         elseif (method_exists($item, 'asstring')) {
144             $this->_buf .= $this->_quote($item->asString());
145         }
146         else {
147             $this->_buf .= sprintf("==Object(%s)==", get_class($item));
148         }
149     }
150
151     function _glean_description($text) {
152         static $two_sentences;
153         if (!$two_sentences) {
154             $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
155                                                     . ".*"
156                                                     . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
157         }
158         
159         if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
160             $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
161     }
162
163     /**
164      * Guess a short description of the page.
165      *
166      * Algorithm:
167      *
168      * This algorithm was suggested on MeatballWiki by
169      * Alex Schroeder <kensanata@yahoo.com>.
170      *
171      * Use the first paragraph in the page which contains at least two
172      * sentences.
173      *
174      * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
175      *
176      * @return string
177      */
178     function getDescription () {
179         return isset($this->_description) ? $this->_description : '';
180     }
181     
182     function asXML () {
183         $xml = '';
184         $basepage = $this->_basepage;
185         
186         foreach ($this->_content as $item) {
187             if (is_string($item)) {
188                 $xml .= $item;
189             }
190             elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
191                 $val = $item->expand($basepage, &$this);
192                 $xml .= $val->asXML();
193             }
194             else {
195                 $xml .= $item->asXML();
196             }
197         }
198         return $xml;
199     }
200
201     function printXML () {
202         $basepage = $this->_basepage;
203         // _content might be changed from a plugin (CreateToc)
204         for ($i=0; $i < count($this->_content); $i++) {
205             $item = $this->_content[$i];
206             if (is_string($item)) {
207                 print $item;
208             }
209             elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
210                 // give the content the chance to know about itself or even 
211                 // to change itself itself
212                 $val = $item->expand($basepage, &$this);
213                 $val->printXML();
214             }
215             else {
216                 $item->printXML();
217             }
218         }
219     }
220 }       
221
222 /**
223  * The base class for all dynamic content.
224  *
225  * Dynamic content is anything that can change even when the original
226  * wiki-text from which it was parsed is unchanged.
227  */
228 class Cached_DynamicContent {
229
230     function cache(&$cache) {
231         $cache[] = $this;
232     }
233
234     function expand($basepage, $obj) {
235         trigger_error("Pure virtual", E_USER_ERROR);
236     }
237
238     function getWikiPageLinks($basepage) {
239         return false;
240     }
241 }
242
243 class XmlRpc_LinkInfo {
244     function XmlRpc_LinkInfo($page, $type, $href) {
245         $this->page = $page;
246         $this->type = $type;
247         $this->href = $href;
248     }
249 }
250
251 class Cached_Link extends Cached_DynamicContent {
252
253     function isInlineElement() {
254         return true;
255     }
256
257     /** Get link info (for XML-RPC support)
258      *
259      * This is here to support the XML-RPC listLinks method.
260      * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
261      */
262     function getLinkInfo($basepage) {
263         return new XmlRpc_LinkInfo($this->_getName($basepage),
264                                    $this->_getType(),
265                                    $this->_getURL($basepage));
266     }
267     
268     function _getURL($basepage) {
269         return $this->_url;
270     }
271 }
272
273 class Cached_WikiLink extends Cached_Link {
274
275     function Cached_WikiLink ($page, $label = false, $anchor = false) {
276         $this->_page = $page;
277         if ($anchor)
278             $this->_anchor = $anchor;
279         if ($label and $label != $page)
280             $this->_label = $label;
281     }
282
283     function _getType() {
284         return 'internal';
285     }
286     
287     function getPagename($basepage) {
288         $page = new WikiPageName($this->_page, $basepage);
289         if ($page->isValid()) return $page->name;
290         else return false;
291     }
292
293     function getWikiPageLinks($basepage) {
294         if ($basepage == '') return false;
295         if ($link = $this->getPagename($basepage)) return array($link);
296         else return false;
297     }
298
299     function _getName($basepage) {
300         return $this->getPagename($basepage);
301     }
302
303     function _getURL($basepage) {
304         return WikiURL($this->getPagename($basepage), false, 'abs_url');
305     }
306
307     function expand($basepage, &$markup) {
308         $label = isset($this->_label) ? $this->_label : false;
309         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
310         $page = new WikiPageName($this->_page, $basepage, $anchor);
311         return WikiLink($page, 'auto', $label);
312     }
313
314     function asXml() {
315         $label = isset($this->_label) ? $this->_label : false;
316         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
317         $page = new WikiPageName($this->_page, false, $anchor);
318         $link = WikiLink($page, 'auto', $label);
319         return $link->asXml();
320     }
321
322     function asString() {
323         if (isset($this->_label))
324             return $this->_label;
325         return $this->_page;
326     }
327 }
328
329 class Cached_WikiLinkIfKnown extends Cached_WikiLink
330 {
331     function Cached_WikiLinkIfKnown ($moniker) {
332         $this->_page = $moniker;
333     }
334
335     function expand($basepage, &$markup) {
336         return WikiLink($this->_page, 'if_known');
337     }
338 }    
339     
340 class Cached_PhpwikiURL extends Cached_DynamicContent
341 {
342     function Cached_PhpwikiURL ($url, $label) {
343         $this->_url = $url;
344         if ($label)
345             $this->_label = $label;
346     }
347
348     function isInlineElement() {
349         return true;
350     }
351
352     function expand($basepage, &$markup) {
353         $label = isset($this->_label) ? $this->_label : false;
354         return LinkPhpwikiURL($this->_url, $label, $basepage);
355     }
356
357     function asXml() {
358         $label = isset($this->_label) ? $this->_label : false;
359         $link = LinkPhpwikiURL($this->_url, $label);
360         return $link->asXml();
361     }
362
363     function asString() {
364         if (isset($this->_label))
365             return $this->_label;
366         return $this->_url;
367     }
368 }    
369     
370 class Cached_ExternalLink extends Cached_Link {
371
372     function Cached_ExternalLink($url, $label=false) {
373         $this->_url = $url;
374         if ($label && $label != $url)
375             $this->_label = $label;
376     }
377
378     function _getType() {
379         return 'external';
380     }
381     
382     function _getName($basepage) {
383         $label = isset($this->_label) ? $this->_label : false;
384         return ($label and is_string($label)) ? $label : $this->_url;
385     }
386
387     function expand($basepage, &$markup) {
388         $label = isset($this->_label) ? $this->_label : false;
389         return LinkURL($this->_url, $label);
390     }
391
392     function asString() {
393         if (isset($this->_label))
394             return $this->_label;
395         return $this->_url;
396     }
397 }
398
399 class Cached_InterwikiLink extends Cached_ExternalLink {
400     
401     function Cached_InterwikiLink($link, $label=false) {
402         $this->_link = $link;
403         if ($label)
404             $this->_label = $label;
405     }
406
407     function _getName($basepage) {
408         $label = isset($this->_label) ? $this->_label : false;
409         return ($label and is_string($label)) ? $label : $link;
410     }
411     
412     function _getURL($basepage) {
413         $link = $this->expand($basepage, &$this);
414         return $link->getAttr('href');
415     }
416
417     function expand($basepage, &$markup) {
418         //include_once('lib/interwiki.php');
419         $intermap = getInterwikiMap($GLOBALS['request']);
420         $label = isset($this->_label) ? $this->_label : false;
421         return $intermap->link($this->_link, $label);
422     }
423
424     function asString() {
425         if (isset($this->_label))
426             return $this->_label;
427         return $this->_link;
428     }
429 }
430
431 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
432 // Thanks to PhpWiki:DanFr for finding this bug. 
433 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
434 class Cached_UserLink extends Cached_WikiLink {
435     function expand($basepage, &$markup) {
436         $label = isset($this->_label) ? $this->_label : false;
437         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
438         $page = new WikiPageName($this->_page, $basepage, $anchor);
439         $link = WikiLink($page, 'auto', $label);
440         // $link = HTML::a(array('href' => $PageName));
441         $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
442         $link->setAttr('class', 'wikiuser');
443         return $link;
444     }
445 }
446
447 class Cached_PluginInvocation extends Cached_DynamicContent {
448     function Cached_PluginInvocation ($pi) {
449         $this->_pi = $pi;
450     }
451
452     function setTightness($top, $bottom) {
453         $this->_tightenable = 0;
454         if ($top) $this->_tightenable |= 1;
455         if ($bottom) $this->_tightenable |= 2;
456     }
457     
458     function isInlineElement() {
459         return false;
460     }
461
462     function expand($basepage, &$markup) {
463         $loader = &$this->_getLoader();
464
465         $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], &$markup, $basepage);
466         $div = HTML::div(array('class' => 'plugin'));
467         
468         if (isset($this->_tightenable)) {
469             if ($this->_tightenable == 3)
470                 return HTML::span(array('class' => 'plugin'), $xml);
471             $div->setInClass('tightenable');
472             $div->setInClass('top', ($this->_tightenable & 1) != 0);
473             $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
474         }
475         $div->pushContent($xml);
476         return $div;
477     }
478
479     function asString() {
480         return $this->_pi;
481     }
482
483
484     function getWikiPageLinks($basepage) {
485         $loader = &$this->_getLoader();
486
487         return $loader->getWikiPageLinks($this->_pi, $basepage);
488     }
489
490     function _getLoader() {
491         static $loader = false;
492
493         if (!$loader) {
494             include_once('lib/WikiPlugin.php');
495             $loader = new WikiPluginLoader;
496         }
497         return $loader;
498     }
499 }
500
501 // (c-file-style: "gnu")
502 // Local Variables:
503 // mode: php
504 // tab-width: 8
505 // c-basic-offset: 4
506 // c-hanging-comment-ender-p: nil
507 // indent-tabs-mode: nil
508 // End:   
509 ?>