Handle some extra cases in triggerJobs()
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 28 Feb 2014 05:35:30 +0000 (21:35 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 28 Feb 2014 05:52:13 +0000 (05:52 +0000)
* $wgEnableAPI may be false, though it is a basic part of MediaWiki.
  This only disables api.php and not internal callers so just do that.
* Various socket functions may disabled. Though this is a terrible
  wack-a-mole security measure to handle MediaWiki exploits or
  non-authenticated services, it may still be in use, so work
  around it.

Change-Id: I54a209e003ffce1fe36a5898fe0da735cf5e32e4

includes/Wiki.php

index fb46d80..0f6a0b8 100644 (file)
@@ -621,7 +621,7 @@ class MediaWiki {
         * the socket once it's done.
         */
        protected function triggerJobs() {
-               global $wgJobRunRate, $wgServer, $wgScriptPath, $wgScriptExtension;
+               global $wgJobRunRate, $wgServer, $wgScriptPath, $wgScriptExtension, $wgEnableAPI;
 
                if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
                        return;
@@ -643,16 +643,30 @@ class MediaWiki {
                        'tasks' => 'jobs', 'maxjobs' => $n, 'sigexpiry' => time() + 5 );
                $query['signature'] = ApiRunJobs::getQuerySignature( $query );
 
+               // Slow job running method in case of API or socket functions being disabled
+               $fallback = function() use ( $query ) {
+                       $api = new ApiMain( new FauxRequest( $query, true ) );
+                       $api->execute();
+               };
+
+               if ( !$wgEnableAPI ) {
+                       $fallback();
+                       return;
+               }
+
                $errno = $errstr = null;
                $info = wfParseUrl( $wgServer );
+               wfSuppressWarnings();
                $sock = fsockopen(
                        $info['host'],
                        isset( $info['port'] ) ? $info['port'] : 80,
                        $errno,
                        $errstr
                );
+               wfRestoreWarnings();
                if ( !$sock ) {
                        wfDebugLog( 'runJobs', "Failed to start cron API (socket error $errno): $errstr\n" );
+                       $fallback();
                        return;
                }
 
@@ -661,7 +675,7 @@ class MediaWiki {
 
                wfDebugLog( 'runJobs', "Running $n job(s) via '$url'\n" );
                // Send a cron API request to be performed in the background.
-               // Give up if this takes to long to send (which should be rare).
+               // Give up if this takes too long to send (which should be rare).
                stream_set_timeout( $sock, 1 );
                $bytes = fwrite( $sock, $req );
                if ( $bytes !== strlen( $req ) ) {