]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/CachedMarkup.php
doc update for the new 1.3.10 release
[SourceForge/phpwiki.git] / lib / CachedMarkup.php
1 <?php 
2 rcs_id('$Id: CachedMarkup.php,v 1.18 2004-05-13 13:48:34 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         if ($page->isValid()) return WikiLink($page, 'auto', $label);
312         else return HTML($label);
313     }
314
315     function asXml() {
316         $label = isset($this->_label) ? $this->_label : false;
317         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
318         $page = new WikiPageName($this->_page, false, $anchor);
319         $link = WikiLink($page, 'auto', $label);
320         return $link->asXml();
321     }
322
323     function asString() {
324         if (isset($this->_label))
325             return $this->_label;
326         return $this->_page;
327     }
328 }
329
330 class Cached_WikiLinkIfKnown extends Cached_WikiLink
331 {
332     function Cached_WikiLinkIfKnown ($moniker) {
333         $this->_page = $moniker;
334     }
335
336     function expand($basepage, &$markup) {
337         return WikiLink($this->_page, 'if_known');
338     }
339 }    
340     
341 class Cached_PhpwikiURL extends Cached_DynamicContent
342 {
343     function Cached_PhpwikiURL ($url, $label) {
344         $this->_url = $url;
345         if ($label)
346             $this->_label = $label;
347     }
348
349     function isInlineElement() {
350         return true;
351     }
352
353     function expand($basepage, &$markup) {
354         $label = isset($this->_label) ? $this->_label : false;
355         return LinkPhpwikiURL($this->_url, $label, $basepage);
356     }
357
358     function asXml() {
359         $label = isset($this->_label) ? $this->_label : false;
360         $link = LinkPhpwikiURL($this->_url, $label);
361         return $link->asXml();
362     }
363
364     function asString() {
365         if (isset($this->_label))
366             return $this->_label;
367         return $this->_url;
368     }
369 }    
370     
371 class Cached_ExternalLink extends Cached_Link {
372
373     function Cached_ExternalLink($url, $label=false) {
374         $this->_url = $url;
375         if ($label && $label != $url)
376             $this->_label = $label;
377     }
378
379     function _getType() {
380         return 'external';
381     }
382     
383     function _getName($basepage) {
384         $label = isset($this->_label) ? $this->_label : false;
385         return ($label and is_string($label)) ? $label : $this->_url;
386     }
387
388     function expand($basepage, &$markup) {
389         $label = isset($this->_label) ? $this->_label : false;
390         return LinkURL($this->_url, $label);
391     }
392
393     function asString() {
394         if (isset($this->_label))
395             return $this->_label;
396         return $this->_url;
397     }
398 }
399
400 class Cached_InterwikiLink extends Cached_ExternalLink {
401     
402     function Cached_InterwikiLink($link, $label=false) {
403         $this->_link = $link;
404         if ($label)
405             $this->_label = $label;
406     }
407
408     function _getName($basepage) {
409         $label = isset($this->_label) ? $this->_label : false;
410         return ($label and is_string($label)) ? $label : $link;
411     }
412     
413     function _getURL($basepage) {
414         $link = $this->expand($basepage, &$this);
415         return $link->getAttr('href');
416     }
417
418     function expand($basepage, &$markup) {
419         //include_once('lib/interwiki.php');
420         $intermap = getInterwikiMap($GLOBALS['request']);
421         $label = isset($this->_label) ? $this->_label : false;
422         return $intermap->link($this->_link, $label);
423     }
424
425     function asString() {
426         if (isset($this->_label))
427             return $this->_label;
428         return $this->_link;
429     }
430 }
431
432 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
433 // Thanks to PhpWiki:DanFr for finding this bug. 
434 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
435 class Cached_UserLink extends Cached_WikiLink {
436     function expand($basepage, &$markup) {
437         $label = isset($this->_label) ? $this->_label : false;
438         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
439         $page = new WikiPageName($this->_page, $basepage, $anchor);
440         $link = WikiLink($page, 'auto', $label);
441         // $link = HTML::a(array('href' => $PageName));
442         $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
443         $link->setAttr('class', 'wikiuser');
444         return $link;
445     }
446 }
447
448 class Cached_PluginInvocation extends Cached_DynamicContent {
449     function Cached_PluginInvocation ($pi) {
450         $this->_pi = $pi;
451     }
452
453     function setTightness($top, $bottom) {
454         $this->_tightenable = 0;
455         if ($top) $this->_tightenable |= 1;
456         if ($bottom) $this->_tightenable |= 2;
457     }
458     
459     function isInlineElement() {
460         return false;
461     }
462
463     function expand($basepage, &$markup) {
464         $loader = &$this->_getLoader();
465
466         $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], &$markup, $basepage);
467         $div = HTML::div(array('class' => 'plugin'));
468         
469         if (isset($this->_tightenable)) {
470             if ($this->_tightenable == 3)
471                 return HTML::span(array('class' => 'plugin'), $xml);
472             $div->setInClass('tightenable');
473             $div->setInClass('top', ($this->_tightenable & 1) != 0);
474             $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
475         }
476         $div->pushContent($xml);
477         return $div;
478     }
479
480     function asString() {
481         return $this->_pi;
482     }
483
484
485     function getWikiPageLinks($basepage) {
486         $loader = &$this->_getLoader();
487
488         return $loader->getWikiPageLinks($this->_pi, $basepage);
489     }
490
491     function _getLoader() {
492         static $loader = false;
493
494         if (!$loader) {
495             include_once('lib/WikiPlugin.php');
496             $loader = new WikiPluginLoader;
497         }
498         return $loader;
499     }
500 }
501
502 // (c-file-style: "gnu")
503 // Local Variables:
504 // mode: php
505 // tab-width: 8
506 // c-basic-offset: 4
507 // c-hanging-comment-ender-p: nil
508 // indent-tabs-mode: nil
509 // End:   
510 ?>