1 <?php rcs_id('$Id: interwiki.php,v 1.23 2002-10-06 16:45:10 dairiki Exp $');
4 function InterWikiMap (&$request) {
5 $dbi = $request->getDbh();
7 $intermap = $this->_getMapFromWikiPage($dbi->getPage(_("InterWikiMap")));
9 if (!$intermap && defined('INTERWIKI_MAP_FILE'))
10 $intermap = $this->_getMapFromFile(INTERWIKI_MAP_FILE);
12 $this->_map = $this->_parseMap($intermap);
13 $this->_regexp = $this->_getRegexp();
16 function GetMap (&$request) {
19 $map = new InterWikiMap($request);
23 function getRegexp() {
24 return $this->_regexp;
27 function link ($link, $linktext = false) {
29 list ($moniker, $page) = split (":", $link, 2);
31 if (!isset($this->_map[$moniker])) {
32 return HTML::span(array('class' => 'bad-interwiki'),
33 $linktext ? $linktext : $link);
36 $url = $this->_map[$moniker];
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;
42 if (strstr($url, '%s'))
43 $url = sprintf($url, $page_enc);
47 $link = HTML::a(array('href' => $url));
50 $link->pushContent(PossiblyGlueIconToText('interwiki', "$moniker:"),
51 HTML::span(array('class' => 'wikipage'), $page));
52 $link->setAttr('class', 'interwiki');
55 $link->pushContent(PossiblyGlueIconToText('interwiki', $linktext));
56 $link->setAttr('class', 'named-interwiki');
63 function _parseMap ($text) {
64 global $AllowedProtocols;
65 if (!preg_match_all("/^\s*(\S+)\s+(\S+)/m",
66 $text, $matches, PREG_SET_ORDER))
68 foreach ($matches as $m) {
74 function _getMapFromWikiPage ($page) {
75 if (! $page->get('locked'))
78 $current = $page->getCurrentRevision();
80 if (preg_match('|^<verbatim>\n(.*)^</verbatim>|ms',
81 $current->getPackedContent(), $m)) {
88 function _getMapFromFile ($filename) {
89 if (defined('WARN_NONPUBLIC_INTERWIKIMAP') and WARN_NONPUBLIC_INTERWIKIMAP) {
90 $error_html = sprintf(_("Loading InterWikiMap from external file %s."), $filename);
91 trigger_error( $error_html, E_USER_NOTICE );
93 if (!file_exists($filename)) {
94 $finder = new FileFinder();
95 $filename = $finder->findFile(INTERWIKI_MAP_FILE);
97 @$fd = fopen ($filename, "rb");
98 @$data = fread ($fd, filesize($filename));
104 function _getRegexp () {
106 return '(?:(?!a)a)'; // Never matches.
108 foreach (array_keys($this->_map) as $moniker)
109 $qkeys[] = preg_quote($moniker, '/');
110 return "(?:" . join("|", $qkeys) . ")";
120 // c-hanging-comment-ender-p: nil
121 // indent-tabs-mode: nil