2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2010-08-01 12:31:50 +00:00
|
|
|
/**
|
2012-05-14 17:59:58 +00:00
|
|
|
* Global functions used everywhere.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-01 12:31:50 +00:00
|
|
|
* @file
|
|
|
|
|
*/
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2007-02-09 09:28:35 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
|
die( "This file is part of MediaWiki, it is not a valid entry point" );
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-06 03:55:49 +00:00
|
|
|
// Hide compatibility functions from Doxygen
|
|
|
|
|
/// @cond
|
|
|
|
|
|
2008-07-07 20:15:16 +00:00
|
|
|
/**
|
|
|
|
|
* Compatibility functions
|
|
|
|
|
*
|
2012-05-26 16:31:32 +00:00
|
|
|
* We support PHP 5.3.2 and up.
|
2008-07-07 20:15:16 +00:00
|
|
|
* Re-implementations of newer functions or functions in non-standard
|
|
|
|
|
* PHP extensions may be included here.
|
|
|
|
|
*/
|
2011-01-02 01:29:00 +00:00
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if( !function_exists( 'iconv' ) ) {
|
2012-02-09 21:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2008-07-07 20:15:16 +00:00
|
|
|
function iconv( $from, $to, $string ) {
|
2011-01-04 01:44:11 +00:00
|
|
|
return Fallback::iconv( $from, $to, $string );
|
2008-07-07 20:15:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-02 01:29:00 +00:00
|
|
|
if ( !function_exists( 'mb_substr' ) ) {
|
2012-02-09 21:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-01-02 01:29:00 +00:00
|
|
|
function mb_substr( $str, $start, $count='end' ) {
|
2011-01-04 01:44:11 +00:00
|
|
|
return Fallback::mb_substr( $str, $start, $count );
|
2008-07-07 20:15:16 +00:00
|
|
|
}
|
2011-01-02 01:29:00 +00:00
|
|
|
|
2012-02-09 21:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2011-01-02 01:29:00 +00:00
|
|
|
function mb_substr_split_unicode( $str, $splitPos ) {
|
2011-01-04 01:44:11 +00:00
|
|
|
return Fallback::mb_substr_split_unicode( $str, $splitPos );
|
2011-01-02 01:29:00 +00:00
|
|
|
}
|
2008-07-07 20:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !function_exists( 'mb_strlen' ) ) {
|
2012-02-09 21:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function mb_strlen( $str, $enc = '' ) {
|
2011-01-04 01:44:11 +00:00
|
|
|
return Fallback::mb_strlen( $str, $enc );
|
2008-07-07 20:15:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-30 19:11:48 +00:00
|
|
|
if( !function_exists( 'mb_strpos' ) ) {
|
2012-02-09 21:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
|
2011-01-04 01:44:11 +00:00
|
|
|
return Fallback::mb_strpos( $haystack, $needle, $offset, $encoding );
|
2011-01-02 01:29:00 +00:00
|
|
|
}
|
2011-06-30 02:59:43 +00:00
|
|
|
|
2011-01-02 01:29:00 +00:00
|
|
|
}
|
2009-03-30 19:11:48 +00:00
|
|
|
|
|
|
|
|
if( !function_exists( 'mb_strrpos' ) ) {
|
2012-02-09 21:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
|
2011-01-04 01:44:11 +00:00
|
|
|
return Fallback::mb_strrpos( $haystack, $needle, $offset, $encoding );
|
2009-03-30 19:11:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-02 01:29:00 +00:00
|
|
|
|
2009-02-04 09:10:32 +00:00
|
|
|
// Support for Wietse Venema's taint feature
|
|
|
|
|
if ( !function_exists( 'istainted' ) ) {
|
2012-02-09 21:33:27 +00:00
|
|
|
/**
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2009-02-04 09:10:32 +00:00
|
|
|
function istainted( $var ) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-02-06 22:14:32 +00:00
|
|
|
/** @codeCoverageIgnore */
|
2009-02-04 09:10:32 +00:00
|
|
|
function taint( $var, $level = 0 ) {}
|
2011-02-06 22:14:32 +00:00
|
|
|
/** @codeCoverageIgnore */
|
2009-02-04 09:10:32 +00:00
|
|
|
function untaint( $var, $level = 0 ) {}
|
|
|
|
|
define( 'TC_HTML', 1 );
|
|
|
|
|
define( 'TC_SHELL', 1 );
|
|
|
|
|
define( 'TC_MYSQL', 1 );
|
|
|
|
|
define( 'TC_PCRE', 1 );
|
|
|
|
|
define( 'TC_SELF', 1 );
|
|
|
|
|
}
|
2008-08-06 03:55:49 +00:00
|
|
|
/// @endcond
|
|
|
|
|
|
2008-02-21 09:32:59 +00:00
|
|
|
/**
|
|
|
|
|
* Like array_diff( $a, $b ) except that it works with two-dimensional arrays.
|
2011-11-28 15:19:20 +00:00
|
|
|
* @param $a array
|
|
|
|
|
* @param $b array
|
|
|
|
|
* @return array
|
2008-02-21 09:32:59 +00:00
|
|
|
*/
|
|
|
|
|
function wfArrayDiff2( $a, $b ) {
|
|
|
|
|
return array_udiff( $a, $b, 'wfArrayDiff2_cmp' );
|
|
|
|
|
}
|
2011-05-28 18:59:42 +00:00
|
|
|
|
2011-05-29 14:24:27 +00:00
|
|
|
/**
|
|
|
|
|
* @param $a
|
|
|
|
|
* @param $b
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2008-02-21 09:32:59 +00:00
|
|
|
function wfArrayDiff2_cmp( $a, $b ) {
|
|
|
|
|
if ( !is_array( $a ) ) {
|
|
|
|
|
return strcmp( $a, $b );
|
|
|
|
|
} elseif ( count( $a ) !== count( $b ) ) {
|
|
|
|
|
return count( $a ) < count( $b ) ? -1 : 1;
|
|
|
|
|
} else {
|
|
|
|
|
reset( $a );
|
|
|
|
|
reset( $b );
|
2010-11-30 18:44:50 +00:00
|
|
|
while( ( list( , $valueA ) = each( $a ) ) && ( list( , $valueB ) = each( $b ) ) ) {
|
2008-02-21 09:32:59 +00:00
|
|
|
$cmp = strcmp( $valueA, $valueB );
|
|
|
|
|
if ( $cmp !== 0 ) {
|
|
|
|
|
return $cmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-03 02:59:05 +00:00
|
|
|
|
2011-05-20 10:55:34 +00:00
|
|
|
/**
|
|
|
|
|
* Array lookup
|
|
|
|
|
* Returns an array where the values in the first array are replaced by the
|
|
|
|
|
* values in the second array with the corresponding keys
|
|
|
|
|
*
|
|
|
|
|
* @param $a Array
|
|
|
|
|
* @param $b Array
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
function wfArrayLookup( $a, $b ) {
|
|
|
|
|
return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Appends to second array if $value differs from that in $default
|
|
|
|
|
*
|
|
|
|
|
* @param $key String|Int
|
|
|
|
|
* @param $value Mixed
|
|
|
|
|
* @param $default Mixed
|
|
|
|
|
* @param $changed Array to alter
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2011-05-20 10:55:34 +00:00
|
|
|
*/
|
|
|
|
|
function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) {
|
|
|
|
|
if ( is_null( $changed ) ) {
|
|
|
|
|
throw new MWException( 'GlobalFunctions::wfAppendToArrayIfNotDefault got null' );
|
|
|
|
|
}
|
|
|
|
|
if ( $default[$key] !== $value ) {
|
|
|
|
|
$changed[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Backwards array plus for people who haven't bothered to read the PHP manual
|
|
|
|
|
* XXX: will not darn your socks for you.
|
|
|
|
|
*
|
|
|
|
|
* @param $array1 Array
|
|
|
|
|
* @param [$array2, [...]] Arrays
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
function wfArrayMerge( $array1/* ... */ ) {
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$args = array_reverse( $args, true );
|
|
|
|
|
$out = array();
|
|
|
|
|
foreach ( $args as $arg ) {
|
|
|
|
|
$out += $arg;
|
|
|
|
|
}
|
|
|
|
|
return $out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
|
|
|
|
|
* e.g.
|
|
|
|
|
* wfMergeErrorArrays(
|
|
|
|
|
* array( array( 'x' ) ),
|
|
|
|
|
* array( array( 'x', '2' ) ),
|
|
|
|
|
* array( array( 'x' ) ),
|
2011-09-18 23:21:46 +00:00
|
|
|
* array( array( 'y' ) )
|
2011-05-20 10:55:34 +00:00
|
|
|
* );
|
|
|
|
|
* returns:
|
|
|
|
|
* array(
|
|
|
|
|
* array( 'x', '2' ),
|
|
|
|
|
* array( 'x' ),
|
|
|
|
|
* array( 'y' )
|
|
|
|
|
* )
|
|
|
|
|
* @param varargs
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
function wfMergeErrorArrays( /*...*/ ) {
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$out = array();
|
|
|
|
|
foreach ( $args as $errors ) {
|
|
|
|
|
foreach ( $errors as $params ) {
|
|
|
|
|
# @todo FIXME: Sometimes get nested arrays for $params,
|
|
|
|
|
# which leads to E_NOTICEs
|
|
|
|
|
$spec = implode( "\t", $params );
|
|
|
|
|
$out[$spec] = $params;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array_values( $out );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
2012-03-15 22:40:27 +00:00
|
|
|
function wfArrayInsertAfter( array $array, array $insert, $after ) {
|
2011-05-20 10:55:34 +00:00
|
|
|
// 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
|
|
|
|
|
*
|
|
|
|
|
* @param $objOrArray Object|Array
|
|
|
|
|
* @param $recursive Bool
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
function wfObjectToArray( $objOrArray, $recursive = true ) {
|
|
|
|
|
$array = array();
|
|
|
|
|
if( is_object( $objOrArray ) ) {
|
|
|
|
|
$objOrArray = get_object_vars( $objOrArray );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $objOrArray as $key => $value ) {
|
|
|
|
|
if ( $recursive && ( is_object( $value ) || is_array( $value ) ) ) {
|
|
|
|
|
$value = wfObjectToArray( $value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array[$key] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper around array_map() which also taints variables
|
|
|
|
|
*
|
|
|
|
|
* @param $function Callback
|
|
|
|
|
* @param $input Array
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
function wfArrayMap( $function, $input ) {
|
|
|
|
|
$ret = array_map( $function, $input );
|
|
|
|
|
foreach ( $ret as $key => $value ) {
|
|
|
|
|
$taint = istainted( $input[$key] );
|
|
|
|
|
if ( $taint ) {
|
|
|
|
|
taint( $ret[$key], $taint );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-11 17:34:39 +00:00
|
|
|
/**
|
|
|
|
|
* Get a random decimal value between 0 and 1, in a way
|
|
|
|
|
* not likely to give duplicate values for any realistic
|
|
|
|
|
* number of articles.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2007-04-19 10:54:48 +00:00
|
|
|
function wfRandom() {
|
2004-10-11 17:34:39 +00:00
|
|
|
# The maximum random value is "only" 2^31-1, so get two random
|
|
|
|
|
# values to reduce the chance of dupes
|
2006-12-11 21:05:14 +00:00
|
|
|
$max = mt_getrandmax() + 1;
|
2010-08-14 13:31:10 +00:00
|
|
|
$rand = number_format( ( mt_rand() * $max + mt_rand() )
|
2004-10-11 17:34:39 +00:00
|
|
|
/ $max / $max, 12, '.', '' );
|
|
|
|
|
return $rand;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
/**
|
|
|
|
|
* Get a random string containing a number of pesudo-random hex
|
|
|
|
|
* characters.
|
|
|
|
|
* @note This is not secure, if you are trying to generate some sort
|
|
|
|
|
* of token please use MWCryptRand instead.
|
|
|
|
|
*
|
|
|
|
|
* @param $length int The length of the string to generate
|
|
|
|
|
* @return String
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
function wfRandomString( $length = 32 ) {
|
|
|
|
|
$str = '';
|
|
|
|
|
while ( strlen( $str ) < $length ) {
|
|
|
|
|
$str .= dechex( mt_rand() );
|
|
|
|
|
}
|
|
|
|
|
return substr( $str, 0, $length );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-08-08 15:45:52 +00:00
|
|
|
* We want some things to be included as literal characters in our title URLs
|
|
|
|
|
* for prettiness, which urlencode encodes by default. According to RFC 1738,
|
|
|
|
|
* all of the following should be safe:
|
|
|
|
|
*
|
|
|
|
|
* ;:@&=$-_.+!*'(),
|
|
|
|
|
*
|
|
|
|
|
* But + is not safe because it's used to indicate a space; &= are only safe in
|
|
|
|
|
* paths and not in queries (and we don't distinguish here); ' seems kind of
|
|
|
|
|
* scary; and urlencode() doesn't touch -_. to begin with. Plus, although /
|
|
|
|
|
* is reserved, we don't care. So the list we unescape is:
|
|
|
|
|
*
|
|
|
|
|
* ;:@$!*(),/
|
|
|
|
|
*
|
2010-08-01 12:31:50 +00:00
|
|
|
* However, IIS7 redirects fail when the url contains a colon (Bug 22709),
|
2010-03-03 22:01:09 +00:00
|
|
|
* so no fancy : for IIS7.
|
2010-08-01 12:31:50 +00:00
|
|
|
*
|
2004-09-02 23:28:24 +00:00
|
|
|
* %2F in the page titles seems to fatally break for some reason.
|
2004-09-03 17:13:55 +00:00
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $s String:
|
2004-09-03 17:13:55 +00:00
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2008-08-08 15:45:52 +00:00
|
|
|
function wfUrlencode( $s ) {
|
2010-03-03 22:01:09 +00:00
|
|
|
static $needle;
|
2011-07-23 20:14:12 +00:00
|
|
|
if ( is_null( $s ) ) {
|
|
|
|
|
$needle = null;
|
2011-10-15 22:58:42 +00:00
|
|
|
return '';
|
2011-07-23 20:14:12 +00:00
|
|
|
}
|
2011-08-25 16:40:49 +00:00
|
|
|
|
2010-03-03 22:01:09 +00:00
|
|
|
if ( is_null( $needle ) ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
$needle = array( '%3B', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%2F' );
|
|
|
|
|
if ( !isset( $_SERVER['SERVER_SOFTWARE'] ) || ( strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7' ) === false ) ) {
|
2010-03-03 22:01:09 +00:00
|
|
|
$needle[] = '%3A';
|
|
|
|
|
}
|
2010-08-01 12:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
2004-03-06 01:49:16 +00:00
|
|
|
$s = urlencode( $s );
|
2008-08-08 15:45:52 +00:00
|
|
|
$s = str_ireplace(
|
2010-03-03 22:01:09 +00:00
|
|
|
$needle,
|
2010-08-14 13:31:10 +00:00
|
|
|
array( ';', '@', '$', '!', '*', '(', ')', ',', '/', ':' ),
|
2008-08-08 15:45:52 +00:00
|
|
|
$s
|
|
|
|
|
);
|
2004-03-06 01:49:16 +00:00
|
|
|
|
|
|
|
|
return $s;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-16 19:09:03 +00:00
|
|
|
/**
|
|
|
|
|
* This function takes two arrays as input, and returns a CGI-style string, e.g.
|
|
|
|
|
* "days=7&limit=100". Options in the first array override options in the second.
|
2011-12-11 18:25:23 +00:00
|
|
|
* Options set to null or false will not be output.
|
2011-06-16 19:09:03 +00:00
|
|
|
*
|
2011-06-30 02:59:43 +00:00
|
|
|
* @param $array1 Array ( String|Array )
|
|
|
|
|
* @param $array2 Array ( String|Array )
|
2011-06-23 15:25:07 +00:00
|
|
|
* @param $prefix String
|
2011-06-16 19:09:03 +00:00
|
|
|
* @return String
|
|
|
|
|
*/
|
2012-05-05 17:02:58 +00:00
|
|
|
function wfArrayToCgi( $array1, $array2 = null, $prefix = '' ) {
|
2011-06-16 19:09:03 +00:00
|
|
|
if ( !is_null( $array2 ) ) {
|
|
|
|
|
$array1 = $array1 + $array2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cgi = '';
|
|
|
|
|
foreach ( $array1 as $key => $value ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
if ( !is_null($value) && $value !== false ) {
|
2011-06-16 19:09:03 +00:00
|
|
|
if ( $cgi != '' ) {
|
|
|
|
|
$cgi .= '&';
|
|
|
|
|
}
|
2011-06-23 15:25:07 +00:00
|
|
|
if ( $prefix !== '' ) {
|
|
|
|
|
$key = $prefix . "[$key]";
|
|
|
|
|
}
|
2011-06-16 19:09:03 +00:00
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
$firstTime = true;
|
2011-06-23 15:25:07 +00:00
|
|
|
foreach ( $value as $k => $v ) {
|
|
|
|
|
$cgi .= $firstTime ? '' : '&';
|
|
|
|
|
if ( is_array( $v ) ) {
|
2012-05-05 17:02:58 +00:00
|
|
|
$cgi .= wfArrayToCgi( $v, null, $key . "[$k]" );
|
2011-06-23 15:25:07 +00:00
|
|
|
} else {
|
|
|
|
|
$cgi .= urlencode( $key . "[$k]" ) . '=' . urlencode( $v );
|
|
|
|
|
}
|
2011-06-16 19:09:03 +00:00
|
|
|
$firstTime = false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ( is_object( $value ) ) {
|
|
|
|
|
$value = $value->__toString();
|
|
|
|
|
}
|
2012-08-28 00:51:38 +00:00
|
|
|
$cgi .= urlencode( $key ) . '=' . urlencode( $value );
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $cgi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-05 17:02:58 +00:00
|
|
|
* This is the logical opposite of wfArrayToCgi(): it accepts a query string as
|
2011-06-16 19:09:03 +00:00
|
|
|
* its argument and returns the same string in array form. This allows compa-
|
|
|
|
|
* tibility with legacy functions that accept raw query strings instead of nice
|
2011-12-11 18:25:23 +00:00
|
|
|
* arrays. Of course, keys and values are urldecode()d.
|
2011-06-16 19:09:03 +00:00
|
|
|
*
|
|
|
|
|
* @param $query String: query string
|
|
|
|
|
* @return array Array version of input
|
|
|
|
|
*/
|
|
|
|
|
function wfCgiToArray( $query ) {
|
2011-06-23 15:25:07 +00:00
|
|
|
if ( isset( $query[0] ) && $query[0] == '?' ) {
|
2011-06-16 19:09:03 +00:00
|
|
|
$query = substr( $query, 1 );
|
|
|
|
|
}
|
|
|
|
|
$bits = explode( '&', $query );
|
|
|
|
|
$ret = array();
|
2011-06-23 15:25:07 +00:00
|
|
|
foreach ( $bits as $bit ) {
|
|
|
|
|
if ( $bit === '' ) {
|
2011-06-16 19:09:03 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2011-12-11 18:25:23 +00:00
|
|
|
if ( strpos( $bit, '=' ) === false ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
// Pieces like &qwerty become 'qwerty' => '' (at least this is what php does)
|
|
|
|
|
$key = $bit;
|
|
|
|
|
$value = '';
|
2011-12-11 18:25:23 +00:00
|
|
|
} else {
|
|
|
|
|
list( $key, $value ) = explode( '=', $bit );
|
|
|
|
|
}
|
2012-08-28 00:51:38 +00:00
|
|
|
$key = urldecode( $key );
|
|
|
|
|
$value = urldecode( $value );
|
2011-06-23 15:25:07 +00:00
|
|
|
if ( strpos( $key, '[' ) !== false ) {
|
|
|
|
|
$keys = array_reverse( explode( '[', $key ) );
|
|
|
|
|
$key = array_pop( $keys );
|
|
|
|
|
$temp = $value;
|
|
|
|
|
foreach ( $keys as $k ) {
|
|
|
|
|
$k = substr( $k, 0, -1 );
|
|
|
|
|
$temp = array( $k => $temp );
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $ret[$key] ) ) {
|
|
|
|
|
$ret[$key] = array_merge( $ret[$key], $temp );
|
|
|
|
|
} else {
|
|
|
|
|
$ret[$key] = $temp;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$ret[$key] = $value;
|
|
|
|
|
}
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append a query string to an existing URL, which may or may not already
|
|
|
|
|
* have query string parameters already. If so, they will be combined.
|
|
|
|
|
*
|
|
|
|
|
* @param $url String
|
|
|
|
|
* @param $query Mixed: string or associative array
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfAppendQuery( $url, $query ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
if ( is_array( $query ) ) {
|
|
|
|
|
$query = wfArrayToCgi( $query );
|
|
|
|
|
}
|
|
|
|
|
if( $query != '' ) {
|
|
|
|
|
if( false === strpos( $url, '?' ) ) {
|
|
|
|
|
$url .= '?';
|
|
|
|
|
} else {
|
|
|
|
|
$url .= '&';
|
|
|
|
|
}
|
|
|
|
|
$url .= $query;
|
|
|
|
|
}
|
|
|
|
|
return $url;
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Expand a potentially local URL to a fully-qualified URL. Assumes $wgServer
|
|
|
|
|
* is correct.
|
2011-08-25 16:40:49 +00:00
|
|
|
*
|
2011-08-03 12:52:17 +00:00
|
|
|
* The meaning of the PROTO_* constants is as follows:
|
|
|
|
|
* PROTO_HTTP: Output a URL starting with http://
|
|
|
|
|
* PROTO_HTTPS: Output a URL starting with https://
|
|
|
|
|
* PROTO_RELATIVE: Output a URL starting with // (protocol-relative URL)
|
|
|
|
|
* PROTO_CURRENT: Output a URL starting with either http:// or https:// , depending on which protocol was used for the current incoming request
|
2011-08-19 15:25:50 +00:00
|
|
|
* PROTO_CANONICAL: For URLs without a domain, like /w/index.php , use $wgCanonicalServer. For protocol-relative URLs, use the protocol of $wgCanonicalServer
|
2011-09-07 14:15:03 +00:00
|
|
|
* PROTO_INTERNAL: Like PROTO_CANONICAL, but uses $wgInternalServer instead of $wgCanonicalServer
|
2011-06-16 19:09:03 +00:00
|
|
|
*
|
|
|
|
|
* @todo this won't work with current-path-relative URLs
|
|
|
|
|
* like "subdir/foo.html", etc.
|
|
|
|
|
*
|
|
|
|
|
* @param $url String: either fully-qualified or a local path + query
|
2011-11-15 18:53:20 +00:00
|
|
|
* @param $defaultProto Mixed: one of the PROTO_* constants. Determines the
|
|
|
|
|
* protocol to use if $url or $wgServer is
|
|
|
|
|
* protocol-relative
|
|
|
|
|
* @return string Fully-qualified URL, current-path-relative URL or false if
|
|
|
|
|
* no valid URL can be constructed
|
2011-06-16 19:09:03 +00:00
|
|
|
*/
|
2011-07-27 14:06:43 +00:00
|
|
|
function wfExpandUrl( $url, $defaultProto = PROTO_CURRENT ) {
|
2011-09-07 14:15:03 +00:00
|
|
|
global $wgServer, $wgCanonicalServer, $wgInternalServer;
|
|
|
|
|
$serverUrl = $wgServer;
|
|
|
|
|
if ( $defaultProto === PROTO_CANONICAL ) {
|
|
|
|
|
$serverUrl = $wgCanonicalServer;
|
|
|
|
|
}
|
|
|
|
|
// Make $wgInternalServer fall back to $wgServer if not set
|
|
|
|
|
if ( $defaultProto === PROTO_INTERNAL && $wgInternalServer !== false ) {
|
|
|
|
|
$serverUrl = $wgInternalServer;
|
|
|
|
|
}
|
2011-07-27 14:06:43 +00:00
|
|
|
if ( $defaultProto === PROTO_CURRENT ) {
|
2011-07-27 08:21:40 +00:00
|
|
|
$defaultProto = WebRequest::detectProtocol() . '://';
|
|
|
|
|
}
|
2011-08-25 16:40:49 +00:00
|
|
|
|
2011-08-19 15:25:50 +00:00
|
|
|
// Analyze $serverUrl to obtain its protocol
|
|
|
|
|
$bits = wfParseUrl( $serverUrl );
|
2011-07-27 08:21:40 +00:00
|
|
|
$serverHasProto = $bits && $bits['scheme'] != '';
|
2011-08-25 16:40:49 +00:00
|
|
|
|
2011-09-07 14:15:03 +00:00
|
|
|
if ( $defaultProto === PROTO_CANONICAL || $defaultProto === PROTO_INTERNAL ) {
|
2011-08-19 15:25:50 +00:00
|
|
|
if ( $serverHasProto ) {
|
|
|
|
|
$defaultProto = $bits['scheme'] . '://';
|
|
|
|
|
} else {
|
2011-09-07 14:15:03 +00:00
|
|
|
// $wgCanonicalServer or $wgInternalServer doesn't have a protocol. This really isn't supposed to happen
|
2011-08-19 15:25:50 +00:00
|
|
|
// Fall back to HTTP in this ridiculous case
|
|
|
|
|
$defaultProto = PROTO_HTTP;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-25 16:40:49 +00:00
|
|
|
|
2011-07-27 08:21:40 +00:00
|
|
|
$defaultProtoWithoutSlashes = substr( $defaultProto, 0, -2 );
|
2011-08-25 16:40:49 +00:00
|
|
|
|
2011-11-09 22:44:12 +00:00
|
|
|
if ( substr( $url, 0, 2 ) == '//' ) {
|
2011-11-15 18:53:20 +00:00
|
|
|
$url = $defaultProtoWithoutSlashes . $url;
|
2011-11-09 22:44:12 +00:00
|
|
|
} elseif ( substr( $url, 0, 1 ) == '/' ) {
|
2011-08-19 15:25:50 +00:00
|
|
|
// If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes, otherwise leave it alone
|
2011-11-15 18:53:20 +00:00
|
|
|
$url = ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) . $serverUrl . $url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$bits = wfParseUrl( $url );
|
|
|
|
|
if ( $bits && isset( $bits['path'] ) ) {
|
|
|
|
|
$bits['path'] = wfRemoveDotSegments( $bits['path'] );
|
|
|
|
|
return wfAssembleUrl( $bits );
|
|
|
|
|
} elseif ( $bits ) {
|
|
|
|
|
# No path to expand
|
2011-06-16 19:09:03 +00:00
|
|
|
return $url;
|
2011-11-15 18:53:20 +00:00
|
|
|
} elseif ( substr( $url, 0, 1 ) != '/' ) {
|
|
|
|
|
# URL is a relative path
|
|
|
|
|
return wfRemoveDotSegments( $url );
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
2011-11-15 18:53:20 +00:00
|
|
|
|
|
|
|
|
# Expanded URL is not valid.
|
|
|
|
|
return false;
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-15 17:38:20 +00:00
|
|
|
/**
|
|
|
|
|
* This function will reassemble a URL parsed with wfParseURL. This is useful
|
|
|
|
|
* if you need to edit part of a URL and put it back together.
|
|
|
|
|
*
|
|
|
|
|
* This is the basic structure used (brackets contain keys for $urlParts):
|
|
|
|
|
* [scheme][delimiter][user]:[pass]@[host]:[port][path]?[query]#[fragment]
|
|
|
|
|
*
|
|
|
|
|
* @todo Need to integrate this into wfExpandUrl (bug 32168)
|
|
|
|
|
*
|
2012-04-19 08:58:54 +00:00
|
|
|
* @since 1.19
|
2011-11-15 17:38:20 +00:00
|
|
|
* @param $urlParts Array URL parts, as output from wfParseUrl
|
|
|
|
|
* @return string URL assembled from its component parts
|
|
|
|
|
*/
|
|
|
|
|
function wfAssembleUrl( $urlParts ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
$result = '';
|
|
|
|
|
|
|
|
|
|
if ( isset( $urlParts['delimiter'] ) ) {
|
|
|
|
|
if ( isset( $urlParts['scheme'] ) ) {
|
|
|
|
|
$result .= $urlParts['scheme'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result .= $urlParts['delimiter'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $urlParts['host'] ) ) {
|
|
|
|
|
if ( isset( $urlParts['user'] ) ) {
|
|
|
|
|
$result .= $urlParts['user'];
|
|
|
|
|
if ( isset( $urlParts['pass'] ) ) {
|
|
|
|
|
$result .= ':' . $urlParts['pass'];
|
|
|
|
|
}
|
|
|
|
|
$result .= '@';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result .= $urlParts['host'];
|
|
|
|
|
|
|
|
|
|
if ( isset( $urlParts['port'] ) ) {
|
|
|
|
|
$result .= ':' . $urlParts['port'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $urlParts['path'] ) ) {
|
|
|
|
|
$result .= $urlParts['path'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $urlParts['query'] ) ) {
|
|
|
|
|
$result .= '?' . $urlParts['query'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $urlParts['fragment'] ) ) {
|
|
|
|
|
$result .= '#' . $urlParts['fragment'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
2011-11-15 17:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-09 22:44:12 +00:00
|
|
|
/**
|
|
|
|
|
* Remove all dot-segments in the provided URL path. For example,
|
|
|
|
|
* '/a/./b/../c/' becomes '/a/c/'. For details on the algorithm, please see
|
|
|
|
|
* RFC3986 section 5.2.4.
|
|
|
|
|
*
|
|
|
|
|
* @todo Need to integrate this into wfExpandUrl (bug 32168)
|
|
|
|
|
*
|
|
|
|
|
* @param $urlPath String URL path, potentially containing dot-segments
|
|
|
|
|
* @return string URL path with all dot-segments removed
|
|
|
|
|
*/
|
|
|
|
|
function wfRemoveDotSegments( $urlPath ) {
|
|
|
|
|
$output = '';
|
2012-01-22 04:57:37 +00:00
|
|
|
$inputOffset = 0;
|
|
|
|
|
$inputLength = strlen( $urlPath );
|
|
|
|
|
|
|
|
|
|
while ( $inputOffset < $inputLength ) {
|
|
|
|
|
$prefixLengthOne = substr( $urlPath, $inputOffset, 1 );
|
|
|
|
|
$prefixLengthTwo = substr( $urlPath, $inputOffset, 2 );
|
|
|
|
|
$prefixLengthThree = substr( $urlPath, $inputOffset, 3 );
|
|
|
|
|
$prefixLengthFour = substr( $urlPath, $inputOffset, 4 );
|
2012-01-23 19:35:05 +00:00
|
|
|
$trimOutput = false;
|
2012-01-22 04:57:37 +00:00
|
|
|
|
|
|
|
|
if ( $prefixLengthTwo == './' ) {
|
|
|
|
|
# Step A, remove leading "./"
|
|
|
|
|
$inputOffset += 2;
|
|
|
|
|
} elseif ( $prefixLengthThree == '../' ) {
|
|
|
|
|
# Step A, remove leading "../"
|
|
|
|
|
$inputOffset += 3;
|
|
|
|
|
} elseif ( ( $prefixLengthTwo == '/.' ) && ( $inputOffset + 2 == $inputLength ) ) {
|
|
|
|
|
# Step B, replace leading "/.$" with "/"
|
|
|
|
|
$inputOffset += 1;
|
|
|
|
|
$urlPath[$inputOffset] = '/';
|
|
|
|
|
} elseif ( $prefixLengthThree == '/./' ) {
|
|
|
|
|
# Step B, replace leading "/./" with "/"
|
|
|
|
|
$inputOffset += 2;
|
|
|
|
|
} elseif ( $prefixLengthThree == '/..' && ( $inputOffset + 3 == $inputLength ) ) {
|
|
|
|
|
# Step C, replace leading "/..$" with "/" and
|
|
|
|
|
# remove last path component in output
|
|
|
|
|
$inputOffset += 2;
|
|
|
|
|
$urlPath[$inputOffset] = '/';
|
2012-01-23 19:35:05 +00:00
|
|
|
$trimOutput = true;
|
2012-01-22 04:57:37 +00:00
|
|
|
} elseif ( $prefixLengthFour == '/../' ) {
|
|
|
|
|
# Step C, replace leading "/../" with "/" and
|
2011-11-10 18:02:38 +00:00
|
|
|
# remove last path component in output
|
2012-01-22 04:57:37 +00:00
|
|
|
$inputOffset += 3;
|
2012-01-23 19:35:05 +00:00
|
|
|
$trimOutput = true;
|
2012-01-22 04:57:37 +00:00
|
|
|
} elseif ( ( $prefixLengthOne == '.' ) && ( $inputOffset + 1 == $inputLength ) ) {
|
|
|
|
|
# Step D, remove "^.$"
|
|
|
|
|
$inputOffset += 1;
|
|
|
|
|
} elseif ( ( $prefixLengthTwo == '..' ) && ( $inputOffset + 2 == $inputLength ) ) {
|
|
|
|
|
# Step D, remove "^..$"
|
|
|
|
|
$inputOffset += 2;
|
2011-11-09 22:44:12 +00:00
|
|
|
} else {
|
2011-11-10 18:02:38 +00:00
|
|
|
# Step E, move leading path segment to output
|
2012-01-23 19:35:05 +00:00
|
|
|
if ( $prefixLengthOne == '/' ) {
|
|
|
|
|
$slashPos = strpos( $urlPath, '/', $inputOffset + 1 );
|
|
|
|
|
} else {
|
|
|
|
|
$slashPos = strpos( $urlPath, '/', $inputOffset );
|
|
|
|
|
}
|
|
|
|
|
if ( $slashPos === false ) {
|
|
|
|
|
$output .= substr( $urlPath, $inputOffset );
|
|
|
|
|
$inputOffset = $inputLength;
|
|
|
|
|
} else {
|
|
|
|
|
$output .= substr( $urlPath, $inputOffset, $slashPos - $inputOffset );
|
|
|
|
|
$inputOffset += $slashPos - $inputOffset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $trimOutput ) {
|
|
|
|
|
$slashPos = strrpos( $output, '/' );
|
|
|
|
|
if ( $slashPos === false ) {
|
|
|
|
|
$output = '';
|
|
|
|
|
} else {
|
|
|
|
|
$output = substr( $output, 0, $slashPos );
|
|
|
|
|
}
|
2011-11-09 22:44:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-16 19:09:03 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a regular expression of url protocols
|
|
|
|
|
*
|
2011-08-15 12:20:00 +00:00
|
|
|
* @param $includeProtocolRelative bool If false, remove '//' from the returned protocol list.
|
2011-11-03 03:30:27 +00:00
|
|
|
* DO NOT USE this directly, use wfUrlProtocolsWithoutProtRel() instead
|
2011-06-16 19:09:03 +00:00
|
|
|
* @return String
|
|
|
|
|
*/
|
2011-08-15 12:20:00 +00:00
|
|
|
function wfUrlProtocols( $includeProtocolRelative = true ) {
|
2011-06-16 19:09:03 +00:00
|
|
|
global $wgUrlProtocols;
|
|
|
|
|
|
2011-08-15 12:20:00 +00:00
|
|
|
// Cache return values separately based on $includeProtocolRelative
|
2011-08-15 13:16:10 +00:00
|
|
|
static $withProtRel = null, $withoutProtRel = null;
|
|
|
|
|
$cachedValue = $includeProtocolRelative ? $withProtRel : $withoutProtRel;
|
|
|
|
|
if ( !is_null( $cachedValue ) ) {
|
|
|
|
|
return $cachedValue;
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Support old-style $wgUrlProtocols strings, for backwards compatibility
|
|
|
|
|
// with LocalSettings files from 1.5
|
|
|
|
|
if ( is_array( $wgUrlProtocols ) ) {
|
|
|
|
|
$protocols = array();
|
|
|
|
|
foreach ( $wgUrlProtocols as $protocol ) {
|
2011-08-15 12:20:00 +00:00
|
|
|
// Filter out '//' if !$includeProtocolRelative
|
|
|
|
|
if ( $includeProtocolRelative || $protocol !== '//' ) {
|
|
|
|
|
$protocols[] = preg_quote( $protocol, '/' );
|
|
|
|
|
}
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-15 13:16:10 +00:00
|
|
|
$retval = implode( '|', $protocols );
|
2011-06-16 19:09:03 +00:00
|
|
|
} else {
|
2011-08-15 12:20:00 +00:00
|
|
|
// 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
|
2011-08-15 13:16:10 +00:00
|
|
|
$retval = $wgUrlProtocols;
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
2011-08-25 16:40:49 +00:00
|
|
|
|
2011-08-15 13:16:10 +00:00
|
|
|
// Cache return value
|
|
|
|
|
if ( $includeProtocolRelative ) {
|
|
|
|
|
$withProtRel = $retval;
|
|
|
|
|
} else {
|
|
|
|
|
$withoutProtRel = $retval;
|
|
|
|
|
}
|
|
|
|
|
return $retval;
|
2011-08-15 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2011-10-15 22:58:42 +00:00
|
|
|
* @return String
|
2011-08-15 12:20:00 +00:00
|
|
|
*/
|
|
|
|
|
function wfUrlProtocolsWithoutProtRel() {
|
|
|
|
|
return wfUrlProtocols( false );
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* parse_url() work-alike, but non-broken. Differences:
|
|
|
|
|
*
|
|
|
|
|
* 1) Does not raise warnings on bad URLs (just returns false)
|
2011-07-12 23:52:43 +00:00
|
|
|
* 2) Handles protocols that don't use :// (e.g., mailto: and news: , as well as protocol-relative URLs) correctly
|
|
|
|
|
* 3) Adds a "delimiter" element to the array, either '://', ':' or '//' (see (2))
|
2011-06-16 19:09:03 +00:00
|
|
|
*
|
|
|
|
|
* @param $url String: a URL to parse
|
|
|
|
|
* @return Array: bits of the URL in an associative array, per PHP docs
|
|
|
|
|
*/
|
|
|
|
|
function wfParseUrl( $url ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
|
|
|
|
|
|
|
|
|
|
// Protocol-relative URLs are handled really badly by parse_url(). It's so bad that the easiest
|
|
|
|
|
// way to handle them is to just prepend 'http:' and strip the protocol out later
|
|
|
|
|
$wasRelative = substr( $url, 0, 2 ) == '//';
|
|
|
|
|
if ( $wasRelative ) {
|
|
|
|
|
$url = "http:$url";
|
|
|
|
|
}
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$bits = parse_url( $url );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
// parse_url() returns an array without scheme for some invalid URLs, e.g.
|
|
|
|
|
// parse_url("%0Ahttp://example.com") == array( 'host' => '%0Ahttp', 'path' => 'example.com' )
|
|
|
|
|
if ( !$bits || !isset( $bits['scheme'] ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-10 18:49:02 +00:00
|
|
|
// parse_url() incorrectly handles schemes case-sensitively. Convert it to lowercase.
|
|
|
|
|
$bits['scheme'] = strtolower( $bits['scheme'] );
|
|
|
|
|
|
2012-08-28 00:51:38 +00:00
|
|
|
// most of the protocols are followed by ://, but mailto: and sometimes news: not, check for it
|
|
|
|
|
if ( in_array( $bits['scheme'] . '://', $wgUrlProtocols ) ) {
|
|
|
|
|
$bits['delimiter'] = '://';
|
|
|
|
|
} elseif ( in_array( $bits['scheme'] . ':', $wgUrlProtocols ) ) {
|
|
|
|
|
$bits['delimiter'] = ':';
|
|
|
|
|
// parse_url detects for news: and mailto: the host part of an url as path
|
|
|
|
|
// We have to correct this wrong detection
|
|
|
|
|
if ( isset( $bits['path'] ) ) {
|
|
|
|
|
$bits['host'] = $bits['path'];
|
|
|
|
|
$bits['path'] = '';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Provide an empty host for eg. file:/// urls (see bug 28627) */
|
|
|
|
|
if ( !isset( $bits['host'] ) ) {
|
|
|
|
|
$bits['host'] = '';
|
|
|
|
|
|
|
|
|
|
/* parse_url loses the third / for file:///c:/ urls (but not on variants) */
|
|
|
|
|
if ( substr( $bits['path'], 0, 1 ) !== '/' ) {
|
|
|
|
|
$bits['path'] = '/' . $bits['path'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the URL was protocol-relative, fix scheme and delimiter
|
|
|
|
|
if ( $wasRelative ) {
|
|
|
|
|
$bits['scheme'] = '';
|
|
|
|
|
$bits['delimiter'] = '//';
|
|
|
|
|
}
|
|
|
|
|
return $bits;
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-29 05:55:27 +00:00
|
|
|
/**
|
|
|
|
|
* Take a URL, make sure it's expanded to fully qualified, and replace any
|
|
|
|
|
* encoded non-ASCII Unicode characters with their UTF-8 original forms
|
|
|
|
|
* for more compact display and legibility for local audiences.
|
|
|
|
|
*
|
|
|
|
|
* @todo handle punycode domains too
|
|
|
|
|
*
|
|
|
|
|
* @param $url string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfExpandIRI( $url ) {
|
|
|
|
|
return preg_replace_callback( '/((?:%[89A-F][0-9A-F])+)/i', 'wfExpandIRI_callback', wfExpandUrl( $url ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Private callback for wfExpandIRI
|
|
|
|
|
* @param array $matches
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfExpandIRI_callback( $matches ) {
|
|
|
|
|
return urldecode( $matches[1] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-16 19:09:03 +00:00
|
|
|
/**
|
2011-11-14 09:13:58 +00:00
|
|
|
* Make URL indexes, appropriate for the el_index field of externallinks.
|
2011-06-16 19:09:03 +00:00
|
|
|
*
|
|
|
|
|
* @param $url String
|
2011-11-14 09:13:58 +00:00
|
|
|
* @return array
|
2011-06-16 19:09:03 +00:00
|
|
|
*/
|
2011-11-14 09:13:58 +00:00
|
|
|
function wfMakeUrlIndexes( $url ) {
|
2011-06-16 19:09:03 +00:00
|
|
|
$bits = wfParseUrl( $url );
|
|
|
|
|
|
|
|
|
|
// Reverse the labels in the hostname, convert to lower case
|
|
|
|
|
// For emails reverse domainpart only
|
|
|
|
|
if ( $bits['scheme'] == 'mailto' ) {
|
|
|
|
|
$mailparts = explode( '@', $bits['host'], 2 );
|
|
|
|
|
if ( count( $mailparts ) === 2 ) {
|
|
|
|
|
$domainpart = strtolower( implode( '.', array_reverse( explode( '.', $mailparts[1] ) ) ) );
|
|
|
|
|
} else {
|
|
|
|
|
// No domain specified, don't mangle it
|
|
|
|
|
$domainpart = '';
|
|
|
|
|
}
|
|
|
|
|
$reversedHost = $domainpart . '@' . $mailparts[0];
|
|
|
|
|
} else {
|
|
|
|
|
$reversedHost = strtolower( implode( '.', array_reverse( explode( '.', $bits['host'] ) ) ) );
|
|
|
|
|
}
|
|
|
|
|
// Add an extra dot to the end
|
|
|
|
|
// Why? Is it in wrong place in mailto links?
|
|
|
|
|
if ( substr( $reversedHost, -1, 1 ) !== '.' ) {
|
|
|
|
|
$reversedHost .= '.';
|
|
|
|
|
}
|
|
|
|
|
// Reconstruct the pseudo-URL
|
|
|
|
|
$prot = $bits['scheme'];
|
|
|
|
|
$index = $prot . $bits['delimiter'] . $reversedHost;
|
|
|
|
|
// Leave out user and password. Add the port, path, query and fragment
|
|
|
|
|
if ( isset( $bits['port'] ) ) {
|
|
|
|
|
$index .= ':' . $bits['port'];
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $bits['path'] ) ) {
|
|
|
|
|
$index .= $bits['path'];
|
|
|
|
|
} else {
|
|
|
|
|
$index .= '/';
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $bits['query'] ) ) {
|
|
|
|
|
$index .= '?' . $bits['query'];
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $bits['fragment'] ) ) {
|
|
|
|
|
$index .= '#' . $bits['fragment'];
|
|
|
|
|
}
|
2011-11-14 09:13:58 +00:00
|
|
|
|
|
|
|
|
if ( $prot == '' ) {
|
|
|
|
|
return array( "http:$index", "https:$index" );
|
|
|
|
|
} else {
|
|
|
|
|
return array( $index );
|
|
|
|
|
}
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-20 10:18:09 +00:00
|
|
|
/**
|
|
|
|
|
* Check whether a given URL has a domain that occurs in a given set of domains
|
|
|
|
|
* @param $url string URL
|
|
|
|
|
* @param $domains array Array of domains (strings)
|
|
|
|
|
* @return bool True if the host part of $url ends in one of the strings in $domains
|
|
|
|
|
*/
|
|
|
|
|
function wfMatchesDomainList( $url, $domains ) {
|
|
|
|
|
$bits = wfParseUrl( $url );
|
|
|
|
|
if ( is_array( $bits ) && isset( $bits['host'] ) ) {
|
|
|
|
|
foreach ( (array)$domains as $domain ) {
|
|
|
|
|
// FIXME: This gives false positives. http://nds-nl.wikipedia.org will match nl.wikipedia.org
|
|
|
|
|
// We should use something that interprets dots instead
|
|
|
|
|
if ( substr( $bits['host'], -strlen( $domain ) ) === $domain ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-02 02:55:26 +00:00
|
|
|
* Sends a line to the debug log if enabled or, optionally, to a comment in output.
|
|
|
|
|
* In normal operation this is a NOP.
|
|
|
|
|
*
|
|
|
|
|
* Controlling globals:
|
|
|
|
|
* $wgDebugLogFile - points to the log file
|
|
|
|
|
* $wgProfileOnly - if set, normal debug messages will not be recorded.
|
2011-06-03 05:44:28 +00:00
|
|
|
* $wgDebugRawPage - if false, 'action=raw' hits will not result in debug output.
|
2004-10-02 02:55:26 +00:00
|
|
|
* $wgDebugComments - if on, some debug items may appear in comments in the HTML output.
|
|
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $text String
|
|
|
|
|
* @param $logonly Bool: set true to avoid appearing in HTML when $wgDebugComments is set
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-09-03 17:13:55 +00:00
|
|
|
function wfDebug( $text, $logonly = false ) {
|
2012-08-27 20:57:15 +00:00
|
|
|
global $wgDebugLogFile, $wgProfileOnly, $wgDebugRawPage, $wgDebugLogPrefix;
|
2008-06-07 12:57:59 +00:00
|
|
|
|
2011-05-31 05:55:06 +00:00
|
|
|
if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
|
2004-07-24 07:24:04 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2012-03-12 19:01:44 +00:00
|
|
|
$timer = wfDebugTimer();
|
|
|
|
|
if ( $timer !== '' ) {
|
|
|
|
|
$text = preg_replace( '/[^\n]/', $timer . '\0', $text, 1 );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 20:57:15 +00:00
|
|
|
if ( !$logonly ) {
|
|
|
|
|
MWDebug::debugMsg( $text );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2012-08-27 20:57:15 +00:00
|
|
|
|
2011-05-25 22:01:08 +00:00
|
|
|
if ( wfRunHooks( 'Debug', array( $text, null /* no log group */ ) ) ) {
|
|
|
|
|
if ( $wgDebugLogFile != '' && !$wgProfileOnly ) {
|
|
|
|
|
# Strip unprintables; they can switch terminal modes when binary data
|
|
|
|
|
# gets dumped, which is pretty annoying.
|
2011-12-09 06:03:27 +00:00
|
|
|
$text = preg_replace( '![\x00-\x08\x0b\x0c\x0e-\x1f]!', ' ', $text );
|
2011-05-25 22:01:08 +00:00
|
|
|
$text = $wgDebugLogPrefix . $text;
|
|
|
|
|
wfErrorLog( $text, $wgDebugLogFile );
|
|
|
|
|
}
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-31 05:55:06 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if debug logging should be suppressed if $wgDebugRawPage = false
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return bool
|
2011-05-31 05:55:06 +00:00
|
|
|
*/
|
|
|
|
|
function wfIsDebugRawPage() {
|
|
|
|
|
static $cache;
|
|
|
|
|
if ( $cache !== null ) {
|
|
|
|
|
return $cache;
|
|
|
|
|
}
|
2011-06-03 05:44:28 +00:00
|
|
|
# Check for raw action using $_GET not $wgRequest, since the latter might not be initialised yet
|
|
|
|
|
if ( ( isset( $_GET['action'] ) && $_GET['action'] == 'raw' )
|
2011-06-30 02:59:43 +00:00
|
|
|
|| (
|
|
|
|
|
isset( $_SERVER['SCRIPT_NAME'] )
|
|
|
|
|
&& substr( $_SERVER['SCRIPT_NAME'], -8 ) == 'load.php'
|
|
|
|
|
) )
|
2011-05-31 05:55:06 +00:00
|
|
|
{
|
|
|
|
|
$cache = true;
|
|
|
|
|
} else {
|
|
|
|
|
$cache = false;
|
|
|
|
|
}
|
|
|
|
|
return $cache;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 13:55:27 +00:00
|
|
|
/**
|
|
|
|
|
* Get microsecond timestamps for debug logs
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
2011-04-23 13:55:27 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-01-27 17:21:18 +00:00
|
|
|
function wfDebugTimer() {
|
2012-02-02 10:33:42 +00:00
|
|
|
global $wgDebugTimestamps, $wgRequestTime;
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if ( !$wgDebugTimestamps ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2010-01-27 17:21:18 +00:00
|
|
|
|
2012-02-02 10:33:42 +00:00
|
|
|
$prefix = sprintf( "%6.4f", microtime( true ) - $wgRequestTime );
|
|
|
|
|
$mem = sprintf( "%5.1fM", ( memory_get_usage( true ) / ( 1024 * 1024 ) ) );
|
|
|
|
|
return "$prefix $mem ";
|
2010-01-27 17:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-28 16:22:10 +00:00
|
|
|
/**
|
|
|
|
|
* Send a line giving PHP memory usage.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $exact Bool: print exact values instead of kilobytes (default: false)
|
2008-08-28 16:22:10 +00:00
|
|
|
*/
|
|
|
|
|
function wfDebugMem( $exact = false ) {
|
|
|
|
|
$mem = memory_get_usage();
|
|
|
|
|
if( !$exact ) {
|
|
|
|
|
$mem = floor( $mem / 1024 ) . ' kilobytes';
|
|
|
|
|
} else {
|
|
|
|
|
$mem .= ' bytes';
|
|
|
|
|
}
|
|
|
|
|
wfDebug( "Memory usage: $mem\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-17 20:07:33 +00:00
|
|
|
/**
|
|
|
|
|
* Send a line to a supplementary debug log file, if configured, or main debug log if not.
|
|
|
|
|
* $wgDebugLogGroups[$logGroup] should be set to a filename to send to a separate log.
|
2005-09-19 12:52:32 +00:00
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $logGroup String
|
|
|
|
|
* @param $text String
|
|
|
|
|
* @param $public Bool: whether to log the event in the public log if no private
|
2005-09-19 12:52:32 +00:00
|
|
|
* log file is specified, (default true)
|
2005-08-17 20:07:33 +00:00
|
|
|
*/
|
2005-09-19 12:52:32 +00:00
|
|
|
function wfDebugLog( $logGroup, $text, $public = true ) {
|
2012-02-02 16:35:02 +00:00
|
|
|
global $wgDebugLogGroups;
|
2010-08-14 13:31:10 +00:00
|
|
|
$text = trim( $text ) . "\n";
|
2005-08-17 20:07:33 +00:00
|
|
|
if( isset( $wgDebugLogGroups[$logGroup] ) ) {
|
2006-01-28 11:42:35 +00:00
|
|
|
$time = wfTimestamp( TS_DB );
|
2006-10-04 09:06:18 +00:00
|
|
|
$wiki = wfWikiID();
|
2012-02-02 16:35:02 +00:00
|
|
|
$host = wfHostname();
|
2011-05-25 22:01:08 +00:00
|
|
|
if ( wfRunHooks( 'Debug', array( $text, $logGroup ) ) ) {
|
|
|
|
|
wfErrorLog( "$time $host $wiki: $text", $wgDebugLogGroups[$logGroup] );
|
|
|
|
|
}
|
2010-08-14 13:31:10 +00:00
|
|
|
} elseif ( $public === true ) {
|
2012-12-07 15:50:58 +00:00
|
|
|
wfDebug( "[$logGroup] $text", true );
|
2005-08-17 20:07:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Log for database errors
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2006-04-19 15:46:24 +00:00
|
|
|
* @param $text String: database error message.
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-06-29 07:09:00 +00:00
|
|
|
function wfLogDBError( $text ) {
|
2012-07-13 18:15:41 +00:00
|
|
|
global $wgDBerrorLog, $wgDBerrorLogTZ;
|
|
|
|
|
static $logDBErrorTimeZoneObject = null;
|
|
|
|
|
|
2004-06-29 07:09:00 +00:00
|
|
|
if ( $wgDBerrorLog ) {
|
2012-02-02 16:35:02 +00:00
|
|
|
$host = wfHostname();
|
|
|
|
|
$wiki = wfWikiID();
|
2012-07-13 14:53:06 +00:00
|
|
|
|
2012-07-13 18:15:41 +00:00
|
|
|
if ( $wgDBerrorLogTZ && !$logDBErrorTimeZoneObject ) {
|
|
|
|
|
$logDBErrorTimeZoneObject = new DateTimeZone( $wgDBerrorLogTZ );
|
2012-07-13 14:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-20 22:41:18 +00:00
|
|
|
// Workaround for https://bugs.php.net/bug.php?id=52063
|
|
|
|
|
// Can be removed when min PHP > 5.3.2
|
|
|
|
|
if ( $logDBErrorTimeZoneObject === null ) {
|
|
|
|
|
$d = date_create( "now" );
|
|
|
|
|
} else {
|
|
|
|
|
$d = date_create( "now", $logDBErrorTimeZoneObject );
|
|
|
|
|
}
|
2012-07-13 18:15:41 +00:00
|
|
|
|
|
|
|
|
$date = $d->format( 'D M j G:i:s T Y' );
|
|
|
|
|
|
2012-07-13 14:53:06 +00:00
|
|
|
$text = "$date\t$host\t$wiki\t$text";
|
2007-03-05 12:11:55 +00:00
|
|
|
wfErrorLog( $text, $wgDBerrorLog );
|
2004-06-29 07:09:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 09:54:40 +00:00
|
|
|
/**
|
|
|
|
|
* Throws a warning that $function is deprecated
|
|
|
|
|
*
|
|
|
|
|
* @param $function String
|
2012-02-13 00:19:06 +00:00
|
|
|
* @param $version String|bool: Version of MediaWiki that the function was deprecated in (Added in 1.19).
|
2012-02-09 18:01:54 +00:00
|
|
|
* @param $component String|bool: Added in 1.19.
|
2012-02-13 20:29:41 +00:00
|
|
|
* @param $callerOffset integer: How far up the callstack is the original
|
|
|
|
|
* caller. 2 = function that called the function that called
|
2012-04-04 23:48:55 +00:00
|
|
|
* wfDeprecated (Added in 1.20)
|
|
|
|
|
*
|
2012-02-03 09:54:40 +00:00
|
|
|
* @return null
|
|
|
|
|
*/
|
2012-02-13 00:19:06 +00:00
|
|
|
function wfDeprecated( $function, $version = false, $component = false, $callerOffset = 2 ) {
|
2012-08-25 11:09:46 +00:00
|
|
|
MWDebug::deprecated( $function, $version, $component, $callerOffset + 1 );
|
2012-02-03 09:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a warning either to the debug log or in a PHP error depending on
|
|
|
|
|
* $wgDevelopmentWarnings
|
|
|
|
|
*
|
|
|
|
|
* @param $msg String: message to send
|
|
|
|
|
* @param $callerOffset Integer: number of items to go back in the backtrace to
|
|
|
|
|
* find the correct caller (1 = function calling wfWarn, ...)
|
|
|
|
|
* @param $level Integer: PHP error level; only used when $wgDevelopmentWarnings
|
|
|
|
|
* is true
|
|
|
|
|
*/
|
|
|
|
|
function wfWarn( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
|
2012-08-25 11:09:46 +00:00
|
|
|
MWDebug::warning( $msg, $callerOffset + 1, $level );
|
2012-02-03 09:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
2007-03-05 12:11:55 +00:00
|
|
|
/**
|
2008-09-24 07:11:41 +00:00
|
|
|
* Log to a file without getting "file size exceeded" signals.
|
2010-08-01 12:31:50 +00:00
|
|
|
*
|
|
|
|
|
* Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will
|
2008-09-24 07:11:41 +00:00
|
|
|
* send lines to the specified port, prefixed by the specified prefix and a space.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $text String
|
|
|
|
|
* @param $file String filename
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2007-03-05 12:11:55 +00:00
|
|
|
*/
|
|
|
|
|
function wfErrorLog( $text, $file ) {
|
2008-09-24 07:11:41 +00:00
|
|
|
if ( substr( $file, 0, 4 ) == 'udp:' ) {
|
2010-07-28 13:51:42 +00:00
|
|
|
# Needs the sockets extension
|
2008-09-24 07:11:41 +00:00
|
|
|
if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
|
|
|
|
|
// IPv6 bracketed host
|
|
|
|
|
$host = $m[2];
|
2009-11-18 06:21:27 +00:00
|
|
|
$port = intval( $m[3] );
|
2008-09-24 08:17:35 +00:00
|
|
|
$prefix = isset( $m[4] ) ? $m[4] : false;
|
2009-11-18 06:21:27 +00:00
|
|
|
$domain = AF_INET6;
|
2008-09-30 08:45:52 +00:00
|
|
|
} elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
|
2008-09-24 07:11:41 +00:00
|
|
|
$host = $m[2];
|
2009-11-18 06:21:27 +00:00
|
|
|
if ( !IP::isIPv4( $host ) ) {
|
|
|
|
|
$host = gethostbyname( $host );
|
|
|
|
|
}
|
|
|
|
|
$port = intval( $m[3] );
|
2008-09-24 08:17:35 +00:00
|
|
|
$prefix = isset( $m[4] ) ? $m[4] : false;
|
2009-11-18 06:21:27 +00:00
|
|
|
$domain = AF_INET;
|
2008-09-24 07:11:41 +00:00
|
|
|
} else {
|
2010-08-14 13:31:10 +00:00
|
|
|
throw new MWException( __METHOD__ . ': Invalid UDP specification' );
|
2008-09-24 07:11:41 +00:00
|
|
|
}
|
2011-02-08 12:08:21 +00:00
|
|
|
|
2008-09-24 08:17:35 +00:00
|
|
|
// Clean it up for the multiplexer
|
|
|
|
|
if ( strval( $prefix ) !== '' ) {
|
|
|
|
|
$text = preg_replace( '/^/m', $prefix . ' ', $text );
|
2011-02-08 12:08:21 +00:00
|
|
|
|
|
|
|
|
// Limit to 64KB
|
2011-09-23 14:14:55 +00:00
|
|
|
if ( strlen( $text ) > 65506 ) {
|
2011-09-23 18:01:40 +00:00
|
|
|
$text = substr( $text, 0, 65506 );
|
2011-02-08 12:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-24 08:17:35 +00:00
|
|
|
if ( substr( $text, -1 ) != "\n" ) {
|
|
|
|
|
$text .= "\n";
|
|
|
|
|
}
|
2011-09-23 14:14:55 +00:00
|
|
|
} elseif ( strlen( $text ) > 65507 ) {
|
2011-09-23 18:01:40 +00:00
|
|
|
$text = substr( $text, 0, 65507 );
|
2008-09-24 07:11:41 +00:00
|
|
|
}
|
2008-09-24 08:17:35 +00:00
|
|
|
|
2009-11-18 06:21:27 +00:00
|
|
|
$sock = socket_create( $domain, SOCK_DGRAM, SOL_UDP );
|
2008-09-24 07:11:41 +00:00
|
|
|
if ( !$sock ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-09-16 20:08:33 +00:00
|
|
|
|
2011-09-23 14:14:55 +00:00
|
|
|
socket_sendto( $sock, $text, strlen( $text ), 0, $host, $port );
|
2009-11-18 06:21:27 +00:00
|
|
|
socket_close( $sock );
|
2008-09-24 07:11:41 +00:00
|
|
|
} else {
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$exists = file_exists( $file );
|
|
|
|
|
$size = $exists ? filesize( $file ) : false;
|
|
|
|
|
if ( !$exists || ( $size !== false && $size + strlen( $text ) < 0x7fffffff ) ) {
|
The beginnings of HipHop compiled mode support. It works now for parser cache hits.
* Work around HipHop issue 314 (volatile broken) and issue 308 (no compilation detection) by adding some large and ugly compilation detection code to WebStart.php and doMaintenance.php.
* Provide an MW_COMPILED constant which can be used to detect compiled mode throughout the codebase.
* Introduced wfIsHipHop(), which detects either compiled or interpreted mode. Used this to work around unusual eval() return value in eval.php.
* Work around lack of ini_get() in Maintenance.php, by duplicating wfIsHipHop().
* In Maintenance::shouldExecute(), accept "include" as an inclusion function name, since all kinds of inclusion give this string in HipHop.
* Introduced new class MWInit, which provides some static functions in the pre-autoloader environment.
* Introduced MWInit::compiledPath(), which provides a relative path for invoking a compiled file, and MWInit::interpretedPath(), which provides an absolute path for interpreting a PHP file. Used these new functions in the appropriate places.
* When we are running compiled code, don't include files which would generate duplicate class, function or constant definitions. Documented the new requirements on the contents of Defines.php and UtfNormalDefines.php.
* In HipHop compiled mode, it's not possible to have executable code in the same file as a class definition.
** Moved MimeMagic initialisation to the constructor.
** Moved Namespace.php global variable initialisation to Setup.php.
** Moved MemcachedSessions.php initialisation to the caller in GlobalFunctions.php.
** Moved Sanitizer.php constants and global variables to static class members. Introduced an accessor function for the attribs regex, as a new place to put code formerly at file level.
** Moved Language.php initialisation of $wgLanguageNames to Language::getLanguageNames(). Removed the global variable, marked "private" since forever.
* In two places: don't use error_log() with type=3 to append to a file, HipHop doesn't support it. Use file_put_contents() with FILE_APPEND instead.
* Work around the terrible breakage of class_exists() by using MWInit::classExists() instead in various places. In WebInstaller::getPageByName(), the class_exists() was marked with a fixme comment already, so I replaced it with an autoloader solution.
2011-04-04 12:59:55 +00:00
|
|
|
file_put_contents( $file, $text, FILE_APPEND );
|
2008-09-24 07:11:41 +00:00
|
|
|
}
|
|
|
|
|
wfRestoreWarnings();
|
2007-03-05 12:11:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
|
|
|
|
*/
|
2006-07-14 05:35:31 +00:00
|
|
|
function wfLogProfilingData() {
|
2004-07-24 07:24:04 +00:00
|
|
|
global $wgRequestTime, $wgDebugLogFile, $wgDebugRawPage, $wgRequest;
|
2011-04-21 16:31:02 +00:00
|
|
|
global $wgProfileLimit, $wgUser;
|
|
|
|
|
|
|
|
|
|
$profiler = Profiler::instance();
|
|
|
|
|
|
2009-02-24 09:50:22 +00:00
|
|
|
# Profiling must actually be enabled...
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( $profiler->isStub() ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2011-04-21 16:31:02 +00:00
|
|
|
|
|
|
|
|
// Get total page request time and only show pages that longer than
|
|
|
|
|
// $wgProfileLimit time (default is 0)
|
2012-02-03 08:32:34 +00:00
|
|
|
$elapsed = microtime( true ) - $wgRequestTime;
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( $elapsed <= $wgProfileLimit ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$profiler->logData();
|
|
|
|
|
|
|
|
|
|
// Check whether this should be logged in the debug file.
|
2011-05-31 05:55:06 +00:00
|
|
|
if ( $wgDebugLogFile == '' || ( !$wgDebugRawPage && wfIsDebugRawPage() ) ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2011-04-21 16:31:02 +00:00
|
|
|
|
2008-04-06 16:14:04 +00:00
|
|
|
$forward = '';
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
|
2008-04-06 16:14:04 +00:00
|
|
|
$forward = ' forwarded for ' . $_SERVER['HTTP_X_FORWARDED_FOR'];
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
|
2008-04-06 16:14:04 +00:00
|
|
|
$forward .= ' client IP ' . $_SERVER['HTTP_CLIENT_IP'];
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( !empty( $_SERVER['HTTP_FROM'] ) ) {
|
2008-04-06 16:14:04 +00:00
|
|
|
$forward .= ' from ' . $_SERVER['HTTP_FROM'];
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( $forward ) {
|
2008-04-06 16:14:04 +00:00
|
|
|
$forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2011-04-30 14:08:12 +00:00
|
|
|
// Don't load $wgUser at this late stage just for statistics purposes
|
2011-05-17 22:03:20 +00:00
|
|
|
// @todo FIXME: We can detect some anons even if it is not loaded. See User::getId()
|
2011-04-30 14:08:12 +00:00
|
|
|
if ( $wgUser->isItemLoaded( 'id' ) && $wgUser->isAnon() ) {
|
2008-04-06 16:14:04 +00:00
|
|
|
$forward .= ' anon';
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2012-11-13 16:51:32 +00:00
|
|
|
|
|
|
|
|
// Command line script uses a FauxRequest object which does not have
|
|
|
|
|
// any knowledge about an URL and throw an exception instead.
|
|
|
|
|
try {
|
|
|
|
|
$requestUrl = $wgRequest->getRequestURL();
|
|
|
|
|
} catch ( MWException $e ) {
|
|
|
|
|
$requestUrl = 'n/a';
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-06 16:14:04 +00:00
|
|
|
$log = sprintf( "%s\t%04.3f\t%s\n",
|
2010-08-14 13:31:10 +00:00
|
|
|
gmdate( 'YmdHis' ), $elapsed,
|
2012-11-13 16:51:32 +00:00
|
|
|
urldecode( $requestUrl . $forward ) );
|
2011-04-21 16:31:02 +00:00
|
|
|
|
|
|
|
|
wfErrorLog( $log . $profiler->getOutput(), $wgDebugLogFile );
|
2003-12-11 20:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-15 16:17:02 +00:00
|
|
|
/**
|
|
|
|
|
* Increment a statistics counter
|
|
|
|
|
*
|
|
|
|
|
* @param $key String
|
|
|
|
|
* @param $count Int
|
2013-01-18 18:26:54 +00:00
|
|
|
* @return void
|
2012-02-15 16:17:02 +00:00
|
|
|
*/
|
|
|
|
|
function wfIncrStats( $key, $count = 1 ) {
|
|
|
|
|
global $wgStatsMethod;
|
|
|
|
|
|
|
|
|
|
$count = intval( $count );
|
2013-01-18 18:26:54 +00:00
|
|
|
if ( $count == 0 ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-02-15 16:17:02 +00:00
|
|
|
|
|
|
|
|
if( $wgStatsMethod == 'udp' ) {
|
2012-12-21 08:26:18 +00:00
|
|
|
global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgAggregateStatsID;
|
2012-02-15 16:17:02 +00:00
|
|
|
static $socket;
|
|
|
|
|
|
2012-12-21 08:26:18 +00:00
|
|
|
$id = $wgAggregateStatsID !== false ? $wgAggregateStatsID : wfWikiID();
|
2012-02-15 16:17:02 +00:00
|
|
|
|
|
|
|
|
if ( !$socket ) {
|
|
|
|
|
$socket = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
|
|
|
|
|
$statline = "stats/{$id} - 1 1 1 1 1 -total\n";
|
|
|
|
|
socket_sendto(
|
|
|
|
|
$socket,
|
|
|
|
|
$statline,
|
|
|
|
|
strlen( $statline ),
|
|
|
|
|
0,
|
|
|
|
|
$wgUDPProfilerHost,
|
|
|
|
|
$wgUDPProfilerPort
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$statline = "stats/{$id} - {$count} 1 1 1 1 {$key}\n";
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
socket_sendto(
|
|
|
|
|
$socket,
|
|
|
|
|
$statline,
|
|
|
|
|
strlen( $statline ),
|
|
|
|
|
0,
|
|
|
|
|
$wgUDPProfilerHost,
|
|
|
|
|
$wgUDPProfilerPort
|
|
|
|
|
);
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
} elseif( $wgStatsMethod == 'cache' ) {
|
|
|
|
|
global $wgMemc;
|
|
|
|
|
$key = wfMemcKey( 'stats', $key );
|
|
|
|
|
if ( is_null( $wgMemc->incr( $key, $count ) ) ) {
|
|
|
|
|
$wgMemc->add( $key, $count );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Disabled
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Check if the wiki read-only lock file is present. This can be used to lock
|
|
|
|
|
* off editing functions, but doesn't guarantee that the database will not be
|
|
|
|
|
* modified.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2004-09-03 17:13:55 +00:00
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-08-13 15:55:59 +00:00
|
|
|
function wfReadOnly() {
|
2005-06-01 06:18:49 +00:00
|
|
|
global $wgReadOnlyFile, $wgReadOnly;
|
2003-04-14 23:10:40 +00:00
|
|
|
|
2006-01-26 07:02:53 +00:00
|
|
|
if ( !is_null( $wgReadOnly ) ) {
|
|
|
|
|
return (bool)$wgReadOnly;
|
2005-06-01 06:18:49 +00:00
|
|
|
}
|
2010-01-06 19:59:42 +00:00
|
|
|
if ( $wgReadOnlyFile == '' ) {
|
2004-08-13 15:55:59 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2006-01-26 07:02:53 +00:00
|
|
|
// Set $wgReadOnly for faster access next time
|
2005-06-25 13:39:19 +00:00
|
|
|
if ( is_file( $wgReadOnlyFile ) ) {
|
2005-08-17 07:37:47 +00:00
|
|
|
$wgReadOnly = file_get_contents( $wgReadOnlyFile );
|
2005-06-25 13:39:19 +00:00
|
|
|
} else {
|
|
|
|
|
$wgReadOnly = false;
|
|
|
|
|
}
|
2006-01-26 07:02:53 +00:00
|
|
|
return (bool)$wgReadOnly;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-15 22:58:42 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2008-02-24 03:50:05 +00:00
|
|
|
function wfReadOnlyReason() {
|
|
|
|
|
global $wgReadOnly;
|
|
|
|
|
wfReadOnly();
|
|
|
|
|
return $wgReadOnly;
|
|
|
|
|
}
|
2003-09-21 13:10:10 +00:00
|
|
|
|
2008-07-26 20:41:52 +00:00
|
|
|
/**
|
|
|
|
|
* Return a Language object from $langcode
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2008-07-26 20:41:52 +00:00
|
|
|
* @param $langcode Mixed: either:
|
|
|
|
|
* - a Language object
|
|
|
|
|
* - code of the language to get the message for, if it is
|
|
|
|
|
* a valid code create a language for that language, if
|
|
|
|
|
* it is a string but not a valid code then make a basic
|
|
|
|
|
* language object
|
2011-09-18 23:21:46 +00:00
|
|
|
* - a boolean: if it's false then use the global object for
|
|
|
|
|
* the current user's language (as a fallback for the old parameter
|
|
|
|
|
* functionality), or if it is true then use global object
|
|
|
|
|
* for the wiki's content language.
|
2008-07-26 20:41:52 +00:00
|
|
|
* @return Language object
|
|
|
|
|
*/
|
2010-07-08 08:23:35 +00:00
|
|
|
function wfGetLangObj( $langcode = false ) {
|
2008-07-26 20:41:52 +00:00
|
|
|
# Identify which language to get or create a language object for.
|
2010-07-08 08:23:35 +00:00
|
|
|
# Using is_object here due to Stub objects.
|
|
|
|
|
if( is_object( $langcode ) ) {
|
|
|
|
|
# Great, we already have the object (hopefully)!
|
2008-07-26 20:41:52 +00:00
|
|
|
return $langcode;
|
2010-07-08 08:23:35 +00:00
|
|
|
}
|
2010-08-01 12:31:50 +00:00
|
|
|
|
2010-07-08 08:23:35 +00:00
|
|
|
global $wgContLang, $wgLanguageCode;
|
|
|
|
|
if( $langcode === true || $langcode === $wgLanguageCode ) {
|
2008-07-26 20:41:52 +00:00
|
|
|
# $langcode is the language code of the wikis content language object.
|
|
|
|
|
# or it is a boolean and value is true
|
|
|
|
|
return $wgContLang;
|
2010-07-08 08:23:35 +00:00
|
|
|
}
|
2010-08-01 12:31:50 +00:00
|
|
|
|
2008-07-26 20:41:52 +00:00
|
|
|
global $wgLang;
|
2010-07-08 08:23:35 +00:00
|
|
|
if( $langcode === false || $langcode === $wgLang->getCode() ) {
|
2008-07-26 20:41:52 +00:00
|
|
|
# $langcode is the language code of user language object.
|
|
|
|
|
# or it was a boolean and value is false
|
|
|
|
|
return $wgLang;
|
2010-07-08 08:23:35 +00:00
|
|
|
}
|
2008-07-26 20:41:52 +00:00
|
|
|
|
2012-03-08 20:56:26 +00:00
|
|
|
$validCodes = array_keys( Language::fetchLanguageNames() );
|
2010-07-08 08:23:35 +00:00
|
|
|
if( in_array( $langcode, $validCodes ) ) {
|
2008-07-26 20:41:52 +00:00
|
|
|
# $langcode corresponds to a valid language.
|
|
|
|
|
return Language::factory( $langcode );
|
2010-07-08 08:23:35 +00:00
|
|
|
}
|
2008-07-26 20:41:52 +00:00
|
|
|
|
|
|
|
|
# $langcode is a string, but not a valid language code; use content language.
|
2009-01-13 20:28:54 +00:00
|
|
|
wfDebug( "Invalid language code passed to wfGetLangObj, falling back to content language.\n" );
|
2008-07-26 20:41:52 +00:00
|
|
|
return $wgContLang;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-03 20:39:35 +00:00
|
|
|
/**
|
2011-07-06 02:26:06 +00:00
|
|
|
* Old function when $wgBetterDirectionality existed
|
2012-02-13 20:26:02 +00:00
|
|
|
* All usage removed, wfUILang can be removed in near future
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2011-07-18 23:01:08 +00:00
|
|
|
* @deprecated since 1.18
|
2011-04-23 13:55:27 +00:00
|
|
|
* @return Language
|
2010-08-03 20:39:35 +00:00
|
|
|
*/
|
2010-07-08 13:34:03 +00:00
|
|
|
function wfUILang() {
|
2011-12-16 00:24:00 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.18' );
|
2011-07-06 02:26:06 +00:00
|
|
|
global $wgLang;
|
|
|
|
|
return $wgLang;
|
2010-07-08 13:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
2010-09-02 17:12:56 +00:00
|
|
|
/**
|
|
|
|
|
* This is the new function for getting translated interface messages.
|
|
|
|
|
* See the Message class for documentation how to use them.
|
|
|
|
|
* The intention is that this function replaces all old wfMsg* functions.
|
|
|
|
|
* @param $key \string Message key.
|
|
|
|
|
* Varargs: normal message parameters.
|
2011-02-20 13:33:42 +00:00
|
|
|
* @return Message
|
2010-09-02 17:12:56 +00:00
|
|
|
* @since 1.17
|
|
|
|
|
*/
|
|
|
|
|
function wfMessage( $key /*...*/) {
|
|
|
|
|
$params = func_get_args();
|
|
|
|
|
array_shift( $params );
|
2010-11-16 20:40:20 +00:00
|
|
|
if ( isset( $params[0] ) && is_array( $params[0] ) ) {
|
|
|
|
|
$params = $params[0];
|
|
|
|
|
}
|
2010-09-02 17:12:56 +00:00
|
|
|
return new Message( $key, $params );
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-31 23:30:00 +00:00
|
|
|
/**
|
|
|
|
|
* This function accepts multiple message keys and returns a message instance
|
|
|
|
|
* for the first message which is non-empty. If all messages are empty then an
|
|
|
|
|
* instance of the first message key is returned.
|
2011-04-23 13:55:27 +00:00
|
|
|
* @param varargs: message keys
|
2011-04-16 23:23:28 +00:00
|
|
|
* @return Message
|
2010-12-31 23:30:00 +00:00
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
|
|
|
|
function wfMessageFallback( /*...*/ ) {
|
2011-01-14 08:32:10 +00:00
|
|
|
$args = func_get_args();
|
The beginnings of HipHop compiled mode support. It works now for parser cache hits.
* Work around HipHop issue 314 (volatile broken) and issue 308 (no compilation detection) by adding some large and ugly compilation detection code to WebStart.php and doMaintenance.php.
* Provide an MW_COMPILED constant which can be used to detect compiled mode throughout the codebase.
* Introduced wfIsHipHop(), which detects either compiled or interpreted mode. Used this to work around unusual eval() return value in eval.php.
* Work around lack of ini_get() in Maintenance.php, by duplicating wfIsHipHop().
* In Maintenance::shouldExecute(), accept "include" as an inclusion function name, since all kinds of inclusion give this string in HipHop.
* Introduced new class MWInit, which provides some static functions in the pre-autoloader environment.
* Introduced MWInit::compiledPath(), which provides a relative path for invoking a compiled file, and MWInit::interpretedPath(), which provides an absolute path for interpreting a PHP file. Used these new functions in the appropriate places.
* When we are running compiled code, don't include files which would generate duplicate class, function or constant definitions. Documented the new requirements on the contents of Defines.php and UtfNormalDefines.php.
* In HipHop compiled mode, it's not possible to have executable code in the same file as a class definition.
** Moved MimeMagic initialisation to the constructor.
** Moved Namespace.php global variable initialisation to Setup.php.
** Moved MemcachedSessions.php initialisation to the caller in GlobalFunctions.php.
** Moved Sanitizer.php constants and global variables to static class members. Introduced an accessor function for the attribs regex, as a new place to put code formerly at file level.
** Moved Language.php initialisation of $wgLanguageNames to Language::getLanguageNames(). Removed the global variable, marked "private" since forever.
* In two places: don't use error_log() with type=3 to append to a file, HipHop doesn't support it. Use file_put_contents() with FILE_APPEND instead.
* Work around the terrible breakage of class_exists() by using MWInit::classExists() instead in various places. In WebInstaller::getPageByName(), the class_exists() was marked with a fixme comment already, so I replaced it with an autoloader solution.
2011-04-04 12:59:55 +00:00
|
|
|
return MWFunction::callArray( 'Message::newFallbackSequence', $args );
|
2010-12-31 23:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
/**
|
|
|
|
|
* Get a message from anywhere, for the current user language.
|
|
|
|
|
*
|
|
|
|
|
* Use wfMsgForContent() instead if the message should NOT
|
|
|
|
|
* change depending on the user preferences.
|
|
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2008-07-10 19:18:00 +00:00
|
|
|
* @param $key String: lookup key for the message, usually
|
|
|
|
|
* defined in languages/Language.php
|
|
|
|
|
*
|
2011-06-15 16:33:13 +00:00
|
|
|
* Parameters to the message, which can be used to insert variable text into
|
|
|
|
|
* it, can be passed to this function in the following formats:
|
|
|
|
|
* - One per argument, starting at the second parameter
|
|
|
|
|
* - As an array in the second parameter
|
|
|
|
|
* These are not shown in the function definition.
|
|
|
|
|
*
|
2011-04-23 13:55:27 +00:00
|
|
|
* @return String
|
2008-07-10 19:18:00 +00:00
|
|
|
*/
|
|
|
|
|
function wfMsg( $key ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
2011-06-21 07:43:27 +00:00
|
|
|
return wfMsgReal( $key, $args );
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as above except doesn't transform the message
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2011-04-23 13:55:27 +00:00
|
|
|
* @param $key String
|
|
|
|
|
* @return String
|
2008-07-10 19:18:00 +00:00
|
|
|
*/
|
|
|
|
|
function wfMsgNoTrans( $key ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
|
|
|
|
return wfMsgReal( $key, $args, true, false, false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a message from anywhere, for the current global language
|
|
|
|
|
* set with $wgLanguageCode.
|
|
|
|
|
*
|
2010-08-14 13:31:10 +00:00
|
|
|
* Use this if the message should NOT change dependent on the
|
2008-07-10 19:18:00 +00:00
|
|
|
* language set in the user's preferences. This is the case for
|
|
|
|
|
* most text written into logs, as well as link targets (such as
|
|
|
|
|
* the name of the copyright policy page). Link titles, on the
|
|
|
|
|
* other hand, should be shown in the UI language.
|
|
|
|
|
*
|
|
|
|
|
* Note that MediaWiki allows users to change the user interface
|
|
|
|
|
* language in their preferences, but a single installation
|
|
|
|
|
* typically only contains content in one language.
|
|
|
|
|
*
|
|
|
|
|
* Be wary of this distinction: If you use wfMsg() where you should
|
|
|
|
|
* use wfMsgForContent(), a user of the software may have to
|
2010-04-23 20:35:25 +00:00
|
|
|
* customize potentially hundreds of messages in
|
|
|
|
|
* order to, e.g., fix a link in every possible language.
|
2008-07-10 19:18:00 +00:00
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2008-07-10 19:18:00 +00:00
|
|
|
* @param $key String: lookup key for the message, usually
|
2011-04-23 13:55:27 +00:00
|
|
|
* defined in languages/Language.php
|
|
|
|
|
* @return String
|
2008-07-10 19:18:00 +00:00
|
|
|
*/
|
|
|
|
|
function wfMsgForContent( $key ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
global $wgForceUIMsgAsContentMsg;
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
|
|
|
|
$forcontent = true;
|
|
|
|
|
if( is_array( $wgForceUIMsgAsContentMsg ) &&
|
|
|
|
|
in_array( $key, $wgForceUIMsgAsContentMsg ) )
|
2010-08-14 13:31:10 +00:00
|
|
|
{
|
2008-07-10 19:18:00 +00:00
|
|
|
$forcontent = false;
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2008-07-10 19:18:00 +00:00
|
|
|
return wfMsgReal( $key, $args, true, $forcontent );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as above except doesn't transform the message
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2011-04-23 13:55:27 +00:00
|
|
|
* @param $key String
|
|
|
|
|
* @return String
|
2008-07-10 19:18:00 +00:00
|
|
|
*/
|
|
|
|
|
function wfMsgForContentNoTrans( $key ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
global $wgForceUIMsgAsContentMsg;
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
|
|
|
|
$forcontent = true;
|
|
|
|
|
if( is_array( $wgForceUIMsgAsContentMsg ) &&
|
|
|
|
|
in_array( $key, $wgForceUIMsgAsContentMsg ) )
|
2010-08-14 13:31:10 +00:00
|
|
|
{
|
2008-07-10 19:18:00 +00:00
|
|
|
$forcontent = false;
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2008-07-10 19:18:00 +00:00
|
|
|
return wfMsgReal( $key, $args, true, $forcontent, false );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Really get a message
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2008-07-10 19:18:00 +00:00
|
|
|
* @param $key String: key to get.
|
|
|
|
|
* @param $args
|
|
|
|
|
* @param $useDB Boolean
|
2009-09-16 00:53:37 +00:00
|
|
|
* @param $forContent Mixed: Language code, or false for user lang, true for content lang.
|
2010-04-23 20:31:23 +00:00
|
|
|
* @param $transform Boolean: Whether or not to transform the message.
|
2008-07-10 19:18:00 +00:00
|
|
|
* @return String: the requested message.
|
|
|
|
|
*/
|
2009-04-09 05:15:43 +00:00
|
|
|
function wfMsgReal( $key, $args, $useDB = true, $forContent = false, $transform = true ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2009-03-25 02:07:09 +00:00
|
|
|
$message = wfMsgGetKey( $key, $useDB, $forContent, $transform );
|
2008-07-10 19:18:00 +00:00
|
|
|
$message = wfMsgReplaceArgs( $message, $args );
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch a message string value, but don't replace any keys yet.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $key String
|
|
|
|
|
* @param $useDB Bool
|
|
|
|
|
* @param $langCode String: Code of the language to get the message for, or
|
|
|
|
|
* behaves as a content language switch if it is a boolean.
|
|
|
|
|
* @param $transform Boolean: whether to parse magic words, etc.
|
2008-07-10 19:18:00 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-06-21 07:43:27 +00:00
|
|
|
function wfMsgGetKey( $key, $useDB = true, $langCode = false, $transform = true ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
wfRunHooks( 'NormalizeMessageKey', array( &$key, &$useDB, &$langCode, &$transform ) );
|
2010-08-01 12:31:50 +00:00
|
|
|
|
2011-01-26 15:42:04 +00:00
|
|
|
$cache = MessageCache::singleton();
|
|
|
|
|
$message = $cache->get( $key, $useDB, $langCode );
|
2010-08-14 13:31:10 +00:00
|
|
|
if( $message === false ) {
|
2010-07-08 09:08:03 +00:00
|
|
|
$message = '<' . htmlspecialchars( $key ) . '>';
|
|
|
|
|
} elseif ( $transform ) {
|
2011-01-26 15:42:04 +00:00
|
|
|
$message = $cache->transform( $message );
|
2010-07-08 09:08:03 +00:00
|
|
|
}
|
2008-07-10 19:18:00 +00:00
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace message parameter keys on the given formatted output.
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $message String
|
|
|
|
|
* @param $args Array
|
2008-07-10 19:18:00 +00:00
|
|
|
* @return string
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
function wfMsgReplaceArgs( $message, $args ) {
|
|
|
|
|
# Fix windows line-endings
|
|
|
|
|
# Some messages are split with explode("\n", $msg)
|
|
|
|
|
$message = str_replace( "\r", '', $message );
|
|
|
|
|
|
|
|
|
|
// Replace arguments
|
|
|
|
|
if ( count( $args ) ) {
|
|
|
|
|
if ( is_array( $args[0] ) ) {
|
|
|
|
|
$args = array_values( $args[0] );
|
|
|
|
|
}
|
|
|
|
|
$replacementKeys = array();
|
|
|
|
|
foreach( $args as $n => $param ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
$replacementKeys['$' . ( $n + 1 )] = $param;
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
$message = strtr( $message, $replacementKeys );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an HTML-escaped version of a message.
|
|
|
|
|
* Parameter replacements, if any, are done *after* the HTML-escaping,
|
|
|
|
|
* so parameters may contain HTML (eg links or form controls). Be sure
|
|
|
|
|
* to pre-escape them if you really do want plaintext, or just wrap
|
|
|
|
|
* the whole thing in htmlspecialchars().
|
|
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $key String
|
2008-07-10 19:18:00 +00:00
|
|
|
* @param string ... parameters
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfMsgHtml( $key ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
2011-06-21 07:43:27 +00:00
|
|
|
return wfMsgReplaceArgs( htmlspecialchars( wfMsgGetKey( $key ) ), $args );
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an HTML version of message
|
|
|
|
|
* Parameter replacements, if any, are done *after* parsing the wiki-text message,
|
|
|
|
|
* so parameters may contain HTML (eg links or form controls). Be sure
|
|
|
|
|
* to pre-escape them if you really do want plaintext, or just wrap
|
|
|
|
|
* the whole thing in htmlspecialchars().
|
|
|
|
|
*
|
2012-08-06 17:32:52 +00:00
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $key String
|
2008-07-10 19:18:00 +00:00
|
|
|
* @param string ... parameters
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfMsgWikiHtml( $key ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
2011-04-18 12:43:53 +00:00
|
|
|
return wfMsgReplaceArgs(
|
2011-08-12 23:14:32 +00:00
|
|
|
MessageCache::singleton()->parse( wfMsgGetKey( $key ), null,
|
|
|
|
|
/* can't be set to false */ true, /* interface */ true )->getText(),
|
2011-04-18 12:43:53 +00:00
|
|
|
$args );
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns message in the requested format
|
2012-08-06 17:32:52 +00:00
|
|
|
*
|
|
|
|
|
* @deprecated since 1.18
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $key String: key of the message
|
|
|
|
|
* @param $options Array: processing rules. Can take the following options:
|
2010-08-14 13:31:10 +00:00
|
|
|
* <i>parse</i>: parses wikitext to HTML
|
|
|
|
|
* <i>parseinline</i>: parses wikitext to HTML and removes the surrounding
|
2008-09-26 21:31:10 +00:00
|
|
|
* p's added by parser or tidy
|
|
|
|
|
* <i>escape</i>: filters message through htmlspecialchars
|
2010-05-30 17:33:59 +00:00
|
|
|
* <i>escapenoentities</i>: same, but allows entity references like   through
|
2008-09-26 21:31:10 +00:00
|
|
|
* <i>replaceafter</i>: parameters are substituted after parsing or escaping
|
|
|
|
|
* <i>parsemag</i>: transform the message using magic phrases
|
|
|
|
|
* <i>content</i>: fetch message for content language instead of interface
|
|
|
|
|
* Also can accept a single associative argument, of the form 'language' => 'xx':
|
2008-09-30 17:15:11 +00:00
|
|
|
* <i>language</i>: Language object or language code to fetch message for
|
2011-04-24 18:47:36 +00:00
|
|
|
* (overriden by <i>content</i>).
|
2008-07-10 19:18:00 +00:00
|
|
|
* Behavior for conflicting options (e.g., parse+parseinline) is undefined.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @return String
|
2008-07-10 19:18:00 +00:00
|
|
|
*/
|
|
|
|
|
function wfMsgExt( $key, $options ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
$args = func_get_args();
|
|
|
|
|
array_shift( $args );
|
|
|
|
|
array_shift( $args );
|
2008-09-26 21:35:03 +00:00
|
|
|
$options = (array)$options;
|
2008-07-10 19:18:00 +00:00
|
|
|
|
2008-09-26 21:31:10 +00:00
|
|
|
foreach( $options as $arrayKey => $option ) {
|
|
|
|
|
if( !preg_match( '/^[0-9]+|language$/', $arrayKey ) ) {
|
|
|
|
|
# An unknown index, neither numeric nor "language"
|
2009-06-06 10:55:04 +00:00
|
|
|
wfWarn( "wfMsgExt called with incorrect parameter key $arrayKey", 1, E_USER_WARNING );
|
2008-09-26 21:31:10 +00:00
|
|
|
} elseif( preg_match( '/^[0-9]+$/', $arrayKey ) && !in_array( $option,
|
|
|
|
|
array( 'parse', 'parseinline', 'escape', 'escapenoentities',
|
|
|
|
|
'replaceafter', 'parsemag', 'content' ) ) ) {
|
|
|
|
|
# A numeric index with unknown value
|
2009-06-06 10:55:04 +00:00
|
|
|
wfWarn( "wfMsgExt called with incorrect parameter $option", 1, E_USER_WARNING );
|
2008-09-26 17:25:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if( in_array( 'content', $options, true ) ) {
|
2008-07-10 19:18:00 +00:00
|
|
|
$forContent = true;
|
|
|
|
|
$langCode = true;
|
2011-01-05 12:24:39 +00:00
|
|
|
$langCodeObj = null;
|
2010-08-14 13:31:10 +00:00
|
|
|
} elseif( array_key_exists( 'language', $options ) ) {
|
2008-07-10 19:18:00 +00:00
|
|
|
$forContent = false;
|
2008-09-30 17:15:11 +00:00
|
|
|
$langCode = wfGetLangObj( $options['language'] );
|
2011-01-05 12:24:39 +00:00
|
|
|
$langCodeObj = $langCode;
|
2008-07-10 19:18:00 +00:00
|
|
|
} else {
|
|
|
|
|
$forContent = false;
|
|
|
|
|
$langCode = false;
|
2011-01-05 12:24:39 +00:00
|
|
|
$langCodeObj = null;
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$string = wfMsgGetKey( $key, /*DB*/true, $langCode, /*Transform*/false );
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if( !in_array( 'replaceafter', $options, true ) ) {
|
2008-07-10 19:18:00 +00:00
|
|
|
$string = wfMsgReplaceArgs( $string, $args );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-18 12:59:50 +00:00
|
|
|
$messageCache = MessageCache::singleton();
|
2012-02-07 02:49:28 +00:00
|
|
|
$parseInline = in_array( 'parseinline', $options, true );
|
|
|
|
|
if( in_array( 'parse', $options, true ) || $parseInline ) {
|
|
|
|
|
$string = $messageCache->parse( $string, null, true, !$forContent, $langCodeObj );
|
|
|
|
|
if ( $string instanceof ParserOutput ) {
|
|
|
|
|
$string = $string->getText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $parseInline ) {
|
|
|
|
|
$m = array();
|
|
|
|
|
if( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $string, $m ) ) {
|
|
|
|
|
$string = $m[1];
|
|
|
|
|
}
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
2010-08-14 13:31:10 +00:00
|
|
|
} elseif ( in_array( 'parsemag', $options, true ) ) {
|
2011-04-18 12:59:50 +00:00
|
|
|
$string = $messageCache->transform( $string,
|
2011-01-05 12:24:39 +00:00
|
|
|
!$forContent, $langCodeObj );
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if ( in_array( 'escape', $options, true ) ) {
|
2008-07-10 19:18:00 +00:00
|
|
|
$string = htmlspecialchars ( $string );
|
2010-08-14 13:31:10 +00:00
|
|
|
} elseif ( in_array( 'escapenoentities', $options, true ) ) {
|
2008-08-18 18:15:47 +00:00
|
|
|
$string = Sanitizer::escapeHtmlAllowEntities( $string );
|
2008-07-10 19:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if( in_array( 'replaceafter', $options, true ) ) {
|
2008-07-10 19:18:00 +00:00
|
|
|
$string = wfMsgReplaceArgs( $string, $args );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 19:42:00 +00:00
|
|
|
/**
|
|
|
|
|
* Since wfMsg() and co suck, they don't return false if the message key they
|
|
|
|
|
* looked up didn't exist but a XHTML string, this function checks for the
|
|
|
|
|
* nonexistance of messages by checking the MessageCache::get() result directly.
|
|
|
|
|
*
|
2012-08-19 20:44:29 +00:00
|
|
|
* @deprecated since 1.18. Use Message::isDisabled().
|
2012-08-06 17:32:52 +00:00
|
|
|
*
|
2011-05-23 19:42:00 +00:00
|
|
|
* @param $key String: the message key looked up
|
|
|
|
|
* @return Boolean True if the message *doesn't* exist.
|
|
|
|
|
*/
|
|
|
|
|
function wfEmptyMsg( $key ) {
|
2012-12-04 11:19:09 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.21' );
|
|
|
|
|
|
2011-05-23 19:42:00 +00:00
|
|
|
return MessageCache::singleton()->get( $key, /*useDB*/true, /*content*/false ) === false;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Throw a debugging exception. This function previously once exited the process,
|
2006-06-08 07:05:57 +00:00
|
|
|
* but now throws an exception instead, with similar results.
|
2004-09-03 17:13:55 +00:00
|
|
|
*
|
2011-10-14 18:04:12 +00:00
|
|
|
* @param $msg String: message shown when dying.
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-06-08 20:06:01 +00:00
|
|
|
function wfDebugDieBacktrace( $msg = '' ) {
|
2006-06-08 07:05:57 +00:00
|
|
|
throw new MWException( $msg );
|
2004-03-08 02:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-26 01:03:34 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch server name for use in error reporting etc.
|
|
|
|
|
* Use real server name if available, so we know which machine
|
|
|
|
|
* in a server farm generated the current page.
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
2006-05-26 01:03:34 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfHostname() {
|
2008-09-05 03:46:07 +00:00
|
|
|
static $host;
|
|
|
|
|
if ( is_null( $host ) ) {
|
2012-05-11 18:13:09 +00:00
|
|
|
|
|
|
|
|
# Hostname overriding
|
|
|
|
|
global $wgOverrideHostname;
|
|
|
|
|
if( $wgOverrideHostname !== false ) {
|
|
|
|
|
# Set static and skip any detection
|
|
|
|
|
$host = $wgOverrideHostname;
|
|
|
|
|
return $host;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-05 03:46:07 +00:00
|
|
|
if ( function_exists( 'posix_uname' ) ) {
|
|
|
|
|
// This function not present on Windows
|
2011-06-16 17:38:26 +00:00
|
|
|
$uname = posix_uname();
|
2008-09-05 03:46:07 +00:00
|
|
|
} else {
|
|
|
|
|
$uname = false;
|
|
|
|
|
}
|
|
|
|
|
if( is_array( $uname ) && isset( $uname['nodename'] ) ) {
|
|
|
|
|
$host = $uname['nodename'];
|
2008-10-06 07:30:38 +00:00
|
|
|
} elseif ( getenv( 'COMPUTERNAME' ) ) {
|
|
|
|
|
# Windows computer name
|
|
|
|
|
$host = getenv( 'COMPUTERNAME' );
|
2008-09-05 03:46:07 +00:00
|
|
|
} else {
|
|
|
|
|
# This may be a virtual server.
|
|
|
|
|
$host = $_SERVER['SERVER_NAME'];
|
|
|
|
|
}
|
2006-05-26 01:03:34 +00:00
|
|
|
}
|
2008-09-05 03:46:07 +00:00
|
|
|
return $host;
|
2006-05-26 01:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-23 17:14:07 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a HTML comment with the elapsed time since request.
|
|
|
|
|
* This method has no side effects.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfReportTime() {
|
|
|
|
|
global $wgRequestTime, $wgShowHostnames;
|
2008-12-23 21:30:19 +00:00
|
|
|
|
2012-02-03 08:32:34 +00:00
|
|
|
$elapsed = microtime( true ) - $wgRequestTime;
|
2008-12-23 21:30:19 +00:00
|
|
|
|
2009-07-23 17:14:07 +00:00
|
|
|
return $wgShowHostnames
|
2010-08-14 13:31:10 +00:00
|
|
|
? sprintf( '<!-- Served by %s in %01.3f secs. -->', wfHostname(), $elapsed )
|
|
|
|
|
: sprintf( '<!-- Served in %01.3f secs. -->', $elapsed );
|
2009-07-23 17:14:07 +00:00
|
|
|
}
|
2005-09-24 13:37:26 +00:00
|
|
|
|
2007-01-02 23:50:56 +00:00
|
|
|
/**
|
|
|
|
|
* Safety wrapper for debug_backtrace().
|
|
|
|
|
*
|
|
|
|
|
* With Zend Optimizer 3.2.0 loaded, this causes segfaults under somewhat
|
|
|
|
|
* murky circumstances, which may be triggered in part by stub objects
|
|
|
|
|
* or other fancy talkin'.
|
|
|
|
|
*
|
2009-03-09 13:57:32 +00:00
|
|
|
* Will return an empty array if Zend Optimizer is detected or if
|
|
|
|
|
* debug_backtrace is disabled, otherwise the output from
|
|
|
|
|
* debug_backtrace() (trimmed).
|
2007-01-02 23:50:56 +00:00
|
|
|
*
|
2011-08-25 16:40:49 +00:00
|
|
|
* @param $limit int This parameter can be used to limit the number of stack frames returned
|
|
|
|
|
*
|
2007-01-02 23:50:56 +00:00
|
|
|
* @return array of backtrace information
|
|
|
|
|
*/
|
2011-06-18 20:15:48 +00:00
|
|
|
function wfDebugBacktrace( $limit = 0 ) {
|
2009-03-09 13:57:32 +00:00
|
|
|
static $disabled = null;
|
|
|
|
|
|
2007-01-02 23:50:56 +00:00
|
|
|
if( extension_loaded( 'Zend Optimizer' ) ) {
|
|
|
|
|
wfDebug( "Zend Optimizer detected; skipping debug_backtrace for safety.\n" );
|
|
|
|
|
return array();
|
|
|
|
|
}
|
2009-03-09 13:57:32 +00:00
|
|
|
|
|
|
|
|
if ( is_null( $disabled ) ) {
|
|
|
|
|
$disabled = false;
|
|
|
|
|
$functions = explode( ',', ini_get( 'disable_functions' ) );
|
|
|
|
|
$functions = array_map( 'trim', $functions );
|
|
|
|
|
$functions = array_map( 'strtolower', $functions );
|
|
|
|
|
if ( in_array( 'debug_backtrace', $functions ) ) {
|
|
|
|
|
wfDebug( "debug_backtrace is in disabled_functions\n" );
|
|
|
|
|
$disabled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $disabled ) {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-18 20:15:48 +00:00
|
|
|
if ( $limit && version_compare( PHP_VERSION, '5.4.0', '>=' ) ) {
|
2012-06-05 22:58:54 +00:00
|
|
|
return array_slice( debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit + 1 ), 1 );
|
2011-06-18 20:11:45 +00:00
|
|
|
} else {
|
|
|
|
|
return array_slice( debug_backtrace(), 1 );
|
|
|
|
|
}
|
2007-01-02 23:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
2011-04-23 13:55:27 +00:00
|
|
|
/**
|
|
|
|
|
* Get a debug backtrace as a string
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2005-04-25 13:43:21 +00:00
|
|
|
function wfBacktrace() {
|
|
|
|
|
global $wgCommandLineMode;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-04-25 13:43:21 +00:00
|
|
|
if ( $wgCommandLineMode ) {
|
|
|
|
|
$msg = '';
|
|
|
|
|
} else {
|
|
|
|
|
$msg = "<ul>\n";
|
|
|
|
|
}
|
2007-01-02 23:50:56 +00:00
|
|
|
$backtrace = wfDebugBacktrace();
|
2005-04-25 13:43:21 +00:00
|
|
|
foreach( $backtrace as $call ) {
|
|
|
|
|
if( isset( $call['file'] ) ) {
|
|
|
|
|
$f = explode( DIRECTORY_SEPARATOR, $call['file'] );
|
2010-08-14 13:31:10 +00:00
|
|
|
$file = $f[count( $f ) - 1];
|
2005-04-25 13:43:21 +00:00
|
|
|
} else {
|
|
|
|
|
$file = '-';
|
|
|
|
|
}
|
|
|
|
|
if( isset( $call['line'] ) ) {
|
|
|
|
|
$line = $call['line'];
|
|
|
|
|
} else {
|
|
|
|
|
$line = '-';
|
|
|
|
|
}
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
|
|
|
|
$msg .= "$file line $line calls ";
|
|
|
|
|
} else {
|
|
|
|
|
$msg .= '<li>' . $file . ' line ' . $line . ' calls ';
|
|
|
|
|
}
|
2010-08-14 13:31:10 +00:00
|
|
|
if( !empty( $call['class'] ) ) {
|
2011-06-17 17:13:43 +00:00
|
|
|
$msg .= $call['class'] . $call['type'];
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2005-04-25 13:43:21 +00:00
|
|
|
$msg .= $call['function'] . '()';
|
|
|
|
|
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
|
|
|
|
$msg .= "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$msg .= "</li>\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
|
|
|
|
$msg .= "\n";
|
|
|
|
|
} else {
|
|
|
|
|
$msg .= "</ul>\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:32:04 +00:00
|
|
|
/**
|
|
|
|
|
* Get the name of the function which called this function
|
2012-06-05 22:58:54 +00:00
|
|
|
* wfGetCaller( 1 ) is the function with the wfGetCaller() call (ie. __FUNCTION__)
|
|
|
|
|
* wfGetCaller( 2 ) [default] is the caller of the function running wfGetCaller()
|
|
|
|
|
* wfGetCaller( 3 ) is the parent of that.
|
2011-05-25 18:32:04 +00:00
|
|
|
*
|
|
|
|
|
* @param $level Int
|
2012-08-29 19:36:37 +00:00
|
|
|
* @return string
|
2011-05-25 18:32:04 +00:00
|
|
|
*/
|
|
|
|
|
function wfGetCaller( $level = 2 ) {
|
2012-06-05 22:58:54 +00:00
|
|
|
$backtrace = wfDebugBacktrace( $level + 1 );
|
2011-05-25 18:32:04 +00:00
|
|
|
if ( isset( $backtrace[$level] ) ) {
|
|
|
|
|
return wfFormatStackFrame( $backtrace[$level] );
|
|
|
|
|
} else {
|
2012-08-29 19:36:37 +00:00
|
|
|
return 'unknown';
|
2011-05-25 18:32:04 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a string consisting of callers in the stack. Useful sometimes
|
|
|
|
|
* for profiling specific points.
|
|
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @param $limit int The maximum depth of the stack frame to return, or false for
|
2011-05-25 18:32:04 +00:00
|
|
|
* the entire stack.
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
function wfGetAllCallers( $limit = 3 ) {
|
|
|
|
|
$trace = array_reverse( wfDebugBacktrace() );
|
|
|
|
|
if ( !$limit || $limit > count( $trace ) - 1 ) {
|
|
|
|
|
$limit = count( $trace ) - 1;
|
|
|
|
|
}
|
|
|
|
|
$trace = array_slice( $trace, -$limit - 1, $limit );
|
|
|
|
|
return implode( '/', array_map( 'wfFormatStackFrame', $trace ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a string representation of frame
|
|
|
|
|
*
|
|
|
|
|
* @param $frame Array
|
2012-08-29 19:36:37 +00:00
|
|
|
* @return string
|
2011-05-25 18:32:04 +00:00
|
|
|
*/
|
|
|
|
|
function wfFormatStackFrame( $frame ) {
|
|
|
|
|
return isset( $frame['class'] ) ?
|
|
|
|
|
$frame['class'] . '::' . $frame['function'] :
|
|
|
|
|
$frame['function'];
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-14 23:10:40 +00:00
|
|
|
|
|
|
|
|
/* Some generic result counters, pulled out of SearchEngine */
|
|
|
|
|
|
2004-09-03 17:13:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @todo document
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $offset Int
|
|
|
|
|
* @param $limit Int
|
|
|
|
|
* @return String
|
2004-09-03 17:13:55 +00:00
|
|
|
*/
|
|
|
|
|
function wfShowingResults( $offset, $limit ) {
|
2012-07-24 01:04:15 +00:00
|
|
|
return wfMessage( 'showingresults' )->numParams( $limit, $offset + 1 )->parse();
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 17:13:55 +00:00
|
|
|
/**
|
2009-02-22 18:26:27 +00:00
|
|
|
* Generate (prev x| next x) (20|50|100...) type links for paging
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $offset String
|
|
|
|
|
* @param $limit Integer
|
|
|
|
|
* @param $link String
|
|
|
|
|
* @param $query String: optional URL query parameter string
|
|
|
|
|
* @param $atend Bool: optional param for specified if this is the last page
|
2011-04-23 13:55:27 +00:00
|
|
|
* @return String
|
2011-10-14 14:57:06 +00:00
|
|
|
* @deprecated in 1.19; use Language::viewPrevNext() instead
|
2004-09-03 17:13:55 +00:00
|
|
|
*/
|
|
|
|
|
function wfViewPrevNext( $offset, $limit, $link, $query = '', $atend = false ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.19' );
|
2012-04-04 23:48:55 +00:00
|
|
|
|
2006-03-07 01:10:39 +00:00
|
|
|
global $wgLang;
|
2011-10-14 14:57:06 +00:00
|
|
|
|
|
|
|
|
$query = wfCgiToArray( $query );
|
|
|
|
|
|
2004-10-02 02:55:26 +00:00
|
|
|
if( is_object( $link ) ) {
|
2011-10-14 14:57:06 +00:00
|
|
|
$title = $link;
|
2004-10-02 02:55:26 +00:00
|
|
|
} else {
|
2005-08-01 23:58:51 +00:00
|
|
|
$title = Title::newFromText( $link );
|
2004-10-02 02:55:26 +00:00
|
|
|
if( is_null( $title ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-14 14:57:06 +00:00
|
|
|
|
|
|
|
|
return $wgLang->viewPrevNext( $title, $offset, $limit, $query, $atend );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 17:13:55 +00:00
|
|
|
/**
|
2011-10-14 14:57:06 +00:00
|
|
|
* Make a list item, used by various special pages
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2011-10-14 14:57:06 +00:00
|
|
|
* @param $page String Page link
|
|
|
|
|
* @param $details String Text between brackets
|
|
|
|
|
* @param $oppositedm Boolean Add the direction mark opposite to your
|
|
|
|
|
* language, to display text properly
|
|
|
|
|
* @return String
|
|
|
|
|
* @deprecated since 1.19; use Language::specialList() instead
|
2004-09-03 17:13:55 +00:00
|
|
|
*/
|
2011-10-14 14:57:06 +00:00
|
|
|
function wfSpecialList( $page, $details, $oppositedm = true ) {
|
2012-02-16 12:59:48 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.19' );
|
|
|
|
|
|
2006-03-07 01:10:39 +00:00
|
|
|
global $wgLang;
|
2011-10-14 14:57:06 +00:00
|
|
|
return $wgLang->specialList( $page, $details, $oppositedm );
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 17:13:55 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo FIXME: We may want to blacklist some broken browsers
|
2004-09-03 17:13:55 +00:00
|
|
|
*
|
2011-04-23 13:55:27 +00:00
|
|
|
* @param $force Bool
|
2004-09-03 17:13:55 +00:00
|
|
|
* @return bool Whereas client accept gzip compression
|
|
|
|
|
*/
|
2011-01-02 04:38:04 +00:00
|
|
|
function wfClientAcceptsGzip( $force = false ) {
|
2010-11-16 20:40:20 +00:00
|
|
|
static $result = null;
|
2011-01-02 04:38:04 +00:00
|
|
|
if ( $result === null || $force ) {
|
2010-11-16 20:40:20 +00:00
|
|
|
$result = false;
|
|
|
|
|
if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
|
2011-05-17 22:03:20 +00:00
|
|
|
# @todo FIXME: We may want to blacklist some broken browsers
|
2010-11-16 20:40:20 +00:00
|
|
|
$m = array();
|
|
|
|
|
if( preg_match(
|
|
|
|
|
'/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'],
|
|
|
|
|
$m )
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
if( isset( $m[2] ) && ( $m[1] == 'q' ) && ( $m[2] == 0 ) ) {
|
|
|
|
|
$result = false;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2011-11-08 09:10:33 +00:00
|
|
|
wfDebug( "wfClientAcceptsGzip: client accepts gzip.\n" );
|
2010-11-16 20:40:20 +00:00
|
|
|
$result = true;
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2010-01-09 19:14:43 +00:00
|
|
|
}
|
2003-05-20 09:30:40 +00:00
|
|
|
}
|
2010-11-16 20:40:20 +00:00
|
|
|
return $result;
|
2003-05-20 09:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2006-04-13 20:01:48 +00:00
|
|
|
* Obtain the offset and limit values from the request string;
|
|
|
|
|
* used in special pages
|
|
|
|
|
*
|
2011-04-23 13:55:27 +00:00
|
|
|
* @param $deflimit Int default limit if none supplied
|
|
|
|
|
* @param $optionname String Name of a user preference to check against
|
2006-04-13 20:01:48 +00:00
|
|
|
* @return array
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-06-08 20:06:01 +00:00
|
|
|
function wfCheckLimits( $deflimit = 50, $optionname = 'rclimit' ) {
|
2004-08-23 02:19:02 +00:00
|
|
|
global $wgRequest;
|
|
|
|
|
return $wgRequest->getLimitOffset( $deflimit, $optionname );
|
2003-06-03 08:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Escapes the given text so that it may be output using addWikiText()
|
|
|
|
|
* without any linking, formatting, etc. making its way through. This
|
|
|
|
|
* is achieved by substituting certain characters with HTML entities.
|
2012-07-10 12:48:06 +00:00
|
|
|
* As required by the callers, "<nowiki>" is not used.
|
2004-09-03 17:13:55 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $text String: text to be escaped
|
2011-04-23 13:55:27 +00:00
|
|
|
* @return String
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-09-03 17:13:55 +00:00
|
|
|
function wfEscapeWikiText( $text ) {
|
2011-01-18 19:39:13 +00:00
|
|
|
$text = strtr( "\n$text", array(
|
|
|
|
|
'"' => '"', '&' => '&', "'" => ''', '<' => '<',
|
|
|
|
|
'=' => '=', '>' => '>', '[' => '[', ']' => ']',
|
|
|
|
|
'{' => '{', '|' => '|', '}' => '}',
|
|
|
|
|
"\n#" => "\n#", "\n*" => "\n*",
|
|
|
|
|
"\n:" => "\n:", "\n;" => "\n;",
|
|
|
|
|
'://' => '://', 'ISBN ' => 'ISBN ', 'RFC ' => 'RFC ',
|
|
|
|
|
) );
|
|
|
|
|
return substr( $text, 1 );
|
2003-09-21 13:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 17:13:55 +00:00
|
|
|
/**
|
2011-04-23 13:55:27 +00:00
|
|
|
* Get the current unix timetstamp with microseconds. Useful for profiling
|
|
|
|
|
* @return Float
|
2004-09-03 17:13:55 +00:00
|
|
|
*/
|
|
|
|
|
function wfTime() {
|
2010-08-14 13:31:10 +00:00
|
|
|
return microtime( true );
|
2004-02-18 19:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Sets dest to source and returns the original value of dest
|
|
|
|
|
* If source is NULL, it just returns the value, it doesn't set the variable
|
2010-12-30 17:30:35 +00:00
|
|
|
* If force is true, it will set the value even if source is NULL
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $dest Mixed
|
|
|
|
|
* @param $source Mixed
|
|
|
|
|
* @param $force Bool
|
|
|
|
|
* @return Mixed
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2010-12-30 17:30:35 +00:00
|
|
|
function wfSetVar( &$dest, $source, $force = false ) {
|
2004-01-10 16:44:31 +00:00
|
|
|
$temp = $dest;
|
2010-12-30 17:30:35 +00:00
|
|
|
if ( !is_null( $source ) || $force ) {
|
2004-07-18 08:48:43 +00:00
|
|
|
$dest = $source;
|
|
|
|
|
}
|
2004-01-10 16:44:31 +00:00
|
|
|
return $temp;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* As for wfSetVar except setting a bit
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $dest Int
|
|
|
|
|
* @param $bit Int
|
|
|
|
|
* @param $state Bool
|
2011-11-29 21:04:20 +00:00
|
|
|
*
|
|
|
|
|
* @return bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-07-24 07:24:04 +00:00
|
|
|
function wfSetBit( &$dest, $bit, $state = true ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
$temp = (bool)( $dest & $bit );
|
2004-07-24 07:24:04 +00:00
|
|
|
if ( !is_null( $state ) ) {
|
|
|
|
|
if ( $state ) {
|
|
|
|
|
$dest |= $bit;
|
|
|
|
|
} else {
|
|
|
|
|
$dest &= ~$bit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $temp;
|
2004-01-10 16:44:31 +00:00
|
|
|
}
|
2003-11-12 10:21:28 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2008-10-09 21:11:10 +00:00
|
|
|
* A wrapper around the PHP function var_export().
|
|
|
|
|
* Either print it or add it to the regular output ($wgOut).
|
|
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @param $var mixed A PHP variable to dump.
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-09-03 17:13:55 +00:00
|
|
|
function wfVarDump( $var ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
global $wgOut;
|
2010-08-14 13:31:10 +00:00
|
|
|
$s = str_replace( "\n", "<br />\n", var_export( $var, true ) . "\n" );
|
2011-06-16 17:38:26 +00:00
|
|
|
if ( headers_sent() || !isset( $wgOut ) || !is_object( $wgOut ) ) {
|
2004-03-20 15:03:26 +00:00
|
|
|
print $s;
|
|
|
|
|
} else {
|
|
|
|
|
$wgOut->addHTML( $s );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Provide a simple HTTP error.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $code Int|String
|
|
|
|
|
* @param $label String
|
|
|
|
|
* @param $desc String
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-04-04 22:33:11 +00:00
|
|
|
function wfHttpError( $code, $label, $desc ) {
|
|
|
|
|
global $wgOut;
|
|
|
|
|
$wgOut->disable();
|
|
|
|
|
header( "HTTP/1.0 $code $label" );
|
|
|
|
|
header( "Status: $code $label" );
|
|
|
|
|
$wgOut->sendCacheControl();
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2007-02-21 01:02:47 +00:00
|
|
|
header( 'Content-type: text/html; charset=utf-8' );
|
2012-02-01 15:44:32 +00:00
|
|
|
print "<!doctype html>" .
|
2010-08-14 13:31:10 +00:00
|
|
|
'<html><head><title>' .
|
2005-08-02 13:35:19 +00:00
|
|
|
htmlspecialchars( $label ) .
|
2010-08-14 13:31:10 +00:00
|
|
|
'</title></head><body><h1>' .
|
2005-05-27 11:03:37 +00:00
|
|
|
htmlspecialchars( $label ) .
|
2010-08-14 13:31:10 +00:00
|
|
|
'</h1><p>' .
|
2007-01-03 09:15:11 +00:00
|
|
|
nl2br( htmlspecialchars( $desc ) ) .
|
2005-05-27 11:03:37 +00:00
|
|
|
"</p></body></html>\n";
|
2004-04-04 21:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
2006-12-11 01:51:21 +00:00
|
|
|
/**
|
|
|
|
|
* Clear away any user-level output buffers, discarding contents.
|
|
|
|
|
*
|
|
|
|
|
* Suitable for 'starting afresh', for instance when streaming
|
|
|
|
|
* relatively large amounts of data without buffering, or wanting to
|
|
|
|
|
* output image files without ob_gzhandler's compression.
|
|
|
|
|
*
|
|
|
|
|
* The optional $resetGzipEncoding parameter controls suppression of
|
2006-12-11 19:54:34 +00:00
|
|
|
* the Content-Encoding header sent by ob_gzhandler; by default it
|
2006-12-11 01:51:21 +00:00
|
|
|
* is left. See comments for wfClearOutputBuffers() for why it would
|
|
|
|
|
* be used.
|
|
|
|
|
*
|
|
|
|
|
* Note that some PHP configuration options may add output buffer
|
|
|
|
|
* layers which cannot be removed; these are left in place.
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $resetGzipEncoding Bool
|
2006-12-11 01:51:21 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfResetOutputBuffers( $resetGzipEncoding = true ) {
|
2007-02-20 04:46:07 +00:00
|
|
|
if( $resetGzipEncoding ) {
|
|
|
|
|
// Suppress Content-Encoding and Content-Length
|
|
|
|
|
// headers from 1.10+s wfOutputHandler
|
|
|
|
|
global $wgDisableOutputCompression;
|
|
|
|
|
$wgDisableOutputCompression = true;
|
|
|
|
|
}
|
2006-12-11 01:51:21 +00:00
|
|
|
while( $status = ob_get_status() ) {
|
|
|
|
|
if( $status['type'] == 0 /* PHP_OUTPUT_HANDLER_INTERNAL */ ) {
|
|
|
|
|
// Probably from zlib.output_compression or other
|
|
|
|
|
// PHP-internal setting which can't be removed.
|
|
|
|
|
//
|
|
|
|
|
// Give up, and hope the result doesn't break
|
|
|
|
|
// output behavior.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if( !ob_end_clean() ) {
|
|
|
|
|
// Could not remove output buffer handler; abort now
|
|
|
|
|
// to avoid getting in some kind of infinite loop.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if( $resetGzipEncoding ) {
|
|
|
|
|
if( $status['name'] == 'ob_gzhandler' ) {
|
|
|
|
|
// Reset the 'Content-Encoding' field set by this handler
|
|
|
|
|
// so we can start fresh.
|
2012-05-26 16:31:32 +00:00
|
|
|
header_remove( 'Content-Encoding' );
|
2008-08-28 14:18:13 +00:00
|
|
|
break;
|
2006-12-11 01:51:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* More legible than passing a 'false' parameter to wfResetOutputBuffers():
|
|
|
|
|
*
|
|
|
|
|
* Clear away output buffers, but keep the Content-Encoding header
|
|
|
|
|
* produced by ob_gzhandler, if any.
|
|
|
|
|
*
|
|
|
|
|
* This should be used for HTTP 304 responses, where you need to
|
|
|
|
|
* preserve the Content-Encoding header of the real result, but
|
|
|
|
|
* also need to suppress the output of ob_gzhandler to keep to spec
|
|
|
|
|
* and avoid breaking Firefox in rare cases where the headers and
|
|
|
|
|
* body are broken over two packets.
|
|
|
|
|
*/
|
|
|
|
|
function wfClearOutputBuffers() {
|
|
|
|
|
wfResetOutputBuffers( false );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Converts an Accept-* header into an array mapping string values to quality
|
|
|
|
|
* factors
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $accept String
|
|
|
|
|
* @param $def String default
|
|
|
|
|
* @return Array
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-06-08 20:06:01 +00:00
|
|
|
function wfAcceptToPrefs( $accept, $def = '*/*' ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
# No arg means accept anything (per HTTP spec)
|
|
|
|
|
if( !$accept ) {
|
2008-05-28 19:17:02 +00:00
|
|
|
return array( $def => 1.0 );
|
2004-04-04 22:33:11 +00:00
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-04-04 22:33:11 +00:00
|
|
|
$prefs = array();
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-06-08 20:06:01 +00:00
|
|
|
$parts = explode( ',', $accept );
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-04-04 22:33:11 +00:00
|
|
|
foreach( $parts as $part ) {
|
2011-05-17 22:03:20 +00:00
|
|
|
# @todo FIXME: Doesn't deal with params like 'text/html; level=1'
|
2011-06-16 17:38:26 +00:00
|
|
|
$values = explode( ';', trim( $part ) );
|
2006-11-23 08:25:56 +00:00
|
|
|
$match = array();
|
2011-06-16 17:38:26 +00:00
|
|
|
if ( count( $values ) == 1 ) {
|
|
|
|
|
$prefs[$values[0]] = 1.0;
|
|
|
|
|
} elseif ( preg_match( '/q\s*=\s*(\d*\.\d+)/', $values[1], $match ) ) {
|
|
|
|
|
$prefs[$values[0]] = floatval( $match[1] );
|
2004-04-04 22:33:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-04-04 22:33:11 +00:00
|
|
|
return $prefs;
|
|
|
|
|
}
|
2004-04-04 21:58:05 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-19 07:12:15 +00:00
|
|
|
* Checks if a given MIME type matches any of the keys in the given
|
|
|
|
|
* array. Basic wildcards are accepted in the array keys.
|
|
|
|
|
*
|
|
|
|
|
* Returns the matching MIME type (or wildcard) if a match, otherwise
|
|
|
|
|
* NULL if no match.
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $type String
|
|
|
|
|
* @param $avail Array
|
2004-10-19 07:12:15 +00:00
|
|
|
* @return string
|
2006-04-19 15:46:24 +00:00
|
|
|
* @private
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
function mimeTypeMatch( $type, $avail ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
if( array_key_exists( $type, $avail ) ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
return $type;
|
2004-04-04 21:58:05 +00:00
|
|
|
} else {
|
2004-04-04 22:33:11 +00:00
|
|
|
$parts = explode( '/', $type );
|
|
|
|
|
if( array_key_exists( $parts[0] . '/*', $avail ) ) {
|
|
|
|
|
return $parts[0] . '/*';
|
|
|
|
|
} elseif( array_key_exists( '*/*', $avail ) ) {
|
|
|
|
|
return '*/*';
|
|
|
|
|
} else {
|
2009-12-11 21:07:27 +00:00
|
|
|
return null;
|
2004-04-04 22:33:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2004-10-19 07:12:15 +00:00
|
|
|
* Returns the 'best' match between a client's requested internet media types
|
|
|
|
|
* and the server's list of available types. Each list should be an associative
|
|
|
|
|
* array of type to preference (preference is a float between 0.0 and 1.0).
|
|
|
|
|
* Wildcards in the types are acceptable.
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $cprefs Array: client's acceptable type list
|
|
|
|
|
* @param $sprefs Array: server's offered types
|
2004-10-19 07:12:15 +00:00
|
|
|
* @return string
|
|
|
|
|
*
|
2011-05-17 22:03:20 +00:00
|
|
|
* @todo FIXME: Doesn't handle params like 'text/plain; charset=UTF-8'
|
2004-09-02 23:28:24 +00:00
|
|
|
* XXX: generalize to negotiate other stuff
|
|
|
|
|
*/
|
2004-04-04 22:33:11 +00:00
|
|
|
function wfNegotiateType( $cprefs, $sprefs ) {
|
|
|
|
|
$combine = array();
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2011-09-18 23:21:46 +00:00
|
|
|
foreach( array_keys( $sprefs ) as $type ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
$parts = explode( '/', $type );
|
|
|
|
|
if( $parts[1] != '*' ) {
|
|
|
|
|
$ckey = mimeTypeMatch( $type, $cprefs );
|
|
|
|
|
if( $ckey ) {
|
|
|
|
|
$combine[$type] = $sprefs[$type] * $cprefs[$ckey];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-04-04 22:33:11 +00:00
|
|
|
foreach( array_keys( $cprefs ) as $type ) {
|
|
|
|
|
$parts = explode( '/', $type );
|
|
|
|
|
if( $parts[1] != '*' && !array_key_exists( $type, $sprefs ) ) {
|
|
|
|
|
$skey = mimeTypeMatch( $type, $sprefs );
|
|
|
|
|
if( $skey ) {
|
|
|
|
|
$combine[$type] = $sprefs[$skey] * $cprefs[$type];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-04-04 22:33:11 +00:00
|
|
|
$bestq = 0;
|
2009-12-11 21:07:27 +00:00
|
|
|
$besttype = null;
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-04-04 22:33:11 +00:00
|
|
|
foreach( array_keys( $combine ) as $type ) {
|
|
|
|
|
if( $combine[$type] > $bestq ) {
|
|
|
|
|
$besttype = $type;
|
|
|
|
|
$bestq = $combine[$type];
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2004-04-04 22:33:11 +00:00
|
|
|
return $besttype;
|
2004-04-04 21:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Reference-counted warning suppression
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $end Bool
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-07-10 03:09:26 +00:00
|
|
|
function wfSuppressWarnings( $end = false ) {
|
|
|
|
|
static $suppressCount = 0;
|
|
|
|
|
static $originalLevel = false;
|
|
|
|
|
|
|
|
|
|
if ( $end ) {
|
|
|
|
|
if ( $suppressCount ) {
|
2005-08-26 13:47:38 +00:00
|
|
|
--$suppressCount;
|
2004-07-10 03:09:26 +00:00
|
|
|
if ( !$suppressCount ) {
|
|
|
|
|
error_reporting( $originalLevel );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ( !$suppressCount ) {
|
2012-12-13 22:16:22 +00:00
|
|
|
$originalLevel = error_reporting( E_ALL & ~( E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE | E_DEPRECATED | E_USER_DEPRECATED | E_STRICT ) );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2005-08-26 13:47:38 +00:00
|
|
|
++$suppressCount;
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Restore error level to previous value
|
|
|
|
|
*/
|
2004-07-10 03:09:26 +00:00
|
|
|
function wfRestoreWarnings() {
|
|
|
|
|
wfSuppressWarnings( true );
|
|
|
|
|
}
|
2004-06-29 12:23:59 +00:00
|
|
|
|
2004-08-10 08:28:43 +00:00
|
|
|
# Autodetect, convert and provide timestamps of various types
|
2004-09-03 17:13:55 +00:00
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
/**
|
2005-05-07 12:48:12 +00:00
|
|
|
* Unix time - the number of seconds since 1970-01-01 00:00:00 UTC
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_UNIX', 0 );
|
2004-08-10 08:28:43 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2005-05-07 12:48:12 +00:00
|
|
|
* MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_MW', 1 );
|
2005-05-07 12:48:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MySQL DATETIME (YYYY-MM-DD HH:MM:SS)
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_DB', 2 );
|
2005-05-07 12:48:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RFC 2822 format, for E-mail and HTTP headers
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_RFC2822', 3 );
|
2005-05-07 12:48:12 +00:00
|
|
|
|
2005-11-03 02:21:01 +00:00
|
|
|
/**
|
|
|
|
|
* ISO 8601 format with no timezone: 1986-02-09T20:00:00Z
|
|
|
|
|
*
|
2006-01-07 13:09:30 +00:00
|
|
|
* This is used by Special:Export
|
2005-11-03 02:21:01 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_ISO_8601', 4 );
|
2005-11-03 02:21:01 +00:00
|
|
|
|
2005-05-07 12:48:12 +00:00
|
|
|
/**
|
|
|
|
|
* An Exif timestamp (YYYY:MM:DD HH:MM:SS)
|
|
|
|
|
*
|
Some small doc tweaks to reduce Doxygen warnings, namely:
* @link. You might think @link would surely mean "here comes a web URL" ... but @link is a valid command
in Doxygen, which means an entirely different kind of link (an internal link to somewhere, so that you can separate
documentation and implementation). The result is a mess, and the best solution I can see is to use "@see" instead of "@link".
* Warning: argument `nourl' of command @param is not found in the argument list of Linker::makeMediaLinkObj($title,$text='')
* Moving few class descriptions to right above classes, and/or formatting into Javadoc style.
* "@addtogroup Special Pages" --> "@addtogroup SpecialPage" so that all special pages have the same @addtogroup tag.
* @fixme --> @todo (must have missed these before)
* "@param $specialPage @see" remove the "@" in the "@see" to stop warning.
* @throws wants type, then a brief description, to stop warning.
This last one is for PHPdocumentor only, but it fixes something for PHPDocumentor, and should be neutral for Doxygen:
* WARNING in includes/api/ApiFormatYaml_spyc.php on line 860: docblock template never terminated with /**#@-*/
2007-04-18 09:50:10 +00:00
|
|
|
* @see http://exif.org/Exif2-2.PDF The Exif 2.2 spec, see page 28 for the
|
2005-05-07 12:48:12 +00:00
|
|
|
* DateTime tag and page 36 for the DateTimeOriginal and
|
|
|
|
|
* DateTimeDigitized tags.
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_EXIF', 5 );
|
2005-05-07 12:48:12 +00:00
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
/**
|
|
|
|
|
* Oracle format time.
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_ORACLE', 6 );
|
2005-05-07 12:48:12 +00:00
|
|
|
|
2006-07-18 01:40:38 +00:00
|
|
|
/**
|
|
|
|
|
* Postgres format time.
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_POSTGRES', 7 );
|
2006-07-18 01:40:38 +00:00
|
|
|
|
2009-01-14 22:20:15 +00:00
|
|
|
/**
|
|
|
|
|
* DB2 format time
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
define( 'TS_DB2', 8 );
|
2009-01-14 22:20:15 +00:00
|
|
|
|
2010-09-30 21:20:09 +00:00
|
|
|
/**
|
2011-04-23 13:55:27 +00:00
|
|
|
* ISO 8601 basic format with no timezone: 19860209T200000Z. This is used by ResourceLoader
|
2010-09-30 21:20:09 +00:00
|
|
|
*/
|
|
|
|
|
define( 'TS_ISO_8601_BASIC', 9 );
|
|
|
|
|
|
2005-05-07 12:48:12 +00:00
|
|
|
/**
|
2011-04-23 13:55:27 +00:00
|
|
|
* Get a timestamp string in one of various formats
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $outputtype Mixed: A timestamp in one of the supported formats, the
|
|
|
|
|
* function will autodetect which format is supplied and act
|
|
|
|
|
* accordingly.
|
2013-01-18 22:47:25 +00:00
|
|
|
* @param $ts Mixed: optional timestamp to convert, default 0 for the current time
|
2010-11-28 21:59:16 +00:00
|
|
|
* @return Mixed: String / false The same date in the format specified in $outputtype or false
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2009-07-23 17:14:07 +00:00
|
|
|
function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
|
2012-09-14 16:28:22 +00:00
|
|
|
try {
|
|
|
|
|
$timestamp = new MWTimestamp( $ts );
|
|
|
|
|
return $timestamp->getTimestamp( $outputtype );
|
|
|
|
|
} catch( TimestampException $e ) {
|
|
|
|
|
wfDebug("wfTimestamp() fed bogus time value: TYPE=$outputtype; VALUE=$ts\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2004-08-10 08:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
2005-04-25 18:38:43 +00:00
|
|
|
/**
|
|
|
|
|
* Return a formatted timestamp, or null if input is null.
|
|
|
|
|
* For dealing with nullable timestamp columns in the database.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $outputtype Integer
|
|
|
|
|
* @param $ts String
|
|
|
|
|
* @return String
|
2005-04-25 18:38:43 +00:00
|
|
|
*/
|
|
|
|
|
function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) {
|
|
|
|
|
if( is_null( $ts ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
return wfTimestamp( $outputtype, $ts );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-05 17:42:14 +00:00
|
|
|
/**
|
|
|
|
|
* Convenience function; returns MediaWiki timestamp for the present time.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfTimestampNow() {
|
|
|
|
|
# return NOW
|
|
|
|
|
return wfTimestamp( TS_MW, time() );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2005-12-08 08:01:39 +00:00
|
|
|
* Check if the operating system is Windows
|
2004-09-03 17:13:55 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @return Bool: true if it's Windows, False otherwise.
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2005-08-02 13:35:19 +00:00
|
|
|
function wfIsWindows() {
|
2010-10-26 15:14:56 +00:00
|
|
|
static $isWindows = null;
|
|
|
|
|
if ( $isWindows === null ) {
|
|
|
|
|
$isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2010-10-26 15:14:56 +00:00
|
|
|
return $isWindows;
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2004-08-21 14:14:58 +00:00
|
|
|
|
The beginnings of HipHop compiled mode support. It works now for parser cache hits.
* Work around HipHop issue 314 (volatile broken) and issue 308 (no compilation detection) by adding some large and ugly compilation detection code to WebStart.php and doMaintenance.php.
* Provide an MW_COMPILED constant which can be used to detect compiled mode throughout the codebase.
* Introduced wfIsHipHop(), which detects either compiled or interpreted mode. Used this to work around unusual eval() return value in eval.php.
* Work around lack of ini_get() in Maintenance.php, by duplicating wfIsHipHop().
* In Maintenance::shouldExecute(), accept "include" as an inclusion function name, since all kinds of inclusion give this string in HipHop.
* Introduced new class MWInit, which provides some static functions in the pre-autoloader environment.
* Introduced MWInit::compiledPath(), which provides a relative path for invoking a compiled file, and MWInit::interpretedPath(), which provides an absolute path for interpreting a PHP file. Used these new functions in the appropriate places.
* When we are running compiled code, don't include files which would generate duplicate class, function or constant definitions. Documented the new requirements on the contents of Defines.php and UtfNormalDefines.php.
* In HipHop compiled mode, it's not possible to have executable code in the same file as a class definition.
** Moved MimeMagic initialisation to the constructor.
** Moved Namespace.php global variable initialisation to Setup.php.
** Moved MemcachedSessions.php initialisation to the caller in GlobalFunctions.php.
** Moved Sanitizer.php constants and global variables to static class members. Introduced an accessor function for the attribs regex, as a new place to put code formerly at file level.
** Moved Language.php initialisation of $wgLanguageNames to Language::getLanguageNames(). Removed the global variable, marked "private" since forever.
* In two places: don't use error_log() with type=3 to append to a file, HipHop doesn't support it. Use file_put_contents() with FILE_APPEND instead.
* Work around the terrible breakage of class_exists() by using MWInit::classExists() instead in various places. In WebInstaller::getPageByName(), the class_exists() was marked with a fixme comment already, so I replaced it with an autoloader solution.
2011-04-04 12:59:55 +00:00
|
|
|
/**
|
|
|
|
|
* Check if we are running under HipHop
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @return Bool
|
The beginnings of HipHop compiled mode support. It works now for parser cache hits.
* Work around HipHop issue 314 (volatile broken) and issue 308 (no compilation detection) by adding some large and ugly compilation detection code to WebStart.php and doMaintenance.php.
* Provide an MW_COMPILED constant which can be used to detect compiled mode throughout the codebase.
* Introduced wfIsHipHop(), which detects either compiled or interpreted mode. Used this to work around unusual eval() return value in eval.php.
* Work around lack of ini_get() in Maintenance.php, by duplicating wfIsHipHop().
* In Maintenance::shouldExecute(), accept "include" as an inclusion function name, since all kinds of inclusion give this string in HipHop.
* Introduced new class MWInit, which provides some static functions in the pre-autoloader environment.
* Introduced MWInit::compiledPath(), which provides a relative path for invoking a compiled file, and MWInit::interpretedPath(), which provides an absolute path for interpreting a PHP file. Used these new functions in the appropriate places.
* When we are running compiled code, don't include files which would generate duplicate class, function or constant definitions. Documented the new requirements on the contents of Defines.php and UtfNormalDefines.php.
* In HipHop compiled mode, it's not possible to have executable code in the same file as a class definition.
** Moved MimeMagic initialisation to the constructor.
** Moved Namespace.php global variable initialisation to Setup.php.
** Moved MemcachedSessions.php initialisation to the caller in GlobalFunctions.php.
** Moved Sanitizer.php constants and global variables to static class members. Introduced an accessor function for the attribs regex, as a new place to put code formerly at file level.
** Moved Language.php initialisation of $wgLanguageNames to Language::getLanguageNames(). Removed the global variable, marked "private" since forever.
* In two places: don't use error_log() with type=3 to append to a file, HipHop doesn't support it. Use file_put_contents() with FILE_APPEND instead.
* Work around the terrible breakage of class_exists() by using MWInit::classExists() instead in various places. In WebInstaller::getPageByName(), the class_exists() was marked with a fixme comment already, so I replaced it with an autoloader solution.
2011-04-04 12:59:55 +00:00
|
|
|
*/
|
|
|
|
|
function wfIsHipHop() {
|
|
|
|
|
return function_exists( 'hphp_thread_set_warmup_enabled' );
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-27 16:56:11 +00:00
|
|
|
/**
|
|
|
|
|
* Swap two variables
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $x Mixed
|
|
|
|
|
* @param $y Mixed
|
2005-03-27 16:56:11 +00:00
|
|
|
*/
|
|
|
|
|
function swap( &$x, &$y ) {
|
|
|
|
|
$z = $x;
|
|
|
|
|
$x = $y;
|
|
|
|
|
$y = $z;
|
|
|
|
|
}
|
|
|
|
|
|
2005-05-15 10:37:56 +00:00
|
|
|
/**
|
2012-05-26 03:19:55 +00:00
|
|
|
* Tries to get the system directory for temporary files. First
|
|
|
|
|
* $wgTmpDirectory is checked, and then the TMPDIR, TMP, and TEMP
|
|
|
|
|
* environment variables are then checked in sequence, and if none are
|
|
|
|
|
* set try sys_get_temp_dir().
|
2010-08-01 12:31:50 +00:00
|
|
|
*
|
|
|
|
|
* NOTE: When possible, use instead the tmpfile() function to create
|
2010-07-18 21:40:49 +00:00
|
|
|
* temporary files to avoid race conditions on file creation, etc.
|
2005-05-15 10:37:56 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @return String
|
2005-05-15 10:37:56 +00:00
|
|
|
*/
|
|
|
|
|
function wfTempDir() {
|
2012-05-26 03:19:55 +00:00
|
|
|
global $wgTmpDirectory;
|
|
|
|
|
|
|
|
|
|
if ( $wgTmpDirectory !== false ) {
|
|
|
|
|
return $wgTmpDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$tmpDir = array_map( "getenv", array( 'TMPDIR', 'TMP', 'TEMP' ) );
|
|
|
|
|
|
|
|
|
|
foreach( $tmpDir as $tmp ) {
|
2005-05-15 10:37:56 +00:00
|
|
|
if( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp ) ) {
|
|
|
|
|
return $tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-05 22:58:54 +00:00
|
|
|
return sys_get_temp_dir();
|
2005-05-15 10:37:56 +00:00
|
|
|
}
|
|
|
|
|
|
2005-05-28 07:03:29 +00:00
|
|
|
/**
|
|
|
|
|
* Make directory, and make all parent directories if they don't exist
|
2010-08-01 12:31:50 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @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.
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2008-07-16 18:36:40 +00:00
|
|
|
* @return bool
|
2005-05-28 07:03:29 +00:00
|
|
|
*/
|
2009-03-15 14:30:39 +00:00
|
|
|
function wfMkdirParents( $dir, $mode = null, $caller = null ) {
|
2008-07-18 18:03:50 +00:00
|
|
|
global $wgDirectoryMode;
|
2008-07-04 06:47:47 +00:00
|
|
|
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( FileBackend::isStoragePath( $dir ) ) { // sanity
|
2012-08-23 03:46:54 +00:00
|
|
|
throw new MWException( __FUNCTION__ . " given storage path '$dir'." );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
2009-03-15 14:30:39 +00:00
|
|
|
if ( !is_null( $caller ) ) {
|
2011-11-03 16:18:48 +00:00
|
|
|
wfDebug( "$caller: called wfMkdirParents($dir)\n" );
|
2009-03-15 14:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if( strval( $dir ) === '' || file_exists( $dir ) ) {
|
2008-06-04 00:14:13 +00:00
|
|
|
return true;
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2008-06-04 00:14:13 +00:00
|
|
|
|
2009-05-14 20:45:14 +00:00
|
|
|
$dir = str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $dir );
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if ( is_null( $mode ) ) {
|
Turn wfMkdirParents() into just a thin wrapper around mkdir( $dir, $mode, true ); this won't work in PHP4, but we don't support that, do we?
NOTE: the old code used to ignore the umask by explicitly forcing the permissions with chmod(). If this is desired behavior, it can be achieved by temporarily setting the umask to 0, as in:
$oldmask = umask( 0 );
$rv = mkdir( $dir, $mode, true );
umask( $oldmask );
return $rv;
However, I can't see why we'd want to do this. In the worst case, users with excessively tight umasks can find themselves with unusable directories, but the proper solution to that is to fix the umask rather than to
ignore it. In the best case, we've just plugged a security hole the user didn't realize they had (because they assumed their umask would Just Work).
2008-11-21 12:52:36 +00:00
|
|
|
$mode = $wgDirectoryMode;
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2008-07-04 06:47:47 +00:00
|
|
|
|
2010-06-20 15:41:38 +00:00
|
|
|
// Turn off the normal warning, we're doing our own below
|
|
|
|
|
wfSuppressWarnings();
|
2010-08-14 13:31:10 +00:00
|
|
|
$ok = mkdir( $dir, $mode, true ); // PHP5 <3
|
2010-06-20 15:41:38 +00:00
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
2009-08-22 16:50:03 +00:00
|
|
|
if( !$ok ) {
|
|
|
|
|
// PHP doesn't report the path in its warning message, so add our own to aid in diagnosis.
|
2012-04-17 20:57:06 +00:00
|
|
|
trigger_error( sprintf( "%s: failed to mkdir \"%s\" mode 0%o", __FUNCTION__, $dir, $mode ),
|
|
|
|
|
E_USER_WARNING );
|
2009-08-22 16:50:03 +00:00
|
|
|
}
|
|
|
|
|
return $ok;
|
2005-05-28 07:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-24 15:41:06 +00:00
|
|
|
/**
|
|
|
|
|
* Remove a directory and all its content.
|
|
|
|
|
* Does not hide error.
|
|
|
|
|
*/
|
|
|
|
|
function wfRecursiveRemoveDir( $dir ) {
|
|
|
|
|
wfDebug( __FUNCTION__ . "( $dir )\n" );
|
|
|
|
|
// taken from http://de3.php.net/manual/en/function.rmdir.php#98622
|
|
|
|
|
if ( is_dir( $dir ) ) {
|
|
|
|
|
$objects = scandir( $dir );
|
|
|
|
|
foreach ( $objects as $object ) {
|
|
|
|
|
if ( $object != "." && $object != ".." ) {
|
|
|
|
|
if ( filetype( $dir . '/' . $object ) == "dir" ) {
|
|
|
|
|
wfRecursiveRemoveDir( $dir . '/' . $object );
|
|
|
|
|
} else {
|
|
|
|
|
unlink( $dir . '/' . $object );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
reset( $objects );
|
|
|
|
|
rmdir( $dir );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-16 21:59:53 +00:00
|
|
|
/**
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $nr Mixed: the number to format
|
|
|
|
|
* @param $acc Integer: the number of digits after the decimal point, default 2
|
|
|
|
|
* @param $round Boolean: whether or not to round the value, default true
|
2005-07-16 21:59:53 +00:00
|
|
|
* @return float
|
|
|
|
|
*/
|
2005-07-24 08:52:49 +00:00
|
|
|
function wfPercent( $nr, $acc = 2, $round = true ) {
|
2005-07-16 21:59:53 +00:00
|
|
|
$ret = sprintf( "%.${acc}f", $nr );
|
|
|
|
|
return $round ? round( $ret, $acc ) . '%' : "$ret%";
|
|
|
|
|
}
|
2005-07-26 16:09:00 +00:00
|
|
|
|
2005-10-22 21:19:50 +00:00
|
|
|
/**
|
|
|
|
|
* Find out whether or not a mixed variable exists in a string
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $needle String
|
|
|
|
|
* @param $str String
|
2011-01-02 04:38:04 +00:00
|
|
|
* @param $insensitive Boolean
|
2009-07-23 17:14:07 +00:00
|
|
|
* @return Boolean
|
2005-10-22 21:19:50 +00:00
|
|
|
*/
|
2011-01-02 04:38:04 +00:00
|
|
|
function in_string( $needle, $str, $insensitive = false ) {
|
|
|
|
|
$func = 'strpos';
|
|
|
|
|
if( $insensitive ) $func = 'stripos';
|
2011-06-30 02:59:43 +00:00
|
|
|
|
2011-01-02 04:38:04 +00:00
|
|
|
return $func( $str, $needle ) !== false;
|
2005-10-22 21:19:50 +00:00
|
|
|
}
|
2005-10-30 11:24:43 +00:00
|
|
|
|
2007-09-17 19:44:15 +00:00
|
|
|
/**
|
|
|
|
|
* Safety wrapper around ini_get() for boolean settings.
|
|
|
|
|
* The values returned from ini_get() are pre-normalized for settings
|
|
|
|
|
* set via php.ini or php_flag/php_admin_flag... but *not*
|
|
|
|
|
* for those set via php_value/php_admin_value.
|
|
|
|
|
*
|
|
|
|
|
* It's fairly common for people to use php_value instead of php_flag,
|
|
|
|
|
* which can leave you with an 'off' setting giving a false positive
|
|
|
|
|
* for code that just takes the ini_get() return value as a boolean.
|
|
|
|
|
*
|
|
|
|
|
* To make things extra interesting, setting via php_value accepts
|
2007-09-26 17:59:29 +00:00
|
|
|
* "true" and "yes" as true, but php.ini and php_flag consider them false. :)
|
2007-09-17 19:44:15 +00:00
|
|
|
* Unrecognized values go false... again opposite PHP's own coercion
|
|
|
|
|
* from string to bool.
|
|
|
|
|
*
|
|
|
|
|
* Luckily, 'properly' set settings will always come back as '0' or '1',
|
|
|
|
|
* so we only have to worry about them and the 'improper' settings.
|
|
|
|
|
*
|
|
|
|
|
* I frickin' hate PHP... :P
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $setting String
|
|
|
|
|
* @return Bool
|
2007-09-17 19:44:15 +00:00
|
|
|
*/
|
|
|
|
|
function wfIniGetBool( $setting ) {
|
|
|
|
|
$val = ini_get( $setting );
|
|
|
|
|
// 'on' and 'true' can't have whitespace around them, but '1' can.
|
2007-09-26 17:59:29 +00:00
|
|
|
return strtolower( $val ) == 'on'
|
|
|
|
|
|| strtolower( $val ) == 'true'
|
|
|
|
|
|| strtolower( $val ) == 'yes'
|
|
|
|
|
|| preg_match( "/^\s*[+-]?0*[1-9]/", $val ); // approx C atoi() function
|
2007-09-17 19:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
2010-06-14 18:09:19 +00:00
|
|
|
/**
|
|
|
|
|
* Wrapper function for PHP's dl(). This doesn't work in most situations from
|
|
|
|
|
* PHP 5.3 onward, and is usually disabled in shared environments anyway.
|
|
|
|
|
*
|
|
|
|
|
* @param $extension String A PHP extension. The file suffix (.so or .dll)
|
|
|
|
|
* should be omitted
|
2011-04-20 23:33:28 +00:00
|
|
|
* @param $fileName String Name of the library, if not $extension.suffix
|
2010-06-14 18:09:19 +00:00
|
|
|
* @return Bool - Whether or not the extension is loaded
|
|
|
|
|
*/
|
2011-04-20 23:33:28 +00:00
|
|
|
function wfDl( $extension, $fileName = null ) {
|
2010-06-14 18:09:19 +00:00
|
|
|
if( extension_loaded( $extension ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-20 23:33:28 +00:00
|
|
|
$canDl = false;
|
|
|
|
|
$sapi = php_sapi_name();
|
2012-05-04 23:10:51 +00:00
|
|
|
if( $sapi == 'cli' || $sapi == 'cgi' || $sapi == 'embed' ) {
|
2011-04-20 23:33:28 +00:00
|
|
|
$canDl = ( function_exists( 'dl' ) && is_callable( 'dl' )
|
2010-06-14 18:09:19 +00:00
|
|
|
&& wfIniGetBool( 'enable_dl' ) && !wfIniGetBool( 'safe_mode' ) );
|
2011-04-20 23:33:28 +00:00
|
|
|
}
|
2010-06-14 18:09:19 +00:00
|
|
|
|
|
|
|
|
if( $canDl ) {
|
2011-04-20 23:33:28 +00:00
|
|
|
$fileName = $fileName ? $fileName : $extension;
|
|
|
|
|
if( wfIsWindows() ) {
|
|
|
|
|
$fileName = 'php_' . $fileName;
|
|
|
|
|
}
|
2010-06-14 18:09:19 +00:00
|
|
|
wfSuppressWarnings();
|
2011-04-20 23:33:28 +00:00
|
|
|
dl( $fileName . '.' . PHP_SHLIB_SUFFIX );
|
2010-06-14 18:09:19 +00:00
|
|
|
wfRestoreWarnings();
|
|
|
|
|
}
|
|
|
|
|
return extension_loaded( $extension );
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-23 08:54:48 +00:00
|
|
|
/**
|
|
|
|
|
* Windows-compatible version of escapeshellarg()
|
|
|
|
|
* 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
|
|
|
|
|
* earlier distro releases of PHP)
|
|
|
|
|
*
|
|
|
|
|
* @param varargs
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
function wfEscapeShellArg( ) {
|
|
|
|
|
wfInitShellLocale();
|
|
|
|
|
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$first = true;
|
|
|
|
|
$retVal = '';
|
|
|
|
|
foreach ( $args as $arg ) {
|
|
|
|
|
if ( !$first ) {
|
|
|
|
|
$retVal .= ' ';
|
|
|
|
|
} else {
|
|
|
|
|
$first = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( wfIsWindows() ) {
|
|
|
|
|
// Escaping for an MSVC-style command line parser and CMD.EXE
|
|
|
|
|
// Refs:
|
|
|
|
|
// * http://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
|
|
|
|
|
// * http://technet.microsoft.com/en-us/library/cc723564.aspx
|
|
|
|
|
// * Bug #13518
|
|
|
|
|
// * CR r63214
|
|
|
|
|
// Double the backslashes before any double quotes. Escape the double quotes.
|
|
|
|
|
$tokens = preg_split( '/(\\\\*")/', $arg, -1, PREG_SPLIT_DELIM_CAPTURE );
|
|
|
|
|
$arg = '';
|
|
|
|
|
$iteration = 0;
|
|
|
|
|
foreach ( $tokens as $token ) {
|
|
|
|
|
if ( $iteration % 2 == 1 ) {
|
|
|
|
|
// Delimiter, a double quote preceded by zero or more slashes
|
|
|
|
|
$arg .= str_replace( '\\', '\\\\', substr( $token, 0, -1 ) ) . '\\"';
|
|
|
|
|
} elseif ( $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;
|
|
|
|
|
}
|
|
|
|
|
$iteration++;
|
|
|
|
|
}
|
|
|
|
|
// Double the backslashes before the end of the string, because
|
|
|
|
|
// we will soon add a quote
|
|
|
|
|
$m = array();
|
|
|
|
|
if ( preg_match( '/^(.*?)(\\\\+)$/', $arg, $m ) ) {
|
|
|
|
|
$arg = $m[1] . str_replace( '\\', '\\\\', $m[2] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add surrounding quotes
|
|
|
|
|
$retVal .= '"' . $arg . '"';
|
|
|
|
|
} else {
|
|
|
|
|
$retVal .= escapeshellarg( $arg );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-26 00:06:42 +00:00
|
|
|
/**
|
2006-05-26 02:41:20 +00:00
|
|
|
* Execute a shell command, with time and memory limits mirrored from the PHP
|
|
|
|
|
* configuration if supported.
|
2010-10-27 23:56:01 +00:00
|
|
|
* @param $cmd String Command line, properly escaped for shell.
|
2012-02-09 19:30:01 +00:00
|
|
|
* @param &$retval null|Mixed optional, will receive the program's exit code.
|
2006-05-26 02:41:20 +00:00
|
|
|
* (non-zero is usually failure)
|
2010-12-01 20:22:45 +00:00
|
|
|
* @param $environ Array optional environment variables which should be
|
2010-10-26 22:17:42 +00:00
|
|
|
* added to the executed command environment.
|
2013-01-13 22:48:03 +00:00
|
|
|
* @param $limits Array optional array with limits(filesize, memory, time, walltime)
|
2012-07-16 20:06:57 +00:00
|
|
|
* this overwrites the global wgShellMax* limits.
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return string collected stdout as a string (trailing newlines stripped)
|
2006-05-26 02:41:20 +00:00
|
|
|
*/
|
2012-07-16 20:06:57 +00:00
|
|
|
function wfShellExec( $cmd, &$retval = null, $environ = array(), $limits = array() ) {
|
2013-01-13 22:48:03 +00:00
|
|
|
global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime,
|
|
|
|
|
$wgMaxShellWallClockTime;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2009-01-07 12:20:30 +00:00
|
|
|
static $disabled;
|
|
|
|
|
if ( is_null( $disabled ) ) {
|
|
|
|
|
$disabled = false;
|
|
|
|
|
if( wfIniGetBool( 'safe_mode' ) ) {
|
|
|
|
|
wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
|
2010-09-30 15:35:10 +00:00
|
|
|
$disabled = 'safemode';
|
2010-10-26 14:31:13 +00:00
|
|
|
} else {
|
|
|
|
|
$functions = explode( ',', ini_get( 'disable_functions' ) );
|
|
|
|
|
$functions = array_map( 'trim', $functions );
|
|
|
|
|
$functions = array_map( 'strtolower', $functions );
|
|
|
|
|
if ( in_array( 'passthru', $functions ) ) {
|
|
|
|
|
wfDebug( "passthru is in disabled_functions\n" );
|
|
|
|
|
$disabled = 'passthru';
|
|
|
|
|
}
|
2009-01-07 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $disabled ) {
|
2006-05-26 02:41:20 +00:00
|
|
|
$retval = 1;
|
2010-09-30 15:35:10 +00:00
|
|
|
return $disabled == 'safemode' ?
|
|
|
|
|
'Unable to run external programs in safe mode.' :
|
|
|
|
|
'Unable to run external programs, passthru() is disabled.';
|
2006-05-26 02:41:20 +00:00
|
|
|
}
|
2009-01-07 12:20:30 +00:00
|
|
|
|
2008-09-29 10:19:11 +00:00
|
|
|
wfInitShellLocale();
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-10-26 22:17:42 +00:00
|
|
|
$envcmd = '';
|
|
|
|
|
foreach( $environ as $k => $v ) {
|
|
|
|
|
if ( wfIsWindows() ) {
|
2010-12-01 20:22:45 +00:00
|
|
|
/* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
|
|
|
|
|
* appear in the environment variable, so we must use carat escaping as documented in
|
|
|
|
|
* http://technet.microsoft.com/en-us/library/cc723564.aspx
|
|
|
|
|
* Note however that the quote isn't listed there, but is needed, and the parentheses
|
2010-10-26 22:17:42 +00:00
|
|
|
* are listed there but doesn't appear to need it.
|
|
|
|
|
*/
|
2010-12-18 15:00:11 +00:00
|
|
|
$envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . '&& ';
|
2010-10-26 22:17:42 +00:00
|
|
|
} else {
|
2010-12-01 20:22:45 +00:00
|
|
|
/* Assume this is a POSIX shell, thus required to accept variable assignments before the command
|
2010-10-26 22:17:42 +00:00
|
|
|
* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
|
|
|
|
|
*/
|
|
|
|
|
$envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$cmd = $envcmd . $cmd;
|
2010-12-01 20:22:45 +00:00
|
|
|
|
2012-05-04 23:10:51 +00:00
|
|
|
if ( php_uname( 's' ) == 'Linux' ) {
|
2012-07-16 20:06:57 +00:00
|
|
|
$time = intval ( isset($limits['time']) ? $limits['time'] : $wgMaxShellTime );
|
2013-01-13 22:48:03 +00:00
|
|
|
if ( isset( $limits['walltime'] ) ) {
|
|
|
|
|
$wallTime = intval( $limits['walltime'] );
|
|
|
|
|
} elseif ( isset( $limits['time'] ) ) {
|
|
|
|
|
$wallTime = $time;
|
|
|
|
|
} else {
|
|
|
|
|
$wallTime = intval( $wgMaxShellWallClockTime );
|
|
|
|
|
}
|
2012-07-16 20:06:57 +00:00
|
|
|
$mem = intval ( isset($limits['memory']) ? $limits['memory'] : $wgMaxShellMemory );
|
|
|
|
|
$filesize = intval ( isset($limits['filesize']) ? $limits['filesize'] : $wgMaxShellFileSize );
|
2006-06-17 10:21:55 +00:00
|
|
|
|
2013-01-13 22:48:03 +00:00
|
|
|
if ( $time > 0 || $mem > 0 || $filesize > 0 || $wallTime > 0 ) {
|
|
|
|
|
$cmd = '/bin/bash ' . escapeshellarg( "$IP/bin/ulimit5.sh" ) .
|
|
|
|
|
" $time $mem $filesize $wallTime " . escapeshellarg( $cmd );
|
2005-11-26 02:57:18 +00:00
|
|
|
}
|
2005-11-26 00:06:42 +00:00
|
|
|
}
|
2006-04-02 03:58:17 +00:00
|
|
|
wfDebug( "wfShellExec: $cmd\n" );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-05-26 02:41:20 +00:00
|
|
|
$retval = 1; // error by default?
|
2008-02-18 07:25:35 +00:00
|
|
|
ob_start();
|
|
|
|
|
passthru( $cmd, $retval );
|
|
|
|
|
$output = ob_get_contents();
|
|
|
|
|
ob_end_clean();
|
2008-09-05 03:32:09 +00:00
|
|
|
|
|
|
|
|
if ( $retval == 127 ) {
|
|
|
|
|
wfDebugLog( 'exec', "Possibly missing executable file: $cmd\n" );
|
|
|
|
|
}
|
2008-02-18 07:25:35 +00:00
|
|
|
return $output;
|
2008-09-29 10:19:11 +00:00
|
|
|
}
|
2009-07-23 17:14:07 +00:00
|
|
|
|
2008-09-29 10:19:11 +00:00
|
|
|
/**
|
|
|
|
|
* Workaround for http://bugs.php.net/bug.php?id=45132
|
|
|
|
|
* escapeshellarg() destroys non-ASCII characters if LANG is not a UTF-8 locale
|
|
|
|
|
*/
|
|
|
|
|
function wfInitShellLocale() {
|
|
|
|
|
static $done = false;
|
2010-08-14 13:31:10 +00:00
|
|
|
if ( $done ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-09-29 10:19:11 +00:00
|
|
|
$done = true;
|
|
|
|
|
global $wgShellLocale;
|
|
|
|
|
if ( !wfIniGetBool( 'safe_mode' ) ) {
|
|
|
|
|
putenv( "LC_CTYPE=$wgShellLocale" );
|
|
|
|
|
setlocale( LC_CTYPE, $wgShellLocale );
|
|
|
|
|
}
|
2005-11-26 00:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
2011-09-23 20:42:22 +00:00
|
|
|
/**
|
2012-04-04 23:48:55 +00:00
|
|
|
* Alias to wfShellWikiCmd()
|
|
|
|
|
* @see wfShellWikiCmd()
|
|
|
|
|
*/
|
|
|
|
|
function wfShellMaintenanceCmd( $script, array $parameters = array(), array $options = array() ) {
|
|
|
|
|
return wfShellWikiCmd( $script, $parameters, $options );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a shell-escaped command line string to run a MediaWiki cli script.
|
2011-09-23 20:42:22 +00:00
|
|
|
* Note that $parameters should be a flat array and an option with an argument
|
|
|
|
|
* should consist of two consecutive items in the array (do not use "--option value").
|
2012-04-04 23:48:55 +00:00
|
|
|
* @param $script string MediaWiki cli script path
|
2011-09-23 20:42:22 +00:00
|
|
|
* @param $parameters Array Arguments and options to the script
|
|
|
|
|
* @param $options Array Associative array of options:
|
|
|
|
|
* 'php': The path to the php executable
|
|
|
|
|
* 'wrapper': Path to a PHP wrapper to handle the maintenance script
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
2012-04-04 23:48:55 +00:00
|
|
|
function wfShellWikiCmd( $script, array $parameters = array(), array $options = array() ) {
|
2011-09-23 20:42:22 +00:00
|
|
|
global $wgPhpCli;
|
|
|
|
|
// Give site config file a chance to run the script in a wrapper.
|
|
|
|
|
// The caller may likely want to call wfBasename() on $script.
|
2012-04-04 23:48:55 +00:00
|
|
|
wfRunHooks( 'wfShellWikiCmd', array( &$script, &$parameters, &$options ) );
|
2011-09-23 20:42:22 +00:00
|
|
|
$cmd = isset( $options['php'] ) ? array( $options['php'] ) : array( $wgPhpCli );
|
|
|
|
|
if ( isset( $options['wrapper'] ) ) {
|
|
|
|
|
$cmd[] = $options['wrapper'];
|
|
|
|
|
}
|
|
|
|
|
$cmd[] = $script;
|
2012-03-19 18:28:47 +00:00
|
|
|
// Escape each parameter for shell
|
|
|
|
|
return implode( " ", array_map( 'wfEscapeShellArg', array_merge( $cmd, $parameters ) ) );
|
2011-09-23 20:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-23 08:54:48 +00:00
|
|
|
/**
|
|
|
|
|
* wfMerge attempts to merge differences between three texts.
|
|
|
|
|
* Returns true for a clean merge and false for failure or a conflict.
|
|
|
|
|
*
|
|
|
|
|
* @param $old String
|
|
|
|
|
* @param $mine String
|
|
|
|
|
* @param $yours String
|
|
|
|
|
* @param $result String
|
|
|
|
|
* @return Bool
|
|
|
|
|
*/
|
|
|
|
|
function wfMerge( $old, $mine, $yours, &$result ) {
|
|
|
|
|
global $wgDiff3;
|
|
|
|
|
|
|
|
|
|
# This check may also protect against code injection in
|
|
|
|
|
# case of broken installations.
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
|
|
|
|
if( !$haveDiff3 ) {
|
|
|
|
|
wfDebug( "diff3 not found\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Make temporary files
|
|
|
|
|
$td = wfTempDir();
|
|
|
|
|
$oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );
|
|
|
|
|
$mytextFile = fopen( $mytextName = tempnam( $td, 'merge-mine-' ), 'w' );
|
|
|
|
|
$yourtextFile = fopen( $yourtextName = tempnam( $td, 'merge-your-' ), 'w' );
|
|
|
|
|
|
2012-11-10 16:18:12 +00:00
|
|
|
# NOTE: diff3 issues a warning to stderr if any of the files does not end with
|
|
|
|
|
# a newline character. To avoid this, we normalize the trailing whitespace before
|
|
|
|
|
# creating the diff.
|
|
|
|
|
|
|
|
|
|
fwrite( $oldtextFile, rtrim( $old ) . "\n" );
|
2011-10-23 08:54:48 +00:00
|
|
|
fclose( $oldtextFile );
|
2012-11-10 16:18:12 +00:00
|
|
|
fwrite( $mytextFile, rtrim( $mine ) . "\n" );
|
2011-10-23 08:54:48 +00:00
|
|
|
fclose( $mytextFile );
|
2012-11-10 16:18:12 +00:00
|
|
|
fwrite( $yourtextFile, rtrim( $yours ) . "\n" );
|
2011-10-23 08:54:48 +00:00
|
|
|
fclose( $yourtextFile );
|
|
|
|
|
|
|
|
|
|
# Check for a conflict
|
2012-12-29 20:39:54 +00:00
|
|
|
$cmd = wfEscapeShellArg( $wgDiff3 ) . ' -a --overlap-only ' .
|
2011-10-23 08:54:48 +00:00
|
|
|
wfEscapeShellArg( $mytextName ) . ' ' .
|
|
|
|
|
wfEscapeShellArg( $oldtextName ) . ' ' .
|
|
|
|
|
wfEscapeShellArg( $yourtextName );
|
|
|
|
|
$handle = popen( $cmd, 'r' );
|
|
|
|
|
|
|
|
|
|
if( fgets( $handle, 1024 ) ) {
|
|
|
|
|
$conflict = true;
|
|
|
|
|
} else {
|
|
|
|
|
$conflict = false;
|
|
|
|
|
}
|
|
|
|
|
pclose( $handle );
|
|
|
|
|
|
|
|
|
|
# Merge differences
|
2012-12-29 20:39:54 +00:00
|
|
|
$cmd = wfEscapeShellArg( $wgDiff3 ) . ' -a -e --merge ' .
|
2011-10-23 08:54:48 +00:00
|
|
|
wfEscapeShellArg( $mytextName, $oldtextName, $yourtextName );
|
|
|
|
|
$handle = popen( $cmd, 'r' );
|
|
|
|
|
$result = '';
|
|
|
|
|
do {
|
|
|
|
|
$data = fread( $handle, 8192 );
|
|
|
|
|
if ( strlen( $data ) == 0 ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$result .= $data;
|
|
|
|
|
} while ( true );
|
|
|
|
|
pclose( $handle );
|
|
|
|
|
unlink( $mytextName );
|
|
|
|
|
unlink( $oldtextName );
|
|
|
|
|
unlink( $yourtextName );
|
|
|
|
|
|
|
|
|
|
if ( $result === '' && $old !== '' && !$conflict ) {
|
|
|
|
|
wfDebug( "Unexpected null result from diff3. Command: $cmd\n" );
|
|
|
|
|
$conflict = true;
|
|
|
|
|
}
|
|
|
|
|
return !$conflict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns unified plain-text diff of two texts.
|
|
|
|
|
* Useful for machine processing of diffs.
|
|
|
|
|
*
|
|
|
|
|
* @param $before String: the text before the changes.
|
|
|
|
|
* @param $after String: the text after the changes.
|
|
|
|
|
* @param $params String: command-line options for the diff command.
|
|
|
|
|
* @return String: unified diff of $before and $after
|
|
|
|
|
*/
|
|
|
|
|
function wfDiff( $before, $after, $params = '-u' ) {
|
|
|
|
|
if ( $before == $after ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global $wgDiff;
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$haveDiff = $wgDiff && file_exists( $wgDiff );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
|
|
|
|
# This check may also protect against code injection in
|
|
|
|
|
# case of broken installations.
|
|
|
|
|
if( !$haveDiff ) {
|
|
|
|
|
wfDebug( "diff executable not found\n" );
|
|
|
|
|
$diffs = new Diff( explode( "\n", $before ), explode( "\n", $after ) );
|
|
|
|
|
$format = new UnifiedDiffFormatter();
|
|
|
|
|
return $format->format( $diffs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Make temporary files
|
|
|
|
|
$td = wfTempDir();
|
|
|
|
|
$oldtextFile = fopen( $oldtextName = tempnam( $td, 'merge-old-' ), 'w' );
|
|
|
|
|
$newtextFile = fopen( $newtextName = tempnam( $td, 'merge-your-' ), 'w' );
|
|
|
|
|
|
|
|
|
|
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 ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$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 ) {
|
|
|
|
|
unset( $diff_lines[0] );
|
|
|
|
|
}
|
|
|
|
|
if ( strpos( $diff_lines[1], '+++' ) === 0 ) {
|
|
|
|
|
unset( $diff_lines[1] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$diff = implode( "\n", $diff_lines );
|
|
|
|
|
|
|
|
|
|
return $diff;
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-31 02:53:35 +00:00
|
|
|
/**
|
|
|
|
|
* This function works like "use VERSION" in Perl, the program will die with a
|
|
|
|
|
* backtrace if the current version of PHP is less than the version provided
|
|
|
|
|
*
|
|
|
|
|
* This is useful for extensions which due to their nature are not kept in sync
|
|
|
|
|
* with releases, and might depend on other versions of PHP than the main code
|
|
|
|
|
*
|
|
|
|
|
* Note: PHP might die due to parsing errors in some cases before it ever
|
|
|
|
|
* manages to call this function, such is life
|
|
|
|
|
*
|
|
|
|
|
* @see perldoc -f use
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $req_ver Mixed: the version to check, can be a string, an integer, or
|
|
|
|
|
* a float
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2005-12-31 02:53:35 +00:00
|
|
|
*/
|
|
|
|
|
function wfUsePHP( $req_ver ) {
|
|
|
|
|
$php_ver = PHP_VERSION;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
if ( version_compare( $php_ver, (string)$req_ver, '<' ) ) {
|
|
|
|
|
throw new MWException( "PHP $req_ver required--this is only $php_ver" );
|
|
|
|
|
}
|
2005-12-31 02:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function works like "use VERSION" in Perl except it checks the version
|
|
|
|
|
* of MediaWiki, the program will die with a backtrace if the current version
|
|
|
|
|
* of MediaWiki is less than the version provided.
|
|
|
|
|
*
|
|
|
|
|
* This is useful for extensions which due to their nature are not kept in sync
|
|
|
|
|
* with releases
|
|
|
|
|
*
|
|
|
|
|
* @see perldoc -f use
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $req_ver Mixed: the version to check, can be a string, an integer, or
|
|
|
|
|
* a float
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2005-12-31 02:53:35 +00:00
|
|
|
*/
|
|
|
|
|
function wfUseMW( $req_ver ) {
|
2011-05-15 13:21:16 +00:00
|
|
|
global $wgVersion;
|
|
|
|
|
|
|
|
|
|
if ( version_compare( $wgVersion, (string)$req_ver, '<' ) ) {
|
|
|
|
|
throw new MWException( "MediaWiki $req_ver required--this is only $wgVersion" );
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2005-12-31 02:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-20 09:03:16 +00:00
|
|
|
/**
|
|
|
|
|
* Return the final portion of a pathname.
|
2012-07-10 12:48:06 +00:00
|
|
|
* Reimplemented because PHP5's "basename()" is buggy with multibyte text.
|
2006-01-20 09:03:16 +00:00
|
|
|
* http://bugs.php.net/bug.php?id=33898
|
|
|
|
|
*
|
|
|
|
|
* PHP's basename() only considers '\' a pathchar on Windows and Netware.
|
2012-07-15 20:32:48 +00:00
|
|
|
* We'll consider it so always, as we don't want '\s' in our Unix paths either.
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $path String
|
|
|
|
|
* @param $suffix String: to remove if present
|
|
|
|
|
* @return String
|
2006-01-20 09:03:16 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfBaseName( $path, $suffix = '' ) {
|
|
|
|
|
$encSuffix = ( $suffix == '' )
|
2007-07-13 18:08:41 +00:00
|
|
|
? ''
|
|
|
|
|
: ( '(?:' . preg_quote( $suffix, '#' ) . ')?' );
|
2006-11-23 08:25:56 +00:00
|
|
|
$matches = array();
|
2007-07-13 18:08:41 +00:00
|
|
|
if( preg_match( "#([^/\\\\]*?){$encSuffix}[/\\\\]*$#", $path, $matches ) ) {
|
2006-01-20 09:03:16 +00:00
|
|
|
return $matches[1];
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-14 02:49:43 +00:00
|
|
|
|
2007-01-12 00:35:26 +00:00
|
|
|
/**
|
|
|
|
|
* Generate a relative path name to the given file.
|
|
|
|
|
* May explode on non-matching case-insensitive paths,
|
|
|
|
|
* funky symlinks, etc.
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $path String: absolute destination path including target filename
|
|
|
|
|
* @param $from String: Absolute source path, directory only
|
|
|
|
|
* @return String
|
2007-01-12 00:35:26 +00:00
|
|
|
*/
|
|
|
|
|
function wfRelativePath( $path, $from ) {
|
2007-01-19 10:40:57 +00:00
|
|
|
// Normalize mixed input on Windows...
|
|
|
|
|
$path = str_replace( '/', DIRECTORY_SEPARATOR, $path );
|
|
|
|
|
$from = str_replace( '/', DIRECTORY_SEPARATOR, $from );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-03-06 01:07:00 +00:00
|
|
|
// Trim trailing slashes -- fix for drive root
|
|
|
|
|
$path = rtrim( $path, DIRECTORY_SEPARATOR );
|
|
|
|
|
$from = rtrim( $from, DIRECTORY_SEPARATOR );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
$pieces = explode( DIRECTORY_SEPARATOR, dirname( $path ) );
|
2007-01-12 00:35:26 +00:00
|
|
|
$against = explode( DIRECTORY_SEPARATOR, $from );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-03-06 01:07:00 +00:00
|
|
|
if( $pieces[0] !== $against[0] ) {
|
|
|
|
|
// Non-matching Windows drive letters?
|
|
|
|
|
// Return a full path.
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2007-01-12 00:35:26 +00:00
|
|
|
|
|
|
|
|
// Trim off common prefix
|
|
|
|
|
while( count( $pieces ) && count( $against )
|
|
|
|
|
&& $pieces[0] == $against[0] ) {
|
|
|
|
|
array_shift( $pieces );
|
|
|
|
|
array_shift( $against );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// relative dots to bump us to the parent
|
|
|
|
|
while( count( $against ) ) {
|
|
|
|
|
array_unshift( $pieces, '..' );
|
|
|
|
|
array_shift( $against );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
array_push( $pieces, wfBaseName( $path ) );
|
|
|
|
|
|
|
|
|
|
return implode( DIRECTORY_SEPARATOR, $pieces );
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 23:37:19 +00:00
|
|
|
/**
|
|
|
|
|
* Do any deferred updates and clear the list
|
2010-12-07 11:49:13 +00:00
|
|
|
*
|
2011-09-10 06:50:30 +00:00
|
|
|
* @deprecated since 1.19
|
|
|
|
|
* @see DeferredUpdates::doUpdate()
|
2011-10-15 22:58:42 +00:00
|
|
|
* @param $commit string
|
2006-01-27 23:37:19 +00:00
|
|
|
*/
|
2010-12-07 15:47:34 +00:00
|
|
|
function wfDoUpdates( $commit = '' ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.19' );
|
2011-09-10 06:50:30 +00:00
|
|
|
DeferredUpdates::doUpdates( $commit );
|
2006-01-27 23:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-16 01:16:45 +00:00
|
|
|
/**
|
|
|
|
|
* Convert an arbitrarily-long digit string from one numeric base
|
|
|
|
|
* to another, optionally zero-padding to a minimum column width.
|
|
|
|
|
*
|
|
|
|
|
* Supports base 2 through 36; digit values 10-36 are represented
|
|
|
|
|
* as lowercase letters a-z. Input is case-insensitive.
|
|
|
|
|
*
|
2012-12-25 20:32:04 +00:00
|
|
|
* @param string $input Input number
|
|
|
|
|
* @param int $sourceBase Base of the input number
|
|
|
|
|
* @param int $destBase Desired base of the output
|
|
|
|
|
* @param int $pad Minimum number of digits in the output (pad with zeroes)
|
|
|
|
|
* @param bool $lowercase Whether to output in lowercase or uppercase
|
|
|
|
|
* @param string $engine Either "gmp", "bcmath", or "php"
|
|
|
|
|
* @return string|bool The output number as a string, or false on error
|
|
|
|
|
*/
|
|
|
|
|
function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1, $lowercase = true, $engine = 'auto' ) {
|
|
|
|
|
if(
|
|
|
|
|
$sourceBase < 2 ||
|
2006-06-16 01:16:45 +00:00
|
|
|
$sourceBase > 36 ||
|
|
|
|
|
$destBase < 2 ||
|
|
|
|
|
$destBase > 36 ||
|
2012-12-25 20:32:04 +00:00
|
|
|
$sourceBase != (int) $sourceBase ||
|
|
|
|
|
$destBase != (int) $destBase ||
|
|
|
|
|
$pad != (int) $pad ||
|
|
|
|
|
!preg_match( "/^[" . substr( '0123456789abcdefghijklmnopqrstuvwxyz', 0, $sourceBase ) . "]+$/i", $input )
|
|
|
|
|
) {
|
2006-06-16 01:16:45 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
static $baseChars = array (
|
|
|
|
|
10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f',
|
|
|
|
|
16 => 'g', 17 => 'h', 18 => 'i', 19 => 'j', 20 => 'k', 21 => 'l',
|
|
|
|
|
22 => 'm', 23 => 'n', 24 => 'o', 25 => 'p', 26 => 'q', 27 => 'r',
|
|
|
|
|
28 => 's', 29 => 't', 30 => 'u', 31 => 'v', 32 => 'w', 33 => 'x',
|
|
|
|
|
34 => 'y', 35 => 'z',
|
|
|
|
|
|
|
|
|
|
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5,
|
|
|
|
|
'6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11,
|
|
|
|
|
'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17,
|
|
|
|
|
'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23,
|
|
|
|
|
'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29,
|
|
|
|
|
'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if( extension_loaded( 'gmp' ) && ( $engine == 'auto' || $engine == 'gmp' ) ) {
|
|
|
|
|
$result = gmp_strval( gmp_init( $input, $sourceBase ), $destBase );
|
|
|
|
|
} elseif( extension_loaded( 'bcmath' ) && ( $engine == 'auto' || $engine == 'bcmath' ) ) {
|
|
|
|
|
$decimal = '0';
|
|
|
|
|
foreach( str_split( strtolower( $input ) ) as $char ) {
|
|
|
|
|
$decimal = bcmul( $decimal, $sourceBase );
|
|
|
|
|
$decimal = bcadd( $decimal, $baseChars[$char] );
|
2006-06-16 01:16:45 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
for( $result = ''; bccomp( $decimal, 0 ); $decimal = bcdiv( $decimal, $destBase, 0 ) ) {
|
|
|
|
|
$result .= $baseChars[bcmod( $decimal, $destBase )];
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
$result = strrev( $result );
|
|
|
|
|
} else {
|
|
|
|
|
$inDigits = array();
|
|
|
|
|
foreach( str_split( strtolower( $input ) ) as $char ) {
|
|
|
|
|
$inDigits[] = $baseChars[$char];
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
// Iterate over the input, modulo-ing out an output digit
|
|
|
|
|
// at a time until input is gone.
|
|
|
|
|
$result = '';
|
|
|
|
|
while( $inDigits ) {
|
|
|
|
|
$work = 0;
|
|
|
|
|
$workDigits = array();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
// Long division...
|
|
|
|
|
foreach( $inDigits as $digit ) {
|
|
|
|
|
$work *= $sourceBase;
|
|
|
|
|
$work += $digit;
|
|
|
|
|
|
|
|
|
|
if( $workDigits || $work >= $destBase ) {
|
|
|
|
|
$workDigits[] = (int) ( $work / $destBase );
|
|
|
|
|
}
|
|
|
|
|
$work %= $destBase;
|
2006-06-16 01:16:45 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
// All that division leaves us with a remainder,
|
|
|
|
|
// which is conveniently our next output digit.
|
|
|
|
|
$result .= $baseChars[$work];
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
// And we continue!
|
|
|
|
|
$inDigits = $workDigits;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
$result = strrev( $result );
|
2006-06-16 01:16:45 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
if( !$lowercase ) {
|
|
|
|
|
$result = strtoupper( $result );
|
2012-12-25 08:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-25 20:32:04 +00:00
|
|
|
return str_pad( $result, $pad, '0', STR_PAD_LEFT );
|
2006-06-16 01:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-25 08:38:17 +00:00
|
|
|
/**
|
|
|
|
|
* Create an object with a given name and an array of construct parameters
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $name String
|
|
|
|
|
* @param $p Array: parameters
|
2011-10-15 22:58:42 +00:00
|
|
|
* @return object
|
2011-07-18 23:01:08 +00:00
|
|
|
* @deprecated since 1.18, warnings in 1.18, removal in 1.20
|
2006-06-25 08:38:17 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfCreateObject( $name, $p ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __FUNCTION__, '1.18' );
|
2011-01-03 02:10:05 +00:00
|
|
|
return MWFunction::newObj( $name, $p );
|
2006-06-25 08:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-15 22:58:42 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2008-05-01 20:25:17 +00:00
|
|
|
function wfHttpOnlySafe() {
|
|
|
|
|
global $wgHttpOnlyBlacklist;
|
2008-12-23 21:30:19 +00:00
|
|
|
|
2008-05-01 20:25:17 +00:00
|
|
|
if( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
|
|
|
|
|
foreach( $wgHttpOnlyBlacklist as $regex ) {
|
|
|
|
|
if( preg_match( $regex, $_SERVER['HTTP_USER_AGENT'] ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-10 06:43:01 +00:00
|
|
|
|
2008-05-01 20:25:17 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-29 22:02:18 +00:00
|
|
|
/**
|
|
|
|
|
* Check if there is sufficent entropy in php's built-in session generation
|
|
|
|
|
* @return bool true = there is sufficient entropy
|
|
|
|
|
*/
|
|
|
|
|
function wfCheckEntropy() {
|
|
|
|
|
return (
|
|
|
|
|
( wfIsWindows() && version_compare( PHP_VERSION, '5.3.3', '>=' ) )
|
|
|
|
|
|| ini_get( 'session.entropy_file' )
|
|
|
|
|
)
|
|
|
|
|
&& intval( ini_get( 'session.entropy_length' ) ) >= 32;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
/**
|
|
|
|
|
* Override session_id before session startup if php's built-in
|
|
|
|
|
* session generation code is not secure.
|
|
|
|
|
*/
|
|
|
|
|
function wfFixSessionID() {
|
|
|
|
|
// If the cookie or session id is already set we already have a session and should abort
|
|
|
|
|
if ( isset( $_COOKIE[ session_name() ] ) || session_id() ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PHP's built-in session entropy is enabled if:
|
|
|
|
|
// - entropy_file is set or you're on Windows with php 5.3.3+
|
|
|
|
|
// - AND entropy_length is > 0
|
|
|
|
|
// We treat it as disabled if it doesn't have an entropy length of at least 32
|
2012-11-29 22:02:18 +00:00
|
|
|
$entropyEnabled = wfCheckEntropy();
|
2012-04-04 23:48:55 +00:00
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
// If built-in entropy is not enabled or not sufficient override php's built in session id generation code
|
|
|
|
|
if ( !$entropyEnabled ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": PHP's built in entropy is disabled or not sufficient, overriding session id generation using our cryptrand source.\n" );
|
2012-03-21 10:27:34 +00:00
|
|
|
session_id( MWCryptRand::generateHex( 32 ) );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-26 07:15:39 +00:00
|
|
|
/**
|
|
|
|
|
* Initialise php session
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $sessionId Bool
|
2006-07-26 07:15:39 +00:00
|
|
|
*/
|
2010-09-06 10:18:53 +00:00
|
|
|
function wfSetupSession( $sessionId = false ) {
|
2012-08-07 05:25:46 +00:00
|
|
|
global $wgSessionsInMemcached, $wgSessionsInObjectCache, $wgCookiePath, $wgCookieDomain,
|
2009-04-10 14:53:32 +00:00
|
|
|
$wgCookieSecure, $wgCookieHttpOnly, $wgSessionHandler;
|
2012-08-07 05:25:46 +00:00
|
|
|
if( $wgSessionsInObjectCache || $wgSessionsInMemcached ) {
|
|
|
|
|
ObjectCacheSessionHandler::install();
|
2009-04-10 14:53:32 +00:00
|
|
|
} elseif( $wgSessionHandler && $wgSessionHandler != ini_get( 'session.save_handler' ) ) {
|
|
|
|
|
# Only set this if $wgSessionHandler isn't null and session.save_handler
|
|
|
|
|
# hasn't already been set to the desired value (that causes errors)
|
2010-08-14 13:31:10 +00:00
|
|
|
ini_set( 'session.save_handler', $wgSessionHandler );
|
2006-07-26 07:15:39 +00:00
|
|
|
}
|
2011-04-01 09:50:09 +00:00
|
|
|
$httpOnlySafe = wfHttpOnlySafe() && $wgCookieHttpOnly;
|
2008-05-01 20:25:17 +00:00
|
|
|
wfDebugLog( 'cookie',
|
|
|
|
|
'session_set_cookie_params: "' . implode( '", "',
|
|
|
|
|
array(
|
|
|
|
|
0,
|
|
|
|
|
$wgCookiePath,
|
|
|
|
|
$wgCookieDomain,
|
|
|
|
|
$wgCookieSecure,
|
2011-04-01 09:50:09 +00:00
|
|
|
$httpOnlySafe ) ) . '"' );
|
|
|
|
|
session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure, $httpOnlySafe );
|
2006-07-26 07:15:39 +00:00
|
|
|
session_cache_limiter( 'private, must-revalidate' );
|
2010-09-06 10:18:53 +00:00
|
|
|
if ( $sessionId ) {
|
|
|
|
|
session_id( $sessionId );
|
2012-03-20 05:17:40 +00:00
|
|
|
} else {
|
|
|
|
|
wfFixSessionID();
|
2010-09-06 10:18:53 +00:00
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
wfSuppressWarnings();
|
|
|
|
|
session_start();
|
|
|
|
|
wfRestoreWarnings();
|
2006-07-26 07:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an object from the precompiled serialized directory
|
|
|
|
|
*
|
2011-04-23 13:55:27 +00:00
|
|
|
* @param $name String
|
2009-07-23 17:14:07 +00:00
|
|
|
* @return Mixed: the variable on success, false on failure
|
2006-07-26 07:15:39 +00:00
|
|
|
*/
|
|
|
|
|
function wfGetPrecompiledData( $name ) {
|
|
|
|
|
global $IP;
|
|
|
|
|
|
|
|
|
|
$file = "$IP/serialized/$name";
|
|
|
|
|
if ( file_exists( $file ) ) {
|
|
|
|
|
$blob = file_get_contents( $file );
|
|
|
|
|
if ( $blob ) {
|
|
|
|
|
return unserialize( $blob );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-04 09:06:18 +00:00
|
|
|
/**
|
|
|
|
|
* Get a cache key
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param varargs
|
|
|
|
|
* @return String
|
2006-10-04 09:06:18 +00:00
|
|
|
*/
|
|
|
|
|
function wfMemcKey( /*... */ ) {
|
2011-12-08 09:07:10 +00:00
|
|
|
global $wgCachePrefix;
|
|
|
|
|
$prefix = $wgCachePrefix === false ? wfWikiID() : $wgCachePrefix;
|
2006-10-04 09:06:18 +00:00
|
|
|
$args = func_get_args();
|
2011-12-08 09:07:10 +00:00
|
|
|
$key = $prefix . ':' . implode( ':', $args );
|
2009-08-03 11:36:47 +00:00
|
|
|
$key = str_replace( ' ', '_', $key );
|
2006-10-04 09:06:18 +00:00
|
|
|
return $key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cache key for a foreign DB
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $db String
|
|
|
|
|
* @param $prefix String
|
|
|
|
|
* @param varargs String
|
|
|
|
|
* @return String
|
2006-10-04 09:06:18 +00:00
|
|
|
*/
|
|
|
|
|
function wfForeignMemcKey( $db, $prefix /*, ... */ ) {
|
|
|
|
|
$args = array_slice( func_get_args(), 2 );
|
2008-05-09 23:52:04 +00:00
|
|
|
if ( $prefix ) {
|
|
|
|
|
$key = "$db-$prefix:" . implode( ':', $args );
|
|
|
|
|
} else {
|
|
|
|
|
$key = $db . ':' . implode( ':', $args );
|
|
|
|
|
}
|
2006-10-04 09:06:18 +00:00
|
|
|
return $key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an ASCII string identifying this wiki
|
|
|
|
|
* This is used as a prefix in memcached keys
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @return String
|
2006-10-04 09:06:18 +00:00
|
|
|
*/
|
2009-06-17 07:31:00 +00:00
|
|
|
function wfWikiID() {
|
2011-12-08 09:07:10 +00:00
|
|
|
global $wgDBprefix, $wgDBname;
|
|
|
|
|
if ( $wgDBprefix ) {
|
2009-06-17 07:31:00 +00:00
|
|
|
return "$wgDBname-$wgDBprefix";
|
|
|
|
|
} else {
|
|
|
|
|
return $wgDBname;
|
2006-10-04 09:06:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Split a wiki ID into DB name and table prefix
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
|
|
|
|
* @param $wiki String
|
2011-09-16 17:58:50 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function wfSplitWikiID( $wiki ) {
|
|
|
|
|
$bits = explode( '-', $wiki, 2 );
|
|
|
|
|
if ( count( $bits ) < 2 ) {
|
|
|
|
|
$bits[] = '';
|
|
|
|
|
}
|
|
|
|
|
return $bits;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
/**
|
2008-05-23 08:54:19 +00:00
|
|
|
* Get a Database object.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $db Integer: index of the connection to get. May be DB_MASTER for the
|
|
|
|
|
* master (for write queries), DB_SLAVE for potentially lagged read
|
|
|
|
|
* queries, or an integer >= 0 for a particular server.
|
2006-11-21 09:53:45 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $groups Mixed: query groups. An array of group names that this query
|
|
|
|
|
* belongs to. May contain a single string if the query is only
|
|
|
|
|
* in one group.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $wiki String: the wiki ID, or false for the current wiki
|
2008-05-23 08:54:19 +00:00
|
|
|
*
|
2008-07-04 06:47:47 +00:00
|
|
|
* Note: multiple calls to wfGetDB(DB_SLAVE) during the course of one request
|
|
|
|
|
* will always return the same object, unless the underlying connection or load
|
|
|
|
|
* balancer is manually destroyed.
|
2010-08-14 13:31:10 +00:00
|
|
|
*
|
2011-05-24 17:48:22 +00:00
|
|
|
* Note 2: use $this->getDB() in maintenance scripts that may be invoked by
|
|
|
|
|
* updater to ensure that a proper database is being updated.
|
|
|
|
|
*
|
2010-08-10 06:17:49 +00:00
|
|
|
* @return DatabaseBase
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2008-09-20 15:00:53 +00:00
|
|
|
function &wfGetDB( $db, $groups = array(), $wiki = false ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a load balancer object.
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $wiki String: wiki ID, or false for the current wiki
|
2008-03-30 09:48:15 +00:00
|
|
|
* @return LoadBalancer
|
|
|
|
|
*/
|
|
|
|
|
function wfGetLB( $wiki = false ) {
|
|
|
|
|
return wfGetLBFactory()->getMainLB( $wiki );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the load balancer factory object
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2010-12-30 02:45:28 +00:00
|
|
|
* @return LBFactory
|
2006-11-21 09:53:45 +00:00
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function &wfGetLBFactory() {
|
|
|
|
|
return LBFactory::singleton();
|
2006-11-21 09:53:45 +00:00
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
|
|
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Find a file.
|
2007-05-30 21:02:32 +00:00
|
|
|
* Shortcut for RepoGroup::singleton()->findFile()
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2010-09-25 16:45:41 +00:00
|
|
|
* @param $title String or Title object
|
2012-02-09 18:01:54 +00:00
|
|
|
* @param $options array Associative array of options:
|
2009-08-15 09:59:59 +00:00
|
|
|
* time: requested time for an archived image, or false for the
|
|
|
|
|
* current version. An image object will be returned which was
|
|
|
|
|
* created at the specified time.
|
|
|
|
|
*
|
|
|
|
|
* ignoreRedirect: If true, do not follow file redirects
|
|
|
|
|
*
|
2010-08-01 12:31:50 +00:00
|
|
|
* private: If true, return restricted (deleted) files if the current
|
2009-08-15 09:59:59 +00:00
|
|
|
* user is allowed to view them. Otherwise, such files will not
|
|
|
|
|
* be found.
|
|
|
|
|
*
|
|
|
|
|
* bypassCache: If true, do not use the process-local cache of File objects
|
|
|
|
|
*
|
2007-05-30 21:02:32 +00:00
|
|
|
* @return File, or false if the file does not exist
|
|
|
|
|
*/
|
2009-08-15 09:59:59 +00:00
|
|
|
function wfFindFile( $title, $options = array() ) {
|
|
|
|
|
return RepoGroup::singleton()->findFile( $title, $options );
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an object referring to a locally registered file.
|
|
|
|
|
* Returns a valid placeholder object if the file does not exist.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2012-01-19 17:27:29 +00:00
|
|
|
* @param $title Title|String
|
2012-04-07 20:51:53 +00:00
|
|
|
* @return LocalFile|null A File, or null if passed an invalid Title
|
2007-05-30 21:02:32 +00:00
|
|
|
*/
|
2008-10-07 21:42:23 +00:00
|
|
|
function wfLocalFile( $title ) {
|
|
|
|
|
return RepoGroup::singleton()->getLocalRepo()->newFile( $title );
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-13 19:03:51 +00:00
|
|
|
/**
|
|
|
|
|
* Stream a file to the browser. Back-compat alias for StreamFile::stream()
|
|
|
|
|
* @deprecated since 1.19
|
|
|
|
|
*/
|
|
|
|
|
function wfStreamFile( $fname, $headers = array() ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __FUNCTION__, '1.19' );
|
2011-08-13 19:03:51 +00:00
|
|
|
StreamFile::stream( $fname, $headers );
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-06 03:55:36 +00:00
|
|
|
/**
|
|
|
|
|
* Should low-performance queries be disabled?
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @return Boolean
|
2011-02-06 22:14:32 +00:00
|
|
|
* @codeCoverageIgnore
|
2007-07-06 03:55:36 +00:00
|
|
|
*/
|
2007-06-21 15:29:05 +00:00
|
|
|
function wfQueriesMustScale() {
|
|
|
|
|
global $wgMiserMode;
|
2007-07-06 04:25:06 +00:00
|
|
|
return $wgMiserMode
|
|
|
|
|
|| ( SiteStats::pages() > 100000
|
2007-07-06 04:23:33 +00:00
|
|
|
&& SiteStats::edits() > 1000000
|
2007-07-06 04:25:06 +00:00
|
|
|
&& SiteStats::users() > 10000 );
|
2007-06-21 15:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
2007-07-06 03:41:04 +00:00
|
|
|
/**
|
|
|
|
|
* Get the path to a specified script file, respecting file
|
|
|
|
|
* extensions; this is a wrapper around $wgScriptExtension etc.
|
2012-08-21 19:38:00 +00:00
|
|
|
* except for 'index' and 'load' which use $wgScript/$wgLoadScript
|
2007-07-06 03:41:04 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $script String: script filename, sans extension
|
|
|
|
|
* @return String
|
2007-07-06 03:41:04 +00:00
|
|
|
*/
|
|
|
|
|
function wfScript( $script = 'index' ) {
|
2012-08-21 19:38:00 +00:00
|
|
|
global $wgScriptPath, $wgScriptExtension, $wgScript, $wgLoadScript;
|
|
|
|
|
if ( $script === 'index' ) {
|
|
|
|
|
return $wgScript;
|
|
|
|
|
} else if ( $script === 'load' ) {
|
|
|
|
|
return $wgLoadScript;
|
|
|
|
|
} else {
|
|
|
|
|
return "{$wgScriptPath}/{$script}{$wgScriptExtension}";
|
|
|
|
|
}
|
2007-07-14 22:06:01 +00:00
|
|
|
}
|
2010-08-14 13:31:10 +00:00
|
|
|
|
2009-07-15 22:41:56 +00:00
|
|
|
/**
|
2010-08-14 13:31:10 +00:00
|
|
|
* Get the script URL.
|
2009-07-15 22:41:56 +00:00
|
|
|
*
|
2012-02-09 19:30:01 +00:00
|
|
|
* @return string script URL
|
2009-07-15 22:41:56 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfGetScriptUrl() {
|
2009-07-15 22:41:56 +00:00
|
|
|
if( isset( $_SERVER['SCRIPT_NAME'] ) ) {
|
|
|
|
|
#
|
|
|
|
|
# as it was called, minus the query string.
|
|
|
|
|
#
|
|
|
|
|
# Some sites use Apache rewrite rules to handle subdomains,
|
|
|
|
|
# and have PHP set up in a weird way that causes PHP_SELF
|
|
|
|
|
# to contain the rewritten URL instead of the one that the
|
|
|
|
|
# outside world sees.
|
|
|
|
|
#
|
|
|
|
|
# If in this mode, use SCRIPT_URL instead, which mod_rewrite
|
|
|
|
|
# provides containing the "before" URL.
|
|
|
|
|
return $_SERVER['SCRIPT_NAME'];
|
|
|
|
|
} else {
|
|
|
|
|
return $_SERVER['URL'];
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-07-14 22:06:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience function converts boolean values into "true"
|
|
|
|
|
* or "false" (string) values
|
|
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $value Boolean
|
|
|
|
|
* @return String
|
2007-07-14 22:06:01 +00:00
|
|
|
*/
|
|
|
|
|
function wfBoolToStr( $value ) {
|
|
|
|
|
return $value ? 'true' : 'false';
|
2007-07-22 14:45:12 +00:00
|
|
|
}
|
Basic integrated audio/video support, with Ogg implementation.
* JavaScript video player based loosely on Greg Maxwell's player
* Image page text snippet customisation
* Abstraction of transform parameters in the parser. Introduced Linker::makeImageLink2().
* Made canRender(), mustRender() depend on file, not just on handler. Moved width=0, height=0 checking to ImageHandler::canRender(), since audio streams have width=height=0 but should be rendered.
Also:
* Automatic upgrade for oldimage rows on image page view, allows media handler selection based on oi_*_mime
* oi_*_mime unconditionally referenced, REQUIRES SCHEMA UPGRADE
* Don't destroy file info for missing files on upgrade
* Simple, centralised extension message file handling
* Made MessageCache::loadAllMessages non-static, optimised for repeated-call case due to abuse in User.php
* Support for lightweight parser output hooks, with callback whitelist for security
* Moved Linker::formatSize() to Language, to join the new formatTimePeriod() and formatBitrate()
* Introduced MagicWordArray, regex capture trick requires that magic word IDs DO NOT CONTAIN HYPHENS.
2007-08-15 10:50:09 +00:00
|
|
|
|
2007-08-15 21:44:58 +00:00
|
|
|
/**
|
2011-04-23 13:55:27 +00:00
|
|
|
* Get a platform-independent path to the null file, e.g. /dev/null
|
2007-08-15 21:44:58 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfGetNull() {
|
|
|
|
|
return wfIsWindows()
|
|
|
|
|
? 'NUL'
|
|
|
|
|
: '/dev/null';
|
2007-11-19 15:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-18 13:18:06 +00:00
|
|
|
/**
|
2011-04-20 00:12:06 +00:00
|
|
|
* Modern version of wfWaitForSlaves(). Instead of looking at replication lag
|
|
|
|
|
* and waiting for it to go down, this waits for the slaves to catch up to the
|
|
|
|
|
* master position. Use this when updating very large numbers of rows, as
|
2008-03-18 13:18:06 +00:00
|
|
|
* in maintenance scripts, to avoid causing too much lag. Of course, this is
|
|
|
|
|
* a no-op if there are no slaves.
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
2011-04-20 00:12:06 +00:00
|
|
|
* @param $maxLag Integer (deprecated)
|
2009-11-09 11:38:52 +00:00
|
|
|
* @param $wiki mixed Wiki identifier accepted by wfGetLB
|
2008-03-18 13:18:06 +00:00
|
|
|
*/
|
2011-04-20 00:12:06 +00:00
|
|
|
function wfWaitForSlaves( $maxLag = false, $wiki = false ) {
|
2011-04-20 15:32:10 +00:00
|
|
|
$lb = wfGetLB( $wiki );
|
2011-04-19 14:52:11 +00:00
|
|
|
// bug 27975 - Don't try to wait for slaves if there are none
|
|
|
|
|
// Prevents permission error when getting master position
|
|
|
|
|
if ( $lb->getServerCount() > 1 ) {
|
|
|
|
|
$dbw = $lb->getConnection( DB_MASTER );
|
|
|
|
|
$pos = $dbw->getMasterPos();
|
|
|
|
|
$lb->waitForAll( $pos );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-05 08:56:35 +00:00
|
|
|
/**
|
2010-12-17 15:31:01 +00:00
|
|
|
* Used to be used for outputting text in the installer/updater
|
2011-07-18 23:01:08 +00:00
|
|
|
* @deprecated since 1.18, warnings in 1.18, remove in 1.20
|
2009-02-05 08:56:35 +00:00
|
|
|
*/
|
|
|
|
|
function wfOut( $s ) {
|
2011-12-13 05:19:05 +00:00
|
|
|
wfDeprecated( __FUNCTION__, '1.18' );
|
2009-02-05 08:56:35 +00:00
|
|
|
global $wgCommandLineMode;
|
2011-06-28 19:46:23 +00:00
|
|
|
if ( $wgCommandLineMode ) {
|
2009-02-05 08:56:35 +00:00
|
|
|
echo $s;
|
|
|
|
|
} else {
|
|
|
|
|
echo htmlspecialchars( $s );
|
|
|
|
|
}
|
|
|
|
|
flush();
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-09 17:04:16 +00:00
|
|
|
/**
|
2010-08-01 12:31:50 +00:00
|
|
|
* Count down from $n to zero on the terminal, with a one-second pause
|
2009-06-09 17:04:16 +00:00
|
|
|
* between showing each number. For use in command-line scripts.
|
2011-02-06 22:14:32 +00:00
|
|
|
* @codeCoverageIgnore
|
2011-10-15 22:58:42 +00:00
|
|
|
* @param $n int
|
2009-06-09 17:04:16 +00:00
|
|
|
*/
|
|
|
|
|
function wfCountDown( $n ) {
|
|
|
|
|
for ( $i = $n; $i >= 0; $i-- ) {
|
|
|
|
|
if ( $i != $n ) {
|
|
|
|
|
echo str_repeat( "\x08", strlen( $i + 1 ) );
|
2010-08-01 12:31:50 +00:00
|
|
|
}
|
2009-06-09 17:04:16 +00:00
|
|
|
echo $i;
|
|
|
|
|
flush();
|
|
|
|
|
if ( $i ) {
|
|
|
|
|
sleep( 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
echo "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
/**
|
|
|
|
|
* Generate a random 32-character hexadecimal token.
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $salt Mixed: some sort of salt, if necessary, to add to random
|
|
|
|
|
* characters before hashing.
|
2011-05-19 00:03:31 +00:00
|
|
|
* @return string
|
2011-02-06 22:14:32 +00:00
|
|
|
* @codeCoverageIgnore
|
2012-03-20 05:17:40 +00:00
|
|
|
* @deprecated since 1.20; Please use MWCryptRand for security purposes and wfRandomString for pesudo-random strings
|
|
|
|
|
* @warning This method is NOT secure. Additionally it has many callers that use it for pesudo-random purposes.
|
2008-04-11 00:43:13 +00:00
|
|
|
*/
|
2008-05-14 19:12:00 +00:00
|
|
|
function wfGenerateToken( $salt = '' ) {
|
2012-03-20 05:17:40 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.20' );
|
2010-09-11 20:27:40 +00:00
|
|
|
$salt = serialize( $salt );
|
|
|
|
|
return md5( mt_rand( 0, 0x7fffffff ) . $salt );
|
2008-07-04 06:47:47 +00:00
|
|
|
}
|
2008-07-10 08:16:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Replace all invalid characters with -
|
2012-08-07 19:09:26 +00:00
|
|
|
* Additional characters can be defined in $wgIllegalFileChars (see bug 20489)
|
|
|
|
|
* By default, $wgIllegalFileChars = ':'
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-23 17:14:07 +00:00
|
|
|
* @param $name Mixed: filename to process
|
2011-04-23 13:55:27 +00:00
|
|
|
* @return String
|
2008-07-10 08:16:58 +00:00
|
|
|
*/
|
|
|
|
|
function wfStripIllegalFilenameChars( $name ) {
|
2009-09-04 03:44:14 +00:00
|
|
|
global $wgIllegalFileChars;
|
2012-08-07 19:09:26 +00:00
|
|
|
$illegalFileChars = $wgIllegalFileChars ? "|[" . $wgIllegalFileChars . "]" : '';
|
2008-07-10 08:16:58 +00:00
|
|
|
$name = wfBaseName( $name );
|
2010-08-14 13:31:10 +00:00
|
|
|
$name = preg_replace(
|
2012-08-07 19:09:26 +00:00
|
|
|
"/[^" . Title::legalChars() . "]" . $illegalFileChars . "/",
|
2010-08-14 13:31:10 +00:00
|
|
|
'-',
|
|
|
|
|
$name
|
|
|
|
|
);
|
2008-07-10 08:16:58 +00:00
|
|
|
return $name;
|
|
|
|
|
}
|
2009-04-24 01:31:17 +00:00
|
|
|
|
2009-08-05 01:33:18 +00:00
|
|
|
/**
|
|
|
|
|
* Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit;
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-08-05 01:33:18 +00:00
|
|
|
* @return Integer value memory was set to.
|
|
|
|
|
*/
|
2010-08-01 12:31:50 +00:00
|
|
|
function wfMemoryLimit() {
|
2009-08-05 01:33:18 +00:00
|
|
|
global $wgMemoryLimit;
|
2010-08-14 13:31:10 +00:00
|
|
|
$memlimit = wfShorthandToInteger( ini_get( 'memory_limit' ) );
|
2009-08-05 01:33:18 +00:00
|
|
|
if( $memlimit != -1 ) {
|
2010-11-19 06:49:15 +00:00
|
|
|
$conflimit = wfShorthandToInteger( $wgMemoryLimit );
|
2009-08-05 01:33:18 +00:00
|
|
|
if( $conflimit == -1 ) {
|
|
|
|
|
wfDebug( "Removing PHP's memory limit\n" );
|
|
|
|
|
wfSuppressWarnings();
|
2010-08-14 13:31:10 +00:00
|
|
|
ini_set( 'memory_limit', $conflimit );
|
2009-08-05 01:33:18 +00:00
|
|
|
wfRestoreWarnings();
|
|
|
|
|
return $conflimit;
|
2009-08-05 02:34:41 +00:00
|
|
|
} elseif ( $conflimit > $memlimit ) {
|
|
|
|
|
wfDebug( "Raising PHP's memory limit to $conflimit bytes\n" );
|
2009-08-05 01:33:18 +00:00
|
|
|
wfSuppressWarnings();
|
2010-08-14 13:31:10 +00:00
|
|
|
ini_set( 'memory_limit', $conflimit );
|
2009-08-05 01:33:18 +00:00
|
|
|
wfRestoreWarnings();
|
2009-08-05 02:34:41 +00:00
|
|
|
return $conflimit;
|
2009-08-05 01:33:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $memlimit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts shorthand byte notation to integer form
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-08-05 01:33:18 +00:00
|
|
|
* @param $string String
|
|
|
|
|
* @return Integer
|
|
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfShorthandToInteger( $string = '' ) {
|
|
|
|
|
$string = trim( $string );
|
2010-11-19 06:49:15 +00:00
|
|
|
if( $string === '' ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2010-11-19 06:49:15 +00:00
|
|
|
$last = $string[strlen( $string ) - 1];
|
2010-08-14 13:31:10 +00:00
|
|
|
$val = intval( $string );
|
|
|
|
|
switch( $last ) {
|
2009-08-04 11:18:05 +00:00
|
|
|
case 'g':
|
2010-11-19 06:49:15 +00:00
|
|
|
case 'G':
|
2009-08-04 11:18:05 +00:00
|
|
|
$val *= 1024;
|
2010-11-19 06:49:15 +00:00
|
|
|
// break intentionally missing
|
2009-08-04 11:18:05 +00:00
|
|
|
case 'm':
|
2010-11-19 06:49:15 +00:00
|
|
|
case 'M':
|
2009-08-04 11:18:05 +00:00
|
|
|
$val *= 1024;
|
2010-11-19 06:49:15 +00:00
|
|
|
// break intentionally missing
|
2009-08-04 11:18:05 +00:00
|
|
|
case 'k':
|
2010-11-19 06:49:15 +00:00
|
|
|
case 'K':
|
2009-08-04 11:18:05 +00:00
|
|
|
$val *= 1024;
|
2009-08-04 15:08:56 +00:00
|
|
|
}
|
2009-08-05 01:33:18 +00:00
|
|
|
|
2009-08-04 11:18:05 +00:00
|
|
|
return $val;
|
2009-08-04 02:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-14 13:31:10 +00:00
|
|
|
/**
|
|
|
|
|
* Get the normalised IETF language tag
|
2011-02-06 14:47:35 +00:00
|
|
|
* See unit test for examples.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2009-07-25 07:23:03 +00:00
|
|
|
* @param $code String: The language code.
|
2011-10-15 22:58:42 +00:00
|
|
|
* @return String: The language code which complying with BCP 47 standards.
|
2009-07-25 07:23:03 +00:00
|
|
|
*/
|
2009-07-24 17:41:01 +00:00
|
|
|
function wfBCP47( $code ) {
|
|
|
|
|
$codeSegment = explode( '-', $code );
|
2011-04-23 13:55:27 +00:00
|
|
|
$codeBCP = array();
|
2009-07-24 17:41:01 +00:00
|
|
|
foreach ( $codeSegment as $segNo => $seg ) {
|
2009-07-24 18:35:09 +00:00
|
|
|
if ( count( $codeSegment ) > 0 ) {
|
2011-06-30 02:59:43 +00:00
|
|
|
// when previous segment is x, it is a private segment and should be lc
|
2011-09-18 23:21:46 +00:00
|
|
|
if( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
|
2011-02-06 14:47:35 +00:00
|
|
|
$codeBCP[$segNo] = strtolower( $seg );
|
2009-07-24 18:35:09 +00:00
|
|
|
// ISO 3166 country code
|
2011-02-06 14:47:35 +00:00
|
|
|
} elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
|
2009-07-25 07:23:03 +00:00
|
|
|
$codeBCP[$segNo] = strtoupper( $seg );
|
2009-07-24 18:35:09 +00:00
|
|
|
// ISO 15924 script code
|
2010-08-14 13:31:10 +00:00
|
|
|
} elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
|
2011-02-06 14:47:35 +00:00
|
|
|
$codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
|
2009-07-25 07:23:03 +00:00
|
|
|
// Use lowercase for other cases
|
2010-08-14 13:31:10 +00:00
|
|
|
} else {
|
2009-07-25 07:23:03 +00:00
|
|
|
$codeBCP[$segNo] = strtolower( $seg );
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2009-07-24 18:35:09 +00:00
|
|
|
} else {
|
2009-07-25 07:23:03 +00:00
|
|
|
// Use lowercase for single segment
|
|
|
|
|
$codeBCP[$segNo] = strtolower( $seg );
|
2009-07-24 18:35:09 +00:00
|
|
|
}
|
2009-07-24 17:41:01 +00:00
|
|
|
}
|
2010-08-14 13:31:10 +00:00
|
|
|
$langCode = implode( '-', $codeBCP );
|
2009-07-24 17:41:01 +00:00
|
|
|
return $langCode;
|
2009-09-16 00:53:37 +00:00
|
|
|
}
|
2010-05-07 12:25:01 +00:00
|
|
|
|
* Rewrote ObjectCache.php to conform to the modern coding style, and to be less convoluted about how CACHE_ANYTHING and CACHE_ACCEL are resolved. Moved most functionality to static members of a new ObjectCache class.
* Moved the global functions to GlobalFunctions.php, where they are now just convenience wrappers. Made them return non-references. Updated callers (none found in extensions).
* Added an advanced configuration method, $wgObjectCaches, which allows a lot more detail in the object cache configuration than $wgMainCacheType.
* Made all object cache classes derive from BagOStuff.
* Split the MWMemcached class into a generic client class and a MediaWiki-specific wrapper class. The wrapper class presents a simple BagOStuff interface to calling code, hiding memcached client internals, and will simplify the task of supporting the PECL extension.
* Added some extra constructor parameters to MWMemcached, configurable via $wgObjectCaches.
* Removed the *_multi() methods from BagOStuff, my grepping indicates that they are not used.
* Rewrote FakeMemCachedClient as a BagOStuff subclass, called EmptyBagOStuff.
* Added an optional "server" parameter to SQLBagOStuff. This allows the server holding the objectcache table to be different from the server holding the core DB.
* Added MultiWriteBagOStuff: a cache class which writes to multiple locations, and reads from them in a defined fallback sequence. This can be used to extend the cache space by adding disk-backed storage to existing in-memory caches.
* Made MWMemcached::get() return false on failure instead of null, to match the BagOStuff documentation and the other BagOStuff subclasses. Anything that was relying on it returning null would have already been broken with SqlBagOStuff.
* Fixed a bug in the memcached client causing keys with spaces or line breaks in them to break the memcached protocol, injecting arbitrary commands or parameters. Since the PECL client apparently also has this flaw, I implemented the fix in the wrapper class.
* Renamed BagOStuff::set_debug() to setDebug(), since we aren't emulating the memcached client anymore
* Fixed spelling error in MWMemcached: persistant -> persistent
2011-03-03 09:37:37 +00:00
|
|
|
/**
|
|
|
|
|
* Get a cache object.
|
|
|
|
|
*
|
2011-06-16 23:40:13 +00:00
|
|
|
* @param $inputType integer Cache type, one the the CACHE_* constants.
|
* Rewrote ObjectCache.php to conform to the modern coding style, and to be less convoluted about how CACHE_ANYTHING and CACHE_ACCEL are resolved. Moved most functionality to static members of a new ObjectCache class.
* Moved the global functions to GlobalFunctions.php, where they are now just convenience wrappers. Made them return non-references. Updated callers (none found in extensions).
* Added an advanced configuration method, $wgObjectCaches, which allows a lot more detail in the object cache configuration than $wgMainCacheType.
* Made all object cache classes derive from BagOStuff.
* Split the MWMemcached class into a generic client class and a MediaWiki-specific wrapper class. The wrapper class presents a simple BagOStuff interface to calling code, hiding memcached client internals, and will simplify the task of supporting the PECL extension.
* Added some extra constructor parameters to MWMemcached, configurable via $wgObjectCaches.
* Removed the *_multi() methods from BagOStuff, my grepping indicates that they are not used.
* Rewrote FakeMemCachedClient as a BagOStuff subclass, called EmptyBagOStuff.
* Added an optional "server" parameter to SQLBagOStuff. This allows the server holding the objectcache table to be different from the server holding the core DB.
* Added MultiWriteBagOStuff: a cache class which writes to multiple locations, and reads from them in a defined fallback sequence. This can be used to extend the cache space by adding disk-backed storage to existing in-memory caches.
* Made MWMemcached::get() return false on failure instead of null, to match the BagOStuff documentation and the other BagOStuff subclasses. Anything that was relying on it returning null would have already been broken with SqlBagOStuff.
* Fixed a bug in the memcached client causing keys with spaces or line breaks in them to break the memcached protocol, injecting arbitrary commands or parameters. Since the PECL client apparently also has this flaw, I implemented the fix in the wrapper class.
* Renamed BagOStuff::set_debug() to setDebug(), since we aren't emulating the memcached client anymore
* Fixed spelling error in MWMemcached: persistant -> persistent
2011-03-03 09:37:37 +00:00
|
|
|
* @return BagOStuff
|
|
|
|
|
*/
|
|
|
|
|
function wfGetCache( $inputType ) {
|
|
|
|
|
return ObjectCache::getInstance( $inputType );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 13:55:27 +00:00
|
|
|
/**
|
|
|
|
|
* Get the main cache object
|
|
|
|
|
*
|
|
|
|
|
* @return BagOStuff
|
|
|
|
|
*/
|
* Rewrote ObjectCache.php to conform to the modern coding style, and to be less convoluted about how CACHE_ANYTHING and CACHE_ACCEL are resolved. Moved most functionality to static members of a new ObjectCache class.
* Moved the global functions to GlobalFunctions.php, where they are now just convenience wrappers. Made them return non-references. Updated callers (none found in extensions).
* Added an advanced configuration method, $wgObjectCaches, which allows a lot more detail in the object cache configuration than $wgMainCacheType.
* Made all object cache classes derive from BagOStuff.
* Split the MWMemcached class into a generic client class and a MediaWiki-specific wrapper class. The wrapper class presents a simple BagOStuff interface to calling code, hiding memcached client internals, and will simplify the task of supporting the PECL extension.
* Added some extra constructor parameters to MWMemcached, configurable via $wgObjectCaches.
* Removed the *_multi() methods from BagOStuff, my grepping indicates that they are not used.
* Rewrote FakeMemCachedClient as a BagOStuff subclass, called EmptyBagOStuff.
* Added an optional "server" parameter to SQLBagOStuff. This allows the server holding the objectcache table to be different from the server holding the core DB.
* Added MultiWriteBagOStuff: a cache class which writes to multiple locations, and reads from them in a defined fallback sequence. This can be used to extend the cache space by adding disk-backed storage to existing in-memory caches.
* Made MWMemcached::get() return false on failure instead of null, to match the BagOStuff documentation and the other BagOStuff subclasses. Anything that was relying on it returning null would have already been broken with SqlBagOStuff.
* Fixed a bug in the memcached client causing keys with spaces or line breaks in them to break the memcached protocol, injecting arbitrary commands or parameters. Since the PECL client apparently also has this flaw, I implemented the fix in the wrapper class.
* Renamed BagOStuff::set_debug() to setDebug(), since we aren't emulating the memcached client anymore
* Fixed spelling error in MWMemcached: persistant -> persistent
2011-03-03 09:37:37 +00:00
|
|
|
function wfGetMainCache() {
|
|
|
|
|
global $wgMainCacheType;
|
|
|
|
|
return ObjectCache::getInstance( $wgMainCacheType );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 13:55:27 +00:00
|
|
|
/**
|
|
|
|
|
* Get the cache object used by the message cache
|
|
|
|
|
*
|
|
|
|
|
* @return BagOStuff
|
|
|
|
|
*/
|
* Rewrote ObjectCache.php to conform to the modern coding style, and to be less convoluted about how CACHE_ANYTHING and CACHE_ACCEL are resolved. Moved most functionality to static members of a new ObjectCache class.
* Moved the global functions to GlobalFunctions.php, where they are now just convenience wrappers. Made them return non-references. Updated callers (none found in extensions).
* Added an advanced configuration method, $wgObjectCaches, which allows a lot more detail in the object cache configuration than $wgMainCacheType.
* Made all object cache classes derive from BagOStuff.
* Split the MWMemcached class into a generic client class and a MediaWiki-specific wrapper class. The wrapper class presents a simple BagOStuff interface to calling code, hiding memcached client internals, and will simplify the task of supporting the PECL extension.
* Added some extra constructor parameters to MWMemcached, configurable via $wgObjectCaches.
* Removed the *_multi() methods from BagOStuff, my grepping indicates that they are not used.
* Rewrote FakeMemCachedClient as a BagOStuff subclass, called EmptyBagOStuff.
* Added an optional "server" parameter to SQLBagOStuff. This allows the server holding the objectcache table to be different from the server holding the core DB.
* Added MultiWriteBagOStuff: a cache class which writes to multiple locations, and reads from them in a defined fallback sequence. This can be used to extend the cache space by adding disk-backed storage to existing in-memory caches.
* Made MWMemcached::get() return false on failure instead of null, to match the BagOStuff documentation and the other BagOStuff subclasses. Anything that was relying on it returning null would have already been broken with SqlBagOStuff.
* Fixed a bug in the memcached client causing keys with spaces or line breaks in them to break the memcached protocol, injecting arbitrary commands or parameters. Since the PECL client apparently also has this flaw, I implemented the fix in the wrapper class.
* Renamed BagOStuff::set_debug() to setDebug(), since we aren't emulating the memcached client anymore
* Fixed spelling error in MWMemcached: persistant -> persistent
2011-03-03 09:37:37 +00:00
|
|
|
function wfGetMessageCacheStorage() {
|
|
|
|
|
global $wgMessageCacheType;
|
|
|
|
|
return ObjectCache::getInstance( $wgMessageCacheType );
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-23 13:55:27 +00:00
|
|
|
/**
|
|
|
|
|
* Get the cache object used by the parser cache
|
|
|
|
|
*
|
|
|
|
|
* @return BagOStuff
|
|
|
|
|
*/
|
* Rewrote ObjectCache.php to conform to the modern coding style, and to be less convoluted about how CACHE_ANYTHING and CACHE_ACCEL are resolved. Moved most functionality to static members of a new ObjectCache class.
* Moved the global functions to GlobalFunctions.php, where they are now just convenience wrappers. Made them return non-references. Updated callers (none found in extensions).
* Added an advanced configuration method, $wgObjectCaches, which allows a lot more detail in the object cache configuration than $wgMainCacheType.
* Made all object cache classes derive from BagOStuff.
* Split the MWMemcached class into a generic client class and a MediaWiki-specific wrapper class. The wrapper class presents a simple BagOStuff interface to calling code, hiding memcached client internals, and will simplify the task of supporting the PECL extension.
* Added some extra constructor parameters to MWMemcached, configurable via $wgObjectCaches.
* Removed the *_multi() methods from BagOStuff, my grepping indicates that they are not used.
* Rewrote FakeMemCachedClient as a BagOStuff subclass, called EmptyBagOStuff.
* Added an optional "server" parameter to SQLBagOStuff. This allows the server holding the objectcache table to be different from the server holding the core DB.
* Added MultiWriteBagOStuff: a cache class which writes to multiple locations, and reads from them in a defined fallback sequence. This can be used to extend the cache space by adding disk-backed storage to existing in-memory caches.
* Made MWMemcached::get() return false on failure instead of null, to match the BagOStuff documentation and the other BagOStuff subclasses. Anything that was relying on it returning null would have already been broken with SqlBagOStuff.
* Fixed a bug in the memcached client causing keys with spaces or line breaks in them to break the memcached protocol, injecting arbitrary commands or parameters. Since the PECL client apparently also has this flaw, I implemented the fix in the wrapper class.
* Renamed BagOStuff::set_debug() to setDebug(), since we aren't emulating the memcached client anymore
* Fixed spelling error in MWMemcached: persistant -> persistent
2011-03-03 09:37:37 +00:00
|
|
|
function wfGetParserCacheStorage() {
|
|
|
|
|
global $wgParserCacheType;
|
|
|
|
|
return ObjectCache::getInstance( $wgParserCacheType );
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-06 19:45:49 +00:00
|
|
|
/**
|
|
|
|
|
* Get the cache object used by the language converter
|
|
|
|
|
*
|
|
|
|
|
* @return BagOStuff
|
|
|
|
|
*/
|
|
|
|
|
function wfGetLangConverterCacheStorage() {
|
|
|
|
|
global $wgLanguageConverterCacheType;
|
|
|
|
|
return ObjectCache::getInstance( $wgLanguageConverterCacheType );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 16:11:40 +00:00
|
|
|
/**
|
|
|
|
|
* Call hook functions defined in $wgHooks
|
|
|
|
|
*
|
|
|
|
|
* @param $event String: event name
|
|
|
|
|
* @param $args Array: parameters passed to hook functions
|
2012-01-06 06:16:05 +00:00
|
|
|
* @return Boolean True if no handler aborted the hook
|
2011-05-28 16:11:40 +00:00
|
|
|
*/
|
|
|
|
|
function wfRunHooks( $event, $args = array() ) {
|
|
|
|
|
return Hooks::run( $event, $args );
|
|
|
|
|
}
|
2011-10-24 02:19:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper around php's unpack.
|
|
|
|
|
*
|
|
|
|
|
* @param $format String: The format string (See php's docs)
|
|
|
|
|
* @param $data: A binary string of binary data
|
|
|
|
|
* @param $length integer or false: The minimun length of $data. This is to
|
|
|
|
|
* prevent reading beyond the end of $data. false to disable the check.
|
|
|
|
|
*
|
|
|
|
|
* Also be careful when using this function to read unsigned 32 bit integer
|
|
|
|
|
* because php might make it negative.
|
|
|
|
|
*
|
|
|
|
|
* @throws MWException if $data not long enough, or if unpack fails
|
2012-02-09 18:01:54 +00:00
|
|
|
* @return array Associative array of the extracted data
|
2011-10-24 02:19:11 +00:00
|
|
|
*/
|
|
|
|
|
function wfUnpack( $format, $data, $length=false ) {
|
|
|
|
|
if ( $length !== false ) {
|
|
|
|
|
$realLen = strlen( $data );
|
|
|
|
|
if ( $realLen < $length ) {
|
|
|
|
|
throw new MWException( "Tried to use wfUnpack on a "
|
|
|
|
|
. "string of length $realLen, but needed one "
|
|
|
|
|
. "of at least length $length."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$result = unpack( $format, $data );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
|
|
|
|
|
if ( $result === false ) {
|
|
|
|
|
// If it cannot extract the packed data.
|
|
|
|
|
throw new MWException( "unpack could not unpack binary data" );
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2012-04-06 23:42:39 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if an image exists on the 'bad image list'.
|
|
|
|
|
*
|
|
|
|
|
* The format of MediaWiki:Bad_image_list is as follows:
|
|
|
|
|
* * Only list items (lines starting with "*") are considered
|
|
|
|
|
* * The first link on a line must be a link to a bad image
|
|
|
|
|
* * Any subsequent links on the same line are considered to be exceptions,
|
|
|
|
|
* i.e. articles where the image may occur inline.
|
|
|
|
|
*
|
|
|
|
|
* @param $name string the image name to check
|
|
|
|
|
* @param $contextTitle Title|bool the page on which the image occurs, if known
|
|
|
|
|
* @param $blacklist string wikitext of a file blacklist
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
|
|
|
|
|
static $badImageCache = null; // based on bad_image_list msg
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
# Handle redirects
|
|
|
|
|
$redirectTitle = RepoGroup::singleton()->checkRedirect( Title::makeTitle( NS_FILE, $name ) );
|
|
|
|
|
if( $redirectTitle ) {
|
|
|
|
|
$name = $redirectTitle->getDbKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Run the extension hook
|
|
|
|
|
$bad = false;
|
|
|
|
|
if( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $bad;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cacheable = ( $blacklist === null );
|
|
|
|
|
if( $cacheable && $badImageCache !== null ) {
|
|
|
|
|
$badImages = $badImageCache;
|
|
|
|
|
} else { // cache miss
|
|
|
|
|
if ( $blacklist === null ) {
|
2012-07-24 01:04:15 +00:00
|
|
|
$blacklist = wfMessage( 'bad_image_list' )->inContentLanguage()->plain(); // site list
|
2012-04-06 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
# Build the list now
|
|
|
|
|
$badImages = array();
|
|
|
|
|
$lines = explode( "\n", $blacklist );
|
|
|
|
|
foreach( $lines as $line ) {
|
|
|
|
|
# List items only
|
|
|
|
|
if ( substr( $line, 0, 1 ) !== '*' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Find all links
|
|
|
|
|
$m = array();
|
|
|
|
|
if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$exceptions = array();
|
|
|
|
|
$imageDBkey = false;
|
|
|
|
|
foreach ( $m[1] as $i => $titleText ) {
|
|
|
|
|
$title = Title::newFromText( $titleText );
|
|
|
|
|
if ( !is_null( $title ) ) {
|
|
|
|
|
if ( $i == 0 ) {
|
|
|
|
|
$imageDBkey = $title->getDBkey();
|
|
|
|
|
} else {
|
|
|
|
|
$exceptions[$title->getPrefixedDBkey()] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $imageDBkey !== false ) {
|
|
|
|
|
$badImages[$imageDBkey] = $exceptions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $cacheable ) {
|
|
|
|
|
$badImageCache = $badImages;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
|
|
|
|
|
$bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $bad;
|
|
|
|
|
}
|