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