]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/CachedMarkup.php
Fix to ensure that getWikiPageLinks() returns an array of strings.
[SourceForge/phpwiki.git] / lib / CachedMarkup.php
1 <?php rcs_id('$Id: CachedMarkup.php,v 1.7 2003-03-25 21:04:41 dairiki 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", 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);
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
202         foreach ($this->_content as $item) {
203             if (is_string($item)) {
204                 print $item;
205             }
206             elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
207                 $val = $item->expand($basepage);
208                 $val->printXML();
209             }
210             else {
211                 $item->printXML();
212             }
213         }
214     }
215 }       
216
217 /**
218  * The base class for all dynamic content.
219  *
220  * Dynamic content is anything that can change even when the original
221  * wiki-text from which it was parsed is unchanged.
222  */
223 class Cached_DynamicContent {
224
225     function cache(&$cache) {
226         $cache[] = $this;
227     }
228
229     function expand($basepage) {
230         trigger_error("Pure virtual", E_USER_ERROR);
231     }
232
233     function getWikiPageLinks($basepage) {
234         return false;
235     }
236 }
237
238 class XmlRpc_LinkInfo {
239     function XmlRpc_LinkInfo($page, $type, $href) {
240         $this->page = $page;
241         $this->type = $type;
242         $this->href = $href;
243     }
244 }
245
246 class Cached_Link extends Cached_DynamicContent {
247
248     function isInlineElement() {
249         return true;
250     }
251
252     /** Get link info (for XML-RPC support)
253      *
254      * This is here to support the XML-RPC listLinks method.
255      * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
256      */
257     function getLinkInfo($basepage) {
258         return new XmlRpc_LinkInfo($this->_getName($basepage),
259                                    $this->_getType(),
260                                    $this->_getURL($basepage));
261     }
262     
263     function _getURL($basepage) {
264         return $this->_url;
265     }
266 }
267
268 class Cached_WikiLink extends Cached_Link {
269
270     function Cached_WikiLink ($page, $label = false, $anchor = false) {
271         $this->_page = $page;
272         if ($anchor)
273             $this->_anchor = $anchor;
274         if ($label and $label != $page)
275             $this->_label = $label;
276     }
277
278     function _getType() {
279         return 'internal';
280     }
281     
282     function getPagename($basepage) {
283         $page = new WikiPageName($this->_page, $basepage);
284         return $page->name;
285     }
286
287     function getWikiPageLinks($basepage) {
288         return array($this->getPagename($basepage));
289     }
290
291     function _getName($basepage) {
292         return $this->getPagename($basepage);
293     }
294
295     function _getURL($basepage) {
296         return WikiURL($this->getPagename($basepage), false, 'abs_url');
297     }
298
299     function expand($basepage) {
300         $label = isset($this->_label) ? $this->_label : false;
301         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
302         $page = new WikiPageName($this->_page, $basepage, $anchor);
303         return WikiLink($page, 'auto', $label);
304     }
305
306     function asString() {
307         if (isset($this->_label))
308             return $this->_label;
309         return $this->_page;
310     }
311 }
312
313 class Cached_WikiLinkIfKnown extends Cached_WikiLink
314 {
315     function Cached_WikiLinkIfKnown ($moniker) {
316         $this->_page = $moniker;
317     }
318
319     function expand($basepage) {
320         return WikiLink($this->_page, 'if_known');
321     }
322 }    
323     
324 class Cached_PhpwikiURL extends Cached_DynamicContent
325 {
326     function Cached_PhpwikiURL ($url, $label) {
327         $this->_url = $url;
328         if ($label)
329             $this->_label = $label;
330     }
331
332     function isInlineElement() {
333         return true;
334     }
335
336     function expand($basepage) {
337         $label = isset($this->_label) ? $this->_label : false;
338         return LinkPhpwikiURL($this->_url, $label, $basepage);
339     }
340
341     function asString() {
342         if (isset($this->_label))
343             return $this->_label;
344         return $this->_url;
345     }
346 }    
347     
348 class Cached_ExternalLink extends Cached_Link {
349
350     function Cached_ExternalLink($url, $label=false) {
351         $this->_url = $url;
352         if ($label && $label != $url)
353             $this->_label = $label;
354     }
355
356     function _getType() {
357         return 'external';
358     }
359     
360     function _getName($basepage) {
361         $label = isset($this->_label) ? $this->_label : false;
362         return ($label and is_string($label)) ? $label : $this->_url;
363     }
364
365     function expand($basepage) {
366         $label = isset($this->_label) ? $this->_label : false;
367         return LinkURL($this->_url, $label);
368     }
369
370     function asString() {
371         if (isset($this->_label))
372             return $this->_label;
373         return $this->_url;
374     }
375 }
376
377 class Cached_InterwikiLink extends Cached_ExternalLink {
378     
379     function Cached_InterwikiLink($link, $label=false) {
380         $this->_link = $link;
381         if ($label)
382             $this->_label = $label;
383     }
384
385     function _getName($basepage) {
386         $label = isset($this->_label) ? $this->_label : false;
387         return ($label and is_string($label)) ? $label : $link;
388     }
389     
390     function _getURL($basepage) {
391         $link = $this->expand($basepage);
392         return $link->getAttr('href');
393     }
394
395     function expand($basepage) {
396         include_once('lib/interwiki.php');
397         $intermap = InterWikiMap::GetMap($GLOBALS['request']);
398         $label = isset($this->_label) ? $this->_label : false;
399         return $intermap->link($this->_link, $label);
400     }
401
402     function asString() {
403         if (isset($this->_label))
404             return $this->_label;
405         return $this->_link;
406     }
407 }
408
409
410
411 class Cached_PluginInvocation extends Cached_DynamicContent {
412     function Cached_PluginInvocation ($pi) {
413         $this->_pi = $pi;
414     }
415
416     function setTightness($top, $bottom) {
417         $this->_tightenable = 0;
418         if ($top) $this->_tightenable |= 1;
419         if ($bottom) $this->_tightenable |= 2;
420     }
421     
422     function isInlineElement() {
423         return false;
424     }
425
426     function expand($basepage) {
427         $loader = &$this->_getLoader();
428
429         $xml = HTML::div(array('class' => 'plugin'),
430                          $loader->expandPI($this->_pi, $GLOBALS['request'], $basepage));
431         
432         if (isset($this->_tightenable)) {
433             $xml->setInClass('tightenable');
434             $xml->setInClass('top', ($this->_tightenable & 1) != 0);
435             $xml->setInClass('bottom', ($this->_tightenable & 2) != 0);
436         }
437
438         return $xml;
439     }
440
441     function asString() {
442         return $this->_pi;
443     }
444
445
446     function getWikiPageLinks($basepage) {
447         $loader = &$this->_getLoader();
448
449         return $loader->getWikiPageLinks($this->_pi, $basepage);
450     }
451
452     function _getLoader() {
453         static $loader = false;
454
455         if (!$loader) {
456             include_once('lib/WikiPlugin.php');
457             $loader = new WikiPluginLoader;
458         }
459         return $loader;
460     }
461 }
462
463 // (c-file-style: "gnu")
464 // Local Variables:
465 // mode: php
466 // tab-width: 8
467 // c-basic-offset: 4
468 // c-hanging-comment-ender-p: nil
469 // indent-tabs-mode: nil
470 // End:   
471 ?>