* (bug 20131) PHP Notice: Undfined index: page_latest in includes/ChangesList.php...
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 621b1fb..b66589a 100644 (file)
@@ -1303,8 +1303,12 @@ function wfAppendQuery( $url, $query ) {
 }
 
 /**
- * Expand a potentially local URL to a fully-qualified URL.
- * Assumes $wgServer is correct. :)
+ * Expand a potentially local URL to a fully-qualified URL.  Assumes $wgServer
+ * is correct.  Also doesn't handle any type of relative URL except one
+ * starting with a single "/": this won't work with current-path-relative URLs
+ * like "subdir/foo.html", protocol-relative URLs like
+ * "//en.wikipedia.org/wiki/", etc.  TODO: improve this!
+ *
  * @param $url String: either fully-qualified or a local path + query
  * @return string Fully-qualified URL
  */
@@ -2718,10 +2722,7 @@ function wfCreateObject( $name, $p ){
  */
 function wfGetHTTP( $url ) {
        wfDeprecated(__FUNCTION__);
-       $status = Http::get( $url );
-       if( $status->isOK() )
-               return $status->value;          
-       return null;
+       return Http::get( $url );
 }
 
 /**
@@ -2834,6 +2835,7 @@ function wfFormatStackFrame($frame) {
 function wfMemcKey( /*... */ ) {
        $args = func_get_args();
        $key = wfWikiID() . ':' . implode( ':', $args );
+       $key = str_replace( ' ', '_', $key );
        return $key;
 }
 
@@ -3190,3 +3192,78 @@ function wfObjectToArray( $object, $recursive = true ) {
        
        return $array;
 }
+
+/**
+ * Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit;
+ * @return Integer value memory was set to.
+ */
+function wfMemoryLimit () {
+       global $wgMemoryLimit;
+       $memlimit = wfShorthandToInteger( ini_get( "memory_limit" ) );
+       $conflimit = wfShorthandToInteger( $wgMemoryLimit );
+       if( $memlimit != -1 ) {
+               if( $conflimit == -1 ) {
+                       wfDebug( "Removing PHP's memory limit\n" );
+                       wfSuppressWarnings();
+                       ini_set( "memory_limit", $conflimit );
+                       wfRestoreWarnings();
+                       return $conflimit;
+               } elseif ( $conflimit > $memlimit ) {
+                       wfDebug( "Raising PHP's memory limit to $conflimit bytes\n" );
+                       wfSuppressWarnings();
+                       ini_set( "memory_limit", $conflimit );
+                       wfRestoreWarnings();
+                       return $conflimit;
+               }
+       }
+       return $memlimit;
+}
+
+/**
+ * Converts shorthand byte notation to integer form
+ * @param $string String
+ * @return Integer
+ */
+function wfShorthandToInteger ( $string = '' ) {
+       $string = trim($string);
+       if( empty($string) ) { return -1; }
+       $last = strtolower($string[strlen($string)-1]);
+       $val = intval($string);
+       switch($last) {
+               case 'g':
+                       $val *= 1024;
+               case 'm':
+                       $val *= 1024;
+               case 'k':
+                       $val *= 1024;
+       }
+
+       return $val;
+}
+
+/* Get the normalised IETF language tag
+ * @param $code String: The language code.
+ * @return $langCode String: The language code which complying with BCP 47 standards.
+ */
+function wfBCP47( $code ) {
+       $codeSegment = explode( '-', $code );
+       foreach ( $codeSegment as $segNo => $seg ) {
+               if ( count( $codeSegment ) > 0 ) {
+                       // ISO 3166 country code
+                       if ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) )
+                               $codeBCP[$segNo] = strtoupper( $seg );
+                       // ISO 15924 script code
+                       else if ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) )
+                               $codeBCP[$segNo] = ucfirst( $seg );
+                       // Use lowercase for other cases
+                       else
+                               $codeBCP[$segNo] = strtolower( $seg );
+               } else {
+               // Use lowercase for single segment
+                       $codeBCP[$segNo] = strtolower( $seg );
+               }
+       }
+       $langCode = implode ( '-' , $codeBCP );
+       return $langCode;
+}