/**
* Returns a regular expression of url protocols
*
+ * @param $includeProtocolRelative bool If false, remove '//' from the returned protocol list.
+ * DO NOT USE this directy, use wfUrlProtocolsWithoutProtRel() instead
* @return String
*/
-function wfUrlProtocols() {
+function wfUrlProtocols( $includeProtocolRelative = true ) {
global $wgUrlProtocols;
- static $retval = null;
- if ( !is_null( $retval ) ) {
- return $retval;
+ // Cache return values separately based on $includeProtocolRelative
+ static $retval = array( true => null, false => null );
+ if ( !is_null( $retval[$includeProtocolRelative] ) ) {
+ return $retval[$includeProtocolRelative];
}
// Support old-style $wgUrlProtocols strings, for backwards compatibility
if ( is_array( $wgUrlProtocols ) ) {
$protocols = array();
foreach ( $wgUrlProtocols as $protocol ) {
- $protocols[] = preg_quote( $protocol, '/' );
+ // Filter out '//' if !$includeProtocolRelative
+ if ( $includeProtocolRelative || $protocol !== '//' ) {
+ $protocols[] = preg_quote( $protocol, '/' );
+ }
}
- $retval = implode( '|', $protocols );
+ $retval[$includeProtocolRelative] = implode( '|', $protocols );
} else {
- $retval = $wgUrlProtocols;
+ // Ignore $includeProtocolRelative in this case
+ // This case exists for pre-1.6 compatibility, and we can safely assume
+ // that '//' won't appear in a pre-1.6 config because protocol-relative
+ // URLs weren't supported until 1.18
+ $retval[$includeProtocolRelative] = $wgUrlProtocols;
}
- return $retval;
+ return $retval[$includeProtocolRelative];
+}
+
+/**
+ * Like wfUrlProtocols(), but excludes '//' from the protocol list. Use this if
+ * you need a regex that matches all URL protocols but does not match protocol-
+ * relative URLs
+ */
+function wfUrlProtocolsWithoutProtRel() {
+ return wfUrlProtocols( false );
}
/**
// encode all comments or tags as safe blue strings
$text = preg_replace( '/\<(!--.*?--|.*?)\>/', '<span style="color:blue;"><\1></span>', $text );
// identify URLs
- $protos = wfUrlProtocols();
+ $protos = wfUrlProtocolsWithoutProtRel();
// This regex hacks around bug 13218 (" included in the URL)
$text = preg_replace( "#(($protos).*?)(")?([ \\'\"<>\n]|<|>|")#", '<a href="\\1">\\1</a>\\3\\4', $text );
// identify requests to api.php