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