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