Merge "Add stats for block errors on create/edit actions"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 29 Nov 2018 01:04:38 +0000 (01:04 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 29 Nov 2018 01:04:38 +0000 (01:04 +0000)
1  2 
includes/api/ApiBase.php

diff --combined includes/api/ApiBase.php
@@@ -21,6 -21,7 +21,7 @@@
   */
  
  use Wikimedia\Rdbms\IDatabase;
+ use MediaWiki\MediaWikiServices;
  
  /**
   * This abstract class implements many basic API functions, and is the base of
@@@ -271,7 -272,7 +272,7 @@@ abstract class ApiBase extends ContextS
        private $mMainModule;
        /** @var string */
        private $mModuleName, $mModulePrefix;
 -      private $mSlaveDB = null;
 +      private $mReplicaDB = null;
        private $mParamCache = [];
        /** @var array|null|bool */
        private $mModuleSource = false;
         * @return IDatabase
         */
        protected function getDB() {
 -              if ( !isset( $this->mSlaveDB ) ) {
 -                      $this->mSlaveDB = wfGetDB( DB_REPLICA, 'api' );
 +              if ( !isset( $this->mReplicaDB ) ) {
 +                      $this->mReplicaDB = wfGetDB( DB_REPLICA, 'api' );
                }
  
 -              return $this->mSlaveDB;
 +              return $this->mReplicaDB;
        }
  
        /**
                foreach ( (array)$actions as $action ) {
                        $errors = array_merge( $errors, $title->getUserPermissionsErrors( $action, $user ) );
                }
                if ( $errors ) {
+                       // track block notices
+                       if ( $this->getConfig()->get( 'EnableBlockNoticeStats' ) ) {
+                               $this->trackBlockNotices( $errors );
+                       }
                        $this->dieStatus( $this->errorArrayToStatus( $errors, $user ) );
                }
        }
  
+       /**
+        * Keep track of errors messages resulting from a block
+        *
+        * @param array $errors
+        */
+       private function trackBlockNotices( array $errors ) {
+               $errorMessageKeys = [
+                       'blockedtext',
+                       'blockedtext-partial',
+                       'autoblockedtext',
+                       'systemblockedtext',
+               ];
+               $statsd = MediaWikiServices::getInstance()->getStatsdDataFactory();
+               foreach ( $errors as $error ) {
+                       if ( in_array( $error[0], $errorMessageKeys ) ) {
+                               $wiki = $this->getConfig()->get( 'DBname' );
+                               $statsd->increment( 'BlockNotices.' . $wiki . '.MediaWikiApi.returned' );
+                               break;
+                       }
+               }
+       }
        /**
         * Will only set a warning instead of failing if the global $wgDebugAPI
         * is set to true. Otherwise behaves exactly as self::dieWithError().