]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/CachedMarkup.php
Glean page description (for meta tag) from CachedMarkup.
[SourceForge/phpwiki.git] / lib / CachedMarkup.php
1 <?php rcs_id('$Id: CachedMarkup.php,v 1.5 2003-03-07 21:46:55 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         $links = array();
69         foreach ($this->_content as $link) {
70             if (! isa($link, 'Cached_WikiLink'))
71                 continue;
72             if (($pagename = $link->getPagename($this->_basepage)))
73                 $links[$pagename] = 1;
74         }
75         return array_keys($links);
76     }
77
78     /** Get link info.
79      *
80      * This is here to support the XML-RPC listLinks() method.
81      *
82      * @return array
83      * Returns an array of hashes.
84      */
85     function getLinkInfo() {
86         $link = array();
87         foreach ($this->_content as $link) {
88             if (! isa($link, 'Cached_Link'))
89                 continue;
90             $info = $link->getLinkInfo($this->_basepage);
91             $links[$info->href] = $info;
92         }
93         return array_values($links);
94     }
95
96     function _append($item) {
97         if (is_array($item)) {
98             foreach ($item as $subitem)
99                 $this->_append($subitem);
100         }
101         elseif (!is_object($item)) {
102             $this->_buf .= $this->_quote((string) $item);
103         }
104         elseif (isa($item, 'Cached_DynamicContent')) {
105             if ($this->_buf) {
106                 $this->_content[] = $this->_buf;
107                 $this->_buf = '';
108             }
109             $this->_content[] = $item;
110         }
111         elseif (isa($item, 'XmlElement')) {
112             if ($item->isEmpty()) {
113                 $this->_buf .= $item->emptyTag();
114             }
115             else {
116                 $this->_buf .= $item->startTag();
117                 foreach ($item->getContent() as $subitem)
118                     $this->_append($subitem);
119                 $this->_buf .= "</$item->_tag>";
120
121                 if (!isset($this->_description) and $item->getTag() == 'p')
122                     $this->_glean_description($item->asString());
123             }
124             if (!$item->isInlineElement())
125                 $this->_buf .= "\n";
126         }
127         elseif (isa($item, 'XmlContent')) {
128             foreach ($item->getContent() as $item)
129                 $this->_append($item);
130         }
131         elseif (method_exists($item, 'asxml')) {
132             $this->_buf .= $item->asXML();
133         }
134         elseif (method_exists($item, 'asstring')) {
135             $this->_buf .= $this->_quote($item->asString());
136         }
137         else {
138             $this->_buf .= sprintf("==Object(%s)==", get_class($item));
139         }
140     }
141
142     function _glean_description($text) {
143         static $two_sentences;
144         if (!$two_sentences) {
145             $two_sentences = pcre_fix_posix_classes("[.?!][\")]*\s+[\"(]*[[:upper:])]"
146                                                     . ".*"
147                                                     . "[.?!][\")]*\s*[\"(]*([[:upper:])]|$)");
148         }
149         
150         if (!isset($this->_description) and preg_match("/$two_sentences/sx", $text))
151             $this->_description = preg_replace("/\s*\n\s*/", " ", trim($text));
152     }
153
154     /**
155      * Guess a short description of the page.
156      *
157      * Algorithm:
158      *
159      * This algorithm was suggested on MeatballWiki by
160      * Alex Schroeder <kensanata@yahoo.com>.
161      *
162      * Use the first paragraph in the page which contains at least two
163      * sentences.
164      *
165      * @see http://www.usemod.com/cgi-bin/mb.pl?MeatballWikiSuggestions
166      *
167      * @return string
168      */
169     function getDescription () {
170         return isset($this->_description) ? $this->_description : '';
171     }
172     
173     function asXML () {
174         $xml = '';
175         $basepage = $this->_basepage;
176         
177         foreach ($this->_content as $item) {
178             if (is_string($item)) {
179                 $xml .= $item;
180             }
181             elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
182                 $val = $item->expand($basepage);
183                 $xml .= $val->asXML();
184             }
185             else {
186                 $xml .= $item->asXML();
187             }
188         }
189         return $xml;
190     }
191
192     function printXML () {
193         $basepage = $this->_basepage;
194
195         foreach ($this->_content as $item) {
196             if (is_string($item)) {
197                 print $item;
198             }
199             elseif (is_subclass_of($item, 'cached_dynamiccontent')) {
200                 $val = $item->expand($basepage);
201                 $val->printXML();
202             }
203             else {
204                 $item->printXML();
205             }
206         }
207     }
208 }       
209
210 /**
211  * The base class for all dynamic content.
212  *
213  * Dynamic content is anything that can change even when the original
214  * wiki-text from which it was parsed is unchanged.
215  */
216 class Cached_DynamicContent {
217
218     function cache(&$cache) {
219         $cache[] = $this;
220     }
221
222     function expand($basepage) {
223         trigger_error("Pure virtual", E_USER_ERROR);
224     }
225 }
226
227 class XmlRpc_LinkInfo {
228     function XmlRpc_LinkInfo($page, $type, $href) {
229         $this->page = $page;
230         $this->type = $type;
231         $this->href = $href;
232     }
233 }
234
235 class Cached_Link extends Cached_DynamicContent {
236
237     function isInlineElement() {
238         return true;
239     }
240
241     /** Get link info (for XML-RPC support)
242      *
243      * This is here to support the XML-RPC listLinks method.
244      * (See http://www.ecyrd.com/JSPWiki/Wiki.jsp?page=WikiRPCInterface)
245      */
246     function getLinkInfo($basepage) {
247         return new XmlRpc_LinkInfo($this->_getName($basepage),
248                                    $this->_getType(),
249                                    $this->_getURL($basepage));
250     }
251     
252     function _getURL($basepage) {
253         return $this->_url;
254     }
255 }
256
257 class Cached_WikiLink extends Cached_Link {
258
259     function Cached_WikiLink ($page, $label = false, $anchor = false) {
260         $this->_page = $page;
261         if ($anchor)
262             $this->_anchor = $anchor;
263         if ($label and $label != $page)
264             $this->_label = $label;
265     }
266
267     function _getType() {
268         return 'internal';
269     }
270     
271     function getPagename($basepage) {
272         $page = new WikiPageName($this->_page, $basepage);
273         return $page->name;
274     }
275
276     function _getName($basepage) {
277         return $this->getPagename($basepage);
278     }
279
280     function _getURL($basepage) {
281         return WikiURL($this->getPagename($basepage), false, 'abs_url');
282     }
283
284     function expand($basepage) {
285         $label = isset($this->_label) ? $this->_label : false;
286         $anchor = isset($this->_anchor) ? (string)$this->_anchor : '';
287         $page = new WikiPageName($this->_page, $basepage, $anchor);
288         return WikiLink($page, 'auto', $label);
289     }
290
291     function asString() {
292         if (isset($this->_label))
293             return $this->_label;
294         return $this->_page;
295     }
296 }
297
298 class Cached_WikiLinkIfKnown extends Cached_WikiLink
299 {
300     function Cached_WikiLinkIfKnown ($moniker) {
301         $this->_page = $moniker;
302     }
303
304     function expand($basepage) {
305         return WikiLink($this->_page, 'if_known');
306     }
307 }    
308     
309 class Cached_PhpwikiURL extends Cached_DynamicContent
310 {
311     function Cached_PhpwikiURL ($url, $label) {
312         $this->_url = $url;
313         if ($label)
314             $this->_label = $label;
315     }
316
317     function isInlineElement() {
318         return true;
319     }
320
321     function expand($basepage) {
322         $label = isset($this->_label) ? $this->_label : false;
323         return LinkPhpwikiURL($this->_url, $label, $basepage);
324     }
325
326     function asString() {
327         if (isset($this->_label))
328             return $this->_label;
329         return $this->_url;
330     }
331 }    
332     
333 class Cached_ExternalLink extends Cached_Link {
334
335     function Cached_ExternalLink($url, $label=false) {
336         $this->_url = $url;
337         if ($label && $label != $url)
338             $this->_label = $label;
339     }
340
341     function _getType() {
342         return 'external';
343     }
344     
345     function _getName($basepage) {
346         $label = isset($this->_label) ? $this->_label : false;
347         return ($label and is_string($label)) ? $label : $this->_url;
348     }
349
350     function expand($basepage) {
351         $label = isset($this->_label) ? $this->_label : false;
352         return LinkURL($this->_url, $label);
353     }
354
355     function asString() {
356         if (isset($this->_label))
357             return $this->_label;
358         return $this->_url;
359     }
360 }
361
362 class Cached_InterwikiLink extends Cached_ExternalLink {
363     
364     function Cached_InterwikiLink($link, $label=false) {
365         $this->_link = $link;
366         if ($label)
367             $this->_label = $label;
368     }
369
370     function _getName($basepage) {
371         $label = isset($this->_label) ? $this->_label : false;
372         return ($label and is_string($label)) ? $label : $link;
373     }
374     
375     function _getURL($basepage) {
376         $link = $this->expand($basepage);
377         return $link->getAttr('href');
378     }
379
380     function expand($basepage) {
381         include_once('lib/interwiki.php');
382         $intermap = InterWikiMap::GetMap($GLOBALS['request']);
383         $label = isset($this->_label) ? $this->_label : false;
384         return $intermap->link($this->_link, $label);
385     }
386
387     function asString() {
388         if (isset($this->_label))
389             return $this->_label;
390         return $this->_link;
391     }
392 }
393
394
395
396 class Cached_PluginInvocation extends Cached_DynamicContent {
397     function Cached_PluginInvocation ($pi) {
398         $this->_pi = $pi;
399     }
400
401     function setTightness($top, $bottom) {
402         $this->_tightenable = 0;
403         if ($top) $this->_tightenable |= 1;
404         if ($bottom) $this->_tightenable |= 2;
405     }
406     
407     function isInlineElement() {
408         return false;
409     }
410
411     function expand($basepage) {
412         static $loader = false;
413
414         if (!$loader) {
415             include_once('lib/WikiPlugin.php');
416             $loader = new WikiPluginLoader;
417         }
418
419         $xml = HTML::div(array('class' => 'plugin'),
420                          $loader->expandPI($this->_pi, $GLOBALS['request'], $basepage));
421         
422         if (isset($this->_tightenable)) {
423             $xml->setInClass('tightenable');
424             $xml->setInClass('top', ($this->_tightenable & 1) != 0);
425             $xml->setInClass('bottom', ($this->_tightenable & 2) != 0);
426         }
427
428         return $xml;
429     }
430
431     function asString() {
432         return $this->_pi;
433     }
434 }
435
436 // (c-file-style: "gnu")
437 // Local Variables:
438 // mode: php
439 // tab-width: 8
440 // c-basic-offset: 4
441 // c-hanging-comment-ender-p: nil
442 // indent-tabs-mode: nil
443 // End:   
444 ?>