- static $repl = null, $repl2 = null;
- if ( $repl === null ) {
- $repl = [
- '"' => '"', '&' => '&', "'" => ''', '<' => '<',
- '=' => '=', '>' => '>', '[' => '[', ']' => ']',
- '{' => '{', '|' => '|', '}' => '}', ';' => ';',
- "\n#" => "\n#", "\r#" => "\r#",
- "\n*" => "\n*", "\r*" => "\r*",
- "\n:" => "\n:", "\r:" => "\r:",
- "\n " => "\n ", "\r " => "\r ",
- "\n\n" => "\n ", "\r\n" => " \n",
- "\n\r" => "\n ", "\r\r" => "\r ",
- "\n\t" => "\n	", "\r\t" => "\r	", // "\n\t\n" is treated like "\n\n"
- "\n----" => "\n----", "\r----" => "\r----",
- '__' => '__', '://' => '://',
- ];
-
- // We have to catch everything "\s" matches in PCRE
- foreach ( [ 'ISBN', 'RFC', 'PMID' ] as $magic ) {
- $repl["$magic "] = "$magic ";
- $repl["$magic\t"] = "$magic	";
- $repl["$magic\r"] = "$magic ";
- $repl["$magic\n"] = "$magic ";
- $repl["$magic\f"] = "$magic";
- }
-
- // And handle protocols that don't use "://"
- global $wgUrlProtocols;
- $repl2 = [];
- foreach ( $wgUrlProtocols as $prot ) {
- if ( substr( $prot, -1 ) === ':' ) {
- $repl2[] = preg_quote( substr( $prot, 0, -1 ), '/' );
- }
- }
- $repl2 = $repl2 ? '/\b(' . implode( '|', $repl2 ) . '):/i' : '/^(?!)/';
- }
- $text = substr( strtr( "\n$text", $repl ), 1 );
- $text = preg_replace( $repl2, '$1:', $text );
- return $text;