OSDN Git Service

BugTrack/2176 showrss: Fix character encoding issues etc.
[pukiwiki/pukiwiki.git] / lib / make_link.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // make_link.php
4 // Copyright
5 //   2003-2017 PukiWiki Development Team
6 //   2001-2002 Originally written by yu-ji
7 // License: GPL v2 or (at your option) any later version
8 //
9 // Hyperlink-related functions
10
11 // Hyperlink decoration
12 function make_link($string, $page = '')
13 {
14         global $vars;
15         static $converter;
16
17         if (! isset($converter)) $converter = new InlineConverter();
18
19         $clone = $converter->get_clone($converter);
20
21         return $clone->convert($string, ($page != '') ? $page : $vars['page']);
22 }
23
24 // Converters of inline element
25 class InlineConverter
26 {
27         var $converters; // as array()
28         var $pattern;
29         var $pos;
30         var $result;
31
32         function get_clone($obj) {
33                 static $clone_func;
34
35                 if (! isset($clone_func)) {
36                         if (version_compare(PHP_VERSION, '5.0.0', '<')) {
37                                 $clone_func = create_function('$a', 'return $a;');
38                         } else {
39                                 $clone_func = create_function('$a', 'return clone $a;');
40                         }
41                 }
42                 return $clone_func($obj);
43         }
44
45         function __clone() {
46                 $converters = array();
47                 foreach ($this->converters as $key=>$converter) {
48                         $converters[$key] = $this->get_clone($converter);
49                 }
50                 $this->converters = $converters;
51         }
52
53         function InlineConverter($converters = NULL, $excludes = NULL)
54         {
55                 $this->__construct($converters, $excludes);
56         }
57         function __construct($converters = NULL, $excludes = NULL)
58         {
59                 if ($converters === NULL) {
60                         $converters = array(
61                                 'plugin',        // Inline plugins
62                                 'note',          // Footnotes
63                                 'url',           // URLs
64                                 'url_interwiki', // URLs (interwiki definition)
65                                 'mailto',        // mailto: URL schemes
66                                 'interwikiname', // InterWikiNames
67                                 'autolink',      // AutoLinks
68                                 'bracketname',   // BracketNames
69                                 'wikiname',      // WikiNames
70                                 'autolink_a',    // AutoLinks(alphabet)
71                         );
72                 }
73
74                 if ($excludes !== NULL)
75                         $converters = array_diff($converters, $excludes);
76
77                 $this->converters = $patterns = array();
78                 $start = 1;
79
80                 foreach ($converters as $name) {
81                         $classname = 'Link_' . $name;
82                         $converter = new $classname($start);
83                         $pattern   = $converter->get_pattern();
84                         if ($pattern === FALSE) continue;
85
86                         $patterns[] = '(' . "\n" . $pattern . "\n" . ')';
87                         $this->converters[$start] = $converter;
88                         $start += $converter->get_count();
89                         ++$start;
90                 }
91                 $this->pattern = join('|', $patterns);
92         }
93
94         function convert($string, $page)
95         {
96                 $this->page   = $page;
97                 $this->result = array();
98
99                 $string = preg_replace_callback('/' . $this->pattern . '/x',
100                         array(& $this, 'replace'), $string);
101
102                 $arr = explode("\x08", make_line_rules(htmlsc($string)));
103                 $retval = '';
104                 while (! empty($arr)) {
105                         $retval .= array_shift($arr) . array_shift($this->result);
106                 }
107                 return $retval;
108         }
109
110         function replace($arr)
111         {
112                 $obj = $this->get_converter($arr);
113
114                 $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
115                         $obj->toString() : make_line_rules(htmlsc($arr[0]));
116
117                 return "\x08"; // Add a mark into latest processed part
118         }
119
120         function get_objects($string, $page)
121         {
122                 $matches = $arr = array();
123                 preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
124                 foreach ($matches as $match) {
125                         $obj = $this->get_converter($match);
126                         if ($obj->set($match, $page) !== FALSE) {
127                                 $arr[] = $this->get_clone($obj);
128                                 if ($obj->body != '')
129                                         $arr = array_merge($arr, $this->get_objects($obj->body, $page));
130                         }
131                 }
132                 return $arr;
133         }
134
135         function & get_converter(& $arr)
136         {
137                 foreach (array_keys($this->converters) as $start) {
138                         if ($arr[$start] == $arr[0])
139                                 return $this->converters[$start];
140                 }
141                 return NULL;
142         }
143 }
144
145 // Base class of inline elements
146 class Link
147 {
148         var $start;   // Origin number of parentheses (0 origin)
149         var $text;    // Matched string
150
151         var $type;
152         var $page;
153         var $name;
154         var $body;
155         var $alias;
156
157         function Link($start)
158         {
159                 $this->__construct($start);
160         }
161         function __construct($start)
162         {
163                 $this->start = $start;
164         }
165
166         // Return a regex pattern to match
167         function get_pattern() {}
168
169         // Return number of parentheses (except (?:...) )
170         function get_count() {}
171
172         // Set pattern that matches
173         function set($arr, $page) {}
174
175         function toString() {}
176
177         // Private: Get needed parts from a matched array()
178         function splice($arr)
179         {
180                 $count = $this->get_count() + 1;
181                 $arr   = array_pad(array_splice($arr, $this->start, $count), $count, '');
182                 $this->text = $arr[0];
183                 return $arr;
184         }
185
186         // Set basic parameters
187         function setParam($page, $name, $body, $type = '', $alias = '')
188         {
189                 static $converter = NULL;
190
191                 $this->page = $page;
192                 $this->name = $name;
193                 $this->body = $body;
194                 $this->type = $type;
195                 if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
196                         is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
197                         $alias = '<img src="' . htmlsc($alias) . '" alt="' . $name . '" />';
198                 } else if ($alias != '') {
199                         if ($converter === NULL)
200                                 $converter = new InlineConverter(array('plugin'));
201
202                         $alias = make_line_rules($converter->convert($alias, $page));
203
204                         // BugTrack/669: A hack removing anchor tags added by AutoLink
205                         $alias = preg_replace('#</?a[^>]*>#i', '', $alias);
206                 }
207                 $this->alias = $alias;
208
209                 return TRUE;
210         }
211 }
212
213 // Inline plugins
214 class Link_plugin extends Link
215 {
216         var $pattern;
217         var $plain,$param;
218
219         function Link_plugin($start)
220         {
221                 $this->__construct($start);
222         }
223         function __construct($start)
224         {
225                 parent::__construct($start);
226         }
227
228         function get_pattern()
229         {
230                 $this->pattern = <<<EOD
231 &
232 (      # (1) plain
233  (\w+) # (2) plugin name
234  (?:
235   \(
236    ((?:(?!\)[;{]).)*) # (3) parameter
237   \)
238  )?
239 )
240 EOD;
241                 return <<<EOD
242 {$this->pattern}
243 (?:
244  \{
245   ((?:(?R)|(?!};).)*) # (4) body
246  \}
247 )?
248 ;
249 EOD;
250         }
251
252         function get_count()
253         {
254                 return 4;
255         }
256
257         function set($arr, $page)
258         {
259                 list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
260
261                 // Re-get true plugin name and patameters (for PHP 4.1.2)
262                 $matches = array();
263                 if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
264                         && $matches[1] != $this->plain) 
265                         list(, $this->plain, $name, $this->param) = $matches;
266
267                 return parent::setParam($page, $name, $body, 'plugin');
268         }
269
270         function toString()
271         {
272                 $body = ($this->body == '') ? '' : make_link($this->body);
273                 $str = FALSE;
274
275                 // Try to call the plugin
276                 if (exist_plugin_inline($this->name))
277                         $str = do_plugin_inline($this->name, $this->param, $body);
278
279                 if ($str !== FALSE) {
280                         return $str; // Succeed
281                 } else {
282                         // No such plugin, or Failed
283                         $body = (($body == '') ? '' : '{' . $body . '}') . ';';
284                         return make_line_rules(htmlsc('&' . $this->plain) . $body);
285                 }
286         }
287 }
288
289 // Footnotes
290 class Link_note extends Link
291 {
292         function Link_note($start)
293         {
294                 $this->__construct($start);
295         }
296         function __construct($start)
297         {
298                 parent::__construct($start);
299         }
300
301         function get_pattern()
302         {
303                 return <<<EOD
304 \(\(
305  ((?>(?=\(\()(?R)|(?!\)\)).)*) # (1) note body
306 \)\)
307 EOD;
308         }
309
310         function get_count()
311         {
312                 return 1;
313         }
314
315         function set($arr, $page)
316         {
317                 global $foot_explain, $vars;
318                 static $note_id = 0;
319
320                 list(, $body) = $this->splice($arr);
321
322                 if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
323                         $script = '';
324                 } else {
325                         $script = get_page_uri($page);
326                 }
327
328                 $id   = ++$note_id;
329                 $note = make_link($body);
330                 $page = isset($vars['page']) ? rawurlencode($vars['page']) : '';
331
332                 // Footnote
333                 $foot_explain[$id] = '<a id="notefoot_' . $id . '" href="' .
334                         $script . '#notetext_' . $id . '" class="note_super">*' .
335                         $id . '</a>' . "\n" .
336                         '<span class="small">' . $note . '</span><br />';
337
338                 // A hyperlink, content-body to footnote
339                 if (! is_numeric(PKWK_FOOTNOTE_TITLE_MAX) || PKWK_FOOTNOTE_TITLE_MAX <= 0) {
340                         $title = '';
341                 } else {
342                         $title = strip_tags($note);
343                         $count = mb_strlen($title, SOURCE_ENCODING);
344                         $title = mb_substr($title, 0, PKWK_FOOTNOTE_TITLE_MAX, SOURCE_ENCODING);
345                         $abbr  = (mb_strlen($title) < $count) ? '...' : '';
346                         $title = ' title="' . $title . $abbr . '"';
347                 }
348                 $name = '<a id="notetext_' . $id . '" href="' . $script .
349                         '#notefoot_' . $id . '" class="note_super"' . $title .
350                         '>*' . $id . '</a>';
351
352                 return parent::setParam($page, $name, $body);
353         }
354
355         function toString()
356         {
357                 return $this->name;
358         }
359 }
360
361 // URLs
362 class Link_url extends Link
363 {
364         function Link_url($start)
365         {
366                 $this->__construct($start);
367         }
368         function __construct($start)
369         {
370                 parent::__construct($start);
371         }
372
373         function get_pattern()
374         {
375                 $s1 = $this->start + 1;
376                 return <<<EOD
377 ((?:\[\[))?       # (1) open bracket
378 ((?($s1)          # (2) alias
379 ((?:(?!\]\]).)+)  # (3) alias name
380  (?:>|:)
381 ))?
382 (                 # (4) url
383  (?:(?:https?|ftp|news):\/\/|mailto:)[\w\/\@\$()!?&%#:;.,~'=*+-]+
384 )
385 (?($s1)\]\])      # close bracket
386 EOD;
387         }
388
389         function get_count()
390         {
391                 return 4;
392         }
393
394         function set($arr, $page)
395         {
396                 list(, , , $alias, $name) = $this->splice($arr);
397                 return parent::setParam($page, htmlsc($name),
398                         '', 'url', $alias == '' ? $name : $alias);
399         }
400
401         function toString()
402         {
403                 if (FALSE) {
404                         $rel = '';
405                 } else {
406                         $rel = ' rel="nofollow"';
407                 }
408                 return '<a href="' . $this->name . '"' . $rel . '>' . $this->alias . '</a>';
409         }
410 }
411
412 // URLs (InterWiki definition on "InterWikiName")
413 class Link_url_interwiki extends Link
414 {
415         function Link_url_interwiki($start)
416         {
417                 $this->__construct($start);
418         }
419         function __construct($start)
420         {
421                 parent::__construct($start);
422         }
423
424         function get_pattern()
425         {
426                 return <<<EOD
427 \[       # open bracket
428 (        # (1) url
429  (?:(?:https?|ftp|news):\/\/|\.\.?\/)[!~*'();\/?:\@&=+\$,%#\w.-]*
430 )
431 \s
432 ([^\]]+) # (2) alias
433 \]       # close bracket
434 EOD;
435         }
436
437         function get_count()
438         {
439                 return 2;
440         }
441
442         function set($arr, $page)
443         {
444                 list(, $name, $alias) = $this->splice($arr);
445                 return parent::setParam($page, htmlsc($name), '', 'url', $alias);
446         }
447
448         function toString()
449         {
450                 return '<a href="' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
451         }
452 }
453
454 // mailto: URL schemes
455 class Link_mailto extends Link
456 {
457         var $is_image, $image;
458
459         function Link_mailto($start)
460         {
461                 $this->__construct($start);
462         }
463         function __construct($start)
464         {
465                 parent::__construct($start);
466         }
467
468         function get_pattern()
469         {
470                 $s1 = $this->start + 1;
471                 return <<<EOD
472 (?:
473  \[\[
474  ((?:(?!\]\]).)+)(?:>|:)  # (1) alias
475 )?
476 ([\w.-]+@[\w-]+\.[\w.-]+) # (2) mailto
477 (?($s1)\]\])              # close bracket if (1)
478 EOD;
479         }
480
481         function get_count()
482         {
483                 return 2;
484         }
485
486         function set($arr, $page)
487         {
488                 list(, $alias, $name) = $this->splice($arr);
489                 return parent::setParam($page, $name, '', 'mailto', $alias == '' ? $name : $alias);
490         }
491         
492         function toString()
493         {
494                 return '<a href="mailto:' . $this->name . '" rel="nofollow">' . $this->alias . '</a>';
495         }
496 }
497
498 // InterWikiName-rendered URLs
499 class Link_interwikiname extends Link
500 {
501         var $url    = '';
502         var $param  = '';
503         var $anchor = '';
504
505         function Link_interwikiname($start)
506         {
507                 $this->__construct($start);
508         }
509         function __construct($start)
510         {
511                 parent::__construct($start);
512         }
513
514         function get_pattern()
515         {
516                 $s2 = $this->start + 2;
517                 $s5 = $this->start + 5;
518                 return <<<EOD
519 \[\[                  # open bracket
520 (?:
521  ((?:(?!\]\]).)+)>    # (1) alias
522 )?
523 (\[\[)?               # (2) open bracket
524 ((?:(?!\s|:|\]\]).)+) # (3) InterWiki
525 (?<! > | >\[\[ )      # not '>' or '>[['
526 :                     # separator
527 (                     # (4) param
528  (\[\[)?              # (5) open bracket
529  (?:(?!>|\]\]).)+
530  (?($s5)\]\])         # close bracket if (5)
531 )
532 (?($s2)\]\])          # close bracket if (2)
533 \]\]                  # close bracket
534 EOD;
535         }
536
537         function get_count()
538         {
539                 return 5;
540         }
541
542         function set($arr, $page)
543         {
544                 list(, $alias, , $name, $this->param) = $this->splice($arr);
545
546                 $matches = array();
547                 if (preg_match('/^([^#]+)(#[A-Za-z][\w-]*)$/', $this->param, $matches))
548                         list(, $this->param, $this->anchor) = $matches;
549
550                 $url = get_interwiki_url($name, $this->param);
551                 $this->url = ($url === FALSE) ?
552                         get_base_uri() . '?' . pagename_urlencode('[[' . $name . ':' . $this->param . ']]') :
553                         htmlsc($url);
554
555                 return parent::setParam(
556                         $page,
557                         htmlsc($name . ':' . $this->param),
558                         '',
559                         'InterWikiName',
560                         $alias == '' ? $name . ':' . $this->param : $alias
561                 );
562         }
563
564         function toString()
565         {
566                 return '<a href="' . $this->url . $this->anchor . '" title="' .
567                         $this->name . '" rel="nofollow">' . $this->alias . '</a>';
568         }
569 }
570
571 // BracketNames
572 class Link_bracketname extends Link
573 {
574         var $anchor, $refer;
575
576         function Link_bracketname($start)
577         {
578                 $this->__construct($start);
579         }
580         function __construct($start)
581         {
582                 parent::__construct($start);
583         }
584
585         function get_pattern()
586         {
587                 global $WikiName, $BracketName;
588
589                 $s2 = $this->start + 2;
590                 return <<<EOD
591 \[\[                     # Open bracket
592 (?:((?:(?!\]\]).)+)>)?   # (1) Alias
593 (\[\[)?                  # (2) Open bracket
594 (                        # (3) PageName
595  (?:$WikiName)
596  |
597  (?:$BracketName)
598 )?
599 (\#(?:[a-zA-Z][\w-]*)?)? # (4) Anchor
600 (?($s2)\]\])             # Close bracket if (2)
601 \]\]                     # Close bracket
602 EOD;
603         }
604
605         function get_count()
606         {
607                 return 4;
608         }
609
610         function set($arr, $page)
611         {
612                 global $WikiName;
613
614                 list(, $alias, , $name, $this->anchor) = $this->splice($arr);
615                 if ($name == '' && $this->anchor == '') return FALSE;
616
617                 if ($name == '' || ! preg_match('/^' . $WikiName . '$/', $name)) {
618                         if ($alias == '') $alias = $name . $this->anchor;
619                         if ($name != '') {
620                                 $name = get_fullname($name, $page);
621                                 if (! is_pagename($name)) return FALSE;
622                         }
623                 }
624
625                 return parent::setParam($page, $name, '', 'pagename', $alias);
626         }
627
628         function toString()
629         {
630                 return make_pagelink(
631                         $this->name,
632                         $this->alias,
633                         $this->anchor,
634                         $this->page
635                 );
636         }
637 }
638
639 // WikiNames
640 class Link_wikiname extends Link
641 {
642         function Link_wikiname($start)
643         {
644                 $this->__construct($start);
645         }
646         function __construct($start)
647         {
648                 parent::__construct($start);
649         }
650
651         function get_pattern()
652         {
653                 global $WikiName, $nowikiname;
654
655                 return $nowikiname ? FALSE : '(' . $WikiName . ')';
656         }
657
658         function get_count()
659         {
660                 return 1;
661         }
662
663         function set($arr, $page)
664         {
665                 list($name) = $this->splice($arr);
666                 return parent::setParam($page, $name, '', 'pagename', $name);
667         }
668
669         function toString()
670         {
671                 return make_pagelink(
672                         $this->name,
673                         $this->alias,
674                         '',
675                         $this->page
676                 );
677         }
678 }
679
680 // AutoLinks
681 class Link_autolink extends Link
682 {
683         var $forceignorepages = array();
684         var $auto;
685         var $auto_a; // alphabet only
686
687         function Link_autolink($start)
688         {
689                 $this->__construct($start);
690         }
691         function __construct($start)
692         {
693                 global $autolink;
694
695                 parent::__construct($start);
696
697                 if (! $autolink || ! file_exists(CACHE_DIR . 'autolink.dat'))
698                         return;
699
700                 @list($auto, $auto_a, $forceignorepages) = file(CACHE_DIR . 'autolink.dat');
701                 $this->auto   = $auto;
702                 $this->auto_a = $auto_a;
703                 $this->forceignorepages = explode("\t", trim($forceignorepages));
704         }
705
706         function get_pattern()
707         {
708                 return isset($this->auto) ? '(' . $this->auto . ')' : FALSE;
709         }
710
711         function get_count()
712         {
713                 return 1;
714         }
715
716         function set($arr, $page)
717         {
718                 global $WikiName;
719
720                 list($name) = $this->splice($arr);
721
722                 // Ignore pages listed, or Expire ones not found
723                 if (in_array($name, $this->forceignorepages) || ! is_page($name))
724                         return FALSE;
725
726                 return parent::setParam($page, $name, '', 'pagename', $name);
727         }
728
729         function toString()
730         {
731                 return make_pagelink($this->name, $this->alias, '', $this->page, TRUE);
732         }
733 }
734
735 class Link_autolink_a extends Link_autolink
736 {
737         function Link_autolink_a($start)
738         {
739                 $this->__construct($start);
740         }
741         function __construct($start)
742         {
743                 parent::__construct($start);
744         }
745
746         function get_pattern()
747         {
748                 return isset($this->auto_a) ? '(' . $this->auto_a . ')' : FALSE;
749         }
750 }
751
752 // Make hyperlink for the page
753 function make_pagelink($page, $alias = '', $anchor = '', $refer = '', $isautolink = FALSE)
754 {
755         global $vars, $link_compact, $related, $_symbol_noexists;
756
757         $script = get_base_uri();
758         $s_page = htmlsc(strip_bracket($page));
759         $s_alias = ($alias == '') ? $s_page : $alias;
760
761         if ($page == '') return '<a href="' . $anchor . '">' . $s_alias . '</a>';
762
763         $r_page  = pagename_urlencode($page);
764         $r_refer = ($refer == '') ? '' : '&amp;refer=' . rawurlencode($refer);
765
766         if (! isset($related[$page]) && $page !== $vars['page'] && is_page($page))
767                 $related[$page] = get_filetime($page);
768
769         if ($isautolink || is_page($page)) {
770                 // Hyperlink to the page
771                 if ($link_compact) {
772                         $title   = '';
773                 } else {
774                         $title   = ' title="' . $s_page . get_pg_passage($page, FALSE) . '"';
775                 }
776
777                 // AutoLink marker
778                 if ($isautolink) {
779                         $al_left  = '<!--autolink-->';
780                         $al_right = '<!--/autolink-->';
781                 } else {
782                         $al_left = $al_right = '';
783                 }
784
785                 return $al_left . '<a ' . 'href="' . $script . '?' . $r_page . $anchor .
786                         '"' . $title . '>' . $s_alias . '</a>' . $al_right;
787         } else {
788                 // Support Page redirection
789                 $redirect_page = get_pagename_on_redirect($page);
790                 if ($redirect_page !== false) {
791                         return make_pagelink($redirect_page, $s_alias);
792                 }
793                 // Dangling link
794                 if (PKWK_READONLY) return $s_alias; // No dacorations
795
796                 $retval = $s_alias . '<a href="' .
797                         $script . '?cmd=edit&amp;page=' . $r_page . $r_refer . '">' .
798                         $_symbol_noexists . '</a>';
799
800                 if ($link_compact) {
801                         return $retval;
802                 } else {
803                         return '<span class="noexists">' . $retval . '</span>';
804                 }
805         }
806 }
807
808 // Resolve relative / (Unix-like)absolute path of the page
809 function get_fullname($name, $refer)
810 {
811         global $defaultpage;
812
813         // 'Here'
814         if ($name == '' || $name == './') return $refer;
815
816         // Absolute path
817         if ($name{0} == '/') {
818                 $name = substr($name, 1);
819                 return ($name == '') ? $defaultpage : $name;
820         }
821
822         // Relative path from 'Here'
823         if (substr($name, 0, 2) == './') {
824                 $arrn    = preg_split('#/#', $name, -1, PREG_SPLIT_NO_EMPTY);
825                 $arrn[0] = $refer;
826                 return join('/', $arrn);
827         }
828
829         // Relative path from dirname()
830         if (substr($name, 0, 3) == '../') {
831                 $arrn = preg_split('#/#', $name,  -1, PREG_SPLIT_NO_EMPTY);
832                 $arrp = preg_split('#/#', $refer, -1, PREG_SPLIT_NO_EMPTY);
833
834                 while (! empty($arrn) && $arrn[0] == '..') {
835                         array_shift($arrn);
836                         array_pop($arrp);
837                 }
838                 $name = ! empty($arrp) ? join('/', array_merge($arrp, $arrn)) :
839                         (! empty($arrn) ? $defaultpage . '/' . join('/', $arrn) : $defaultpage);
840         }
841
842         return $name;
843 }
844
845 // Render an InterWiki into a URL
846 function get_interwiki_url($name, $param)
847 {
848         global $WikiName, $interwiki;
849         static $interwikinames;
850         static $encode_aliases = array('sjis'=>'SJIS', 'euc'=>'EUC-JP', 'utf8'=>'UTF-8');
851
852         if (! isset($interwikinames)) {
853                 $interwikinames = $matches = array();
854                 foreach (get_source($interwiki) as $line)
855                         if (preg_match('/\[(' . '(?:(?:https?|ftp|news):\/\/|\.\.?\/)' .
856                             '[!~*\'();\/?:\@&=+\$,%#\w.-]*)\s([^\]]+)\]\s?([^\s]*)/',
857                             $line, $matches))
858                                 $interwikinames[$matches[2]] = array($matches[1], $matches[3]);
859         }
860
861         if (! isset($interwikinames[$name])) return FALSE;
862
863         list($url, $opt) = $interwikinames[$name];
864
865         // Encoding
866         switch ($opt) {
867
868         case '':    /* FALLTHROUGH */
869         case 'std': // Simply URL-encode the string, whose base encoding is the internal-encoding
870                 $param = rawurlencode($param);
871                 break;
872
873         case 'asis': /* FALLTHROUGH */
874         case 'raw' : // Truly as-is
875                 break;
876
877         case 'yw': // YukiWiki
878                 if (! preg_match('/' . $WikiName . '/', $param))
879                         $param = '[[' . mb_convert_encoding($param, 'SJIS', SOURCE_ENCODING) . ']]';
880                 break;
881
882         case 'moin': // MoinMoin
883                 $param = str_replace('%', '_', rawurlencode($param));
884                 break;
885
886         default:
887                 // Alias conversion of $opt
888                 if (isset($encode_aliases[$opt])) $opt = & $encode_aliases[$opt];
889
890                 // Encoding conversion into specified encode, and URLencode
891                 if (strpos($url, '$1') === FALSE && substr($url, -1) === '?') {
892                         // PukiWiki site
893                         $param = pagename_urlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
894                 } else {
895                         $param = rawurlencode(mb_convert_encoding($param, $opt, SOURCE_ENCODING));
896                 }
897         }
898
899         // Replace or Add the parameter
900         if (strpos($url, '$1') !== FALSE) {
901                 $url = str_replace('$1', $param, $url);
902         } else {
903                 $url .= $param;
904         }
905
906         $len = strlen($url);
907         if ($len > 512) die_message('InterWiki URL too long: ' . $len . ' characters');
908
909         return $url;
910 }