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