]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/CachedMarkup.php
add working dynamic hyperwiki sidebar box (hyperapplet not yet),
[SourceForge/phpwiki.git] / lib / CachedMarkup.php
1 <?php 
2 rcs_id('$Id: CachedMarkup.php,v 1.27 2004-12-17 16:12:09 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
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         //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
249     }
250 }
251
252 class Cached_Link extends Cached_DynamicContent {
253
254     function isInlineElement() {
255         return true;
256     }
257
258     /** Get link info (for XML-RPC support)
259      *
260      * This is here to support the XML-RPC listLinks method.
261      * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
262      */
263     function getLinkInfo($basepage) {
264         return new XmlRpc_LinkInfo($this->_getName($basepage),
265                                    $this->_getType(),
266                                    $this->_getURL($basepage));
267     }
268     
269     function _getURL($basepage) {
270         return $this->_url;
271     }
272 }
273
274 class Cached_WikiLink extends Cached_Link {
275
276     function Cached_WikiLink ($page, $label = false, $anchor = false) {
277         $this->_page = $page;
278         if ($anchor)
279             $this->_anchor = $anchor;
280         if ($label and $label != $page)
281             $this->_label = $label;
282     }
283
284     function _getType() {
285         return 'internal';
286     }
287     
288     function getPagename($basepage) {
289         $page = new WikiPageName($this->_page, $basepage);
290         if ($page->isValid()) return $page->name;
291         else return false;
292     }
293
294     function getWikiPageLinks($basepage) {
295         if ($basepage == '') return false;
296         if ($link = $this->getPagename($basepage)) return array($link);
297         else return false;
298     }
299
300     function _getName($basepage) {
301         return $this->getPagename($basepage);
302     }
303
304     function _getURL($basepage) {
305         return WikiURL($this->getPagename($basepage));
306         //return WikiURL($this->getPagename($basepage), false, 'abs_url');
307     }
308
309     function expand($basepage, &$markup) {
310         $label = isset($this->_label) ? $this->_label : false;
311         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
312         $page = new WikiPageName($this->_page, $basepage, $anchor);
313         if ($page->isValid()) return WikiLink($page, 'auto', $label);
314         else return HTML($label);
315     }
316
317     function asXml() {
318         $label = isset($this->_label) ? $this->_label : false;
319         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
320         $page = new WikiPageName($this->_page, false, $anchor);
321         $link = WikiLink($page, 'auto', $label);
322         return $link->asXml();
323     }
324
325     function asString() {
326         if (isset($this->_label))
327             return $this->_label;
328         return $this->_page;
329     }
330 }
331
332 class Cached_WikiLinkIfKnown extends Cached_WikiLink
333 {
334     function Cached_WikiLinkIfKnown ($moniker) {
335         $this->_page = $moniker;
336     }
337
338     function expand($basepage, &$markup) {
339         return WikiLink($this->_page, 'if_known');
340     }
341 }    
342     
343 class Cached_PhpwikiURL extends Cached_DynamicContent
344 {
345     function Cached_PhpwikiURL ($url, $label) {
346         $this->_url = $url;
347         if ($label)
348             $this->_label = $label;
349     }
350
351     function isInlineElement() {
352         return true;
353     }
354
355     function expand($basepage, &$markup) {
356         $label = isset($this->_label) ? $this->_label : false;
357         return LinkPhpwikiURL($this->_url, $label, $basepage);
358     }
359
360     function asXml() {
361         $label = isset($this->_label) ? $this->_label : false;
362         $link = LinkPhpwikiURL($this->_url, $label);
363         return $link->asXml();
364     }
365
366     function asString() {
367         if (isset($this->_label))
368             return $this->_label;
369         return $this->_url;
370     }
371 }    
372     
373 class Cached_ExternalLink extends Cached_Link {
374
375     function Cached_ExternalLink($url, $label=false) {
376         $this->_url = $url;
377         if ($label && $label != $url)
378             $this->_label = $label;
379     }
380
381     function _getType() {
382         return 'external';
383     }
384     
385     function _getName($basepage) {
386         $label = isset($this->_label) ? $this->_label : false;
387         return ($label and is_string($label)) ? $label : $this->_url;
388     }
389
390     function expand($basepage, &$markup) {
391         $label = isset($this->_label) ? $this->_label : false;
392         return LinkURL($this->_url, $label);
393     }
394
395     function asString() {
396         if (isset($this->_label))
397             return $this->_label;
398         return $this->_url;
399     }
400 }
401
402 class Cached_InterwikiLink extends Cached_ExternalLink {
403     
404     function Cached_InterwikiLink($link, $label=false) {
405         $this->_link = $link;
406         if ($label)
407             $this->_label = $label;
408     }
409
410     function _getName($basepage) {
411         $label = isset($this->_label) ? $this->_label : false;
412         return ($label and is_string($label)) ? $label : $link;
413     }
414     
415     function _getURL($basepage) {
416         $link = $this->expand($basepage, $this);
417         return $link->getAttr('href');
418     }
419
420     function expand($basepage, &$markup) {
421         $intermap = getInterwikiMap();
422         $label = isset($this->_label) ? $this->_label : false;
423         return $intermap->link($this->_link, $label);
424     }
425
426     function asString() {
427         if (isset($this->_label))
428             return $this->_label;
429         return $this->_link;
430     }
431 }
432
433 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
434 // Thanks to PhpWiki:DanFr for finding this bug. 
435 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
436 class Cached_UserLink extends Cached_WikiLink {
437     function expand($basepage, &$markup) {
438         $label = isset($this->_label) ? $this->_label : false;
439         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
440         $page = new WikiPageName($this->_page, $basepage, $anchor);
441         $link = WikiLink($page, 'auto', $label);
442         // $link = HTML::a(array('href' => $PageName));
443         $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
444         $link->setAttr('class', 'wikiuser');
445         return $link;
446     }
447 }
448
449 class Cached_PluginInvocation extends Cached_DynamicContent {
450     function Cached_PluginInvocation ($pi) {
451         $this->_pi = $pi;
452     }
453
454     function setTightness($top, $bottom) {
455         $this->_tightenable = 0;
456         if ($top) $this->_tightenable |= 1;
457         if ($bottom) $this->_tightenable |= 2;
458     }
459     
460     function isInlineElement() {
461         return false;
462     }
463
464     function expand($basepage, &$markup) {
465         $loader = &$this->_getLoader();
466
467         $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
468         $div = HTML::div(array('class' => 'plugin'));
469         if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
470             $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
471         
472         if (isset($this->_tightenable)) {
473             if ($this->_tightenable == 3) {
474                 $span = HTML::span(array('class' => 'plugin'), $xml);
475                 if ($id)
476                     $span->setAttr('id', $id);
477                 return $span;
478             }
479             $div->setInClass('tightenable');
480             $div->setInClass('top', ($this->_tightenable & 1) != 0);
481             $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
482         }
483         if ($id)
484             $div->setAttr('id', $id);
485         $div->pushContent($xml);
486         return $div;
487     }
488
489     function asString() {
490         return $this->_pi;
491     }
492
493
494     function getWikiPageLinks($basepage) {
495         $loader = &$this->_getLoader();
496
497         return $loader->getWikiPageLinks($this->_pi, $basepage);
498     }
499
500     function _getLoader() {
501         static $loader = false;
502
503         if (!$loader) {
504             include_once('lib/WikiPlugin.php');
505             $loader = new WikiPluginLoader;
506         }
507         return $loader;
508     }
509 }
510
511 // (c-file-style: "gnu")
512 // Local Variables:
513 // mode: php
514 // tab-width: 8
515 // c-basic-offset: 4
516 // c-hanging-comment-ender-p: nil
517 // indent-tabs-mode: nil
518 // End:   
519 ?>