+
+/**
+ * Insert array into another array after the specified *KEY*
+ * @param $array Array: The array.
+ * @param $insert Array: The array to insert.
+ * @param $after Mixed: The key to insert after
+ */
+function wfArrayInsertAfter( $array, $insert, $after ) {
+ // Find the offset of the element to insert after.
+ $keys = array_keys($array);
+ $offsetByKey = array_flip( $keys );
+
+ $offset = $offsetByKey[$after];
+
+ // Insert at the specified offset
+ $before = array_slice( $array, 0, $offset + 1, true );
+ $after = array_slice( $array, $offset + 1, count($array)-$offset, true );
+
+ $output = $before + $insert + $after;
+
+ return $output;
+}
+
+/* Recursively converts the parameter (an object) to an array with the same data */
+function wfObjectToArray( $object, $recursive = true ) {
+ $array = array();
+ foreach ( get_object_vars($object) as $key => $value ) {
+ if ( is_object($value) && $recursive ) {
+ $value = wfObjectToArray( $value );
+ }
+
+ $array[$key] = $value;
+ }
+
+ 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;
+}