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" );
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-20 08:23:18 +00:00
|
|
|
use Liuggio\StatsdClient\Sender\SocketSender;
|
2015-03-23 00:53:24 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2016-02-01 20:44:03 +00:00
|
|
|
use MediaWiki\Session\SessionManager;
|
2017-04-17 15:06:53 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-09-29 08:16:05 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2017-02-24 16:17:16 +00:00
|
|
|
use Wikimedia\Rdbms\DBReplicationWaitError;
|
2015-02-20 08:23:18 +00:00
|
|
|
|
2008-08-06 03:55:49 +00:00
|
|
|
// Hide compatibility functions from Doxygen
|
|
|
|
|
/// @cond
|
2008-07-07 20:15:16 +00:00
|
|
|
/**
|
|
|
|
|
* Compatibility functions
|
|
|
|
|
*
|
2016-02-11 20:54:09 +00:00
|
|
|
* We support PHP 5.5.9 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
|
|
|
|
2014-04-13 17:11:18 +00:00
|
|
|
// hash_equals function only exists in PHP >= 5.6.0
|
2016-10-13 05:34:26 +00:00
|
|
|
// https://secure.php.net/hash_equals
|
2014-04-13 17:11:18 +00:00
|
|
|
if ( !function_exists( 'hash_equals' ) ) {
|
|
|
|
|
/**
|
2014-06-24 05:33:46 +00:00
|
|
|
* Check whether a user-provided string is equal to a fixed-length secret string
|
|
|
|
|
* without revealing bytes of the secret string through timing differences.
|
2014-04-13 17:11:18 +00:00
|
|
|
*
|
2014-06-24 05:33:46 +00:00
|
|
|
* The usual way to compare strings (PHP's === operator or the underlying memcmp()
|
|
|
|
|
* function in C) is to compare corresponding bytes and stop at the first difference,
|
|
|
|
|
* which would take longer for a partial match than for a complete mismatch. This
|
|
|
|
|
* is not secure when one of the strings (e.g. an HMAC or token) must remain secret
|
|
|
|
|
* and the other may come from an attacker. Statistical analysis of timing measurements
|
|
|
|
|
* over many requests may allow the attacker to guess the string's bytes one at a time
|
|
|
|
|
* (and check his guesses) even if the timing differences are extremely small.
|
|
|
|
|
*
|
|
|
|
|
* When making such a security-sensitive comparison, it is essential that the sequence
|
|
|
|
|
* in which instructions are executed and memory locations are accessed not depend on
|
|
|
|
|
* the secret string's value. HOWEVER, for simplicity, we do not attempt to minimize
|
|
|
|
|
* the inevitable leakage of the string's length. That is generally known anyway as
|
|
|
|
|
* a chararacteristic of the hash function used to compute the secret value.
|
2014-04-13 17:11:18 +00:00
|
|
|
*
|
|
|
|
|
* Longer explanation: http://www.emerose.com/timing-attacks-explained
|
|
|
|
|
*
|
|
|
|
|
* @codeCoverageIgnore
|
2014-06-24 05:33:46 +00:00
|
|
|
* @param string $known_string Fixed-length secret string to compare against
|
2014-04-13 17:11:18 +00:00
|
|
|
* @param string $user_string User-provided string
|
|
|
|
|
* @return bool True if the strings are the same, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
function hash_equals( $known_string, $user_string ) {
|
|
|
|
|
// Strict type checking as in PHP's native implementation
|
|
|
|
|
if ( !is_string( $known_string ) ) {
|
|
|
|
|
trigger_error( 'hash_equals(): Expected known_string to be a string, ' .
|
|
|
|
|
gettype( $known_string ) . ' given', E_USER_WARNING );
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_string( $user_string ) ) {
|
|
|
|
|
trigger_error( 'hash_equals(): Expected user_string to be a string, ' .
|
|
|
|
|
gettype( $user_string ) . ' given', E_USER_WARNING );
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$known_string_len = strlen( $known_string );
|
2014-06-24 05:33:46 +00:00
|
|
|
if ( $known_string_len !== strlen( $user_string ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = 0;
|
|
|
|
|
for ( $i = 0; $i < $known_string_len; $i++ ) {
|
|
|
|
|
$result |= ord( $known_string[$i] ) ^ ord( $user_string[$i] );
|
2014-04-13 17:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ( $result === 0 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-08-06 03:55:49 +00:00
|
|
|
/// @endcond
|
|
|
|
|
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
/**
|
|
|
|
|
* Load an extension
|
|
|
|
|
*
|
2015-03-30 21:35:45 +00:00
|
|
|
* This queues an extension to be loaded through
|
|
|
|
|
* the ExtensionRegistry system.
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
*
|
2015-05-06 12:48:07 +00:00
|
|
|
* @param string $ext Name of the extension to load
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
* @param string|null $path Absolute path of where to find the extension.json file
|
2015-09-16 13:55:13 +00:00
|
|
|
* @since 1.25
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
*/
|
2015-05-06 12:48:07 +00:00
|
|
|
function wfLoadExtension( $ext, $path = null ) {
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
if ( !$path ) {
|
2015-05-06 12:48:07 +00:00
|
|
|
global $wgExtensionDirectory;
|
|
|
|
|
$path = "$wgExtensionDirectory/$ext/extension.json";
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
}
|
2015-03-30 21:35:45 +00:00
|
|
|
ExtensionRegistry::getInstance()->queue( $path );
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load multiple extensions at once
|
|
|
|
|
*
|
|
|
|
|
* Same as wfLoadExtension, but more efficient if you
|
|
|
|
|
* are loading multiple extensions.
|
|
|
|
|
*
|
|
|
|
|
* If you want to specify custom paths, you should interact with
|
|
|
|
|
* ExtensionRegistry directly.
|
|
|
|
|
*
|
|
|
|
|
* @see wfLoadExtension
|
|
|
|
|
* @param string[] $exts Array of extension names to load
|
2015-09-16 13:55:13 +00:00
|
|
|
* @since 1.25
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
*/
|
|
|
|
|
function wfLoadExtensions( array $exts ) {
|
2015-05-06 12:48:07 +00:00
|
|
|
global $wgExtensionDirectory;
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
$registry = ExtensionRegistry::getInstance();
|
|
|
|
|
foreach ( $exts as $ext ) {
|
2015-05-06 12:48:07 +00:00
|
|
|
$registry->queue( "$wgExtensionDirectory/$ext/extension.json" );
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load a skin
|
|
|
|
|
*
|
|
|
|
|
* @see wfLoadExtension
|
2015-05-06 12:48:07 +00:00
|
|
|
* @param string $skin Name of the extension to load
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
* @param string|null $path Absolute path of where to find the skin.json file
|
2015-09-16 13:55:13 +00:00
|
|
|
* @since 1.25
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
*/
|
2015-05-06 12:48:07 +00:00
|
|
|
function wfLoadSkin( $skin, $path = null ) {
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
if ( !$path ) {
|
2015-05-06 12:48:07 +00:00
|
|
|
global $wgStyleDirectory;
|
|
|
|
|
$path = "$wgStyleDirectory/$skin/skin.json";
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
}
|
2015-03-30 21:35:45 +00:00
|
|
|
ExtensionRegistry::getInstance()->queue( $path );
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load multiple skins at once
|
|
|
|
|
*
|
|
|
|
|
* @see wfLoadExtensions
|
|
|
|
|
* @param string[] $skins Array of extension names to load
|
2015-09-16 13:55:13 +00:00
|
|
|
* @since 1.25
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
*/
|
|
|
|
|
function wfLoadSkins( array $skins ) {
|
2015-05-06 12:48:07 +00:00
|
|
|
global $wgStyleDirectory;
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
$registry = ExtensionRegistry::getInstance();
|
|
|
|
|
foreach ( $skins as $skin ) {
|
2015-05-06 12:48:07 +00:00
|
|
|
$registry->queue( "$wgStyleDirectory/$skin/skin.json" );
|
Implement extension registration from an extension.json file
Introduces wfLoadExtension()/wfLoadSkin() which should be used in
LocalSettings.php rather than require-ing a PHP entry point.
Extensions and skins would add "extension.json" or "skin.json" files
in their root, which contains all the information typically
present in PHP entry point files (classes to autoload, special pages,
API modules, etc.) A full schema can be found at
docs/extension.schema.json, and a script to validate these to the
schema is provided. An additional script is provided to convert
typical PHP entry point files into their JSON equivalents.
The basic flow of loading an extension goes like:
* Get the ExtensionRegistry singleton instance
* ExtensionRegistry takes a filename, reads the file or tries
to get the parsed JSON from APC if possible.
* The JSON is run through a Processor instance,
which registers things with the appropriate
global settings.
* The output of the processor is cached in APC if possible.
* The extension/skin is marked as loaded in the
ExtensionRegistry and a callback function is executed
if one was specified.
For ideal performance, a batch loading method is also provided:
* The absolute path name to the JSON file is queued
in the ExtensionRegistry instance.
* When loadFromQueue() is called, it constructs a hash
unique to the members of the current queue, and sees
if the queue has been cached in APC. If not, it processes
each file individually, and combines the result of each
Processor into one giant array, which is cached in APC.
* The giant array then sets various global settings,
defines constants, and calls callbacks.
To invalidate the cached processed info, by default the mtime
of each JSON file is checked. However that can be slow if you
have a large number of extensions, so you can set $wgExtensionInfoMTime
to the mtime of one file, and `touch` it whenever you update
your extensions.
Change-Id: I7074b65d07c5c7d4e3f1fb0755d74a0b07ed4596
2014-10-15 00:31:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-21 09:32:59 +00:00
|
|
|
/**
|
|
|
|
|
* Like array_diff( $a, $b ) except that it works with two-dimensional arrays.
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param array $a
|
|
|
|
|
* @param array $b
|
2011-11-28 15:19:20 +00:00
|
|
|
* @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
|
|
|
/**
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param array|string $a
|
|
|
|
|
* @param array|string $b
|
2011-05-29 14:24:27 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2008-02-21 09:32:59 +00:00
|
|
|
function wfArrayDiff2_cmp( $a, $b ) {
|
2013-09-27 19:10:33 +00:00
|
|
|
if ( is_string( $a ) && is_string( $b ) ) {
|
2008-02-21 09:32:59 +00:00
|
|
|
return strcmp( $a, $b );
|
|
|
|
|
} elseif ( count( $a ) !== count( $b ) ) {
|
|
|
|
|
return count( $a ) < count( $b ) ? -1 : 1;
|
|
|
|
|
} else {
|
|
|
|
|
reset( $a );
|
|
|
|
|
reset( $b );
|
2013-04-17 14:52:47 +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
|
|
|
|
2017-02-10 05:31:32 +00:00
|
|
|
/**
|
|
|
|
|
* Like array_filter with ARRAY_FILTER_USE_BOTH, but works pre-5.6.
|
|
|
|
|
*
|
|
|
|
|
* @param array $arr
|
|
|
|
|
* @param callable $callback Will be called with the array value and key (in that order) and
|
|
|
|
|
* should return a bool which will determine whether the array element is kept.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
function wfArrayFilter( array $arr, callable $callback ) {
|
|
|
|
|
if ( defined( 'ARRAY_FILTER_USE_BOTH' ) ) {
|
|
|
|
|
return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH );
|
|
|
|
|
}
|
|
|
|
|
$filteredKeys = array_filter( array_keys( $arr ), function ( $key ) use ( $arr, $callback ) {
|
|
|
|
|
return call_user_func( $callback, $arr[$key], $key );
|
|
|
|
|
} );
|
|
|
|
|
return array_intersect_key( $arr, array_fill_keys( $filteredKeys, true ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Like array_filter with ARRAY_FILTER_USE_KEY, but works pre-5.6.
|
|
|
|
|
*
|
|
|
|
|
* @param array $arr
|
|
|
|
|
* @param callable $callback Will be called with the array key and should return a bool which
|
|
|
|
|
* will determine whether the array element is kept.
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
function wfArrayFilterByKey( array $arr, callable $callback ) {
|
|
|
|
|
return wfArrayFilter( $arr, function ( $val, $key ) use ( $callback ) {
|
|
|
|
|
return call_user_func( $callback, $key );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-20 10:55:34 +00:00
|
|
|
/**
|
|
|
|
|
* Appends to second array if $value differs from that in $default
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string|int $key
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @param mixed $default
|
|
|
|
|
* @param array $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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merge arrays in the style of getUserPermissionsErrors, with duplicate removal
|
|
|
|
|
* e.g.
|
2017-02-25 21:53:36 +00:00
|
|
|
* wfMergeErrorArrays(
|
|
|
|
|
* [ [ 'x' ] ],
|
|
|
|
|
* [ [ 'x', '2' ] ],
|
|
|
|
|
* [ [ 'x' ] ],
|
|
|
|
|
* [ [ 'y' ] ]
|
|
|
|
|
* );
|
2011-05-20 10:55:34 +00:00
|
|
|
* returns:
|
2017-02-25 21:53:36 +00:00
|
|
|
* [
|
|
|
|
|
* [ 'x', '2' ],
|
|
|
|
|
* [ 'x' ],
|
|
|
|
|
* [ 'y' ]
|
|
|
|
|
* ]
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param array $array1,...
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return array
|
2011-05-20 10:55:34 +00:00
|
|
|
*/
|
|
|
|
|
function wfMergeErrorArrays( /*...*/ ) {
|
|
|
|
|
$args = func_get_args();
|
2016-02-17 09:09:32 +00:00
|
|
|
$out = [];
|
2011-05-20 10:55:34 +00:00
|
|
|
foreach ( $args as $errors ) {
|
|
|
|
|
foreach ( $errors as $params ) {
|
2015-10-19 12:30:40 +00:00
|
|
|
$originalParams = $params;
|
|
|
|
|
if ( $params[0] instanceof MessageSpecifier ) {
|
|
|
|
|
$msg = $params[0];
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = array_merge( [ $msg->getKey() ], $msg->getParams() );
|
2015-10-19 12:30:40 +00:00
|
|
|
}
|
2011-05-20 10:55:34 +00:00
|
|
|
# @todo FIXME: Sometimes get nested arrays for $params,
|
|
|
|
|
# which leads to E_NOTICEs
|
|
|
|
|
$spec = implode( "\t", $params );
|
2015-10-19 12:30:40 +00:00
|
|
|
$out[$spec] = $originalParams;
|
2011-05-20 10:55:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array_values( $out );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert array into another array after the specified *KEY*
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $array The array.
|
|
|
|
|
* @param array $insert The array to insert.
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param mixed $after The key to insert after
|
|
|
|
|
* @return array
|
2011-05-20 10:55:34 +00:00
|
|
|
*/
|
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
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param object|array $objOrArray
|
|
|
|
|
* @param bool $recursive
|
|
|
|
|
* @return array
|
2011-05-20 10:55:34 +00:00
|
|
|
*/
|
|
|
|
|
function wfObjectToArray( $objOrArray, $recursive = true ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$array = [];
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( is_object( $objOrArray ) ) {
|
2011-05-20 10:55:34 +00:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
2015-12-03 13:32:04 +00:00
|
|
|
* @note This is designed for use in relation to Special:RandomPage
|
|
|
|
|
* and the page_random database field.
|
|
|
|
|
*
|
2004-10-11 17:34:39 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2007-04-19 10:54:48 +00:00
|
|
|
function wfRandom() {
|
2015-12-03 13:32:04 +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;
|
2013-10-04 13:43:09 +00:00
|
|
|
$rand = number_format( ( mt_rand() * $max + mt_rand() ) / $max / $max, 12, '.', '' );
|
2004-10-11 17:34:39 +00:00
|
|
|
return $rand;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
/**
|
2015-12-03 13:32:04 +00:00
|
|
|
* Get a random string containing a number of pseudo-random hex characters.
|
|
|
|
|
*
|
2012-03-20 05:17:40 +00:00
|
|
|
* @note This is not secure, if you are trying to generate some sort
|
|
|
|
|
* of token please use MWCryptRand instead.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param int $length The length of the string to generate
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return string
|
2012-03-20 05:17:40 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
|
|
|
|
function wfRandomString( $length = 32 ) {
|
|
|
|
|
$str = '';
|
2013-04-29 04:33:06 +00:00
|
|
|
for ( $n = 0; $n < $length; $n += 7 ) {
|
|
|
|
|
$str .= sprintf( '%07x', mt_rand() & 0xfffffff );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
|
*
|
|
|
|
|
* ;:@&=$-_.+!*'(),
|
|
|
|
|
*
|
2015-07-09 05:19:35 +00:00
|
|
|
* RFC 1738 says ~ is unsafe, however RFC 3986 considers it an unreserved
|
|
|
|
|
* character which should not be encoded. More importantly, google chrome
|
|
|
|
|
* always converts %7E back to ~, and converting it in this function can
|
2015-09-10 15:23:07 +00:00
|
|
|
* cause a redirect loop (T105265).
|
2015-07-09 05:19:35 +00:00
|
|
|
*
|
2008-08-08 15:45:52 +00:00
|
|
|
* But + is not safe because it's used to indicate a space; &= are only safe in
|
2015-09-10 15:23:07 +00:00
|
|
|
* 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 /
|
2008-08-08 15:45:52 +00:00
|
|
|
* is reserved, we don't care. So the list we unescape is:
|
|
|
|
|
*
|
2015-09-10 15:23:07 +00:00
|
|
|
* ;:@$!*(),/~
|
2008-08-08 15:45:52 +00:00
|
|
|
*
|
2016-09-11 21:41:26 +00:00
|
|
|
* However, IIS7 redirects fail when the url contains a colon (see T24709),
|
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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $s
|
2004-09-03 17:13:55 +00:00
|
|
|
* @return string
|
2013-03-07 16:27:38 +00:00
|
|
|
*/
|
2008-08-08 15:45:52 +00:00
|
|
|
function wfUrlencode( $s ) {
|
2010-03-03 22:01:09 +00:00
|
|
|
static $needle;
|
2013-10-04 13:43:09 +00:00
|
|
|
|
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 ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$needle = [ '%3B', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%2F', '%7E' ];
|
2013-10-04 13:43:09 +00:00
|
|
|
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,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ ';', '@', '$', '!', '*', '(', ')', ',', '/', '~', ':' ],
|
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
|
|
|
/**
|
2015-12-05 19:07:03 +00:00
|
|
|
* This function takes one or two arrays as input, and returns a CGI-style string, e.g.
|
2011-06-16 19:09:03 +00:00
|
|
|
* "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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $array1 ( String|Array )
|
2015-12-05 19:07:03 +00:00
|
|
|
* @param array|null $array2 ( String|Array )
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $prefix
|
|
|
|
|
* @return string
|
2011-06-16 19:09:03 +00:00
|
|
|
*/
|
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 ) {
|
2013-02-03 20:05:24 +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
|
2013-03-13 07:42:41 +00:00
|
|
|
* its argument and returns the same string in array form. This allows compatibility
|
|
|
|
|
* 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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $query Query string
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string[] Array version of input
|
2011-06-16 19:09:03 +00:00
|
|
|
*/
|
|
|
|
|
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 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
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 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$temp = [ $k => $temp ];
|
2011-06-23 15:25:07 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $url
|
|
|
|
|
* @param string|string[] $query String or associative array
|
2011-06-16 19:09:03 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfAppendQuery( $url, $query ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
if ( is_array( $query ) ) {
|
|
|
|
|
$query = wfArrayToCgi( $query );
|
|
|
|
|
}
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $query != '' ) {
|
2016-05-03 02:17:48 +00:00
|
|
|
// Remove the fragment, if there is one
|
|
|
|
|
$fragment = false;
|
|
|
|
|
$hashPos = strpos( $url, '#' );
|
|
|
|
|
if ( $hashPos !== false ) {
|
|
|
|
|
$fragment = substr( $url, $hashPos );
|
|
|
|
|
$url = substr( $url, 0, $hashPos );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add parameter
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( false === strpos( $url, '?' ) ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
$url .= '?';
|
|
|
|
|
} else {
|
|
|
|
|
$url .= '&';
|
|
|
|
|
}
|
|
|
|
|
$url .= $query;
|
2016-05-03 02:17:48 +00:00
|
|
|
|
|
|
|
|
// Put the fragment back
|
|
|
|
|
if ( $fragment !== false ) {
|
|
|
|
|
$url .= $fragment;
|
|
|
|
|
}
|
2012-08-28 00:51:38 +00:00
|
|
|
}
|
|
|
|
|
return $url;
|
2011-06-16 19:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-04 13:43:09 +00:00
|
|
|
* Expand a potentially local URL to a fully-qualified URL. Assumes $wgServer
|
2011-06-16 19:09:03 +00:00
|
|
|
* 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)
|
2013-10-04 13:43:09 +00:00
|
|
|
* PROTO_CURRENT: Output a URL starting with either http:// or https:// , depending
|
|
|
|
|
* on which protocol was used for the current incoming request
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $url Either fully-qualified or a local path + query
|
|
|
|
|
* @param string $defaultProto One of the PROTO_* constants. Determines the
|
2013-10-04 13:43:09 +00:00
|
|
|
* protocol to use if $url or $wgServer is protocol-relative
|
2016-12-08 05:04:53 +00:00
|
|
|
* @return string|false Fully-qualified URL, current-path-relative URL or false if
|
2013-10-04 13:43:09 +00:00
|
|
|
* 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 ) {
|
2014-05-11 12:24:31 +00:00
|
|
|
global $wgServer, $wgCanonicalServer, $wgInternalServer, $wgRequest,
|
|
|
|
|
$wgHttpsPort;
|
2011-09-07 14:15:03 +00:00
|
|
|
if ( $defaultProto === PROTO_CANONICAL ) {
|
|
|
|
|
$serverUrl = $wgCanonicalServer;
|
2013-11-16 19:55:08 +00:00
|
|
|
} elseif ( $defaultProto === PROTO_INTERNAL && $wgInternalServer !== false ) {
|
|
|
|
|
// Make $wgInternalServer fall back to $wgServer if not set
|
2011-09-07 14:15:03 +00:00
|
|
|
$serverUrl = $wgInternalServer;
|
2013-11-16 19:55:08 +00:00
|
|
|
} else {
|
|
|
|
|
$serverUrl = $wgServer;
|
|
|
|
|
if ( $defaultProto === PROTO_CURRENT ) {
|
|
|
|
|
$defaultProto = $wgRequest->getProtocol() . '://';
|
|
|
|
|
}
|
2011-09-07 14:15:03 +00:00
|
|
|
}
|
2013-11-16 19:55:08 +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 {
|
2013-10-04 13:43:09 +00:00
|
|
|
// $wgCanonicalServer or $wgInternalServer doesn't have a protocol.
|
|
|
|
|
// This really isn't supposed to happen. Fall back to HTTP in this
|
|
|
|
|
// ridiculous case.
|
2011-08-19 15:25:50 +00:00
|
|
|
$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 ) == '/' ) {
|
2013-10-04 13:43:09 +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 );
|
2014-05-11 12:24:31 +00:00
|
|
|
|
|
|
|
|
// ensure proper port for HTTPS arrives in URL
|
2015-09-12 13:54:13 +00:00
|
|
|
// https://phabricator.wikimedia.org/T67184
|
2014-05-11 12:24:31 +00:00
|
|
|
if ( $defaultProto === PROTO_HTTPS && $wgHttpsPort != 443 ) {
|
|
|
|
|
$bits['port'] = $wgHttpsPort;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 18:53:20 +00:00
|
|
|
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]
|
|
|
|
|
*
|
2016-09-11 21:41:26 +00:00
|
|
|
* @todo Need to integrate this into wfExpandUrl (see T34168)
|
2011-11-15 17:38:20 +00:00
|
|
|
*
|
2012-04-19 08:58:54 +00:00
|
|
|
* @since 1.19
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $urlParts URL parts, as output from wfParseUrl
|
2011-11-15 17:38:20 +00:00
|
|
|
* @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.
|
|
|
|
|
*
|
2016-09-11 21:41:26 +00:00
|
|
|
* @todo Need to integrate this into wfExpandUrl (see T34168)
|
2011-11-09 22:44:12 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $urlPath URL path, potentially containing dot-segments
|
2011-11-09 22:44:12 +00:00
|
|
|
* @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
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param bool $includeProtocolRelative If false, remove '//' from the returned protocol list.
|
2011-11-03 03:30:27 +00:00
|
|
|
* DO NOT USE this directly, use wfUrlProtocolsWithoutProtRel() instead
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return string
|
2011-06-16 19:09:03 +00:00
|
|
|
*/
|
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 ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$protocols = [];
|
2011-06-16 19:09:03 +00:00
|
|
|
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
|
2014-04-23 09:41:35 +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:
|
|
|
|
|
*
|
2013-10-04 13:43:09 +00:00
|
|
|
* 1) Does not raise warnings on bad URLs (just returns false).
|
|
|
|
|
* 2) Handles protocols that don't use :// (e.g., mailto: and news:, as well as
|
|
|
|
|
* protocol-relative URLs) correctly.
|
2017-06-18 10:30:47 +00:00
|
|
|
* 3) Adds a "delimiter" element to the array (see (2)).
|
|
|
|
|
* 4) Verifies that the protocol is on the $wgUrlProtocols whitelist.
|
|
|
|
|
* 5) Rejects some invalid URLs that parse_url doesn't, e.g. the empty string or URLs starting with
|
|
|
|
|
* a line feed character.
|
2011-06-16 19:09:03 +00:00
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $url A URL to parse
|
2017-06-18 10:30:47 +00:00
|
|
|
* @return string[]|bool Bits of the URL in an associative array, or false on failure.
|
|
|
|
|
* Possible fields:
|
|
|
|
|
* - scheme: URI scheme (protocol), e.g. 'http', 'mailto'. Lowercase, always present, but can
|
|
|
|
|
* be an empty string for protocol-relative URLs.
|
|
|
|
|
* - delimiter: either '://', ':' or '//'. Always present.
|
|
|
|
|
* - host: domain name / IP. Always present, but could be an empty string, e.g. for file: URLs.
|
|
|
|
|
* - user: user name, e.g. for HTTP Basic auth URLs such as http://user:pass@example.com/
|
|
|
|
|
* Missing when there is no username.
|
|
|
|
|
* - pass: password, same as above.
|
|
|
|
|
* - path: path including the leading /. Will be missing when empty (e.g. 'http://example.com')
|
|
|
|
|
* - query: query string (as a string; see wfCgiToArray() for parsing it), can be missing.
|
|
|
|
|
* - fragment: the part after #, can be missing.
|
2011-06-16 19:09:03 +00:00
|
|
|
*/
|
|
|
|
|
function wfParseUrl( $url ) {
|
2012-08-28 00:51:38 +00:00
|
|
|
global $wgUrlProtocols; // Allow all protocols defined in DefaultSettings/LocalSettings.php
|
|
|
|
|
|
2013-10-04 13:43:09 +00:00
|
|
|
// 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.
|
2012-08-28 00:51:38 +00:00
|
|
|
$wasRelative = substr( $url, 0, 2 ) == '//';
|
|
|
|
|
if ( $wasRelative ) {
|
|
|
|
|
$url = "http:$url";
|
|
|
|
|
}
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2012-08-28 00:51:38 +00:00
|
|
|
$bits = parse_url( $url );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2012-08-28 00:51:38 +00:00
|
|
|
// parse_url() returns an array without scheme for some invalid URLs, e.g.
|
2016-07-25 01:25:09 +00:00
|
|
|
// parse_url("%0Ahttp://example.com") == [ 'host' => '%0Ahttp', 'path' => 'example.com' ]
|
2012-08-28 00:51:38 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 21:41:26 +00:00
|
|
|
/* Provide an empty host for eg. file:/// urls (see T30627) */
|
2012-08-28 00:51:38 +00:00
|
|
|
if ( !isset( $bits['host'] ) ) {
|
|
|
|
|
$bits['host'] = '';
|
|
|
|
|
|
2016-09-11 21:41:26 +00:00
|
|
|
// See T47069
|
2013-02-18 12:17:23 +00:00
|
|
|
if ( isset( $bits['path'] ) ) {
|
|
|
|
|
/* parse_url loses the third / for file:///c:/ urls (but not on variants) */
|
|
|
|
|
if ( substr( $bits['path'], 0, 1 ) !== '/' ) {
|
|
|
|
|
$bits['path'] = '/' . $bits['path'];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$bits['path'] = '';
|
2012-08-28 00:51:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $url
|
2012-03-29 05:55:27 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfExpandIRI( $url ) {
|
2013-10-04 13:43:09 +00:00
|
|
|
return preg_replace_callback(
|
|
|
|
|
'/((?:%[89A-F][0-9A-F])+)/i',
|
|
|
|
|
'wfExpandIRI_callback',
|
|
|
|
|
wfExpandUrl( $url )
|
|
|
|
|
);
|
2012-03-29 05:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $url
|
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 == '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ "http:$index", "https:$index" ];
|
2011-11-14 09:13:58 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $index ];
|
2011-11-14 09:13:58 +00:00
|
|
|
}
|
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
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $url URL
|
|
|
|
|
* @param array $domains Array of domains (strings)
|
2011-08-20 10:18:09 +00:00
|
|
|
* @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'] ) ) {
|
2013-06-10 02:04:24 +00:00
|
|
|
$host = '.' . $bits['host'];
|
2011-08-20 10:18:09 +00:00
|
|
|
foreach ( (array)$domains as $domain ) {
|
2013-06-10 02:04:24 +00:00
|
|
|
$domain = '.' . $domain;
|
|
|
|
|
if ( substr( $host, -strlen( $domain ) ) === $domain ) {
|
2011-08-20 10:18:09 +00:00
|
|
|
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
|
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.
|
|
|
|
|
*
|
2014-06-23 22:25:55 +00:00
|
|
|
* @since 1.25 support for additional context data
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $text
|
2015-12-30 18:31:56 +00:00
|
|
|
* @param string|bool $dest Destination of the message:
|
|
|
|
|
* - 'all': both to the log and HTML (debug toolbar or HTML comments)
|
|
|
|
|
* - 'private': excluded from HTML output
|
|
|
|
|
* For backward compatibility, it can also take a boolean:
|
|
|
|
|
* - true: same as 'all'
|
|
|
|
|
* - false: same as 'private'
|
2014-06-23 22:25:55 +00:00
|
|
|
* @param array $context Additional logging context data
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfDebug( $text, $dest = 'all', array $context = [] ) {
|
2014-03-21 05:01:24 +00:00
|
|
|
global $wgDebugRawPage, $wgDebugLogPrefix;
|
2014-06-23 22:25:55 +00:00
|
|
|
global $wgDebugTimestamps, $wgRequestTime;
|
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
|
|
|
|
2014-06-23 22:25:55 +00:00
|
|
|
$text = trim( $text );
|
|
|
|
|
|
|
|
|
|
if ( $wgDebugTimestamps ) {
|
|
|
|
|
$context['seconds_elapsed'] = sprintf(
|
|
|
|
|
'%6.4f',
|
|
|
|
|
microtime( true ) - $wgRequestTime
|
|
|
|
|
);
|
|
|
|
|
$context['memory_used'] = sprintf(
|
|
|
|
|
'%5.1fM',
|
|
|
|
|
( memory_get_usage( true ) / ( 1024 * 1024 ) )
|
|
|
|
|
);
|
2012-03-12 19:01:44 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-21 05:01:24 +00:00
|
|
|
if ( $wgDebugLogPrefix !== '' ) {
|
2014-06-23 22:25:55 +00:00
|
|
|
$context['prefix'] = $wgDebugLogPrefix;
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
2015-12-30 18:31:56 +00:00
|
|
|
$context['private'] = ( $dest === false || $dest === 'private' );
|
2014-03-21 05:01:24 +00:00
|
|
|
|
2015-03-23 00:53:24 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'wfDebug' );
|
2014-06-23 22:25:55 +00:00
|
|
|
$logger->debug( $text, $context );
|
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'
|
2013-12-01 20:39:00 +00:00
|
|
|
)
|
|
|
|
|
) {
|
2011-05-31 05:55:06 +00:00
|
|
|
$cache = true;
|
|
|
|
|
} else {
|
|
|
|
|
$cache = false;
|
|
|
|
|
}
|
|
|
|
|
return $cache;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-28 16:22:10 +00:00
|
|
|
/**
|
|
|
|
|
* Send a line giving PHP memory usage.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2014-05-22 14:22:12 +00:00
|
|
|
* @param bool $exact Print exact byte values instead of kibibytes (default: false)
|
2008-08-28 16:22:10 +00:00
|
|
|
*/
|
|
|
|
|
function wfDebugMem( $exact = false ) {
|
|
|
|
|
$mem = memory_get_usage();
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !$exact ) {
|
2014-05-22 14:22:12 +00:00
|
|
|
$mem = floor( $mem / 1024 ) . ' KiB';
|
2008-08-28 16:22:10 +00:00
|
|
|
} else {
|
2014-05-22 14:22:12 +00:00
|
|
|
$mem .= ' B';
|
2008-08-28 16:22:10 +00:00
|
|
|
}
|
|
|
|
|
wfDebug( "Memory usage: $mem\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-17 20:07:33 +00:00
|
|
|
/**
|
2014-12-17 00:12:23 +00:00
|
|
|
* Send a line to a supplementary debug log file, if configured, or main debug
|
|
|
|
|
* log if not.
|
|
|
|
|
*
|
|
|
|
|
* To configure a supplementary log file, set $wgDebugLogGroups[$logGroup] to
|
|
|
|
|
* a string filename or an associative array mapping 'destination' to the
|
|
|
|
|
* desired filename. The associative array may also contain a 'sample' key
|
|
|
|
|
* with an integer value, specifying a sampling factor. Sampled log events
|
|
|
|
|
* will be emitted with a 1 in N random chance.
|
2013-11-05 19:42:49 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.23 support for sampling log messages via $wgDebugLogGroups.
|
2014-06-23 22:25:55 +00:00
|
|
|
* @since 1.25 support for additional context data
|
2014-12-17 00:12:23 +00:00
|
|
|
* @since 1.25 sample behavior dependent on configured $wgMWLoggerDefaultSpi
|
2005-09-19 12:52:32 +00:00
|
|
|
*
|
2014-03-23 09:17:48 +00:00
|
|
|
* @param string $logGroup
|
|
|
|
|
* @param string $text
|
2013-11-14 11:17:25 +00:00
|
|
|
* @param string|bool $dest Destination of the message:
|
|
|
|
|
* - 'all': both to the log and HTML (debug toolbar or HTML comments)
|
2014-12-12 08:41:27 +00:00
|
|
|
* - 'private': only to the specific log if set in $wgDebugLogGroups and
|
2013-11-14 11:17:25 +00:00
|
|
|
* discarded otherwise
|
|
|
|
|
* For backward compatibility, it can also take a boolean:
|
|
|
|
|
* - true: same as 'all'
|
|
|
|
|
* - false: same as 'private'
|
2014-06-23 22:25:55 +00:00
|
|
|
* @param array $context Additional logging context data
|
2005-08-17 20:07:33 +00:00
|
|
|
*/
|
2014-06-23 22:25:55 +00:00
|
|
|
function wfDebugLog(
|
2016-02-17 09:09:32 +00:00
|
|
|
$logGroup, $text, $dest = 'all', array $context = []
|
2014-06-23 22:25:55 +00:00
|
|
|
) {
|
2014-03-21 05:01:24 +00:00
|
|
|
$text = trim( $text );
|
2013-11-05 19:42:49 +00:00
|
|
|
|
2015-03-23 00:53:24 +00:00
|
|
|
$logger = LoggerFactory::getInstance( $logGroup );
|
2015-12-30 18:31:56 +00:00
|
|
|
$context['private'] = ( $dest === false || $dest === 'private' );
|
2014-12-17 00:12:23 +00:00
|
|
|
$logger->info( $text, $context );
|
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
|
|
|
*
|
2014-06-23 22:25:55 +00:00
|
|
|
* @since 1.25 support for additional context data
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $text Database error message.
|
2014-06-23 22:25:55 +00:00
|
|
|
* @param array $context Additional logging context data
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfLogDBError( $text, array $context = [] ) {
|
2015-03-23 00:53:24 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'wfLogDBError' );
|
2014-06-23 22:25:55 +00:00
|
|
|
$logger->error( trim( $text ), $context );
|
2004-06-29 07:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-03 09:54:40 +00:00
|
|
|
/**
|
|
|
|
|
* Throws a warning that $function is deprecated
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $function
|
2013-10-04 13:43:09 +00:00
|
|
|
* @param string|bool $version Version of MediaWiki that the function
|
|
|
|
|
* was deprecated in (Added in 1.19).
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string|bool $component Added in 1.19.
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $callerOffset How far up the call stack is the original
|
2012-02-13 20:29:41 +00:00
|
|
|
* 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
|
2013-03-18 09:32:14 +00:00
|
|
|
* $wgDevelopmentWarnings. To log warnings in production, use wfLogWarning() instead.
|
2012-02-03 09:54:40 +00:00
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string $msg Message to send
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $callerOffset Number of items to go back in the backtrace to
|
2012-02-03 09:54:40 +00:00
|
|
|
* find the correct caller (1 = function calling wfWarn, ...)
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $level PHP error level; defaults to E_USER_NOTICE;
|
2013-03-18 09:32:14 +00:00
|
|
|
* only used when $wgDevelopmentWarnings is true
|
2012-02-03 09:54:40 +00:00
|
|
|
*/
|
|
|
|
|
function wfWarn( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
|
2013-03-18 09:32:14 +00:00
|
|
|
MWDebug::warning( $msg, $callerOffset + 1, $level, 'auto' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Send a warning as a PHP error and the debug log. This is intended for logging
|
|
|
|
|
* warnings in production. For logging development warnings, use WfWarn instead.
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $msg Message to send
|
|
|
|
|
* @param int $callerOffset Number of items to go back in the backtrace to
|
2013-03-18 09:32:14 +00:00
|
|
|
* find the correct caller (1 = function calling wfLogWarning, ...)
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $level PHP error level; defaults to E_USER_WARNING
|
2013-03-18 09:32:14 +00:00
|
|
|
*/
|
|
|
|
|
function wfLogWarning( $msg, $callerOffset = 1, $level = E_USER_WARNING ) {
|
|
|
|
|
MWDebug::warning( $msg, $callerOffset + 1, $level, 'production' );
|
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.
|
2014-06-23 22:25:55 +00:00
|
|
|
* @since 1.25 support for additional context data
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $text
|
|
|
|
|
* @param string $file Filename
|
2014-06-23 22:25:55 +00:00
|
|
|
* @param array $context Additional logging context data
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2015-11-23 22:53:38 +00:00
|
|
|
* @deprecated since 1.25 Use \MediaWiki\Logger\LegacyLogger::emit or UDPTransport
|
2007-03-05 12:11:55 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfErrorLog( $text, $file, array $context = [] ) {
|
2014-12-15 23:12:57 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.25' );
|
2015-03-23 00:53:24 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'wfErrorLog' );
|
2014-06-23 22:25:55 +00:00
|
|
|
$context['destination'] = $file;
|
|
|
|
|
$logger->info( trim( $text ), $context );
|
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() {
|
2015-04-01 23:30:16 +00:00
|
|
|
global $wgDebugLogGroups, $wgDebugRawPage;
|
2011-04-21 16:31:02 +00:00
|
|
|
|
2015-02-20 08:23:18 +00:00
|
|
|
$context = RequestContext::getMain();
|
2015-04-01 23:16:09 +00:00
|
|
|
$request = $context->getRequest();
|
2015-04-01 23:30:16 +00:00
|
|
|
|
|
|
|
|
$profiler = Profiler::instance();
|
|
|
|
|
$profiler->setContext( $context );
|
|
|
|
|
$profiler->logData();
|
|
|
|
|
|
2015-02-20 08:23:18 +00:00
|
|
|
$config = $context->getConfig();
|
2017-05-26 00:23:44 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
|
if ( $config->get( 'StatsdServer' ) && $stats->hasData() ) {
|
2015-07-23 20:02:22 +00:00
|
|
|
try {
|
|
|
|
|
$statsdServer = explode( ':', $config->get( 'StatsdServer' ) );
|
|
|
|
|
$statsdHost = $statsdServer[0];
|
|
|
|
|
$statsdPort = isset( $statsdServer[1] ) ? $statsdServer[1] : 8125;
|
|
|
|
|
$statsdSender = new SocketSender( $statsdHost, $statsdPort );
|
2015-07-23 20:13:27 +00:00
|
|
|
$statsdClient = new SamplingStatsdClient( $statsdSender, true, false );
|
2016-04-12 22:08:20 +00:00
|
|
|
$statsdClient->setSamplingRates( $config->get( 'StatsdSamplingRates' ) );
|
2017-05-26 00:23:44 +00:00
|
|
|
$statsdClient->send( $stats->getData() );
|
2015-07-23 20:02:22 +00:00
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
|
MWExceptionHandler::logException( $ex );
|
|
|
|
|
}
|
2015-02-20 08:23:18 +00:00
|
|
|
}
|
2013-03-28 20:18:30 +00:00
|
|
|
|
2009-02-24 09:50:22 +00:00
|
|
|
# Profiling must actually be enabled...
|
2014-12-04 03:48:27 +00:00
|
|
|
if ( $profiler instanceof ProfilerStub ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2011-04-21 16:31:02 +00:00
|
|
|
|
2014-03-27 09:46:40 +00:00
|
|
|
if ( isset( $wgDebugLogGroups['profileoutput'] )
|
|
|
|
|
&& $wgDebugLogGroups['profileoutput'] === false
|
|
|
|
|
) {
|
2014-03-21 06:57:25 +00:00
|
|
|
// Explicitly disabled
|
2014-03-27 09:46:40 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2011-04-21 16:31:02 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$ctx = [ 'elapsed' => $request->getElapsedTime() ];
|
2011-04-21 16:31:02 +00:00
|
|
|
if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
|
2014-03-21 06:57:25 +00:00
|
|
|
$ctx['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'] ) ) {
|
2014-03-21 06:57:25 +00:00
|
|
|
$ctx['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'] ) ) {
|
2014-03-21 06:57:25 +00:00
|
|
|
$ctx['from'] = $_SERVER['HTTP_FROM'];
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2014-03-21 06:57:25 +00:00
|
|
|
if ( isset( $ctx['forwarded_for'] ) ||
|
|
|
|
|
isset( $ctx['client_ip'] ) ||
|
|
|
|
|
isset( $ctx['from'] ) ) {
|
|
|
|
|
$ctx['proxy'] = $_SERVER['REMOTE_ADDR'];
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2014-03-21 06:57:25 +00:00
|
|
|
|
2011-04-30 14:08:12 +00:00
|
|
|
// Don't load $wgUser at this late stage just for statistics purposes
|
2014-03-21 06:57:25 +00:00
|
|
|
// @todo FIXME: We can detect some anons even if it is not loaded.
|
|
|
|
|
// See User::getId()
|
2015-04-01 23:16:09 +00:00
|
|
|
$user = $context->getUser();
|
|
|
|
|
$ctx['anon'] = $user->isItemLoaded( 'id' ) && $user->isAnon();
|
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 {
|
2015-04-01 23:16:09 +00:00
|
|
|
$ctx['url'] = urldecode( $request->getRequestURL() );
|
2015-01-09 23:44:47 +00:00
|
|
|
} catch ( Exception $ignored ) {
|
2014-03-21 06:57:25 +00:00
|
|
|
// no-op
|
2012-11-13 16:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-21 06:57:25 +00:00
|
|
|
$ctx['output'] = $profiler->getOutput();
|
2011-04-21 16:31:02 +00:00
|
|
|
|
2015-03-23 00:53:24 +00:00
|
|
|
$log = LoggerFactory::getInstance( 'profileoutput' );
|
2014-11-17 17:32:45 +00:00
|
|
|
$log->info( "Elapsed: {elapsed}; URL: <{url}>\n{output}", $ctx );
|
2003-12-11 20:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-15 16:17:02 +00:00
|
|
|
/**
|
|
|
|
|
* Increment a statistics counter
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param int $count
|
2013-01-18 18:26:54 +00:00
|
|
|
* @return void
|
2012-02-15 16:17:02 +00:00
|
|
|
*/
|
|
|
|
|
function wfIncrStats( $key, $count = 1 ) {
|
2017-04-17 15:06:53 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
2015-02-20 08:23:18 +00:00
|
|
|
$stats->updateCount( $key, $count );
|
2012-02-15 16:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2013-05-10 07:37:04 +00:00
|
|
|
* Check whether the wiki is in read-only mode.
|
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() {
|
2017-05-23 04:28:41 +00:00
|
|
|
return MediaWikiServices::getInstance()->getReadOnlyMode()
|
2017-04-10 05:34:30 +00:00
|
|
|
->isReadOnly();
|
2003-04-14 23:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-15 22:58:42 +00:00
|
|
|
/**
|
2015-10-01 23:19:05 +00:00
|
|
|
* Check if the site is in read-only mode and return the message if so
|
|
|
|
|
*
|
|
|
|
|
* This checks wfConfiguredReadOnlyReason() and the main load balancer
|
2016-09-05 19:55:19 +00:00
|
|
|
* for replica DB lag. This may result in DB connection being made.
|
2013-05-10 07:37:04 +00:00
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return string|bool String when in read-only mode; false otherwise
|
2011-10-15 22:58:42 +00:00
|
|
|
*/
|
2008-02-24 03:50:05 +00:00
|
|
|
function wfReadOnlyReason() {
|
2017-05-23 04:28:41 +00:00
|
|
|
return MediaWikiServices::getInstance()->getReadOnlyMode()
|
2017-04-10 05:34:30 +00:00
|
|
|
->getReason();
|
2015-10-01 23:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
|
|
|
|
|
*
|
|
|
|
|
* @return string|bool String when in read-only mode; false otherwise
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
function wfConfiguredReadOnlyReason() {
|
2017-05-23 04:28:41 +00:00
|
|
|
return MediaWikiServices::getInstance()->getConfiguredReadOnlyMode()
|
2017-04-10 05:34:30 +00:00
|
|
|
->getReason();
|
2008-02-24 03:50:05 +00:00
|
|
|
}
|
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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param Language|string|bool $langcode Either:
|
2008-07-26 20:41:52 +00:00
|
|
|
* - 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.
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return Language
|
2008-07-26 20:41:52 +00:00
|
|
|
*/
|
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.
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( is_object( $langcode ) ) {
|
2010-07-08 08:23:35 +00:00
|
|
|
# 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;
|
2013-04-17 14:52:47 +00:00
|
|
|
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;
|
2013-04-17 14:52:47 +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() );
|
2013-04-17 14:52:47 +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-09-02 17:12:56 +00:00
|
|
|
/**
|
2013-02-26 10:41:02 +00:00
|
|
|
* This is the function for getting translated interface messages.
|
|
|
|
|
*
|
|
|
|
|
* @see Message class for documentation how to use them.
|
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Messages_API
|
|
|
|
|
*
|
|
|
|
|
* This function replaces all old wfMsg* functions.
|
|
|
|
|
*
|
2015-03-09 16:14:20 +00:00
|
|
|
* @param string|string[]|MessageSpecifier $key Message key, or array of keys, or a MessageSpecifier
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param mixed $params,... Normal message parameters
|
2011-02-20 13:33:42 +00:00
|
|
|
* @return Message
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2010-09-02 17:12:56 +00:00
|
|
|
* @since 1.17
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
|
|
|
|
* @see Message::__construct
|
2010-09-02 17:12:56 +00:00
|
|
|
*/
|
2014-03-25 18:52:26 +00:00
|
|
|
function wfMessage( $key /*...*/ ) {
|
2016-12-20 14:05:03 +00:00
|
|
|
$message = new Message( $key );
|
|
|
|
|
|
|
|
|
|
// We call Message::params() to reduce code duplication
|
2010-09-02 17:12:56 +00:00
|
|
|
$params = func_get_args();
|
|
|
|
|
array_shift( $params );
|
2016-12-20 14:05:03 +00:00
|
|
|
if ( $params ) {
|
|
|
|
|
call_user_func_array( [ $message, 'params' ], $params );
|
2010-11-16 20:40:20 +00:00
|
|
|
}
|
2016-12-20 14:05:03 +00:00
|
|
|
|
|
|
|
|
return $message;
|
2010-09-02 17:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
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.
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string|string[] $keys,... Message keys
|
2011-04-16 23:23:28 +00:00
|
|
|
* @return Message
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2010-12-31 23:30:00 +00:00
|
|
|
* @since 1.18
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
|
|
|
|
* @see Message::newFallbackSequence
|
2010-12-31 23:30:00 +00:00
|
|
|
*/
|
|
|
|
|
function wfMessageFallback( /*...*/ ) {
|
2011-01-14 08:32:10 +00:00
|
|
|
$args = func_get_args();
|
2013-07-19 03:30:42 +00:00
|
|
|
return call_user_func_array( 'Message::newFallbackSequence', $args );
|
2010-12-31 23:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-10 19:18:00 +00:00
|
|
|
/**
|
|
|
|
|
* Replace message parameter keys on the given formatted output.
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $message
|
|
|
|
|
* @param array $args
|
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
|
2016-01-13 17:55:08 +00:00
|
|
|
if ( is_array( $args ) && $args ) {
|
2008-07-10 19:18:00 +00:00
|
|
|
if ( is_array( $args[0] ) ) {
|
|
|
|
|
$args = array_values( $args[0] );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$replacementKeys = [];
|
2013-04-17 14:52:47 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $wgOverrideHostname !== false ) {
|
2012-05-11 18:13:09 +00:00
|
|
|
# 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;
|
|
|
|
|
}
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( is_array( $uname ) && isset( $uname['nodename'] ) ) {
|
2008-09-05 03:46:07 +00:00
|
|
|
$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
|
|
|
/**
|
Convert <!-- timing data --> to a <script></script> block
This patch replaces:
<!-- Served by mw1069 in 0.976 secs. -->
With:
<script>mw.config.set({"wgBackendResponseTime":976,"wgHostname":"mw1069"});</script>
In the default HTML output of MediaWiki.
While the latter is a nearly twice as long, it is almost as readable for human
beings, while being substantially easy to get via JavaScript.
To get the values from the comment, you have to do something like:
var comments, comment, hostname, duration;
comments = $.grep( document.body.childNodes, function ( el ) {
return el.nodeType === 8
} );
comment = comments.length
? comments.pop().nodeValue.match( /(\S+) in ([\d.]+)/ ).slice( 1 )
: [ null, null ];
hostname = comment[0];
respTime = parseFloat( comment[1] );
On the other hand, to get the values from the JavaScript code, you can simply:
var hostname = mw.config.get( 'wgHostname' );
var respTime = mw.config.get( 'wgBackendResponseTime' );
I believe that the ability to parse the number easily via JavaScript will make
it easier to include with other client-side measurements as part of reports on
site performance as experienced by users.
Change-Id: I895cd03f0968815484ff8cda4b23cc602ac555f0
2014-03-15 08:05:44 +00:00
|
|
|
* Returns a script tag that stores the amount of time it took MediaWiki to
|
|
|
|
|
* handle the request in milliseconds as 'wgBackendResponseTime'.
|
|
|
|
|
*
|
|
|
|
|
* If $wgShowHostnames is true, the script will also set 'wgHostname' to the
|
|
|
|
|
* hostname of the server handling the request.
|
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
|
|
|
|
Convert <!-- timing data --> to a <script></script> block
This patch replaces:
<!-- Served by mw1069 in 0.976 secs. -->
With:
<script>mw.config.set({"wgBackendResponseTime":976,"wgHostname":"mw1069"});</script>
In the default HTML output of MediaWiki.
While the latter is a nearly twice as long, it is almost as readable for human
beings, while being substantially easy to get via JavaScript.
To get the values from the comment, you have to do something like:
var comments, comment, hostname, duration;
comments = $.grep( document.body.childNodes, function ( el ) {
return el.nodeType === 8
} );
comment = comments.length
? comments.pop().nodeValue.match( /(\S+) in ([\d.]+)/ ).slice( 1 )
: [ null, null ];
hostname = comment[0];
respTime = parseFloat( comment[1] );
On the other hand, to get the values from the JavaScript code, you can simply:
var hostname = mw.config.get( 'wgHostname' );
var respTime = mw.config.get( 'wgBackendResponseTime' );
I believe that the ability to parse the number easily via JavaScript will make
it easier to include with other client-side measurements as part of reports on
site performance as experienced by users.
Change-Id: I895cd03f0968815484ff8cda4b23cc602ac555f0
2014-03-15 08:05:44 +00:00
|
|
|
$responseTime = round( ( microtime( true ) - $wgRequestTime ) * 1000 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$reportVars = [ 'wgBackendResponseTime' => $responseTime ];
|
Convert <!-- timing data --> to a <script></script> block
This patch replaces:
<!-- Served by mw1069 in 0.976 secs. -->
With:
<script>mw.config.set({"wgBackendResponseTime":976,"wgHostname":"mw1069"});</script>
In the default HTML output of MediaWiki.
While the latter is a nearly twice as long, it is almost as readable for human
beings, while being substantially easy to get via JavaScript.
To get the values from the comment, you have to do something like:
var comments, comment, hostname, duration;
comments = $.grep( document.body.childNodes, function ( el ) {
return el.nodeType === 8
} );
comment = comments.length
? comments.pop().nodeValue.match( /(\S+) in ([\d.]+)/ ).slice( 1 )
: [ null, null ];
hostname = comment[0];
respTime = parseFloat( comment[1] );
On the other hand, to get the values from the JavaScript code, you can simply:
var hostname = mw.config.get( 'wgHostname' );
var respTime = mw.config.get( 'wgBackendResponseTime' );
I believe that the ability to parse the number easily via JavaScript will make
it easier to include with other client-side measurements as part of reports on
site performance as experienced by users.
Change-Id: I895cd03f0968815484ff8cda4b23cc602ac555f0
2014-03-15 08:05:44 +00:00
|
|
|
if ( $wgShowHostnames ) {
|
2014-07-21 12:47:42 +00:00
|
|
|
$reportVars['wgHostname'] = wfHostname();
|
Convert <!-- timing data --> to a <script></script> block
This patch replaces:
<!-- Served by mw1069 in 0.976 secs. -->
With:
<script>mw.config.set({"wgBackendResponseTime":976,"wgHostname":"mw1069"});</script>
In the default HTML output of MediaWiki.
While the latter is a nearly twice as long, it is almost as readable for human
beings, while being substantially easy to get via JavaScript.
To get the values from the comment, you have to do something like:
var comments, comment, hostname, duration;
comments = $.grep( document.body.childNodes, function ( el ) {
return el.nodeType === 8
} );
comment = comments.length
? comments.pop().nodeValue.match( /(\S+) in ([\d.]+)/ ).slice( 1 )
: [ null, null ];
hostname = comment[0];
respTime = parseFloat( comment[1] );
On the other hand, to get the values from the JavaScript code, you can simply:
var hostname = mw.config.get( 'wgHostname' );
var respTime = mw.config.get( 'wgBackendResponseTime' );
I believe that the ability to parse the number easily via JavaScript will make
it easier to include with other client-side measurements as part of reports on
site performance as experienced by users.
Change-Id: I895cd03f0968815484ff8cda4b23cc602ac555f0
2014-03-15 08:05:44 +00:00
|
|
|
}
|
|
|
|
|
return Skin::makeVariablesScript( $reportVars );
|
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().
|
|
|
|
|
*
|
2014-03-31 14:19:57 +00:00
|
|
|
* Will return an empty array if debug_backtrace is disabled, otherwise
|
|
|
|
|
* the output from debug_backtrace() (trimmed).
|
2007-01-02 23:50:56 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param int $limit This parameter can be used to limit the number of stack frames returned
|
2011-08-25 16:40:49 +00:00
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return array Array of backtrace information
|
2007-01-02 23:50:56 +00:00
|
|
|
*/
|
2011-06-18 20:15:48 +00:00
|
|
|
function wfDebugBacktrace( $limit = 0 ) {
|
2009-03-09 13:57:32 +00:00
|
|
|
static $disabled = null;
|
|
|
|
|
|
|
|
|
|
if ( is_null( $disabled ) ) {
|
2014-03-31 14:19:57 +00:00
|
|
|
$disabled = !function_exists( 'debug_backtrace' );
|
|
|
|
|
if ( $disabled ) {
|
|
|
|
|
wfDebug( "debug_backtrace() is disabled\n" );
|
2009-03-09 13:57:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $disabled ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2009-03-09 13:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-27 04:21:03 +00:00
|
|
|
if ( $limit ) {
|
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
|
|
|
|
|
*
|
2014-10-06 17:42:58 +00:00
|
|
|
* @param bool|null $raw If true, the return value is plain text. If false, HTML.
|
|
|
|
|
* Defaults to $wgCommandLineMode if unset.
|
2011-04-23 13:55:27 +00:00
|
|
|
* @return string
|
2014-10-06 17:42:58 +00:00
|
|
|
* @since 1.25 Supports $raw parameter.
|
2011-04-23 13:55:27 +00:00
|
|
|
*/
|
2014-10-06 17:42:58 +00:00
|
|
|
function wfBacktrace( $raw = null ) {
|
2005-04-25 13:43:21 +00:00
|
|
|
global $wgCommandLineMode;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2014-10-06 17:42:58 +00:00
|
|
|
if ( $raw === null ) {
|
|
|
|
|
$raw = $wgCommandLineMode;
|
2005-04-25 13:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-06 17:42:58 +00:00
|
|
|
if ( $raw ) {
|
|
|
|
|
$frameFormat = "%s line %s calls %s()\n";
|
|
|
|
|
$traceFormat = "%s";
|
2005-04-25 13:43:21 +00:00
|
|
|
} else {
|
2014-10-06 17:42:58 +00:00
|
|
|
$frameFormat = "<li>%s line %s calls %s()</li>\n";
|
|
|
|
|
$traceFormat = "<ul>\n%s</ul>\n";
|
2005-04-25 13:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
2014-10-06 17:42:58 +00:00
|
|
|
$frames = array_map( function ( $frame ) use ( $frameFormat ) {
|
|
|
|
|
$file = !empty( $frame['file'] ) ? basename( $frame['file'] ) : '-';
|
2014-12-12 21:30:36 +00:00
|
|
|
$line = isset( $frame['line'] ) ? $frame['line'] : '-';
|
2014-10-06 17:42:58 +00:00
|
|
|
$call = $frame['function'];
|
|
|
|
|
if ( !empty( $frame['class'] ) ) {
|
|
|
|
|
$call = $frame['class'] . $frame['type'] . $call;
|
|
|
|
|
}
|
|
|
|
|
return sprintf( $frameFormat, $file, $line, $call );
|
|
|
|
|
}, wfDebugBacktrace() );
|
|
|
|
|
|
|
|
|
|
return sprintf( $traceFormat, implode( '', $frames ) );
|
2005-04-25 13:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $level
|
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.
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $limit The maximum depth of the stack frame to return, or false for the entire stack.
|
|
|
|
|
* @return string
|
2011-05-25 18:32:04 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param array $frame
|
2012-08-29 19:36:37 +00:00
|
|
|
* @return string
|
2011-05-25 18:32:04 +00:00
|
|
|
*/
|
|
|
|
|
function wfFormatStackFrame( $frame ) {
|
2015-10-09 16:58:26 +00:00
|
|
|
if ( !isset( $frame['function'] ) ) {
|
|
|
|
|
return 'NO_FUNCTION_GIVEN';
|
|
|
|
|
}
|
2015-10-09 17:20:25 +00:00
|
|
|
return isset( $frame['class'] ) && isset( $frame['type'] ) ?
|
|
|
|
|
$frame['class'] . $frame['type'] . $frame['function'] :
|
2011-05-25 18:32:04 +00:00
|
|
|
$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
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param int $offset
|
|
|
|
|
* @param int $limit
|
|
|
|
|
* @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
|
|
|
/**
|
2015-12-28 20:35:53 +00:00
|
|
|
* Whether the client accept gzip encoding
|
|
|
|
|
*
|
|
|
|
|
* Uses the Accept-Encoding header to check if the client supports gzip encoding.
|
|
|
|
|
* Use this when considering to send a gzip-encoded response to the client.
|
2004-09-03 17:13:55 +00:00
|
|
|
*
|
2015-12-28 20:35:53 +00:00
|
|
|
* @param bool $force Forces another check even if we already have a cached result.
|
|
|
|
|
* @return bool
|
2004-09-03 17:13:55 +00:00
|
|
|
*/
|
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;
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
|
2011-05-17 22:03:20 +00:00
|
|
|
# @todo FIXME: We may want to blacklist some broken browsers
|
2016-02-17 09:09:32 +00:00
|
|
|
$m = [];
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( preg_match(
|
2013-12-01 20:39:00 +00:00
|
|
|
'/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_ENCODING'],
|
|
|
|
|
$m
|
|
|
|
|
)
|
|
|
|
|
) {
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( isset( $m[2] ) && ( $m[1] == 'q' ) && ( $m[2] == 0 ) ) {
|
2010-11-16 20:40:20 +00:00
|
|
|
$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
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $text Text to be escaped
|
|
|
|
|
* @return string
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-09-03 17:13:55 +00:00
|
|
|
function wfEscapeWikiText( $text ) {
|
2016-09-23 00:29:21 +00:00
|
|
|
global $wgEnableMagicLinks;
|
|
|
|
|
static $repl = null, $repl2 = null;
|
2016-11-03 18:50:44 +00:00
|
|
|
if ( $repl === null || defined( 'MW_PARSER_TEST' ) || defined( 'MW_PHPUNIT_TEST' ) ) {
|
|
|
|
|
// Tests depend upon being able to change $wgEnableMagicLinks, so don't cache
|
|
|
|
|
// in those situations
|
2016-09-23 00:29:21 +00:00
|
|
|
$repl = [
|
|
|
|
|
'"' => '"', '&' => '&', "'" => ''', '<' => '<',
|
|
|
|
|
'=' => '=', '>' => '>', '[' => '[', ']' => ']',
|
|
|
|
|
'{' => '{', '|' => '|', '}' => '}', ';' => ';',
|
|
|
|
|
"\n#" => "\n#", "\r#" => "\r#",
|
|
|
|
|
"\n*" => "\n*", "\r*" => "\r*",
|
|
|
|
|
"\n:" => "\n:", "\r:" => "\r:",
|
|
|
|
|
"\n " => "\n ", "\r " => "\r ",
|
|
|
|
|
"\n\n" => "\n ", "\r\n" => " \n",
|
|
|
|
|
"\n\r" => "\n ", "\r\r" => "\r ",
|
|
|
|
|
"\n\t" => "\n	", "\r\t" => "\r	", // "\n\t\n" is treated like "\n\n"
|
|
|
|
|
"\n----" => "\n----", "\r----" => "\r----",
|
|
|
|
|
'__' => '__', '://' => '://',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$magicLinks = array_keys( array_filter( $wgEnableMagicLinks ) );
|
|
|
|
|
// We have to catch everything "\s" matches in PCRE
|
|
|
|
|
foreach ( $magicLinks as $magic ) {
|
|
|
|
|
$repl["$magic "] = "$magic ";
|
|
|
|
|
$repl["$magic\t"] = "$magic	";
|
|
|
|
|
$repl["$magic\r"] = "$magic ";
|
|
|
|
|
$repl["$magic\n"] = "$magic ";
|
|
|
|
|
$repl["$magic\f"] = "$magic";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// And handle protocols that don't use "://"
|
|
|
|
|
global $wgUrlProtocols;
|
|
|
|
|
$repl2 = [];
|
|
|
|
|
foreach ( $wgUrlProtocols as $prot ) {
|
|
|
|
|
if ( substr( $prot, -1 ) === ':' ) {
|
|
|
|
|
$repl2[] = preg_quote( substr( $prot, 0, -1 ), '/' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$repl2 = $repl2 ? '/\b(' . implode( '|', $repl2 ) . '):/i' : '/^(?!)/';
|
|
|
|
|
}
|
|
|
|
|
$text = substr( strtr( "\n$text", $repl ), 1 );
|
|
|
|
|
$text = preg_replace( $repl2, '$1:', $text );
|
|
|
|
|
return $text;
|
2003-09-21 13:10:10 +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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param mixed $dest
|
|
|
|
|
* @param mixed $source
|
|
|
|
|
* @param bool $force
|
|
|
|
|
* @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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $dest
|
|
|
|
|
* @param int $bit
|
|
|
|
|
* @param bool $state
|
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).
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param mixed $var 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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int|string $code
|
|
|
|
|
* @param string $label
|
|
|
|
|
* @param string $desc
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-04-04 22:33:11 +00:00
|
|
|
function wfHttpError( $code, $label, $desc ) {
|
|
|
|
|
global $wgOut;
|
2015-06-01 14:31:52 +00:00
|
|
|
HttpStatus::header( $code );
|
2015-01-12 21:46:18 +00:00
|
|
|
if ( $wgOut ) {
|
|
|
|
|
$wgOut->disable();
|
|
|
|
|
$wgOut->sendCacheControl();
|
|
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2017-02-20 05:29:54 +00:00
|
|
|
MediaWiki\HeaderCallback::warnIfHeadersSent();
|
2007-02-21 01:02:47 +00:00
|
|
|
header( 'Content-type: text/html; charset=utf-8' );
|
2015-06-01 14:31:52 +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.
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param bool $resetGzipEncoding
|
2006-12-11 01:51:21 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfResetOutputBuffers( $resetGzipEncoding = true ) {
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $resetGzipEncoding ) {
|
2007-02-20 04:46:07 +00:00
|
|
|
// Suppress Content-Encoding and Content-Length
|
|
|
|
|
// headers from 1.10+s wfOutputHandler
|
|
|
|
|
global $wgDisableOutputCompression;
|
|
|
|
|
$wgDisableOutputCompression = true;
|
|
|
|
|
}
|
2013-04-17 14:52:47 +00:00
|
|
|
while ( $status = ob_get_status() ) {
|
2015-08-21 15:48:03 +00:00
|
|
|
if ( isset( $status['flags'] ) ) {
|
|
|
|
|
$flags = PHP_OUTPUT_HANDLER_CLEANABLE | PHP_OUTPUT_HANDLER_REMOVABLE;
|
|
|
|
|
$deleteable = ( $status['flags'] & $flags ) === $flags;
|
|
|
|
|
} elseif ( isset( $status['del'] ) ) {
|
|
|
|
|
$deleteable = $status['del'];
|
|
|
|
|
} else {
|
|
|
|
|
// Guess that any PHP-internal setting can't be removed.
|
|
|
|
|
$deleteable = $status['type'] !== 0; /* PHP_OUTPUT_HANDLER_INTERNAL */
|
|
|
|
|
}
|
|
|
|
|
if ( !$deleteable ) {
|
2006-12-11 01:51:21 +00:00
|
|
|
// Give up, and hope the result doesn't break
|
|
|
|
|
// output behavior.
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-08-21 16:01:10 +00:00
|
|
|
if ( $status['name'] === 'MediaWikiTestCase::wfResetOutputBuffersBarrier' ) {
|
|
|
|
|
// Unit testing barrier to prevent this function from breaking PHPUnit.
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !ob_end_clean() ) {
|
2006-12-11 01:51:21 +00:00
|
|
|
// Could not remove output buffer handler; abort now
|
|
|
|
|
// to avoid getting in some kind of infinite loop.
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $resetGzipEncoding ) {
|
|
|
|
|
if ( $status['name'] == 'ob_gzhandler' ) {
|
2006-12-11 01:51:21 +00:00
|
|
|
// 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
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $accept
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string $def Default
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return float[] Associative array of string => float pairs
|
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)
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !$accept ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [ $def => 1.0 ];
|
2004-04-04 22:33:11 +00:00
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$prefs = [];
|
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
|
|
|
|
2013-04-17 14:52:47 +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 ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$match = [];
|
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.
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $type
|
|
|
|
|
* @param array $avail
|
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 ) {
|
2013-04-17 14:52:47 +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 {
|
2016-02-17 19:54:59 +00:00
|
|
|
$mainType = explode( '/', $type )[0];
|
|
|
|
|
if ( array_key_exists( "$mainType/*", $avail ) ) {
|
|
|
|
|
return "$mainType/*";
|
2013-04-17 14:52:47 +00:00
|
|
|
} elseif ( array_key_exists( '*/*', $avail ) ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
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.
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param array $cprefs Client's acceptable type list
|
|
|
|
|
* @param array $sprefs 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 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$combine = [];
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
foreach ( array_keys( $sprefs ) as $type ) {
|
2016-02-17 19:54:59 +00:00
|
|
|
$subType = explode( '/', $type )[1];
|
|
|
|
|
if ( $subType != '*' ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
$ckey = mimeTypeMatch( $type, $cprefs );
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $ckey ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
$combine[$type] = $sprefs[$type] * $cprefs[$ckey];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2004-08-10 20:17:55 +00:00
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
foreach ( array_keys( $cprefs ) as $type ) {
|
2016-02-17 19:54:59 +00:00
|
|
|
$subType = explode( '/', $type )[1];
|
|
|
|
|
if ( $subType != '*' && !array_key_exists( $type, $sprefs ) ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
$skey = mimeTypeMatch( $type, $sprefs );
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $skey ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
$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
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
foreach ( array_keys( $combine ) as $type ) {
|
|
|
|
|
if ( $combine[$type] > $bestq ) {
|
2004-04-04 22:33:11 +00:00
|
|
|
$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
|
|
|
*
|
2015-06-10 18:29:05 +00:00
|
|
|
* @deprecated since 1.26, use MediaWiki\suppressWarnings() directly
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param bool $end
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-07-10 03:09:26 +00:00
|
|
|
function wfSuppressWarnings( $end = false ) {
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings( $end );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
2015-06-10 18:29:05 +00:00
|
|
|
* @deprecated since 1.26, use MediaWiki\restoreWarnings() directly
|
2004-09-02 23:28:24 +00:00
|
|
|
* Restore error level to previous value
|
|
|
|
|
*/
|
2004-07-10 03:09:26 +00:00
|
|
|
function wfRestoreWarnings() {
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings( true );
|
2004-07-10 03:09:26 +00:00
|
|
|
}
|
2004-06-29 12:23:59 +00:00
|
|
|
|
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
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param mixed $outputtype A timestamp in one of the supported formats, the
|
|
|
|
|
* function will autodetect which format is supplied and act accordingly.
|
|
|
|
|
* @param mixed $ts Optional timestamp to convert, default 0 for the current time
|
|
|
|
|
* @return string|bool 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 ) {
|
2016-09-21 00:19:00 +00:00
|
|
|
$ret = MWTimestamp::convert( $outputtype, $ts );
|
|
|
|
|
if ( $ret === false ) {
|
2013-02-03 20:05:24 +00:00
|
|
|
wfDebug( "wfTimestamp() fed bogus time value: TYPE=$outputtype; VALUE=$ts\n" );
|
2012-09-14 16:28:22 +00:00
|
|
|
}
|
2016-09-21 00:19:00 +00:00
|
|
|
return $ret;
|
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
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param int $outputtype
|
|
|
|
|
* @param string $ts
|
|
|
|
|
* @return string
|
2005-04-25 18:38:43 +00:00
|
|
|
*/
|
|
|
|
|
function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) {
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( is_null( $ts ) ) {
|
2005-04-25 18:38:43 +00:00
|
|
|
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
|
2016-09-22 03:28:42 +00:00
|
|
|
return MWTimestamp::now( TS_MW );
|
2011-05-05 17:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*
|
2014-03-25 18:52:26 +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 ) {
|
2015-01-28 04:38:57 +00:00
|
|
|
$isWindows = strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN';
|
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
|
|
|
/**
|
2013-10-29 22:23:30 +00:00
|
|
|
* Check if we are running under HHVM
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +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
|
|
|
*/
|
2013-10-29 22:23:30 +00:00
|
|
|
function wfIsHHVM() {
|
|
|
|
|
return defined( 'HHVM_VERSION' );
|
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
|
|
|
}
|
|
|
|
|
|
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
|
2016-01-21 05:35:59 +00:00
|
|
|
* environment variables are then checked in sequence, then
|
|
|
|
|
* sys_get_temp_dir(), then upload_tmp_dir from php.ini.
|
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
|
|
|
*
|
2014-03-25 18:52:26 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 00:22:36 +00:00
|
|
|
return TempFSFile::getUsableTempDirectory();
|
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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $dir Full path to directory to create
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param int $mode Chmod value to use, default is $wgDirectoryMode
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $caller 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
|
|
|
}
|
|
|
|
|
|
2015-03-02 18:25:25 +00:00
|
|
|
if ( strval( $dir ) === '' || is_dir( $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
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$dir = str_replace( [ '\\', '/' ], DIRECTORY_SEPARATOR, $dir );
|
2009-05-14 20:45:14 +00:00
|
|
|
|
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
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2010-08-14 13:31:10 +00:00
|
|
|
$ok = mkdir( $dir, $mode, true ); // PHP5 <3
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2010-06-20 15:41:38 +00:00
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !$ok ) {
|
2015-09-11 13:44:59 +00:00
|
|
|
// directory may have been created on another request since we last checked
|
2013-09-12 22:55:45 +00:00
|
|
|
if ( is_dir( $dir ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-22 16:50:03 +00:00
|
|
|
// PHP doesn't report the path in its warning message, so add our own to aid in diagnosis.
|
2013-03-18 09:32:14 +00:00
|
|
|
wfLogWarning( sprintf( "failed to mkdir \"%s\" mode 0%o", $dir, $mode ) );
|
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.
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $dir
|
2012-02-24 15:41:06 +00:00
|
|
|
*/
|
|
|
|
|
function wfRecursiveRemoveDir( $dir ) {
|
|
|
|
|
wfDebug( __FUNCTION__ . "( $dir )\n" );
|
2016-10-13 05:34:26 +00:00
|
|
|
// taken from https://secure.php.net/manual/en/function.rmdir.php#98622
|
2012-02-24 15:41:06 +00:00
|
|
|
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
|
|
|
/**
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param int $nr The number to format
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param int $acc The number of digits after the decimal point, default 2
|
|
|
|
|
* @param bool $round Whether or not to round the value, default true
|
|
|
|
|
* @return string
|
2005-07-16 21:59:53 +00:00
|
|
|
*/
|
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
|
|
|
|
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
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $setting
|
|
|
|
|
* @return bool
|
2007-09-17 19:44:15 +00:00
|
|
|
*/
|
|
|
|
|
function wfIniGetBool( $setting ) {
|
2013-05-29 09:16:25 +00:00
|
|
|
$val = strtolower( ini_get( $setting ) );
|
2007-09-17 19:44:15 +00:00
|
|
|
// 'on' and 'true' can't have whitespace around them, but '1' can.
|
2013-05-29 09:16:25 +00:00
|
|
|
return $val == 'on'
|
|
|
|
|
|| $val == 'true'
|
|
|
|
|
|| $val == 'yes'
|
2007-09-26 17:59:29 +00:00
|
|
|
|| preg_match( "/^\s*[+-]?0*[1-9]/", $val ); // approx C atoi() function
|
2007-09-17 19:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-23 08:54:48 +00:00
|
|
|
/**
|
2016-09-23 23:55:44 +00:00
|
|
|
* Version of escapeshellarg() that works better on Windows.
|
2011-10-23 08:54:48 +00:00
|
|
|
*
|
2016-09-23 23:55:44 +00:00
|
|
|
* Originally, this fixed the incorrect use of single quotes on Windows
|
|
|
|
|
* (https://bugs.php.net/bug.php?id=26285) and the locale problems on Linux in
|
|
|
|
|
* PHP 5.2.6+ (bug backported to earlier distro releases of PHP).
|
2011-10-23 08:54:48 +00:00
|
|
|
*
|
2014-10-09 01:47:35 +00:00
|
|
|
* @param string ... strings to escape and glue together, or a single array of strings parameter
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string
|
2011-10-23 08:54:48 +00:00
|
|
|
*/
|
2014-03-25 18:52:26 +00:00
|
|
|
function wfEscapeShellArg( /*...*/ ) {
|
2011-10-23 08:54:48 +00:00
|
|
|
$args = func_get_args();
|
2014-10-09 01:47:35 +00:00
|
|
|
if ( count( $args ) === 1 && is_array( reset( $args ) ) ) {
|
|
|
|
|
// If only one argument has been passed, and that argument is an array,
|
|
|
|
|
// treat it as a list of arguments
|
|
|
|
|
$args = reset( $args );
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-23 08:54:48 +00:00
|
|
|
$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
|
2013-10-04 13:43:09 +00:00
|
|
|
// @codingStandardsIgnoreStart For long URLs
|
2011-10-23 08:54:48 +00:00
|
|
|
// Refs:
|
2016-10-13 05:34:26 +00:00
|
|
|
// * https://web.archive.org/web/20020708081031/http://mailman.lyra.org/pipermail/scite-interest/2002-March/000436.html
|
|
|
|
|
// * https://technet.microsoft.com/en-us/library/cc723564.aspx
|
2016-09-11 21:41:26 +00:00
|
|
|
// * T15518
|
2011-10-23 08:54:48 +00:00
|
|
|
// * CR r63214
|
|
|
|
|
// Double the backslashes before any double quotes. Escape the double quotes.
|
2013-10-04 13:43:09 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2011-10-23 08:54:48 +00:00
|
|
|
$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
|
2016-02-17 09:09:32 +00:00
|
|
|
$m = [];
|
2011-10-23 08:54:48 +00:00
|
|
|
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
|
|
|
/**
|
2013-04-18 05:05:47 +00:00
|
|
|
* Check if wfShellExec() is effectively disabled via php.ini config
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2016-02-13 00:18:26 +00:00
|
|
|
* @return bool|string False or 'disabled'
|
2013-04-18 05:05:47 +00:00
|
|
|
* @since 1.22
|
2006-05-26 02:41:20 +00:00
|
|
|
*/
|
2013-04-18 05:05:47 +00:00
|
|
|
function wfShellExecDisabled() {
|
|
|
|
|
static $disabled = null;
|
2009-01-07 12:20:30 +00:00
|
|
|
if ( is_null( $disabled ) ) {
|
2016-02-13 00:18:26 +00:00
|
|
|
if ( !function_exists( 'proc_open' ) ) {
|
2014-03-31 14:19:57 +00:00
|
|
|
wfDebug( "proc_open() is disabled\n" );
|
|
|
|
|
$disabled = 'disabled';
|
2010-10-26 14:31:13 +00:00
|
|
|
} else {
|
2014-03-31 14:19:57 +00:00
|
|
|
$disabled = false;
|
2009-01-07 12:20:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-04-18 05:05:47 +00:00
|
|
|
return $disabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a shell command, with time and memory limits mirrored from the PHP
|
|
|
|
|
* configuration if supported.
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string|string[] $cmd If string, a properly shell-escaped command line,
|
2014-07-21 22:46:16 +00:00
|
|
|
* or an array of unescaped arguments, in which case each value will be escaped
|
|
|
|
|
* Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param null|mixed &$retval Optional, will receive the program's exit code.
|
|
|
|
|
* (non-zero is usually failure). If there is an error from
|
|
|
|
|
* read, select, or proc_open(), this will be set to -1.
|
|
|
|
|
* @param array $environ Optional environment variables which should be
|
|
|
|
|
* added to the executed command environment.
|
|
|
|
|
* @param array $limits Optional array with limits(filesize, memory, time, walltime)
|
|
|
|
|
* this overwrites the global wgMaxShell* limits.
|
2013-10-22 01:16:14 +00:00
|
|
|
* @param array $options Array of options:
|
2014-04-23 09:41:35 +00:00
|
|
|
* - duplicateStderr: Set this to true to duplicate stderr to stdout,
|
|
|
|
|
* including errors from limit.sh
|
2015-03-04 06:23:00 +00:00
|
|
|
* - profileMethod: By default this function will profile based on the calling
|
|
|
|
|
* method. Set this to a string for an alternative method to profile from
|
2013-11-21 17:19:44 +00:00
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return string Collected stdout as a string
|
2013-04-18 05:05:47 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfShellExec( $cmd, &$retval = null, $environ = [],
|
|
|
|
|
$limits = [], $options = []
|
2013-10-04 13:43:09 +00:00
|
|
|
) {
|
2013-04-18 05:05:47 +00:00
|
|
|
global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime,
|
|
|
|
|
$wgMaxShellWallClockTime, $wgShellCgroup;
|
|
|
|
|
|
|
|
|
|
$disabled = wfShellExecDisabled();
|
2009-01-07 12:20:30 +00:00
|
|
|
if ( $disabled ) {
|
2006-05-26 02:41:20 +00:00
|
|
|
$retval = 1;
|
2016-02-13 00:18:26 +00:00
|
|
|
return 'Unable to run external programs, proc_open() is disabled.';
|
2006-05-26 02:41:20 +00:00
|
|
|
}
|
2009-01-07 12:20:30 +00:00
|
|
|
|
2013-09-12 01:40:56 +00:00
|
|
|
$includeStderr = isset( $options['duplicateStderr'] ) && $options['duplicateStderr'];
|
2015-03-04 06:23:00 +00:00
|
|
|
$profileMethod = isset( $options['profileMethod'] ) ? $options['profileMethod'] : wfGetCaller();
|
2013-09-12 01:40:56 +00:00
|
|
|
|
2010-10-26 22:17:42 +00:00
|
|
|
$envcmd = '';
|
2013-04-17 14:52:47 +00:00
|
|
|
foreach ( $environ as $k => $v ) {
|
2010-10-26 22:17:42 +00:00
|
|
|
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
|
2016-10-13 05:34:26 +00:00
|
|
|
* https://technet.microsoft.com/en-us/library/cc723564.aspx
|
2010-12-01 20:22:45 +00:00
|
|
|
* 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 ) . ' ';
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-21 22:46:16 +00:00
|
|
|
if ( is_array( $cmd ) ) {
|
2014-10-09 01:47:35 +00:00
|
|
|
$cmd = wfEscapeShellArg( $cmd );
|
2014-07-21 22:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-26 22:17:42 +00:00
|
|
|
$cmd = $envcmd . $cmd;
|
2010-12-01 20:22:45 +00:00
|
|
|
|
2013-10-22 01:16:14 +00:00
|
|
|
$useLogPipe = false;
|
2014-06-20 01:13:48 +00:00
|
|
|
if ( is_executable( '/bin/bash' ) ) {
|
2015-06-16 19:06:19 +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 );
|
|
|
|
|
}
|
2015-06-16 19:06:19 +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 ) {
|
2013-02-05 04:19:53 +00:00
|
|
|
$cmd = '/bin/bash ' . escapeshellarg( "$IP/includes/limit.sh" ) . ' ' .
|
|
|
|
|
escapeshellarg( $cmd ) . ' ' .
|
|
|
|
|
escapeshellarg(
|
2013-10-21 04:18:04 +00:00
|
|
|
"MW_INCLUDE_STDERR=" . ( $includeStderr ? '1' : '' ) . ';' .
|
2013-02-05 04:19:53 +00:00
|
|
|
"MW_CPU_LIMIT=$time; " .
|
|
|
|
|
'MW_CGROUP=' . escapeshellarg( $wgShellCgroup ) . '; ' .
|
|
|
|
|
"MW_MEM_LIMIT=$mem; " .
|
|
|
|
|
"MW_FILE_SIZE_LIMIT=$filesize; " .
|
2013-10-22 01:16:14 +00:00
|
|
|
"MW_WALL_CLOCK_LIMIT=$wallTime; " .
|
|
|
|
|
"MW_USE_LOG_PIPE=yes"
|
2013-02-05 04:19:53 +00:00
|
|
|
);
|
2013-10-22 01:16:14 +00:00
|
|
|
$useLogPipe = true;
|
2013-10-21 04:18:04 +00:00
|
|
|
} elseif ( $includeStderr ) {
|
2013-09-12 01:40:56 +00:00
|
|
|
$cmd .= ' 2>&1';
|
2005-11-26 02:57:18 +00:00
|
|
|
}
|
2013-09-12 01:40:56 +00:00
|
|
|
} elseif ( $includeStderr ) {
|
|
|
|
|
$cmd .= ' 2>&1';
|
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
|
|
|
|
2016-04-19 17:53:39 +00:00
|
|
|
// Don't try to execute commands that exceed Linux's MAX_ARG_STRLEN.
|
|
|
|
|
// Other platforms may be more accomodating, but we don't want to be
|
|
|
|
|
// accomodating, because very long commands probably include user
|
|
|
|
|
// input. See T129506.
|
|
|
|
|
if ( strlen( $cmd ) > SHELL_MAX_ARG_STRLEN ) {
|
2016-05-20 18:11:58 +00:00
|
|
|
throw new Exception( __METHOD__ .
|
|
|
|
|
'(): total length of $cmd must not exceed SHELL_MAX_ARG_STRLEN' );
|
2016-04-19 17:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$desc = [
|
|
|
|
|
0 => [ 'file', 'php://stdin', 'r' ],
|
|
|
|
|
1 => [ 'pipe', 'w' ],
|
|
|
|
|
2 => [ 'file', 'php://stderr', 'w' ] ];
|
2013-10-22 01:16:14 +00:00
|
|
|
if ( $useLogPipe ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$desc[3] = [ 'pipe', 'w' ];
|
2013-10-22 01:16:14 +00:00
|
|
|
}
|
|
|
|
|
$pipes = null;
|
2015-03-04 06:23:00 +00:00
|
|
|
$scoped = Profiler::instance()->scopedProfileIn( __FUNCTION__ . '-' . $profileMethod );
|
2013-10-22 01:16:14 +00:00
|
|
|
$proc = proc_open( $cmd, $desc, $pipes );
|
|
|
|
|
if ( !$proc ) {
|
2014-02-04 21:16:13 +00:00
|
|
|
wfDebugLog( 'exec', "proc_open() failed: $cmd" );
|
2013-10-22 01:16:14 +00:00
|
|
|
$retval = -1;
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
$outBuffer = $logBuffer = '';
|
2016-02-17 09:09:32 +00:00
|
|
|
$emptyArray = [];
|
2013-10-22 01:16:14 +00:00
|
|
|
$status = false;
|
|
|
|
|
$logMsg = false;
|
|
|
|
|
|
2015-10-14 07:40:50 +00:00
|
|
|
/* According to the documentation, it is possible for stream_select()
|
|
|
|
|
* to fail due to EINTR. I haven't managed to induce this in testing
|
|
|
|
|
* despite sending various signals. If it did happen, the error
|
|
|
|
|
* message would take the form:
|
|
|
|
|
*
|
|
|
|
|
* stream_select(): unable to select [4]: Interrupted system call (max_fd=5)
|
|
|
|
|
*
|
|
|
|
|
* where [4] is the value of the macro EINTR and "Interrupted system
|
|
|
|
|
* call" is string which according to the Linux manual is "possibly"
|
|
|
|
|
* localised according to LC_MESSAGES.
|
|
|
|
|
*/
|
2013-10-22 01:16:14 +00:00
|
|
|
$eintr = defined( 'SOCKET_EINTR' ) ? SOCKET_EINTR : 4;
|
|
|
|
|
$eintrMessage = "stream_select(): unable to select [$eintr]";
|
|
|
|
|
|
2014-07-18 20:03:06 +00:00
|
|
|
$running = true;
|
|
|
|
|
$timeout = null;
|
|
|
|
|
$numReadyPipes = 0;
|
|
|
|
|
|
|
|
|
|
while ( $running === true || $numReadyPipes !== 0 ) {
|
|
|
|
|
if ( $running ) {
|
|
|
|
|
$status = proc_get_status( $proc );
|
|
|
|
|
// If the process has terminated, switch to nonblocking selects
|
|
|
|
|
// for getting any data still waiting to be read.
|
|
|
|
|
if ( !$status['running'] ) {
|
|
|
|
|
$running = false;
|
|
|
|
|
$timeout = 0;
|
|
|
|
|
}
|
2013-10-22 01:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$readyPipes = $pipes;
|
|
|
|
|
|
|
|
|
|
// Clear last error
|
2014-05-11 15:32:18 +00:00
|
|
|
// @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged
|
2013-10-22 01:16:14 +00:00
|
|
|
@trigger_error( '' );
|
2014-07-18 20:03:06 +00:00
|
|
|
$numReadyPipes = @stream_select( $readyPipes, $emptyArray, $emptyArray, $timeout );
|
|
|
|
|
if ( $numReadyPipes === false ) {
|
2014-05-11 15:32:18 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2013-10-22 01:16:14 +00:00
|
|
|
$error = error_get_last();
|
|
|
|
|
if ( strncmp( $error['message'], $eintrMessage, strlen( $eintrMessage ) ) == 0 ) {
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
trigger_error( $error['message'], E_USER_WARNING );
|
|
|
|
|
$logMsg = $error['message'];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-13 00:40:49 +00:00
|
|
|
foreach ( $readyPipes as $fd => $pipe ) {
|
2013-10-22 01:16:14 +00:00
|
|
|
$block = fread( $pipe, 65536 );
|
|
|
|
|
if ( $block === '' ) {
|
|
|
|
|
// End of file
|
|
|
|
|
fclose( $pipes[$fd] );
|
|
|
|
|
unset( $pipes[$fd] );
|
|
|
|
|
if ( !$pipes ) {
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $block === false ) {
|
|
|
|
|
// Read error
|
|
|
|
|
$logMsg = "Error reading from pipe";
|
|
|
|
|
break 2;
|
|
|
|
|
} elseif ( $fd == 1 ) {
|
|
|
|
|
// From stdout
|
|
|
|
|
$outBuffer .= $block;
|
|
|
|
|
} elseif ( $fd == 3 ) {
|
|
|
|
|
// From log FD
|
|
|
|
|
$logBuffer .= $block;
|
|
|
|
|
if ( strpos( $block, "\n" ) !== false ) {
|
|
|
|
|
$lines = explode( "\n", $logBuffer );
|
|
|
|
|
$logBuffer = array_pop( $lines );
|
|
|
|
|
foreach ( $lines as $line ) {
|
|
|
|
|
wfDebugLog( 'exec', $line );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-09-05 03:32:09 +00:00
|
|
|
|
2013-10-22 01:16:14 +00:00
|
|
|
foreach ( $pipes as $pipe ) {
|
|
|
|
|
fclose( $pipe );
|
2008-09-05 03:32:09 +00:00
|
|
|
}
|
2013-10-22 01:16:14 +00:00
|
|
|
|
|
|
|
|
// Use the status previously collected if possible, since proc_get_status()
|
|
|
|
|
// just calls waitpid() which will not return anything useful the second time.
|
2014-07-18 20:03:06 +00:00
|
|
|
if ( $running ) {
|
2013-10-22 01:16:14 +00:00
|
|
|
$status = proc_get_status( $proc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $logMsg !== false ) {
|
|
|
|
|
// Read/select error
|
|
|
|
|
$retval = -1;
|
|
|
|
|
proc_close( $proc );
|
|
|
|
|
} elseif ( $status['signaled'] ) {
|
|
|
|
|
$logMsg = "Exited with signal {$status['termsig']}";
|
|
|
|
|
$retval = 128 + $status['termsig'];
|
|
|
|
|
proc_close( $proc );
|
|
|
|
|
} else {
|
|
|
|
|
if ( $status['running'] ) {
|
|
|
|
|
$retval = proc_close( $proc );
|
|
|
|
|
} else {
|
|
|
|
|
$retval = $status['exitcode'];
|
|
|
|
|
proc_close( $proc );
|
|
|
|
|
}
|
|
|
|
|
if ( $retval == 127 ) {
|
|
|
|
|
$logMsg = "Possibly missing executable file";
|
|
|
|
|
} elseif ( $retval >= 129 && $retval <= 192 ) {
|
|
|
|
|
$logMsg = "Probably exited with signal " . ( $retval - 128 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $logMsg !== false ) {
|
2014-02-04 21:16:13 +00:00
|
|
|
wfDebugLog( 'exec', "$logMsg: $cmd" );
|
2013-10-22 01:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $outBuffer;
|
2008-09-29 10:19:11 +00:00
|
|
|
}
|
2009-07-23 17:14:07 +00:00
|
|
|
|
2013-09-12 01:40:56 +00:00
|
|
|
/**
|
|
|
|
|
* Execute a shell command, returning both stdout and stderr. Convenience
|
|
|
|
|
* function, as all the arguments to wfShellExec can become unwieldy.
|
|
|
|
|
*
|
|
|
|
|
* @note This also includes errors from limit.sh, e.g. if $wgMaxShellFileSize is exceeded.
|
2014-10-08 18:12:11 +00:00
|
|
|
* @param string|string[] $cmd If string, a properly shell-escaped command line,
|
|
|
|
|
* or an array of unescaped arguments, in which case each value will be escaped
|
|
|
|
|
* Example: [ 'convert', '-font', 'font name' ] would produce "'convert' '-font' 'font name'"
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param null|mixed &$retval Optional, will receive the program's exit code.
|
|
|
|
|
* (non-zero is usually failure)
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param array $environ Optional environment variables which should be
|
2014-04-23 09:41:35 +00:00
|
|
|
* added to the executed command environment.
|
|
|
|
|
* @param array $limits Optional array with limits(filesize, memory, time, walltime)
|
2014-06-23 13:02:37 +00:00
|
|
|
* this overwrites the global wgMaxShell* limits.
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return string Collected stdout and stderr as a string
|
2013-09-12 01:40:56 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfShellExecWithStderr( $cmd, &$retval = null, $environ = [], $limits = [] ) {
|
2015-03-04 06:23:00 +00:00
|
|
|
return wfShellExec( $cmd, $retval, $environ, $limits,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'duplicateStderr' => true, 'profileMethod' => wfGetCaller() ] );
|
2013-09-12 01:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
2008-09-29 10:19:11 +00:00
|
|
|
/**
|
2017-05-09 16:12:41 +00:00
|
|
|
* Formerly set the locale for locale-sensitive operations
|
2015-07-28 16:17:40 +00:00
|
|
|
*
|
2017-05-09 16:12:41 +00:00
|
|
|
* This is now done in Setup.php.
|
2015-07-28 16:17:40 +00:00
|
|
|
*
|
2017-05-09 16:12:41 +00:00
|
|
|
* @deprecated since 1.30, no longer needed
|
2015-07-28 16:17:40 +00:00
|
|
|
* @see $wgShellLocale
|
2008-09-29 10:19:11 +00:00
|
|
|
*/
|
|
|
|
|
function wfInitShellLocale() {
|
2005-11-26 00:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-04 23:48:55 +00:00
|
|
|
/**
|
|
|
|
|
* 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").
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $script MediaWiki cli script path
|
|
|
|
|
* @param array $parameters Arguments and options to the script
|
|
|
|
|
* @param array $options Associative array of options:
|
2017-02-25 21:53:36 +00:00
|
|
|
* 'php': The path to the php executable
|
|
|
|
|
* 'wrapper': Path to a PHP wrapper to handle the maintenance script
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string
|
2011-09-23 20:42:22 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfShellWikiCmd( $script, array $parameters = [], array $options = [] ) {
|
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.
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'wfShellWikiCmd', [ &$script, &$parameters, &$options ] );
|
|
|
|
|
$cmd = isset( $options['php'] ) ? [ $options['php'] ] : [ $wgPhpCli ];
|
2011-09-23 20:42:22 +00:00
|
|
|
if ( isset( $options['wrapper'] ) ) {
|
|
|
|
|
$cmd[] = $options['wrapper'];
|
|
|
|
|
}
|
|
|
|
|
$cmd[] = $script;
|
2012-03-19 18:28:47 +00:00
|
|
|
// Escape each parameter for shell
|
2014-10-09 01:47:35 +00:00
|
|
|
return 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.
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $old
|
|
|
|
|
* @param string $mine
|
|
|
|
|
* @param string $yours
|
|
|
|
|
* @param string $result
|
|
|
|
|
* @return bool
|
2011-10-23 08:54:48 +00:00
|
|
|
*/
|
|
|
|
|
function wfMerge( $old, $mine, $yours, &$result ) {
|
|
|
|
|
global $wgDiff3;
|
|
|
|
|
|
|
|
|
|
# This check may also protect against code injection in
|
|
|
|
|
# case of broken installations.
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2011-10-23 08:54:48 +00:00
|
|
|
$haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2011-10-23 08:54:48 +00:00
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !$haveDiff3 ) {
|
2011-10-23 08:54:48 +00:00
|
|
|
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
|
2015-04-03 18:07:39 +00:00
|
|
|
$cmd = wfEscapeShellArg( $wgDiff3, '-a', '--overlap-only', $mytextName,
|
|
|
|
|
$oldtextName, $yourtextName );
|
2011-10-23 08:54:48 +00:00
|
|
|
$handle = popen( $cmd, 'r' );
|
|
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( fgets( $handle, 1024 ) ) {
|
2011-10-23 08:54:48 +00:00
|
|
|
$conflict = true;
|
|
|
|
|
} else {
|
|
|
|
|
$conflict = false;
|
|
|
|
|
}
|
|
|
|
|
pclose( $handle );
|
|
|
|
|
|
|
|
|
|
# Merge differences
|
2015-04-03 18:07:39 +00:00
|
|
|
$cmd = wfEscapeShellArg( $wgDiff3, '-a', '-e', '--merge', $mytextName,
|
|
|
|
|
$oldtextName, $yourtextName );
|
2011-10-23 08:54:48 +00:00
|
|
|
$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.
|
2015-03-23 18:51:39 +00:00
|
|
|
* "Useful" for machine processing of diffs.
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 1.25, use DiffEngine/UnifiedDiffFormatter directly
|
2011-10-23 08:54:48 +00:00
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $before The text before the changes.
|
|
|
|
|
* @param string $after The text after the changes.
|
|
|
|
|
* @param string $params Command-line options for the diff command.
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string Unified diff of $before and $after
|
2011-10-23 08:54:48 +00:00
|
|
|
*/
|
|
|
|
|
function wfDiff( $before, $after, $params = '-u' ) {
|
|
|
|
|
if ( $before == $after ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
global $wgDiff;
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2011-10-23 08:54:48 +00:00
|
|
|
$haveDiff = $wgDiff && file_exists( $wgDiff );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2011-10-23 08:54:48 +00:00
|
|
|
|
|
|
|
|
# This check may also protect against code injection in
|
|
|
|
|
# case of broken installations.
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( !$haveDiff ) {
|
2011-10-23 08:54:48 +00:00
|
|
|
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' );
|
2015-03-23 18:51:39 +00:00
|
|
|
if ( !$h ) {
|
|
|
|
|
unlink( $oldtextName );
|
|
|
|
|
unlink( $newtextName );
|
|
|
|
|
throw new Exception( __METHOD__ . '(): popen() failed' );
|
|
|
|
|
}
|
2011-10-23 08:54:48 +00:00
|
|
|
|
|
|
|
|
$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 );
|
2014-09-15 01:52:31 +00:00
|
|
|
if ( isset( $diff_lines[0] ) && strpos( $diff_lines[0], '---' ) === 0 ) {
|
2011-10-23 08:54:48 +00:00
|
|
|
unset( $diff_lines[0] );
|
|
|
|
|
}
|
2014-09-15 01:52:31 +00:00
|
|
|
if ( isset( $diff_lines[1] ) && strpos( $diff_lines[1], '+++' ) === 0 ) {
|
2011-10-23 08:54:48 +00:00
|
|
|
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
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string|int|float $req_ver 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
|
|
|
|
|
*
|
2013-10-28 19:48:53 +00:00
|
|
|
* Note: Due to the behavior of PHP's version_compare() which is used in this
|
2013-12-30 17:48:11 +00:00
|
|
|
* function, if you want to allow the 'wmf' development versions add a 'c' (or
|
2013-10-28 19:48:53 +00:00
|
|
|
* any single letter other than 'a', 'b' or 'p') as a post-fix to your
|
|
|
|
|
* targeted version number. For example if you wanted to allow any variation
|
|
|
|
|
* of 1.22 use `wfUseMW( '1.22c' )`. Using an 'a' or 'b' instead of 'c' will
|
|
|
|
|
* not result in the same comparison due to the internal logic of
|
|
|
|
|
* version_compare().
|
|
|
|
|
*
|
2005-12-31 02:53:35 +00:00
|
|
|
* @see perldoc -f use
|
|
|
|
|
*
|
2015-05-14 05:51:55 +00:00
|
|
|
* @deprecated since 1.26, use the "requires' property of extension.json
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string|int|float $req_ver 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.
|
2016-10-13 05:34:26 +00:00
|
|
|
* https://bugs.php.net/bug.php?id=33898
|
2006-01-20 09:03:16 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $path
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $suffix String to remove if present
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string
|
2006-01-20 09:03:16 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfBaseName( $path, $suffix = '' ) {
|
2013-10-04 13:43:09 +00:00
|
|
|
if ( $suffix == '' ) {
|
|
|
|
|
$encSuffix = '';
|
|
|
|
|
} else {
|
|
|
|
|
$encSuffix = '(?:' . preg_quote( $suffix, '#' ) . ')?';
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$matches = [];
|
2013-04-17 14:52:47 +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.
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $path Absolute destination path including target filename
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $from Absolute source path, directory only
|
2014-03-25 18:52:26 +00:00
|
|
|
* @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
|
|
|
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $pieces[0] !== $against[0] ) {
|
2008-03-06 01:07:00 +00:00
|
|
|
// Non-matching Windows drive letters?
|
|
|
|
|
// Return a full path.
|
|
|
|
|
return $path;
|
|
|
|
|
}
|
2007-01-12 00:35:26 +00:00
|
|
|
|
|
|
|
|
// Trim off common prefix
|
2013-04-17 14:52:47 +00:00
|
|
|
while ( count( $pieces ) && count( $against )
|
2007-01-12 00:35:26 +00:00
|
|
|
&& $pieces[0] == $against[0] ) {
|
|
|
|
|
array_shift( $pieces );
|
|
|
|
|
array_shift( $against );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// relative dots to bump us to the parent
|
2013-04-17 14:52:47 +00:00
|
|
|
while ( count( $against ) ) {
|
2007-01-12 00:35:26 +00:00
|
|
|
array_unshift( $pieces, '..' );
|
|
|
|
|
array_shift( $against );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
array_push( $pieces, wfBaseName( $path ) );
|
|
|
|
|
|
|
|
|
|
return implode( DIRECTORY_SEPARATOR, $pieces );
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
2016-08-02 21:53:25 +00:00
|
|
|
* @deprecated since 1.27 Use Wikimedia\base_convert() directly
|
2015-11-24 22:53:58 +00:00
|
|
|
*
|
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
|
|
|
|
|
*/
|
2013-10-04 13:43:09 +00:00
|
|
|
function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1,
|
|
|
|
|
$lowercase = true, $engine = 'auto'
|
|
|
|
|
) {
|
2015-10-29 22:56:07 +00:00
|
|
|
return Wikimedia\base_convert( $input, $sourceBase, $destBase, $pad, $lowercase, $engine );
|
2006-06-16 01:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-20 05:17:40 +00:00
|
|
|
/**
|
2016-02-01 20:44:03 +00:00
|
|
|
* @deprecated since 1.27, PHP's session generation isn't used with
|
2016-04-06 22:22:33 +00:00
|
|
|
* MediaWiki\Session\SessionManager
|
2012-03-20 05:17:40 +00:00
|
|
|
*/
|
|
|
|
|
function wfFixSessionID() {
|
2016-02-01 20:44:03 +00:00
|
|
|
wfDeprecated( __FUNCTION__, '1.27' );
|
2012-03-20 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
2013-08-07 16:21:22 +00:00
|
|
|
/**
|
2016-02-01 20:44:03 +00:00
|
|
|
* Reset the session id
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2016-04-06 22:22:33 +00:00
|
|
|
* @deprecated since 1.27, use MediaWiki\Session\SessionManager instead
|
2013-08-07 16:21:22 +00:00
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
function wfResetSessionID() {
|
2016-02-01 20:44:03 +00:00
|
|
|
wfDeprecated( __FUNCTION__, '1.27' );
|
|
|
|
|
$session = SessionManager::getGlobalSession();
|
|
|
|
|
$delay = $session->delaySave();
|
|
|
|
|
|
|
|
|
|
$session->resetId();
|
|
|
|
|
|
|
|
|
|
// Make sure a session is started, since that's what the old
|
|
|
|
|
// wfResetSessionID() did.
|
|
|
|
|
if ( session_id() !== $session->getId() ) {
|
|
|
|
|
wfSetupSession( $session->getId() );
|
2013-08-07 16:21:22 +00:00
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
ScopedCallback::consume( $delay );
|
2013-08-07 16:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-26 07:15:39 +00:00
|
|
|
/**
|
|
|
|
|
* Initialise php session
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2016-04-06 22:22:33 +00:00
|
|
|
* @deprecated since 1.27, use MediaWiki\Session\SessionManager instead.
|
2016-02-01 20:44:03 +00:00
|
|
|
* Generally, "using" SessionManager will be calling ->getSessionById() or
|
|
|
|
|
* ::getGlobalSession() (depending on whether you were passing $sessionId
|
|
|
|
|
* here), then calling $session->persist().
|
|
|
|
|
* @param bool|string $sessionId
|
2006-07-26 07:15:39 +00:00
|
|
|
*/
|
2010-09-06 10:18:53 +00:00
|
|
|
function wfSetupSession( $sessionId = false ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
wfDeprecated( __FUNCTION__, '1.27' );
|
2015-08-03 20:35:50 +00:00
|
|
|
|
2010-09-06 10:18:53 +00:00
|
|
|
if ( $sessionId ) {
|
|
|
|
|
session_id( $sessionId );
|
|
|
|
|
}
|
2015-08-03 20:35:50 +00:00
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
$session = SessionManager::getGlobalSession();
|
|
|
|
|
$session->persist();
|
2015-08-03 20:35:50 +00:00
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( session_id() !== $session->getId() ) {
|
|
|
|
|
session_id( $session->getId() );
|
2015-08-03 20:35:50 +00:00
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
MediaWiki\quietCall( 'session_start' );
|
2006-07-26 07:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an object from the precompiled serialized directory
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @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
|
|
|
/**
|
2015-06-23 04:52:30 +00:00
|
|
|
* Make a cache key for the local wiki.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2017-05-23 21:57:35 +00:00
|
|
|
* @deprecated since 1.30 Call makeKey on a BagOStuff instance
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string $args,...
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string
|
2006-10-04 09:06:18 +00:00
|
|
|
*/
|
2014-03-25 18:52:26 +00:00
|
|
|
function wfMemcKey( /*...*/ ) {
|
2015-10-09 23:35:08 +00:00
|
|
|
return call_user_func_array(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ ObjectCache::getLocalClusterInstance(), 'makeKey' ],
|
2015-10-09 23:35:08 +00:00
|
|
|
func_get_args()
|
|
|
|
|
);
|
2006-10-04 09:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-23 04:52:30 +00:00
|
|
|
* Make a cache key for a foreign DB.
|
|
|
|
|
*
|
|
|
|
|
* Must match what wfMemcKey() would produce in context of the foreign wiki.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $db
|
|
|
|
|
* @param string $prefix
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string $args,...
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string
|
2006-10-04 09:06:18 +00:00
|
|
|
*/
|
2014-03-25 18:52:26 +00:00
|
|
|
function wfForeignMemcKey( $db, $prefix /*...*/ ) {
|
2006-10-04 09:06:18 +00:00
|
|
|
$args = array_slice( func_get_args(), 2 );
|
2015-10-09 23:35:08 +00:00
|
|
|
$keyspace = $prefix ? "$db-$prefix" : $db;
|
|
|
|
|
return call_user_func_array(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ ObjectCache::getLocalClusterInstance(), 'makeKeyInternal' ],
|
|
|
|
|
[ $keyspace, $args ]
|
2015-10-09 23:35:08 +00:00
|
|
|
);
|
2006-10-04 09:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-23 04:52:30 +00:00
|
|
|
/**
|
|
|
|
|
* Make a cache key with database-agnostic prefix.
|
|
|
|
|
*
|
|
|
|
|
* Doesn't have a wiki-specific namespace. Uses a generic 'global' prefix
|
|
|
|
|
* instead. Must have a prefix as otherwise keys that use a database name
|
|
|
|
|
* in the first segment will clash with wfMemcKey/wfForeignMemcKey.
|
|
|
|
|
*
|
2017-05-23 21:57:35 +00:00
|
|
|
* @deprecated since 1.30 Call makeGlobalKey on a BagOStuff instance
|
2015-06-23 04:52:30 +00:00
|
|
|
* @since 1.26
|
|
|
|
|
* @param string $args,...
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function wfGlobalCacheKey( /*...*/ ) {
|
2015-10-09 23:35:08 +00:00
|
|
|
return call_user_func_array(
|
2016-02-17 09:09:32 +00:00
|
|
|
[ ObjectCache::getLocalClusterInstance(), 'makeGlobalKey' ],
|
2015-10-09 23:35:08 +00:00
|
|
|
func_get_args()
|
|
|
|
|
);
|
2015-06-23 04:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-04 09:06:18 +00:00
|
|
|
/**
|
|
|
|
|
* Get an ASCII string identifying this wiki
|
|
|
|
|
* This is used as a prefix in memcached keys
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +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
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $wiki
|
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
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param int $db Index of the connection to get. May be DB_MASTER for the
|
2016-09-05 19:55:19 +00:00
|
|
|
* master (for write queries), DB_REPLICA for potentially lagged read
|
2009-07-23 17:14:07 +00:00
|
|
|
* queries, or an integer >= 0 for a particular server.
|
2006-11-21 09:53:45 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string|string[] $groups Query groups. An array of group names that this query
|
2009-07-23 17:14:07 +00:00
|
|
|
* belongs to. May contain a single string if the query is only
|
|
|
|
|
* in one group.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string|bool $wiki The wiki ID, or false for the current wiki
|
2008-05-23 08:54:19 +00:00
|
|
|
*
|
2016-09-05 19:55:19 +00:00
|
|
|
* Note: multiple calls to wfGetDB(DB_REPLICA) during the course of one request
|
2008-07-04 06:47:47 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2016-05-01 19:29:11 +00:00
|
|
|
* @todo Replace calls to wfGetDB with calls to LoadBalancer::getConnection()
|
|
|
|
|
* on an injected instance of LoadBalancer.
|
|
|
|
|
*
|
2017-02-07 04:49:57 +00:00
|
|
|
* @return \Wikimedia\Rdbms\Database
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfGetDB( $db, $groups = [], $wiki = false ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a load balancer object.
|
|
|
|
|
*
|
2016-05-01 19:29:11 +00:00
|
|
|
* @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancer()
|
|
|
|
|
* or MediaWikiServices::getDBLoadBalancerFactory() instead.
|
|
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string|bool $wiki Wiki ID, or false for the current wiki
|
2017-02-18 00:26:47 +00:00
|
|
|
* @return \Wikimedia\Rdbms\LoadBalancer
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function wfGetLB( $wiki = false ) {
|
2016-05-01 19:29:11 +00:00
|
|
|
if ( $wiki === false ) {
|
2017-05-23 04:28:41 +00:00
|
|
|
return MediaWikiServices::getInstance()->getDBLoadBalancer();
|
2016-05-01 19:29:11 +00:00
|
|
|
} else {
|
2017-05-23 04:28:41 +00:00
|
|
|
$factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
2016-05-01 19:29:11 +00:00
|
|
|
return $factory->getMainLB( $wiki );
|
|
|
|
|
}
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the load balancer factory object
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2016-05-01 19:29:11 +00:00
|
|
|
* @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead.
|
|
|
|
|
*
|
2017-02-18 00:26:47 +00:00
|
|
|
* @return \Wikimedia\Rdbms\LBFactory
|
2006-11-21 09:53:45 +00:00
|
|
|
*/
|
2014-12-10 01:34:48 +00:00
|
|
|
function wfGetLBFactory() {
|
2017-05-23 04:28:41 +00:00
|
|
|
return MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
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
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param string $title String or Title object
|
2015-03-05 01:02:05 +00:00
|
|
|
* @param array $options Associative array of options (see RepoGroup::findFile)
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return File|bool File, or false if the file does not exist
|
2007-05-30 21:02:32 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfFindFile( $title, $options = [] ) {
|
2009-08-15 09:59:59 +00:00
|
|
|
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
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param Title|string $title
|
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
|
|
|
}
|
|
|
|
|
|
2007-07-06 03:55:36 +00:00
|
|
|
/**
|
|
|
|
|
* Should low-performance queries be disabled?
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return bool
|
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
|
2015-10-07 16:47:25 +00:00
|
|
|
* extensions; this is a wrapper around $wgScriptPath 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
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $script Script filename, sans extension
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return string
|
2007-07-06 03:41:04 +00:00
|
|
|
*/
|
|
|
|
|
function wfScript( $script = 'index' ) {
|
2015-10-07 16:47:25 +00:00
|
|
|
global $wgScriptPath, $wgScript, $wgLoadScript;
|
2012-08-21 19:38:00 +00:00
|
|
|
if ( $script === 'index' ) {
|
|
|
|
|
return $wgScript;
|
2013-04-17 14:52:47 +00:00
|
|
|
} elseif ( $script === 'load' ) {
|
2012-08-21 19:38:00 +00:00
|
|
|
return $wgLoadScript;
|
|
|
|
|
} else {
|
2015-10-07 16:47:25 +00:00
|
|
|
return "{$wgScriptPath}/{$script}.php";
|
2012-08-21 19:38:00 +00:00
|
|
|
}
|
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
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @return string Script URL
|
2009-07-15 22:41:56 +00:00
|
|
|
*/
|
2010-08-14 13:31:10 +00:00
|
|
|
function wfGetScriptUrl() {
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( isset( $_SERVER['SCRIPT_NAME'] ) ) {
|
2015-10-14 07:40:50 +00:00
|
|
|
/* 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.
|
|
|
|
|
*/
|
2009-07-15 22:41:56 +00:00
|
|
|
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
|
|
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param bool $value
|
|
|
|
|
* @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() {
|
2013-10-04 13:43:09 +00:00
|
|
|
return wfIsWindows() ? 'NUL' : '/dev/null';
|
2007-11-19 15:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
2008-03-18 13:18:06 +00:00
|
|
|
/**
|
2016-09-05 20:21:26 +00:00
|
|
|
* Waits for the replica DBs to catch up to the master position
|
2014-10-21 20:37:54 +00:00
|
|
|
*
|
|
|
|
|
* Use this when updating very large numbers of rows, as in maintenance scripts,
|
2016-09-05 20:21:26 +00:00
|
|
|
* to avoid causing too much lag. Of course, this is a no-op if there are no replica DBs.
|
2014-10-21 20:37:54 +00:00
|
|
|
*
|
|
|
|
|
* By default this waits on the main DB cluster of the current wiki.
|
|
|
|
|
* If $cluster is set to "*" it will wait on all DB clusters, including
|
|
|
|
|
* external ones. If the lag being waiting on is caused by the code that
|
|
|
|
|
* does this check, it makes since to use $ifWritesSince, particularly if
|
|
|
|
|
* cluster is "*", to avoid excess overhead.
|
|
|
|
|
*
|
2014-10-22 17:32:48 +00:00
|
|
|
* Never call this function after a big DB write that is still in a transaction.
|
2014-10-21 20:37:54 +00:00
|
|
|
* This only makes sense after the possible lag inducing changes were committed.
|
2011-06-30 02:59:43 +00:00
|
|
|
*
|
2014-07-19 22:19:36 +00:00
|
|
|
* @param float|null $ifWritesSince Only wait if writes were done since this UNIX timestamp
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string|bool $wiki Wiki identifier accepted by wfGetLB
|
2013-10-04 13:43:09 +00:00
|
|
|
* @param string|bool $cluster Cluster name accepted by LBFactory. Default: false.
|
2014-09-20 20:53:16 +00:00
|
|
|
* @param int|null $timeout Max wait time. Default: 1 day (cli), ~10 seconds (web)
|
2014-08-01 06:11:34 +00:00
|
|
|
* @return bool Success (able to connect and no timeouts reached)
|
2015-12-22 19:21:27 +00:00
|
|
|
* @deprecated since 1.27 Use LBFactory::waitForReplication
|
2008-03-18 13:18:06 +00:00
|
|
|
*/
|
2014-09-20 20:53:16 +00:00
|
|
|
function wfWaitForSlaves(
|
|
|
|
|
$ifWritesSince = null, $wiki = false, $cluster = false, $timeout = null
|
|
|
|
|
) {
|
|
|
|
|
if ( $timeout === null ) {
|
|
|
|
|
$timeout = ( PHP_SAPI === 'cli' ) ? 86400 : 10;
|
|
|
|
|
}
|
2014-07-19 22:19:36 +00:00
|
|
|
|
2014-10-21 20:37:54 +00:00
|
|
|
if ( $cluster === '*' ) {
|
2015-12-22 19:21:27 +00:00
|
|
|
$cluster = false;
|
|
|
|
|
$wiki = false;
|
|
|
|
|
} elseif ( $wiki === false ) {
|
|
|
|
|
$wiki = wfWikiID();
|
2014-10-22 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-22 19:21:27 +00:00
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
wfGetLBFactory()->waitForReplication( [
|
2015-12-22 19:21:27 +00:00
|
|
|
'wiki' => $wiki,
|
|
|
|
|
'cluster' => $cluster,
|
|
|
|
|
'timeout' => $timeout,
|
|
|
|
|
// B/C: first argument used to be "max seconds of lag"; ignore such values
|
|
|
|
|
'ifWritesSince' => ( $ifWritesSince > 1e9 ) ? $ifWritesSince : null
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-12-22 19:21:27 +00:00
|
|
|
} catch ( DBReplicationWaitError $e ) {
|
|
|
|
|
return false;
|
2011-04-19 14:52:11 +00:00
|
|
|
}
|
2014-08-01 06:11:34 +00:00
|
|
|
|
2015-12-22 19:21:27 +00:00
|
|
|
return true;
|
2011-04-19 14:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
2009-06-09 17:04:16 +00:00
|
|
|
/**
|
2014-03-25 18:52:26 +00:00
|
|
|
* Count down from $seconds 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.
|
2014-03-25 18:52:26 +00:00
|
|
|
*
|
2011-02-06 22:14:32 +00:00
|
|
|
* @codeCoverageIgnore
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param int $seconds
|
2009-06-09 17:04:16 +00:00
|
|
|
*/
|
2014-03-25 18:52:26 +00:00
|
|
|
function wfCountDown( $seconds ) {
|
|
|
|
|
for ( $i = $seconds; $i >= 0; $i-- ) {
|
|
|
|
|
if ( $i != $seconds ) {
|
2009-06-09 17:04:16 +00:00
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-10 08:16:58 +00:00
|
|
|
/**
|
2016-07-24 19:44:35 +00:00
|
|
|
* Replace all invalid characters with '-'.
|
|
|
|
|
* Additional characters can be defined in $wgIllegalFileChars (see T22489).
|
|
|
|
|
* By default, $wgIllegalFileChars includes ':', '/', '\'.
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $name Filename to process
|
|
|
|
|
* @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 . "]" : '';
|
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
|
|
|
|
|
);
|
2016-07-24 19:44:35 +00:00
|
|
|
// $wgIllegalFileChars may not include '/' and '\', so we still need to do this
|
|
|
|
|
$name = wfBaseName( $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
|
|
|
/**
|
2015-08-08 00:08:33 +00:00
|
|
|
* Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2015-08-17 15:42:10 +00:00
|
|
|
* @return int Resulting value of the memory limit.
|
2009-08-05 01:33:18 +00:00
|
|
|
*/
|
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' ) );
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $memlimit != -1 ) {
|
2010-11-19 06:49:15 +00:00
|
|
|
$conflimit = wfShorthandToInteger( $wgMemoryLimit );
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $conflimit == -1 ) {
|
2009-08-05 01:33:18 +00:00
|
|
|
wfDebug( "Removing PHP's memory limit\n" );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2010-08-14 13:31:10 +00:00
|
|
|
ini_set( 'memory_limit', $conflimit );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2009-08-05 01:33:18 +00:00
|
|
|
return $conflimit;
|
2009-08-05 02:34:41 +00:00
|
|
|
} elseif ( $conflimit > $memlimit ) {
|
|
|
|
|
wfDebug( "Raising PHP's memory limit to $conflimit bytes\n" );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2010-08-14 13:31:10 +00:00
|
|
|
ini_set( 'memory_limit', $conflimit );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2009-08-05 02:34:41 +00:00
|
|
|
return $conflimit;
|
2009-08-05 01:33:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $memlimit;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-08 00:08:33 +00:00
|
|
|
/**
|
|
|
|
|
* Set PHP's time limit to the larger of php.ini or $wgTransactionalTimeLimit
|
|
|
|
|
*
|
|
|
|
|
* @return int Prior time limit
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
function wfTransactionalTimeLimit() {
|
|
|
|
|
global $wgTransactionalTimeLimit;
|
|
|
|
|
|
|
|
|
|
$timeLimit = ini_get( 'max_execution_time' );
|
|
|
|
|
// Note that CLI scripts use 0
|
|
|
|
|
if ( $timeLimit > 0 && $wgTransactionalTimeLimit > $timeLimit ) {
|
|
|
|
|
set_time_limit( $wgTransactionalTimeLimit );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ignore_user_abort( true ); // ignore client disconnects
|
|
|
|
|
|
|
|
|
|
return $timeLimit;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-05 01:33:18 +00:00
|
|
|
/**
|
|
|
|
|
* Converts shorthand byte notation to integer form
|
2011-04-23 13:55:27 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $string
|
2015-09-08 17:59:44 +00:00
|
|
|
* @param int $default Returned if $string is empty
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return int
|
2009-08-05 01:33:18 +00:00
|
|
|
*/
|
2015-09-08 17:59:44 +00:00
|
|
|
function wfShorthandToInteger( $string = '', $default = -1 ) {
|
2010-08-14 13:31:10 +00:00
|
|
|
$string = trim( $string );
|
2013-04-17 14:52:47 +00:00
|
|
|
if ( $string === '' ) {
|
2015-09-08 17:59:44 +00:00
|
|
|
return $default;
|
2010-08-14 13:31:10 +00:00
|
|
|
}
|
2010-11-19 06:49:15 +00:00
|
|
|
$last = $string[strlen( $string ) - 1];
|
2010-08-14 13:31:10 +00:00
|
|
|
$val = intval( $string );
|
2013-04-26 14:42:31 +00:00
|
|
|
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
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $code The language code.
|
2014-03-25 18:52:26 +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 );
|
2016-02-17 09:09:32 +00:00
|
|
|
$codeBCP = [];
|
2009-07-24 17:41:01 +00:00
|
|
|
foreach ( $codeSegment as $segNo => $seg ) {
|
2013-06-30 19:51:58 +00:00
|
|
|
// when previous segment is x, it is a private segment and should be lc
|
|
|
|
|
if ( $segNo > 0 && strtolower( $codeSegment[( $segNo - 1 )] ) == 'x' ) {
|
|
|
|
|
$codeBCP[$segNo] = strtolower( $seg );
|
|
|
|
|
// ISO 3166 country code
|
|
|
|
|
} elseif ( ( strlen( $seg ) == 2 ) && ( $segNo > 0 ) ) {
|
|
|
|
|
$codeBCP[$segNo] = strtoupper( $seg );
|
|
|
|
|
// ISO 15924 script code
|
|
|
|
|
} elseif ( ( strlen( $seg ) == 4 ) && ( $segNo > 0 ) ) {
|
|
|
|
|
$codeBCP[$segNo] = ucfirst( strtolower( $seg ) );
|
|
|
|
|
// Use lowercase for other cases
|
2009-07-24 18:35:09 +00:00
|
|
|
} else {
|
2009-07-25 07:23:03 +00:00
|
|
|
$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
|
|
|
/**
|
2015-08-24 21:21:56 +00:00
|
|
|
* Get a specific cache object.
|
* 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
|
|
|
*
|
2015-08-24 21:21:56 +00:00
|
|
|
* @param int|string $cacheType A CACHE_* constants, or other key in $wgObjectCaches
|
* 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
|
|
|
|
|
*/
|
2015-08-24 21:21:56 +00:00
|
|
|
function wfGetCache( $cacheType ) {
|
|
|
|
|
return ObjectCache::getInstance( $cacheType );
|
* 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
|
|
|
}
|
|
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 16:11:40 +00:00
|
|
|
/**
|
|
|
|
|
* Call hook functions defined in $wgHooks
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $event Event name
|
|
|
|
|
* @param array $args Parameters passed to hook functions
|
|
|
|
|
* @param string|null $deprecatedVersion Optionally mark hook as deprecated with version number
|
2014-01-08 21:32:21 +00:00
|
|
|
*
|
2014-03-25 18:52:26 +00:00
|
|
|
* @return bool True if no handler aborted the hook
|
2016-08-02 21:53:25 +00:00
|
|
|
* @deprecated since 1.25 - use Hooks::run
|
2011-05-28 16:11:40 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
function wfRunHooks( $event, array $args = [], $deprecatedVersion = null ) {
|
2014-01-08 21:32:21 +00:00
|
|
|
return Hooks::run( $event, $args, $deprecatedVersion );
|
2011-05-28 16:11:40 +00:00
|
|
|
}
|
2011-10-24 02:19:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper around php's unpack.
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $format The format string (See php's docs)
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param string $data A binary string of binary data
|
|
|
|
|
* @param int|bool $length The minimum length of $data or false. This is to
|
2011-10-24 02:19:11 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @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
|
|
|
*/
|
2013-04-13 11:36:24 +00:00
|
|
|
function wfUnpack( $format, $data, $length = false ) {
|
2011-10-24 02:19:11 +00:00
|
|
|
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."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\suppressWarnings();
|
2011-10-24 02:19:11 +00:00
|
|
|
$result = unpack( $format, $data );
|
2015-06-10 18:29:05 +00:00
|
|
|
MediaWiki\restoreWarnings();
|
2011-10-24 02:19:11 +00:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $name The image name to check
|
2014-03-25 18:52:26 +00:00
|
|
|
* @param Title|bool $contextTitle The page on which the image occurs, if known
|
2014-04-23 09:41:35 +00:00
|
|
|
* @param string $blacklist Wikitext of a file blacklist
|
2012-04-06 23:42:39 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
|
2015-09-27 05:14:41 +00:00
|
|
|
# Handle redirects; callers almost always hit wfFindFile() anyway,
|
|
|
|
|
# so just use that method because it has a fast process cache.
|
|
|
|
|
$file = wfFindFile( $name ); // get the final name
|
|
|
|
|
$name = $file ? $file->getTitle()->getDBkey() : $name;
|
2012-04-06 23:42:39 +00:00
|
|
|
|
|
|
|
|
# Run the extension hook
|
|
|
|
|
$bad = false;
|
2016-02-17 09:09:32 +00:00
|
|
|
if ( !Hooks::run( 'BadImage', [ $name, &$bad ] ) ) {
|
2016-10-16 20:18:05 +00:00
|
|
|
return (bool)$bad;
|
2012-04-06 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-31 18:42:48 +00:00
|
|
|
$cache = ObjectCache::getLocalServerInstance( 'hash' );
|
2017-05-25 19:26:19 +00:00
|
|
|
$key = $cache->makeKey(
|
|
|
|
|
'bad-image-list', ( $blacklist === null ) ? 'default' : md5( $blacklist )
|
|
|
|
|
);
|
2015-09-25 04:59:25 +00:00
|
|
|
$badImages = $cache->get( $key );
|
|
|
|
|
|
|
|
|
|
if ( $badImages === false ) { // cache miss
|
2012-04-06 23:42:39 +00:00
|
|
|
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
|
2016-02-17 09:09:32 +00:00
|
|
|
$badImages = [];
|
2012-04-06 23:42:39 +00:00
|
|
|
$lines = explode( "\n", $blacklist );
|
2013-04-17 14:52:47 +00:00
|
|
|
foreach ( $lines as $line ) {
|
2012-04-06 23:42:39 +00:00
|
|
|
# List items only
|
|
|
|
|
if ( substr( $line, 0, 1 ) !== '*' ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Find all links
|
2016-02-17 09:09:32 +00:00
|
|
|
$m = [];
|
2012-04-06 23:42:39 +00:00
|
|
|
if ( !preg_match_all( '/\[\[:?(.*?)\]\]/', $line, $m ) ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$exceptions = [];
|
2012-04-06 23:42:39 +00:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-25 04:59:25 +00:00
|
|
|
$cache->set( $key, $badImages, 60 );
|
2012-04-06 23:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
|
|
|
|
|
$bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
|
2015-09-25 04:59:25 +00:00
|
|
|
|
2012-04-06 23:42:39 +00:00
|
|
|
return $bad;
|
|
|
|
|
}
|
2013-08-21 01:39:45 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-08-24 15:06:25 +00:00
|
|
|
* Determine whether the client at a given source IP is likely to be able to
|
|
|
|
|
* access the wiki via HTTPS.
|
2013-08-21 01:39:45 +00:00
|
|
|
*
|
|
|
|
|
* @param string $ip The IPv4/6 address in the normal human-readable form
|
2014-04-23 09:41:35 +00:00
|
|
|
* @return bool
|
2013-08-21 01:39:45 +00:00
|
|
|
*/
|
|
|
|
|
function wfCanIPUseHTTPS( $ip ) {
|
|
|
|
|
$canDo = true;
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'CanIPUseHTTPS', [ $ip, &$canDo ] );
|
2013-08-21 01:39:45 +00:00
|
|
|
return !!$canDo;
|
|
|
|
|
}
|
2013-12-16 02:07:04 +00:00
|
|
|
|
2014-06-18 02:45:32 +00:00
|
|
|
/**
|
|
|
|
|
* Determine input string is represents as infinity
|
|
|
|
|
*
|
|
|
|
|
* @param string $str The string to determine
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.25
|
|
|
|
|
*/
|
|
|
|
|
function wfIsInfinity( $str ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ];
|
2014-06-18 02:45:32 +00:00
|
|
|
return in_array( $str, $infinityValues );
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 21:01:52 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if these thumbnail parameters match one that MediaWiki
|
|
|
|
|
* requests from file description pages and/or parser output.
|
|
|
|
|
*
|
|
|
|
|
* $params is considered non-standard if they involve a non-standard
|
|
|
|
|
* width or any non-default parameters aside from width and page number.
|
|
|
|
|
* The number of possible files with standard parameters is far less than
|
|
|
|
|
* that of all combinations; rate-limiting for them can thus be more generious.
|
|
|
|
|
*
|
|
|
|
|
* @param File $file
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @return bool
|
|
|
|
|
* @since 1.24 Moved from thumb.php to GlobalFunctions in 1.25
|
|
|
|
|
*/
|
|
|
|
|
function wfThumbIsStandard( File $file, array $params ) {
|
2015-02-03 20:27:05 +00:00
|
|
|
global $wgThumbLimits, $wgImageLimits, $wgResponsiveImages;
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$multipliers = [ 1 ];
|
2015-02-03 20:27:05 +00:00
|
|
|
if ( $wgResponsiveImages ) {
|
|
|
|
|
// These available sizes are hardcoded currently elsewhere in MediaWiki.
|
|
|
|
|
// @see Linker::processResponsiveImages
|
|
|
|
|
$multipliers[] = 1.5;
|
|
|
|
|
$multipliers[] = 2;
|
|
|
|
|
}
|
2015-02-04 21:01:52 +00:00
|
|
|
|
|
|
|
|
$handler = $file->getHandler();
|
|
|
|
|
if ( !$handler || !isset( $params['width'] ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$basicParams = [];
|
2015-02-04 21:01:52 +00:00
|
|
|
if ( isset( $params['page'] ) ) {
|
|
|
|
|
$basicParams['page'] = $params['page'];
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$thumbLimits = [];
|
|
|
|
|
$imageLimits = [];
|
2015-02-03 20:27:05 +00:00
|
|
|
// Expand limits to account for multipliers
|
|
|
|
|
foreach ( $multipliers as $multiplier ) {
|
|
|
|
|
$thumbLimits = array_merge( $thumbLimits, array_map(
|
|
|
|
|
function ( $width ) use ( $multiplier ) {
|
|
|
|
|
return round( $width * $multiplier );
|
|
|
|
|
}, $wgThumbLimits )
|
|
|
|
|
);
|
|
|
|
|
$imageLimits = array_merge( $imageLimits, array_map(
|
|
|
|
|
function ( $pair ) use ( $multiplier ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2015-02-03 20:27:05 +00:00
|
|
|
round( $pair[0] * $multiplier ),
|
|
|
|
|
round( $pair[1] * $multiplier ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-02-03 20:27:05 +00:00
|
|
|
}, $wgImageLimits )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 21:01:52 +00:00
|
|
|
// Check if the width matches one of $wgThumbLimits
|
2015-02-03 20:27:05 +00:00
|
|
|
if ( in_array( $params['width'], $thumbLimits ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$normalParams = $basicParams + [ 'width' => $params['width'] ];
|
2015-02-04 21:01:52 +00:00
|
|
|
// Append any default values to the map (e.g. "lossy", "lossless", ...)
|
|
|
|
|
$handler->normaliseParams( $file, $normalParams );
|
|
|
|
|
} else {
|
|
|
|
|
// If not, then check if the width matchs one of $wgImageLimits
|
|
|
|
|
$match = false;
|
2015-02-03 20:27:05 +00:00
|
|
|
foreach ( $imageLimits as $pair ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$normalParams = $basicParams + [ 'width' => $pair[0], 'height' => $pair[1] ];
|
2015-02-04 21:01:52 +00:00
|
|
|
// Decide whether the thumbnail should be scaled on width or height.
|
|
|
|
|
// Also append any default values to the map (e.g. "lossy", "lossless", ...)
|
|
|
|
|
$handler->normaliseParams( $file, $normalParams );
|
|
|
|
|
// Check if this standard thumbnail size maps to the given width
|
|
|
|
|
if ( $normalParams['width'] == $params['width'] ) {
|
|
|
|
|
$match = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !$match ) {
|
|
|
|
|
return false; // not standard for description pages
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the given values for non-page, non-width, params are just defaults
|
|
|
|
|
foreach ( $params as $key => $value ) {
|
|
|
|
|
if ( !isset( $normalParams[$key] ) || $normalParams[$key] != $value ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-08-27 17:49:50 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
|
|
|
|
|
*
|
|
|
|
|
* Values that exist in both values will be combined with += (all values of the array
|
|
|
|
|
* of $newValues will be added to the values of the array of $baseArray, while values,
|
|
|
|
|
* that exists in both, the value of $baseArray will be used).
|
|
|
|
|
*
|
|
|
|
|
* @param array $baseArray The array where you want to add the values of $newValues to
|
|
|
|
|
* @param array $newValues An array with new values
|
|
|
|
|
* @return array The combined array
|
|
|
|
|
* @since 1.26
|
|
|
|
|
*/
|
|
|
|
|
function wfArrayPlus2d( array $baseArray, array $newValues ) {
|
|
|
|
|
// First merge items that are in both arrays
|
|
|
|
|
foreach ( $baseArray as $name => &$groupVal ) {
|
|
|
|
|
if ( isset( $newValues[$name] ) ) {
|
|
|
|
|
$groupVal += $newValues[$name];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Now add items that didn't exist yet
|
|
|
|
|
$baseArray += $newValues;
|
|
|
|
|
|
|
|
|
|
return $baseArray;
|
|
|
|
|
}
|