]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/interwiki.php
Connect the rest of PhpWiki to the IniConfig system. Also the keyword regular expres...
[SourceForge/phpwiki.git] / lib / interwiki.php
1 <?php rcs_id('$Id: interwiki.php,v 1.24 2004-04-19 23:13:03 zorloc 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         $link = HTML::a(array('href' => $url));
48
49         if (!$linktext) {
50             $link->pushContent(PossiblyGlueIconToText('interwiki', "$moniker:"),
51                                HTML::span(array('class' => 'wikipage'), $page));
52             $link->setAttr('class', 'interwiki');
53         }
54         else {
55             $link->pushContent(PossiblyGlueIconToText('interwiki', $linktext));
56             $link->setAttr('class', 'named-interwiki');
57         }
58         
59         return $link;
60     }
61
62
63     function _parseMap ($text) {
64         if (!preg_match_all("/^\s*(\S+)\s+(\S+)/m",
65                             $text, $matches, PREG_SET_ORDER))
66             return false;
67         foreach ($matches as $m) {
68             $map[$m[1]] = $m[2];
69         }
70         return $map;
71     }
72
73     function _getMapFromWikiPage ($page) {
74         if (! $page->get('locked'))
75             return false;
76         
77         $current = $page->getCurrentRevision();
78         
79         if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms',
80                        $current->getPackedContent(), $m)) {
81             return $m[1];
82         }
83         return false;
84     }
85
86     // Fixme!
87     function _getMapFromFile ($filename) {
88         if (defined('WARN_NONPUBLIC_INTERWIKIMAP') and WARN_NONPUBLIC_INTERWIKIMAP) {
89             $error_html = sprintf(_("Loading InterWikiMap from external file %s."), $filename);
90             trigger_error( $error_html, E_USER_NOTICE );
91         }
92         if (!file_exists($filename)) {
93             $finder = new FileFinder();
94             $filename = $finder->findFile(INTERWIKI_MAP_FILE);
95         }
96         @$fd = fopen ($filename, "rb");
97         @$data = fread ($fd, filesize($filename));
98         @fclose ($fd);
99
100         return $data;
101     }
102
103     function _getRegexp () {
104         if (!$this->_map)
105             return '(?:(?!a)a)'; //  Never matches.
106         
107         foreach (array_keys($this->_map) as $moniker)
108             $qkeys[] = preg_quote($moniker, '/');
109         return "(?:" . join("|", $qkeys) . ")";
110     }
111 }
112             
113
114 // For emacs users
115 // Local Variables:
116 // mode: php
117 // tab-width: 8
118 // c-basic-offset: 4
119 // c-hanging-comment-ender-p: nil
120 // indent-tabs-mode: nil
121 // End:
122 ?>