]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/interwiki.php
Issue warning when loading InterWikiMap from a file instead of the wiki page.
[SourceForge/phpwiki.git] / lib / interwiki.php
1 <?php rcs_id('$Id: interwiki.php,v 1.18 2002-02-03 19:21:58 carstenklapp Exp $');
2
3 class InterWikiMap {
4     function InterWikiMap (&$request) {
5         $dbi = $request->getDbh();
6
7         $intermap = $this->_getMapFromWikiPage($dbi->getPage(_("InterWikiMap")));
8
9         if (!$intermap && defined('INTERWIKI_MAP_FILE'))
10             $intermap = $this->_getMapFromFile(INTERWIKI_MAP_FILE);
11
12         $this->_map = $this->_parseMap($intermap);
13         $this->_regexp = $this->_getRegexp();
14     }
15
16     function GetMap (&$request) {
17         static $map;
18         if (empty($map))
19             $map = new InterWikiMap($request);
20         return $map;
21     }
22     
23     function getRegexp() {
24         return $this->_regexp;
25     }
26
27     function link ($link, $linktext = false) {
28
29         list ($moniker, $page) = split (":", $link, 2);
30         
31         if (!isset($this->_map[$moniker])) {
32             return HTML::span(array('class' => 'bad-interwiki'),
33                               $linktext ? $linktext : $link);
34         }
35
36         $url = $this->_map[$moniker];
37         
38         // Urlencode page only if it's a query arg.
39         // FIXME: this is a somewhat broken heuristic.
40         $page_enc = strstr($url, '?') ? rawurlencode($page) : $page;
41
42         if (strstr($url, '%s'))
43             $url = sprintf($url, $page_enc);
44         else
45             $url .= $page_enc;
46
47         if ($moniker == "Category") {
48             $link = HTML::a(array('href' => $url, 'class' => 'wiki'), $link);
49         } else {
50             $link = HTML::a(array('href' => $url),
51                             IconForLink('interwiki'));
52             if (!$linktext) {
53                 $link->pushContent("$moniker:",
54                                    HTML::span(array('class' => 'wikipage'), $page));
55                 $link->setAttr('class', 'interwiki');
56             } else {
57                 $link->pushContent($linktext);
58                 $link->setAttr('class', 'named-interwiki');
59             }
60         }
61         
62         return $link;
63     }
64
65
66     function _parseMap ($text) {
67         global $AllowedProtocols;
68         if (!preg_match_all("/^\s*(\S+)\s+((?:$AllowedProtocols):[^\s<>\"']+)/m",
69                             $text, $matches, PREG_SET_ORDER))
70             return false;
71         foreach ($matches as $m)
72             $map[$m[1]] = $m[2];
73         $map['Category'] = 'Category';
74         return $map;
75     }
76         
77     function _getMapFromWikiPage ($page) {
78         if (! $page->get('locked'))
79             return false;
80         
81         $current = $page->getCurrentRevision();
82         
83         if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms',
84                        $current->getPackedContent(), $m)) {
85             return $m[1];
86         }
87         return false;
88     }
89
90     function _getMapFromFile ($filename) {
91         $error_html = sprintf(_("Loading InterWikiMap from external file %s."), $filename);
92         trigger_error( $error_html, E_USER_NOTICE );
93
94         @$fd = fopen ($filename, "rb");
95         @$data = fread ($fd, filesize($filename));
96         @fclose ($fd);
97
98         return $data;
99     }
100
101     function _getRegexp () {
102         if (!$this->_map)
103             return '(?:(?!a)a)'; //  Never matches.
104         
105         foreach (array_keys($this->_map) as $moniker)
106             $qkeys[] = preg_quote($moniker, '/');
107         return "(?:" . join("|", $qkeys) . ")";
108     }
109 }
110             
111
112 // For emacs users
113 // Local Variables:
114 // mode: php
115 // tab-width: 8
116 // c-basic-offset: 4
117 // c-hanging-comment-ender-p: nil
118 // indent-tabs-mode: nil
119 // End:
120 ?>