]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/CachedMarkup.php
fix zipreader workaround (add new to ZipReader invocation). Thanks to Tom Eicher
[SourceForge/phpwiki.git] / lib / CachedMarkup.php
1 <?php 
2 rcs_id('$Id: CachedMarkup.php,v 1.40 2005-12-27 08:50:45 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 = new 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 hash of linkto=>pagenames, relation=>pagenames (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             if (!($item_links = $item->getWikiPageLinks($this->_basepage)))
92                 continue;
93             $links = array_merge($links, $item_links);
94         }
95         return array_unique($links);
96     }
97
98     /** Get link info.
99      *
100      * This is here to support the XML-RPC listLinks() method.
101      *
102      * @return array
103      * Returns an array of hashes.
104      */
105     function getLinkInfo() {
106         $link = array();
107         foreach ($this->_content as $link) {
108             if (! isa($link, 'Cached_Link'))
109                 continue;
110             $info = $link->getLinkInfo($this->_basepage);
111             $links[$info->href] = $info;
112         }
113         return array_values($links);
114     }
115
116     function _append($item) {
117         if (is_array($item)) {
118             foreach ($item as $subitem)
119                 $this->_append($subitem);
120         }
121         elseif (!is_object($item)) {
122             $this->_buf .= $this->_quote((string) $item);
123         }
124         elseif (isa($item, 'Cached_DynamicContent')) {
125             if ($this->_buf) {
126                 $this->_content[] = $this->_buf;
127                 $this->_buf = '';
128             }
129             $this->_content[] = $item;
130         }
131         elseif (isa($item, 'XmlElement')) {
132             if ($item->isEmpty()) {
133                 $this->_buf .= $item->emptyTag();
134             }
135             else {
136                 $this->_buf .= $item->startTag();
137                 foreach ($item->getContent() as $subitem)
138                     $this->_append($subitem);
139                 $this->_buf .= "</$item->_tag>";
140
141                 if (!isset($this->_description) and $item->getTag() == 'p')
142                     $this->_glean_description($item->asString());
143             }
144             if (!$item->isInlineElement())
145                 $this->_buf .= "\n";
146         }
147         elseif (isa($item, 'XmlContent')) {
148             foreach ($item->getContent() as $item)
149                 $this->_append($item);
150         }
151         elseif (method_exists($item, 'asXML')) {
152             $this->_buf .= $item->asXML();
153         }
154         elseif (method_exists($item, 'asString')) {
155             $this->_buf .= $this->_quote($item->asString());
156         }
157         else {
158             $this->_buf .= sprintf("==Object(%s)==", get_class($item));
159         }
160     }
161
162     function _glean_description($text) {
163         static $two_sentences;
164         if (!$two_sentences) {
165             $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
166                                                     . ".*"
167                                                     . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
168         }
169         
170         if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
171             $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
172     }
173
174     /**
175      * Guess a short description of the page.
176      *
177      * Algorithm:
178      *
179      * This algorithm was suggested on MeatballWiki by
180      * Alex Schroeder <kensanata@yahoo.com>.
181      *
182      * Use the first paragraph in the page which contains at least two
183      * sentences.
184      *
185      * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
186      *
187      * @return string
188      */
189     function getDescription () {
190         return isset($this->_description) ? $this->_description : '';
191     }
192     
193     function asXML () {
194         $xml = '';
195         $basepage = $this->_basepage;
196         
197         foreach ($this->_content as $item) {
198             if (is_string($item)) {
199                 $xml .= $item;
200             }
201             elseif (is_subclass_of($item, 
202                                    check_php_version(5) 
203                                      ? 'Cached_DynamicContent' 
204                                      : 'cached_dynamiccontent'))
205             {
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, 
225                                    check_php_version(5) 
226                                      ? 'Cached_DynamicContent' 
227                                      : 'cached_dynamiccontent')) 
228             {   // give the content the chance to know about itself or even 
229                 // to change itself
230                 $val = $item->expand($basepage, $this);
231                 $val->printXML();
232             }
233             else {
234                 $item->printXML();
235             }
236         }
237     }
238 }       
239
240 /**
241  * The base class for all dynamic content.
242  *
243  * Dynamic content is anything that can change even when the original
244  * wiki-text from which it was parsed is unchanged.
245  */
246 class Cached_DynamicContent {
247
248     function cache(&$cache) {
249         $cache[] = $this;
250     }
251
252     function expand($basepage, &$obj) {
253         trigger_error("Pure virtual", E_USER_ERROR);
254     }
255
256     function getWikiPageLinks($basepage) {
257         return false;
258     }
259 }
260
261 class XmlRpc_LinkInfo {
262     function XmlRpc_LinkInfo($page, $type, $href, $relation = '') {
263         $this->page = $page;
264         $this->type = $type;
265         $this->href = $href;
266         $this->relation = $relation;
267         //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
268     }
269 }
270
271 class Cached_Link extends Cached_DynamicContent {
272
273     function isInlineElement() {
274         return true;
275     }
276
277     /** Get link info (for XML-RPC support)
278      *
279      * This is here to support the XML-RPC listLinks method.
280      * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
281      */
282     function getLinkInfo($basepage) {
283         return new XmlRpc_LinkInfo($this->_getName($basepage),
284                                    $this->_getType(),
285                                    $this->_getURL($basepage),
286                                    $this->_getRelation($basepage));
287     }
288     
289     function _getURL($basepage) {
290         return $this->_url;
291     }
292     function __getRelation($basepage) {
293         return $this->_relation;
294     }
295 }
296
297 class Cached_WikiLink extends Cached_Link {
298
299     function Cached_WikiLink ($page, $label = false, $anchor = false) {
300         $this->_page = $page;
301         if ($anchor)
302             $this->_anchor = $anchor;
303         if ($label and $label != $page)
304             $this->_label = $label;
305     }
306
307     function _getType() {
308         return 'internal';
309     }
310     
311     function getPagename($basepage) {
312         $page = new WikiPageName($this->_page, $basepage);
313         if ($page->isValid()) return $page->name;
314         else return false;
315     }
316
317     function getWikiPageLinks($basepage) {
318         if ($basepage == '') return false;
319         if ($link = $this->getPagename($basepage)) 
320             return array(array('linkto' => $link, 'relation' => 0));
321         else return false;
322     }
323
324     function _getName($basepage) {
325         return $this->getPagename($basepage);
326     }
327
328     function _getURL($basepage) {
329         return WikiURL($this->getPagename($basepage));
330         //return WikiURL($this->getPagename($basepage), false, 'abs_url');
331     }
332
333     function expand($basepage, &$markup) {
334         $label = isset($this->_label) ? $this->_label : false;
335         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
336         $page = new WikiPageName($this->_page, $basepage, $anchor);
337         if ($page->isValid()) return WikiLink($page, 'auto', $label);
338         else return HTML($label);
339     }
340
341     function asXML() {
342         $label = isset($this->_label) ? $this->_label : false;
343         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
344         $page = new WikiPageName($this->_page, false, $anchor);
345         $link = WikiLink($page, 'auto', $label);
346         return $link->asXML();
347     }
348
349     function asString() {
350         if (isset($this->_label))
351             return $this->_label;
352         return $this->_page;
353     }
354 }
355
356 class Cached_WikiLinkIfKnown extends Cached_WikiLink
357 {
358     function Cached_WikiLinkIfKnown ($moniker) {
359         $this->_page = $moniker;
360     }
361
362     function expand($basepage, &$markup) {
363         return WikiLink($this->_page, 'if_known');
364     }
365 }    
366     
367 class Cached_PhpwikiURL extends Cached_DynamicContent
368 {
369     function Cached_PhpwikiURL ($url, $label) {
370         $this->_url = $url;
371         if ($label)
372             $this->_label = $label;
373     }
374
375     function isInlineElement() {
376         return true;
377     }
378
379     function expand($basepage, &$markup) {
380         $label = isset($this->_label) ? $this->_label : false;
381         return LinkPhpwikiURL($this->_url, $label, $basepage);
382     }
383
384     function asXML() {
385         $label = isset($this->_label) ? $this->_label : false;
386         $link = LinkPhpwikiURL($this->_url, $label);
387         return $link->asXML();
388     }
389
390     function asString() {
391         if (isset($this->_label))
392             return $this->_label;
393         return $this->_url;
394     }
395 }    
396
397 class Cached_SemanticLink extends Cached_WikiLink {
398
399     function Cached_SemanticLink ($url, $label) {
400         $this->_url = $url;
401         if ($label && $label != $url)
402             $this->_label = $label;
403     }
404
405     function isInlineElement() {
406         return true;
407     }
408
409     function getPagename($basepage) {
410         $page = new WikiPageName($this->_page, $basepage);
411         if ($page->isValid()) return $page->name;
412         else return false;
413     }
414
415     /* add relation to the link table */
416     function getWikiPageLinks($basepage) {
417         if ($basepage == '') return false;
418         if ($link = $this->getPagename($basepage)) 
419             return array(array('linkto' => $link, 'relation' => $this->_relation));
420         else return false;
421     }
422
423     function _expand($url, $label = false) {
424         $m = array();
425         if (!preg_match('/^ ([^:]*) (:[:-]) (.*) $/x', $url, $m)) {
426             return HTML::strong(array('class' => 'rawurl'),
427                                 HTML::u(array('class' => 'baduri'),
428                                         _("BAD semantic relation link")));
429         }
430         $relation = $this->_relation = urldecode($m[1]);
431         $page   = $this->_page = urldecode($m[3]);
432         $class = 'wikilink';
433         // do not link to the attribute value, but to the attribute
434         $is_attribute = ($m[2] == ':-');
435         if ($label) {
436             return HTML::span(
437                               HTML::a(array('href'  => WikiURL($is_attribute ? $relation : $page),
438                                             'class' => $class),
439                                       $label)
440                               );
441         } elseif ($is_attribute) {
442             return HTML::span(
443                               HTML::a(array('href'  => WikiURL($relation),
444                                             'class' => $class),
445                                       $url)
446                               );
447         } else {
448             return HTML::span(
449                               HTML::a(array('href'  => WikiURL($relation),
450                                             'class' => $class),
451                                       $relation),
452                               $m[2],
453                               HTML::a(array('href'  => WikiURL($page),
454                                             'class' => $class),
455                                       $page)
456                               );
457         }
458     }
459     function expand($basepage, &$markup) {
460         $label = isset($this->_label) ? $this->_label : false;
461         return $this->_expand($this->_url, $label);
462     }
463
464     function asXML() {
465         $label = isset($this->_label) ? $this->_label : false;
466         $link = $this->_expand($this->_url, $label);
467         return $link->asXML();
468     }
469
470     function asString() {
471         if (isset($this->_label))
472             return $this->_label;
473         return $this->_url;
474     }
475 }
476     
477 class Cached_ExternalLink extends Cached_Link {
478
479     function Cached_ExternalLink($url, $label=false) {
480         $this->_url = $url;
481         if ($label && $label != $url)
482             $this->_label = $label;
483     }
484
485     function _getType() {
486         return 'external';
487     }
488     
489     function _getName($basepage) {
490         $label = isset($this->_label) ? $this->_label : false;
491         return ($label and is_string($label)) ? $label : $this->_url;
492     }
493
494     function expand($basepage, &$markup) {
495         global $request;
496
497         $label = isset($this->_label) ? $this->_label : false;
498         $link = LinkURL($this->_url, $label);
499
500         if (GOOGLE_LINKS_NOFOLLOW) {
501             // Ignores nofollow when the user who saved the page was authenticated. 
502             $page = $request->getPage($basepage);
503             $current = $page->getCurrentRevision();
504             if (!$current->get('author_id'))
505                 $link->setAttr('rel', 'nofollow');
506         }
507         return $link;
508     }
509
510     function asString() {
511         if (isset($this->_label))
512             return $this->_label;
513         return $this->_url;
514     }
515 }
516
517 class Cached_InterwikiLink extends Cached_ExternalLink {
518     
519     function Cached_InterwikiLink($link, $label=false) {
520         $this->_link = $link;
521         if ($label)
522             $this->_label = $label;
523     }
524
525     function _getName($basepage) {
526         $label = isset($this->_label) ? $this->_label : false;
527         return ($label and is_string($label)) ? $label : $link;
528     }
529     
530     function _getURL($basepage) {
531         $link = $this->expand($basepage, $this);
532         return $link->getAttr('href');
533     }
534
535     function expand($basepage, &$markup) {
536         $intermap = getInterwikiMap();
537         $label = isset($this->_label) ? $this->_label : false;
538         return $intermap->link($this->_link, $label);
539     }
540
541     function asString() {
542         if (isset($this->_label))
543             return $this->_label;
544         return $this->_link;
545     }
546 }
547
548 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
549 // Thanks to PhpWiki:DanFr for finding this bug. 
550 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
551 class Cached_UserLink extends Cached_WikiLink {
552     function expand($basepage, &$markup) {
553         $label = isset($this->_label) ? $this->_label : false;
554         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
555         $page = new WikiPageName($this->_page, $basepage, $anchor);
556         $link = WikiLink($page, 'auto', $label);
557         // $link = HTML::a(array('href' => $PageName));
558         $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
559         $link->setAttr('class', 'wikiuser');
560         return $link;
561     }
562 }
563
564 class Cached_PluginInvocation extends Cached_DynamicContent {
565
566     function Cached_PluginInvocation ($pi) {
567         $this->_pi = $pi;
568     }
569
570     function setTightness($top, $bottom) {
571         $this->_tightenable = 0;
572         if ($top) $this->_tightenable |= 1;
573         if ($bottom) $this->_tightenable |= 2;
574     }
575     
576     function isInlineElement() {
577         return false;
578     }
579
580     function expand($basepage, &$markup) {
581         $loader = $this->_getLoader();
582
583         $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
584         $div = HTML::div(array('class' => 'plugin'));
585         if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
586             $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
587         
588         if (isset($this->_tightenable)) {
589             if ($this->_tightenable == 3) {
590                 $span = HTML::span(array('class' => 'plugin'), $xml);
591                 if (!empty($id))
592                     $span->setAttr('id', $id);
593                 return $span;
594             }
595             $div->setInClass('tightenable');
596             $div->setInClass('top', ($this->_tightenable & 1) != 0);
597             $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
598         }
599         if (!empty($id))
600             $div->setAttr('id', $id);
601         $div->pushContent($xml);
602         return $div;
603     }
604
605     function asString() {
606         return $this->_pi;
607     }
608
609
610     function getWikiPageLinks($basepage) {
611         $loader = &$this->_getLoader();
612
613         return $loader->getWikiPageLinks($this->_pi, $basepage);
614     }
615
616     function & _getLoader() {
617         static $loader = false;
618
619         if (!$loader) {
620             include_once('lib/WikiPlugin.php');
621             $loader = new WikiPluginLoader;
622         }
623         return $loader;
624     }
625 }
626
627 // (c-file-style: "gnu")
628 // Local Variables:
629 // mode: php
630 // tab-width: 8
631 // c-basic-offset: 4
632 // c-hanging-comment-ender-p: nil
633 // indent-tabs-mode: nil
634 // End:   
635 ?>