* @return String
*/
function wfEscapeWikiText( $text ) {
- $text = strtr( "\n$text", array(
- '"' => '"', '&' => '&', "'" => ''', '<' => '<',
- '=' => '=', '>' => '>', '[' => '[', ']' => ']',
- '{' => '{', '|' => '|', '}' => '}',
- "\n#" => "\n#", "\n*" => "\n*",
- "\n:" => "\n:", "\n;" => "\n;",
- '://' => '://', 'ISBN ' => 'ISBN ', 'RFC ' => 'RFC ',
- ) );
- return substr( $text, 1 );
+ static $repl = null, $repl2 = null;
+ if ( $repl === null ) {
+ $repl = array(
+ '"' => '"', '&' => '&', "'" => ''', '<' => '<',
+ '=' => '=', '>' => '>', '[' => '[', ']' => ']',
+ '{' => '{', '|' => '|', '}' => '}',
+ "\n#" => "\n#", "\r#" => "\r#",
+ "\n*" => "\n*", "\r*" => "\r*",
+ "\n:" => "\n:", "\r:" => "\r:",
+ "\n;" => "\n;", "\r;" => "\r;",
+ "\n " => "\n ", "\r " => "\r ",
+ '__' => '__', '://' => '://',
+ );
+
+ // We have to catch everything "\s" matches in PCRE
+ foreach ( array( '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 = array();
+ foreach ( $wgUrlProtocols as $prot ) {
+ if ( substr( $prot, -1 ) === ':' ) {
+ $repl2[] = preg_quote( substr( $prot, 0, -1 ), '/' );
+ }
+ }
+ $repl2 = $repl2 ? '/\b(' . join( '|', $repl2 ) . '):/i' : '/^(?!)/';
+ }
+ $text = substr( strtr( "\n$text", $repl ), 1 );
+ $text = preg_replace( $repl2, '$1:', $text );
+ return $text;
}
/**