Merge "phpunit: Avoid use of deprecated getMock for PHPUnit 5 compat"
[lhc/web/wiklou.git] / includes / MediaWiki.php
index cfe4965..ef0563e 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 use MediaWiki\Logger\LoggerFactory;
+use Psr\Log\LoggerInterface;
 use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\ChronologyProtector;
 use Wikimedia\Rdbms\LBFactory;
@@ -73,7 +74,7 @@ class MediaWiki {
                if ( $request->getCheck( 'search' ) ) {
                        // Compatibility with old search URLs which didn't use Special:Search
                        // Just check for presence here, so blank requests still
-                       // show the search page when using ugly URLs (bug 8054).
+                       // show the search page when using ugly URLs (T10054).
                        $ret = SpecialPage::getTitleFor( 'Search' );
                } elseif ( $curid ) {
                        // URLs like this are generated by RC, because rc_title isn't always accurate
@@ -183,7 +184,7 @@ class MediaWiki {
                $unused = null; // To pass it by reference
                Hooks::run( 'BeforeInitialize', [ &$title, &$unused, &$output, &$user, $request, $this ] );
 
-               // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
+               // Invalid titles. T23776: The interwikis must redirect even if the page name is empty.
                if ( is_null( $title ) || ( $title->getDBkey() == '' && !$title->isExternal() )
                        || $title->isSpecial( 'Badtitle' )
                ) {
@@ -203,7 +204,7 @@ class MediaWiki {
                        ? [] // relies on HMAC key signature alone
                        : $title->getUserPermissionsErrors( 'read', $user );
                if ( count( $permErrors ) ) {
-                       // Bug 32276: allowing the skin to generate output with $wgTitle or
+                       // T34276: allowing the skin to generate output with $wgTitle or
                        // $this->context->title set to the input title would allow anonymous users to
                        // determine whether a page exists, potentially leaking private data. In fact, the
                        // curid and oldid request  parameters would allow page titles to be enumerated even
@@ -520,7 +521,7 @@ class MediaWiki {
                        try {
                                $this->main();
                        } catch ( ErrorPageError $e ) {
-                               // Bug 62091: while exceptions are convenient to bubble up GUI errors,
+                               // T64091: while exceptions are convenient to bubble up GUI errors,
                                // they are not internal application faults. As with normal requests, this
                                // should commit, print the output, do deferred updates, jobs, and profiling.
                                $this->doPreOutputCommit();
@@ -942,24 +943,45 @@ class MediaWiki {
                        $n = intval( $jobRunRate );
                }
 
-               $runJobsLogger = LoggerFactory::getInstance( 'runJobs' );
+               $logger = LoggerFactory::getInstance( 'runJobs' );
 
-               // Fall back to running the job(s) while the user waits if needed
-               if ( !$this->config->get( 'RunJobsAsync' ) ) {
-                       $runner = new JobRunner( $runJobsLogger );
-                       $runner->run( [ 'maxJobs' => $n ] );
-                       return;
-               }
-
-               // Do not send request if there are probably no jobs
                try {
-                       $group = JobQueueGroup::singleton();
-                       if ( !$group->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
-                               return;
+                       if ( $this->config->get( 'RunJobsAsync' ) ) {
+                               // Send an HTTP request to the job RPC entry point if possible
+                               $invokedWithSuccess = $this->triggerAsyncJobs( $n, $logger );
+                               if ( !$invokedWithSuccess ) {
+                                       // Fall back to blocking on running the job(s)
+                                       $logger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
+                                       $this->triggerSyncJobs( $n, $logger );
+                               }
+                       } else {
+                               $this->triggerSyncJobs( $n, $logger );
                        }
                } catch ( JobQueueError $e ) {
+                       // Do not make the site unavailable (T88312)
                        MWExceptionHandler::logException( $e );
-                       return; // do not make the site unavailable
+               }
+       }
+
+       /**
+        * @param integer $n Number of jobs to try to run
+        * @param LoggerInterface $runJobsLogger
+        */
+       private function triggerSyncJobs( $n, LoggerInterface $runJobsLogger ) {
+               $runner = new JobRunner( $runJobsLogger );
+               $runner->run( [ 'maxJobs' => $n ] );
+       }
+
+       /**
+        * @param integer $n Number of jobs to try to run
+        * @param LoggerInterface $runJobsLogger
+        * @return bool Success
+        */
+       private function triggerAsyncJobs( $n, LoggerInterface $runJobsLogger ) {
+               // Do not send request if there are probably no jobs
+               $group = JobQueueGroup::singleton();
+               if ( !$group->queuesHaveJobs( JobQueueGroup::TYPE_DEFAULT ) ) {
+                       return true;
                }
 
                $query = [ 'title' => 'Special:RunJobs',
@@ -1026,12 +1048,6 @@ class MediaWiki {
                        $runJobsLogger->error( "Failed to start cron API (socket error $errno): $errstr" );
                }
 
-               // Fall back to running the job(s) while the user waits if needed
-               if ( !$invokedWithSuccess ) {
-                       $runJobsLogger->warning( "Jobs switched to blocking; Special:RunJobs disabled" );
-
-                       $runner = new JobRunner( $runJobsLogger );
-                       $runner->run( [ 'maxJobs'  => $n ] );
-               }
+               return $invokedWithSuccess;
        }
 }