<?php
+/**
+ * Global functions used everywhere
+ * @file
+ */
if ( !defined( 'MEDIAWIKI' ) ) {
die( "This file is part of MediaWiki, it is not a valid entry point" );
}
-/**
- * Global functions used everywhere
- */
-
require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php';
// Hide compatibility functions from Doxygen
# Assume will only ever use utf-8 and iso-8859-1.
# This will *not* work in all circumstances.
function iconv( $from, $to, $string ) {
+ if ( substr( $to, -8 ) == '//IGNORE' ) $to = substr( $to, 0, strlen( $to ) - 8 );
if(strcasecmp( $from, $to ) == 0) return $string;
if(strcasecmp( $from, 'utf-8' ) == 0) return utf8_decode( $string );
if(strcasecmp( $to, 'utf-8' ) == 0) return utf8_encode( $string );
$split = mb_substr_split_unicode( $str, intval( $start ) );
$str = substr( $str, $split );
}
-
+
if( $count !== 'end' ) {
$split = mb_substr_split_unicode( $str, intval( $count ) );
$str = substr( $str, 0, $split );
}
-
+
return $str;
}
-
+
function mb_substr_split_unicode( $str, $splitPos ) {
if( $splitPos == 0 ) {
return 0;
}
-
+
$byteLen = strlen( $str );
-
+
if( $splitPos > 0 ) {
if( $splitPos > 256 ) {
// Optimize large string offsets by skipping ahead N bytes.
$charPos = 0;
$bytePos = 0;
}
-
+
while( $charPos++ < $splitPos ) {
++$bytePos;
// Move past any tail bytes
--$bytePos;
}
}
-
+
return $bytePos;
}
}
$ar = array();
preg_match_all( '/'.$needle.'/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
- if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
+ if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
isset( $ar[0][count($ar[0])-1][1] ) ) {
return $ar[0][count($ar[0])-1][1];
} else {
return false;
- }
+ }
}
}
*
* ;:@$!*(),/
*
- * However, IIS7 redirects fail when the url contains a colon (Bug 22709),
+ * However, IIS7 redirects fail when the url contains a colon (Bug 22709),
* so no fancy : for IIS7.
- *
+ *
* %2F in the page titles seems to fatally break for some reason.
*
* @param $s String:
if (! isset($_SERVER['SERVER_SOFTWARE']) || ( strpos($_SERVER['SERVER_SOFTWARE'], "Microsoft-IIS/7") === false)) {
$needle[] = '%3A';
}
- }
-
+ }
+
$s = urlencode( $s );
$s = str_ireplace(
$needle,
/**
* Log to a file without getting "file size exceeded" signals.
- *
- * Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will
+ *
+ * Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will
* send lines to the specified port, prefixed by the specified prefix and a space.
*/
function wfErrorLog( $text, $file ) {
if ( substr( $file, 0, 4 ) == 'udp:' ) {
+ # Needs the sockets extension
if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
// IPv6 bracketed host
$protocol = $m[1];
# Great, we already have the object (hopefully)!
return $langcode;
}
-
+
global $wgContLang, $wgLanguageCode;
if( $langcode === true || $langcode === $wgLanguageCode ) {
# $langcode is the language code of the wikis content language object.
# or it is a boolean and value is true
return $wgContLang;
}
-
+
global $wgLang;
if( $langcode === false || $langcode === $wgLang->getCode() ) {
# $langcode is the language code of user language object.
return $wgContLang;
}
+/**
+ * Use this instead of $wgContLang, when working with user interface.
+ * User interface is currently hard coded according to wiki content language
+ * in many ways, especially regarding to text direction. There is lots stuff
+ * to fix, hence this function to keep the old behaviour unless the global
+ * $wgBetterDirectionality is enabled (or removed when everything works).
+ */
function wfUILang() {
global $wgBetterDirectionality;
- return wfGetLangObj( $wgBetterDirectionality ? false: true );
+ return wfGetLangObj( !$wgBetterDirectionality );
}
/**
* defined in languages/Language.php
*
* This function also takes extra optional parameters (not
- * shown in the function definition), which can by used to
+ * shown in the function definition), which can be used to
* insert variable text into the predefined message.
*/
function wfMsg( $key ) {
* @return string
*/
function wfMsgGetKey( $key, $useDB, $langCode = false, $transform = true ) {
- global $wgContLang, $wgMessageCache;
+ global $wgMessageCache;
wfRunHooks('NormalizeMessageKey', array(&$key, &$useDB, &$langCode, &$transform));
-
+
if ( !is_object( $wgMessageCache ) ) {
throw new MWException( "Trying to get message before message cache is initialised" );
}
*/
function wfShowingResultsNum( $offset, $limit, $num ) {
global $wgLang;
- return wfMsgExt( 'showingresultsnum', array( 'parseinline' ), $wgLang->formatNum( $limit ),
+ return wfMsgExt( 'showingresultsnum', array( 'parseinline' ), $wgLang->formatNum( $limit ),
$wgLang->formatNum( $offset+1 ), $wgLang->formatNum( $num ) );
}
$q .= '&'.$query;
}
$plink = '<a href="' . $title->escapeLocalUrl( $q ) . "\" title=\"{$pTitle}\" class=\"mw-prevlink\">{$prev}</a>";
- } else {
+ } else {
$plink = $prev;
}
# Make 'next' link
$nlink = '<a href="' . $title->escapeLocalUrl( $q ) . "\" title=\"{$nTitle}\" class=\"mw-nextlink\">{$next}</a>";
}
# Make links to set number of items per page
- $nums = $wgLang->pipeList( array(
+ $nums = $wgLang->pipeList( array(
wfNumLink( $offset, 20, $title, $query ),
wfNumLink( $offset, 50, $title, $query ),
wfNumLink( $offset, 100, $title, $query ),
*/
function wfNumLink( $offset, $limit, $title, $query = '' ) {
global $wgLang;
- if( $query == '' ) {
+ if( $query == '' ) {
$q = '';
- } else {
+ } else {
$q = $query.'&';
}
$q .= "limit={$limit}&offset={$offset}";
* Windows doesn't recognise single-quotes in the shell, but the escapeshellarg()
* function puts single quotes in regardless of OS.
*
- * Also fixes the locale problems on Linux in PHP 5.2.6+ (bug backported to
+ * Also fixes the locale problems on Linux in PHP 5.2.6+ (bug backported to
* earlier distro releases of PHP)
*/
function wfEscapeShellArg( ) {
// Double the backslashes before any double quotes. Escape the double quotes.
$tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
$arg = '';
- $delim = false;
+ $iteration = 0;
foreach ( $tokens as $token ) {
- if ( $delim ) {
+ if ( $iteration % 2 == 1 ) {
+ // Delimiter, a double quote preceded by zero or more slashes
$arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
- } else {
+ } else if ( $iteration % 4 == 2 ) {
+ // ^ in $token will be outside quotes, need to be escaped
+ $arg .= str_replace( '^', '^^', $token );
+ } else { // $iteration % 4 == 0
+ // ^ in $token will appear inside double quotes, so leave as is
$arg .= $token;
}
- $delim = !$delim;
+ $iteration++;
}
// Double the backslashes before the end of the string, because
// we will soon add a quote
if ($before == $after) {
return '';
}
-
+
global $wgDiff;
# This check may also protect against code injection in
fwrite( $oldtextFile, $before ); fclose( $oldtextFile );
fwrite( $newtextFile, $after ); fclose( $newtextFile );
-
+
// Get the diff of the two files
$cmd = "$wgDiff " . $params . ' ' .wfEscapeShellArg( $oldtextName, $newtextName );
-
+
$h = popen( $cmd, 'r' );
-
+
$diff = '';
-
+
do {
$data = fread( $h, 8192 );
if ( strlen( $data ) == 0 ) {
}
$diff .= $data;
} while ( true );
-
+
// Clean up
pclose( $h );
unlink( $oldtextName );
unlink( $newtextName );
-
+
// Kill the --- and +++ lines. They're not useful.
$diff_lines = explode( "\n", $diff );
if (strpos( $diff_lines[0], '---' ) === 0) {
if (strpos( $diff_lines[1], '+++' ) === 0) {
unset($diff_lines[1]);
}
-
+
$diff = implode( "\n", $diff_lines );
-
+
return $diff;
}
}
function wfGetSiteNotice() {
- global $wgUser, $wgSiteNotice;
+ global $wgUser;
$fname = 'wfGetSiteNotice';
wfProfileIn( $fname );
$siteNotice = '';
* we'll use sys_get_temp_dir(). The TMPDIR, TMP, and TEMP environment
* variables are then checked in sequence, and if none are set /tmp is
* returned as the generic Unix default.
+ * It is common to call it with tempnam().
*
- * NOTE: When possible, use the tempfile() function to create temporary
- * files to avoid race conditions on file creation, etc.
+ * NOTE: When possible, use instead the tmpfile() function to create
+ * temporary files to avoid race conditions on file creation, etc.
*
* @return String
*/
/**
* Make directory, and make all parent directories if they don't exist
- *
+ *
* @param $dir String: full path to directory to create
* @param $mode Integer: chmod value to use, default is $wgDirectoryMode
* @param $caller String: optional caller param for debugging.
$cmd = escapeshellarg( $script ) . " $time $mem $filesize " . escapeshellarg( $cmd );
}
}
- } elseif ( php_uname( 's' ) == 'Windows NT' &&
- version_compare( PHP_VERSION, '5.3.0', '<' ) )
+ } elseif ( php_uname( 's' ) == 'Windows NT' &&
+ version_compare( PHP_VERSION, '5.3.0', '<' ) )
{
# This is a hack to work around PHP's flawed invocation of cmd.exe
# http://news.php.net/php.internals/21796
/**
* Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
* e.g.
- * wfMergeErrorArrays(
- * array( array( 'x' ) ),
- * array( array( 'x', '2' ) ),
- * array( array( 'x' ) ),
+ * wfMergeErrorArrays(
+ * array( array( 'x' ) ),
+ * array( array( 'x', '2' ) ),
+ * array( array( 'x' ) ),
* array( array( 'y') )
* );
* returns:
- * array(
+ * array(
* array( 'x', '2' ),
* array( 'x' ),
* array( 'y' )
* Initialise php session
*/
function wfSetupSession() {
- global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain,
+ global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain,
$wgCookieSecure, $wgCookieHttpOnly, $wgSessionHandler;
if( $wgSessionsInMemcached ) {
require_once( 'MemcachedSessions.php' );
*
* ignoreRedirect: If true, do not follow file redirects
*
- * private: If true, return restricted (deleted) files if the current
+ * private: If true, return restricted (deleted) files if the current
* user is allowed to view them. Otherwise, such files will not
* be found.
*
}
/**
- * Count down from $n to zero on the terminal, with a one-second pause
+ * Count down from $n to zero on the terminal, with a one-second pause
* between showing each number. For use in command-line scripts.
*/
function wfCountDown( $n ) {
for ( $i = $n; $i >= 0; $i-- ) {
if ( $i != $n ) {
echo str_repeat( "\x08", strlen( $i + 1 ) );
- }
+ }
echo $i;
flush();
if ( $i ) {
// 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 ) {
+ 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 () {
+function wfMemoryLimit() {
global $wgMemoryLimit;
$memlimit = wfShorthandToInteger( ini_get( "memory_limit" ) );
$conflimit = wfShorthandToInteger( $wgMemoryLimit );