OSDN Git Service

BugTrack/2452 Remove 'create_function' function for stability
[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_exists;
34                 if (! isset($clone_exists)) {
35                         if (version_compare(PHP_VERSION, '5.0.0', '<')) {
36                                 $clone_exists = false;
37                         } else {
38                                 $clone_exists = true;
39                         }
40                 }
41                 if ($clone_exists) {
42                         return clone ($obj);
43                 }
44                 return $obj;
45         }
46
47         function __clone() {
48                 $converters = array();
49                 foreach ($this->converters as $key=>$converter) {
50                         $converters[$key] = $this->get_clone($converter);
51                 }
52                 $this->converters = $converters;
53         }
54
55         function InlineConverter($converters = NULL, $excludes = NULL)
56         {
57                 $this->__construct($converters, $excludes);
58         }
59         function __construct($converters = NULL, $excludes = NULL)
60         {
61                 if ($converters === NULL) {
62                         $converters = array(
63                                 'plugin',        // Inline plugins
64                                 'note',          // Footnotes
65                                 'url',           // URLs
66                                 'url_interwiki', // URLs (interwiki definition)
67                                 'mailto',        // mailto: URL schemes
68                                 'interwikiname', // InterWikiNames
69                                 'autolink',      // AutoLinks
70                                 'bracketname',   // BracketNames
71                                 'wikiname',      // WikiNames
72                                 'autolink_a',    // AutoLinks(alphabet)
73                         );
74                 }
75
76                 if ($excludes !== NULL)
77                         $converters = array_diff($converters, $excludes);
78
79                 $this->converters = $patterns = array();
80                 $start = 1;
81
82                 foreach ($converters as $name) {
83                         $classname = 'Link_' . $name;
84                         $converter = new $classname($start);
85                         $pattern   = $converter->get_pattern();
86                         if ($pattern === FALSE) continue;
87
88                         $patterns[] = '(' . "\n" . $pattern . "\n" . ')';
89                         $this->converters[$start] = $converter;
90                         $start += $converter->get_count();
91                         ++$start;
92                 }
93                 $this->pattern = join('|', $patterns);
94         }
95
96         function convert($string, $page)
97         {
98                 $this->page   = $page;
99                 $this->result = array();
100
101                 $string = preg_replace_callback('/' . $this->pattern . '/x',
102                         array(& $this, 'replace'), $string);
103
104                 $arr = explode("\x08", make_line_rules(htmlsc($string)));
105                 $retval = '';
106                 while (! empty($arr)) {
107                         $retval .= array_shift($arr) . array_shift($this->result);
108                 }
109                 return $retval;
110         }
111
112         function replace($arr)
113         {
114                 $obj = $this->get_converter($arr);
115
116                 $this->result[] = ($obj !== NULL && $obj->set($arr, $this->page) !== FALSE) ?
117                         $obj->toString() : make_line_rules(htmlsc($arr[0]));
118
119                 return "\x08"; // Add a mark into latest processed part
120         }
121
122         function get_objects($string, $page)
123         {
124                 $matches = $arr = array();
125                 preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
126                 foreach ($matches as $match) {
127                         $obj = $this->get_converter($match);
128                         if ($obj->set($match, $page) !== FALSE) {
129                                 $arr[] = $this->get_clone($obj);
130                                 if ($obj->body != '')
131                                         $arr = array_merge($arr, $this->get_objects($obj->body, $page));
132                         }
133                 }
134                 return $arr;
135         }
136
137         function & get_converter(& $arr)
138         {
139                 foreach (array_keys($this->converters) as $start) {
140                         if ($arr[$start] == $arr[0])
141                                 return $this->converters[$start];
142                 }
143                 return NULL;
144         }
145 }
146
147 // Base class of inline elements
148 class Link
149 {
150         var $start;   // Origin number of parentheses (0 origin)
151         var $text;    // Matched string
152
153         var $type;
154         var $page;
155         var $name;
156         var $body;
157         var $alias;
158
159         function Link($start)
160         {
161                 $this->__construct($start);
162         }
163         function __construct($start)
164         {
165                 $this->start = $start;
166         }
167
168         // Return a regex pattern to match
169         function get_pattern() {}
170
171         // Return number of parentheses (except (?:...) )
172         function get_count() {}
173
174         // Set pattern that matches
175         function set($arr, $page) {}
176
177         function toString() {}
178
179         // Private: Get needed parts from a matched array()
180         function splice($arr)
181         {
182                 $count = $this->get_count() + 1;
183                 $arr   = array_pad(array_splice($arr, $this->start, $count), $count, '');
184                 $this->text = $arr[0];
185                 return $arr;
186         }
187
188         // Set basic parameters
189         function setParam($page, $name, $body, $type = '', $alias = '')
190         {
191                 static $converter = NULL;
192
193                 $this->page = $page;
194                 $this->name = $name;
195                 $this->body = $body;
196                 $this->type = $type;
197                 if (! PKWK_DISABLE_INLINE_IMAGE_FROM_URI &&
198                         is_url($alias) && preg_match('/\.(gif|png|jpe?g)$/i', $alias)) {
199                         $alias = '<img src="' . htmlsc($alias) . '" alt="' . $name . '" />';
200                 } else if ($alias != '') {
201                         if ($converter === NULL)
202                                 $converter = new InlineConverter(array('plugin'));
203
204                         $alias = make_line_rules($converter->convert($alias, $page));
205
206                         // BugTrack/669: A hack removing anchor tags added by AutoLink
207                         $alias = preg_replace('#</?a[^>]*>#i', '', $alias);
208                 }
209                 $this->alias = $alias;
210
211                 return TRUE;
212         }
213 }
214
215 // Inline plugins
216 class Link_plugin extends Link
217 {
218         var $pattern;
219         var $plain,$param;
220
221         function Link_plugin($start)
222         {
223                 $this->__construct($start);
224         }
225         function __construct($start)
226         {
227                 parent::__construct($start);
228         }
229
230         function get_pattern()
231         {
232                 $this->pattern = <<<EOD
233 &
234 (      # (1) plain
235  (\w+) # (2) plugin name
236  (?:
237   \(
238    ((?:(?!\)[;{]).)*) # (3) parameter
239   \)
240  )?
241 )
242 EOD;
243                 return <<<EOD
244 {$this->pattern}
245 (?:
246  \{
247   ((?:(?R)|(?!};).)*) # (4) body
248  \}
249 )?
250 ;
251 EOD;
252         }
253
254         function get_count()
255         {
256                 return 4;
257         }
258
259         function set($arr, $page)
260         {
261                 list($all, $this->plain, $name, $this->param, $body) = $this->splice($arr);
262
263                 // Re-get true plugin name and patameters (for PHP 4.1.2)
264                 $matches = array();
265                 if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
266                         && $matches[1] != $this->plain) 
267                         list(, $this->plain, $name, $this->param) = $matches;
268
269                 return parent::setParam($page, $name, $body, 'plugin');
270         }
271
272         function toString()
273         {
274                 $body = ($this->body == '') ? '' : make_link($this->body);
275                 $str = FALSE;
276
277                 // Try to call the plugin
278                 if (exist_plugin_inline($this->name))
279                         $str = do_plugin_inline($this->name, $this->param, $body);
280
281                 if ($str !== FALSE) {
282                         return $str; // Succeed
283                 } else {
284                         // No such plugin, or Failed
285                         $body = (($body == '') ? '' : '{' . $body . '}') . ';';
286                         return make_line_rules(htmlsc('&' . $this->plain) . $body);
287                 }
288         }
289 }
290
291 // Footnotes
292 class Link_note extends Link
293 {
294         function Link_note($start)
295         {
296                 $this->__construct($start);
297         }
298         function __construct($start)
299         {
300                 parent::__construct($start);
301         }
302
303         function get_pattern()
304         {
305                 return <<<EOD
306 \(\(
307  ((?>(?=\(\()(?R)|(?!\)\)).)*) # (1) note body
308 \)\)
309 EOD;
310         }
311
312         function get_count()
313         {
314                 return 1;
315         }
316
317         function set($arr, $page)
318         {
319                 global $foot_explain, $vars;
320                 static $note_id = 0;
321
322                 list(, $body) = $this->splice($arr);
323
324                 if (PKWK_ALLOW_RELATIVE_FOOTNOTE_ANCHOR) {
325                         $script = '';
326                 } else {
327                         $script = get_page_uri($page);
328                 }
329                 $id   = ++$note_id;
330                 $note = make_link($body);
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  = (PKWK_FOOTNOTE_TITLE_MAX < $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 }