]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/CachedMarkup.php
asXML case
[SourceForge/phpwiki.git] / lib / CachedMarkup.php
1 <?php 
2 rcs_id('$Id: CachedMarkup.php,v 1.37 2005-10-12 06:15:44 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, 
206                                    check_php_version(5) 
207                                      ? 'Cached_DynamicContent' 
208                                      : 'cached_dynamiccontent'))
209             {
210                 $val = $item->expand($basepage, $this);
211                 $xml .= $val->asXML();
212             }
213             else {
214                 $xml .= $item->asXML();
215             }
216         }
217         return $xml;
218     }
219
220     function printXML () {
221         $basepage = $this->_basepage;
222         // _content might be changed from a plugin (CreateToc)
223         for ($i=0; $i < count($this->_content); $i++) {
224             $item = $this->_content[$i];
225             if (is_string($item)) {
226                 print $item;
227             }
228             elseif (is_subclass_of($item, 
229                                    check_php_version(5) 
230                                      ? 'Cached_DynamicContent' 
231                                      : 'cached_dynamiccontent')) 
232             {   // give the content the chance to know about itself or even 
233                 // to change itself
234                 $val = $item->expand($basepage, $this);
235                 $val->printXML();
236             }
237             else {
238                 $item->printXML();
239             }
240         }
241     }
242 }       
243
244 /**
245  * The base class for all dynamic content.
246  *
247  * Dynamic content is anything that can change even when the original
248  * wiki-text from which it was parsed is unchanged.
249  */
250 class Cached_DynamicContent {
251
252     function cache(&$cache) {
253         $cache[] = $this;
254     }
255
256     function expand($basepage, &$obj) {
257         trigger_error("Pure virtual", E_USER_ERROR);
258     }
259
260     function getWikiPageLinks($basepage) {
261         return false;
262     }
263 }
264
265 class XmlRpc_LinkInfo {
266     function XmlRpc_LinkInfo($page, $type, $href) {
267         $this->page = $page;
268         $this->type = $type;
269         $this->href = $href;
270         //$this->pageref = str_replace("/RPC2.php", "/index.php", $href);
271     }
272 }
273
274 class Cached_Link extends Cached_DynamicContent {
275
276     function isInlineElement() {
277         return true;
278     }
279
280     /** Get link info (for XML-RPC support)
281      *
282      * This is here to support the XML-RPC listLinks method.
283      * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
284      */
285     function getLinkInfo($basepage) {
286         return new XmlRpc_LinkInfo($this->_getName($basepage),
287                                    $this->_getType(),
288                                    $this->_getURL($basepage));
289     }
290     
291     function _getURL($basepage) {
292         return $this->_url;
293     }
294 }
295
296 class Cached_WikiLink extends Cached_Link {
297
298     function Cached_WikiLink ($page, $label = false, $anchor = false) {
299         $this->_page = $page;
300         if ($anchor)
301             $this->_anchor = $anchor;
302         if ($label and $label != $page)
303             $this->_label = $label;
304     }
305
306     function _getType() {
307         return 'internal';
308     }
309     
310     function getPagename($basepage) {
311         $page = new WikiPageName($this->_page, $basepage);
312         if ($page->isValid()) return $page->name;
313         else return false;
314     }
315
316     function getWikiPageLinks($basepage) {
317         if ($basepage == '') return false;
318         if ($link = $this->getPagename($basepage)) return array($link);
319         else return false;
320     }
321
322     function _getName($basepage) {
323         return $this->getPagename($basepage);
324     }
325
326     function _getURL($basepage) {
327         return WikiURL($this->getPagename($basepage));
328         //return WikiURL($this->getPagename($basepage), false, 'abs_url');
329     }
330
331     function expand($basepage, &$markup) {
332         $label = isset($this->_label) ? $this->_label : false;
333         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
334         $page = new WikiPageName($this->_page, $basepage, $anchor);
335         if ($page->isValid()) return WikiLink($page, 'auto', $label);
336         else return HTML($label);
337     }
338
339     function asXML() {
340         $label = isset($this->_label) ? $this->_label : false;
341         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
342         $page = new WikiPageName($this->_page, false, $anchor);
343         $link = WikiLink($page, 'auto', $label);
344         return $link->asXML();
345     }
346
347     function asString() {
348         if (isset($this->_label))
349             return $this->_label;
350         return $this->_page;
351     }
352 }
353
354 class Cached_WikiLinkIfKnown extends Cached_WikiLink
355 {
356     function Cached_WikiLinkIfKnown ($moniker) {
357         $this->_page = $moniker;
358     }
359
360     function expand($basepage, &$markup) {
361         return WikiLink($this->_page, 'if_known');
362     }
363 }    
364     
365 class Cached_PhpwikiURL extends Cached_DynamicContent
366 {
367     function Cached_PhpwikiURL ($url, $label) {
368         $this->_url = $url;
369         if ($label)
370             $this->_label = $label;
371     }
372
373     function isInlineElement() {
374         return true;
375     }
376
377     function expand($basepage, &$markup) {
378         $label = isset($this->_label) ? $this->_label : false;
379         return LinkPhpwikiURL($this->_url, $label, $basepage);
380     }
381
382     function asXML() {
383         $label = isset($this->_label) ? $this->_label : false;
384         $link = LinkPhpwikiURL($this->_url, $label);
385         return $link->asXML();
386     }
387
388     function asString() {
389         if (isset($this->_label))
390             return $this->_label;
391         return $this->_url;
392     }
393 }    
394     
395 class Cached_ExternalLink extends Cached_Link {
396
397     function Cached_ExternalLink($url, $label=false) {
398         $this->_url = $url;
399         if ($label && $label != $url)
400             $this->_label = $label;
401     }
402
403     function _getType() {
404         return 'external';
405     }
406     
407     function _getName($basepage) {
408         $label = isset($this->_label) ? $this->_label : false;
409         return ($label and is_string($label)) ? $label : $this->_url;
410     }
411
412     function expand($basepage, &$markup) {
413         global $request;
414
415         $label = isset($this->_label) ? $this->_label : false;
416         $link = LinkURL($this->_url, $label);
417
418         if (GOOGLE_LINKS_NOFOLLOW) {
419             // Ignores nofollow when the user who saved the page was authenticated. 
420             $page = $request->getPage($basepage);
421             $current = $page->getCurrentRevision();
422             if (!$current->get('author_id'))
423                 $link->setAttr('rel', 'nofollow');
424         }
425         return $link;
426     }
427
428     function asString() {
429         if (isset($this->_label))
430             return $this->_label;
431         return $this->_url;
432     }
433 }
434
435 class Cached_InterwikiLink extends Cached_ExternalLink {
436     
437     function Cached_InterwikiLink($link, $label=false) {
438         $this->_link = $link;
439         if ($label)
440             $this->_label = $label;
441     }
442
443     function _getName($basepage) {
444         $label = isset($this->_label) ? $this->_label : false;
445         return ($label and is_string($label)) ? $label : $link;
446     }
447     
448     function _getURL($basepage) {
449         $link = $this->expand($basepage, $this);
450         return $link->getAttr('href');
451     }
452
453     function expand($basepage, &$markup) {
454         $intermap = getInterwikiMap();
455         $label = isset($this->_label) ? $this->_label : false;
456         return $intermap->link($this->_link, $label);
457     }
458
459     function asString() {
460         if (isset($this->_label))
461             return $this->_label;
462         return $this->_link;
463     }
464 }
465
466 // Needed to put UserPages to backlinks. Special method to markup userpages with icons
467 // Thanks to PhpWiki:DanFr for finding this bug. 
468 // Fixed since 1.3.8, prev. versions had no userpages in backlinks
469 class Cached_UserLink extends Cached_WikiLink {
470     function expand($basepage, &$markup) {
471         $label = isset($this->_label) ? $this->_label : false;
472         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
473         $page = new WikiPageName($this->_page, $basepage, $anchor);
474         $link = WikiLink($page, 'auto', $label);
475         // $link = HTML::a(array('href' => $PageName));
476         $link->setContent(PossiblyGlueIconToText('wikiuser', $this->_page));
477         $link->setAttr('class', 'wikiuser');
478         return $link;
479     }
480 }
481
482 class Cached_PluginInvocation extends Cached_DynamicContent {
483
484     function Cached_PluginInvocation ($pi) {
485         $this->_pi = $pi;
486     }
487
488     function setTightness($top, $bottom) {
489         $this->_tightenable = 0;
490         if ($top) $this->_tightenable |= 1;
491         if ($bottom) $this->_tightenable |= 2;
492     }
493     
494     function isInlineElement() {
495         return false;
496     }
497
498     function expand($basepage, &$markup) {
499         $loader = &$this->_getLoader();
500
501         $xml = $loader->expandPI($this->_pi, $GLOBALS['request'], $markup, $basepage);
502         $div = HTML::div(array('class' => 'plugin'));
503         if (is_array($plugin_cmdline = $loader->parsePI($this->_pi)) and $plugin_cmdline[1])
504             $id = GenerateId($plugin_cmdline[1]->getName() . 'Plugin');
505         
506         if (isset($this->_tightenable)) {
507             if ($this->_tightenable == 3) {
508                 $span = HTML::span(array('class' => 'plugin'), $xml);
509                 if (!empty($id))
510                     $span->setAttr('id', $id);
511                 return $span;
512             }
513             $div->setInClass('tightenable');
514             $div->setInClass('top', ($this->_tightenable & 1) != 0);
515             $div->setInClass('bottom', ($this->_tightenable & 2) != 0);
516         }
517         if (!empty($id))
518             $div->setAttr('id', $id);
519         $div->pushContent($xml);
520         return $div;
521     }
522
523     function asString() {
524         return $this->_pi;
525     }
526
527
528     function getWikiPageLinks($basepage) {
529         $loader = &$this->_getLoader();
530
531         return $loader->getWikiPageLinks($this->_pi, $basepage);
532     }
533
534     function _getLoader() {
535         static $loader = false;
536
537         if (!$loader) {
538             include_once('lib/WikiPlugin.php');
539             $loader = new WikiPluginLoader;
540         }
541         return $loader;
542     }
543 }
544
545 // (c-file-style: "gnu")
546 // Local Variables:
547 // mode: php
548 // tab-width: 8
549 // c-basic-offset: 4
550 // c-hanging-comment-ender-p: nil
551 // indent-tabs-mode: nil
552 // End:   
553 ?>