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