Merge "Add option to chose what language to fetch file description in."
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index dd23538..414c822 100644 (file)
@@ -93,6 +93,18 @@ if ( !function_exists( 'mb_strrpos' ) ) {
                return Fallback::mb_strrpos( $haystack, $needle, $offset, $encoding );
        }
 }
+
+// gzdecode function only exists in PHP >= 5.4.0
+// http://php.net/gzdecode
+if ( !function_exists( 'gzdecode' ) ) {
+       /**
+        * @codeCoverageIgnore
+        * @return string
+        */
+       function gzdecode( $data ) {
+               return gzinflate( substr( $data, 10, -8 ) );
+       }
+}
 /// @endcond
 
 /**
@@ -2010,15 +2022,45 @@ function wfCheckLimits( $deflimit = 50, $optionname = 'rclimit' ) {
  * @return String
  */
 function wfEscapeWikiText( $text ) {
-       $text = strtr( "\n$text", array(
-               '"' => '&#34;', '&' => '&#38;', "'" => '&#39;', '<' => '&#60;',
-               '=' => '&#61;', '>' => '&#62;', '[' => '&#91;', ']' => '&#93;',
-               '{' => '&#123;', '|' => '&#124;', '}' => '&#125;',
-               "\n#" => "\n&#35;", "\n*" => "\n&#42;",
-               "\n:" => "\n&#58;", "\n;" => "\n&#59;",
-               '://' => '&#58;//', 'ISBN ' => 'ISBN&#32;', 'RFC ' => 'RFC&#32;',
-       ) );
-       return substr( $text, 1 );
+       static $repl = null, $repl2 = null;
+       if ( $repl === null ) {
+               $repl = array(
+                       '"' => '&#34;', '&' => '&#38;', "'" => '&#39;', '<' => '&#60;',
+                       '=' => '&#61;', '>' => '&#62;', '[' => '&#91;', ']' => '&#93;',
+                       '{' => '&#123;', '|' => '&#124;', '}' => '&#125;', ';' => '&#59;',
+                       "\n#" => "\n&#35;", "\r#" => "\r&#35;",
+                       "\n*" => "\n&#42;", "\r*" => "\r&#42;",
+                       "\n:" => "\n&#58;", "\r:" => "\r&#58;",
+                       "\n " => "\n&#32;", "\r " => "\r&#32;",
+                       "\n\n" => "\n&#10;", "\r\n" => "&#13;\n",
+                       "\n\r" => "\n&#13;", "\r\r" => "\r&#13;",
+                       "\n\t" => "\n&#9;", "\r\t" => "\r&#9;", // "\n\t\n" is treated like "\n\n"
+                       "\n----" => "\n&#45;---", "\r----" => "\r&#45;---",
+                       '__' => '_&#95;', '://' => '&#58;//',
+               );
+
+               // We have to catch everything "\s" matches in PCRE
+               foreach ( array( 'ISBN', 'RFC', 'PMID' ) as $magic ) {
+                       $repl["$magic "] = "$magic&#32;";
+                       $repl["$magic\t"] = "$magic&#9;";
+                       $repl["$magic\r"] = "$magic&#13;";
+                       $repl["$magic\n"] = "$magic&#10;";
+                       $repl["$magic\f"] = "$magic&#12;";
+               }
+
+               // 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&#58;', $text );
+       return $text;
 }
 
 /**
@@ -2495,7 +2537,7 @@ function wfMkdirParents( $dir, $mode = null, $caller = null ) {
                wfDebug( "$caller: called wfMkdirParents($dir)\n" );
        }
 
-       if ( strval( $dir ) === '' || file_exists( $dir ) ) {
+       if ( strval( $dir ) === '' || ( file_exists( $dir ) && is_dir( $dir ) ) ) {
                return true;
        }
 
@@ -2511,6 +2553,11 @@ function wfMkdirParents( $dir, $mode = null, $caller = null ) {
        wfRestoreWarnings();
 
        if ( !$ok ) {
+               //directory may have been created on another request since we last checked
+               if ( is_dir( $dir ) ) {
+                       return true;
+               }
+
                // PHP doesn't report the path in its warning message, so add our own to aid in diagnosis.
                wfLogWarning( sprintf( "failed to mkdir \"%s\" mode 0%o", $dir, $mode ) );
        }
@@ -3156,9 +3203,9 @@ function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1, $lowercase = t
                $sourceBase > 36 ||
                $destBase < 2 ||
                $destBase > 36 ||
-               $sourceBase != (int) $sourceBase ||
-               $destBase != (int) $destBase ||
-               $pad != (int) $pad ||
+               $sourceBase != (int)$sourceBase ||
+               $destBase != (int)$destBase ||
+               $pad != (int)$pad ||
                !preg_match( "/^[" . substr( '0123456789abcdefghijklmnopqrstuvwxyz', 0, $sourceBase ) . "]+$/i", $input )
        ) {
                return false;
@@ -3212,7 +3259,7 @@ function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1, $lowercase = t
                                $work += $digit;
 
                                if ( $workDigits || $work >= $destBase ) {
-                                       $workDigits[] = (int) ( $work / $destBase );
+                                       $workDigits[] = (int)( $work / $destBase );
                                }
                                $work %= $destBase;
                        }
@@ -3969,8 +4016,8 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
 }
 
 /**
- * Determine whether the client at a given source IP is likely to be able to 
- * access the wiki via HTTPS. 
+ * Determine whether the client at a given source IP is likely to be able to
+ * access the wiki via HTTPS.
  *
  * @param string $ip The IPv4/6 address in the normal human-readable form
  * @return boolean