+/**
+ * Should low-performance queries be disabled?
+ *
+ * @return bool
+ */
+function wfQueriesMustScale() {
+ global $wgMiserMode;
+ return $wgMiserMode
+ || ( SiteStats::pages() > 100000
+ && SiteStats::edits() > 1000000
+ && SiteStats::users() > 10000 );
+}
+
+/**
+ * Get the path to a specified script file, respecting file
+ * extensions; this is a wrapper around $wgScriptExtension etc.
+ *
+ * @param string $script Script filename, sans extension
+ * @return string
+ */
+function wfScript( $script = 'index' ) {
+ global $wgScriptPath, $wgScriptExtension;
+ return "{$wgScriptPath}/{$script}{$wgScriptExtension}";
+}
+
+/**
+ * Convenience function converts boolean values into "true"
+ * or "false" (string) values
+ *
+ * @param bool $value
+ * @return string
+ */
+function wfBoolToStr( $value ) {
+ return $value ? 'true' : 'false';
+}
+
+/**
+ * Load an extension messages file
+ *
+ * @param string $extensionName Name of extension to load messages from\for.
+ * @param string $langcode Language to load messages for, or false for default
+ * behvaiour (en, content language and user language).
+ */
+function wfLoadExtensionMessages( $extensionName, $langcode = false ) {
+ global $wgExtensionMessagesFiles, $wgMessageCache, $wgLang, $wgContLang;
+
+ #For recording whether extension message files have been loaded in a given language.
+ static $loaded = array();
+
+ if( !array_key_exists( $extensionName, $loaded ) ) {
+ $loaded[$extensionName] = array();
+ }
+
+ if( !$langcode && !array_key_exists( '*', $loaded[$extensionName] ) ) {
+ # Just do en, content language and user language.
+ $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], false );
+ # Mark that they have been loaded.
+ $loaded[$extensionName]['en'] = true;
+ $loaded[$extensionName][$wgLang->getCode()] = true;
+ $loaded[$extensionName][$wgContLang->getCode()] = true;
+ # Mark that this part has been done to avoid weird if statements.
+ $loaded[$extensionName]['*'] = true;
+ } elseif( is_string( $langcode ) && !array_key_exists( $langcode, $loaded[$extensionName] ) ) {
+ # Load messages for specified language.
+ $wgMessageCache->loadMessagesFile( $wgExtensionMessagesFiles[$extensionName], $langcode );
+ # Mark that they have been loaded.
+ $loaded[$extensionName][$langcode] = true;
+ }
+}
+
+/**
+ * Get a platform-independent path to the null file, e.g.
+ * /dev/null
+ *
+ * @return string
+ */
+function wfGetNull() {
+ return wfIsWindows()
+ ? 'NUL'
+ : '/dev/null';
+}
+
+/**
+ * Displays a maxlag error
+ *
+ * @param string $host Server that lags the most
+ * @param int $lag Maxlag (actual)
+ * @param int $maxLag Maxlag (requested)
+ */
+function wfMaxlagError( $host, $lag, $maxLag ) {
+ global $wgShowHostnames;
+ header( 'HTTP/1.1 503 Service Unavailable' );
+ header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
+ header( 'X-Database-Lag: ' . intval( $lag ) );
+ header( 'Content-Type: text/plain' );
+ if( $wgShowHostnames ) {
+ echo "Waiting for $host: $lag seconds lagged\n";
+ } else {
+ echo "Waiting for a database server: $lag seconds lagged\n";
+ }
+}
+
+/**
+ * Throws an E_USER_NOTICE saying that $function is deprecated
+ * @param string $function
+ * @return null
+ */
+function wfDeprecated( $function ) {
+ global $wgDebugLogFile;
+ if ( !$wgDebugLogFile ) {
+ return;
+ }
+ $callers = wfDebugBacktrace();
+ if( isset( $callers[2] ) ){
+ $callerfunc = $callers[2];
+ $callerfile = $callers[1];
+ if( isset( $callerfile['file'] ) && isset( $callerfile['line'] ) ){
+ $file = $callerfile['file'] . ' at line ' . $callerfile['line'];
+ } else {
+ $file = '(internal function)';
+ }
+ $func = '';
+ if( isset( $callerfunc['class'] ) )
+ $func .= $callerfunc['class'] . '::';
+ $func .= @$callerfunc['function'];
+ $msg = "Use of $function is deprecated. Called from $func in $file";
+ } else {
+ $msg = "Use of $function is deprecated.";
+ }
+ wfDebug( "$msg\n" );
+}
+
+/**
+ * Sleep until the worst slave's replication lag is less than or equal to
+ * $maxLag, in seconds. Use this when updating very large numbers of rows, as
+ * in maintenance scripts, to avoid causing too much lag. Of course, this is
+ * a no-op if there are no slaves.
+ *
+ * Every time the function has to wait for a slave, it will print a message to
+ * that effect (and then sleep for a little while), so it's probably not best
+ * to use this outside maintenance scripts in its present form.
+ *
+ * @param int $maxLag
+ * @return null
+ */
+function wfWaitForSlaves( $maxLag ) {
+ if( $maxLag ) {
+ $lb = wfGetLB();
+ list( $host, $lag ) = $lb->getMaxLag();
+ while( $lag > $maxLag ) {
+ $name = @gethostbyaddr( $host );
+ if( $name !== false ) {
+ $host = $name;
+ }
+ print "Waiting for $host (lagged $lag seconds)...\n";
+ sleep($maxLag);
+ list( $host, $lag ) = $lb->getMaxLag();
+ }
+ }
+}
+
+/** Generate a random 32-character hexadecimal token.
+ * @param mixed $salt Some sort of salt, if necessary, to add to random characters before hashing.
+ */
+ function wfGenerateToken( $salt = '' ) {
+ $salt = serialize($salt);
+
+ return md5( mt_rand( 0, 0x7fffffff ) . $salt );
+}