From: Dayllan Maza Date: Fri, 16 Nov 2018 03:33:47 +0000 (-0500) Subject: Add stats for block errors on create/edit actions X-Git-Tag: 1.34.0-rc.0~3408^2 X-Git-Url: http://git.cyclocoop.org/data/%28%5B%5E/%7B%7B%20url_for%28%27admin_users%27%29%20%7D%7D?a=commitdiff_plain;h=b89342ffddae35af71c6dabc34c456fd5641c1df;p=lhc%2Fweb%2Fwiklou.git Add stats for block errors on create/edit actions Monitoring block errors is behind $wgEnableBlockNoticeStats and it is disabled by default. The reason behind this metric is to get an idea on how frequently blocked users attempt to edit a page. Similar tracking is being added to MobileFrontend and VisualEditor. Depends-On: I6bd1c95548616677e1f72ba6bcfc6f2b551c1ca6 Bug: T201717 Change-Id: I18bdb5ce61509ba3c2cea9aa6247656b9bd388e0 --- diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index c66e5d52bb..fbb474b3e0 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -21,6 +21,7 @@ */ use Wikimedia\Rdbms\IDatabase; +use MediaWiki\MediaWikiServices; /** * This abstract class implements many basic API functions, and is the base of @@ -2069,11 +2070,41 @@ abstract class ApiBase extends ContextSource { 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().