2016-11-15 19:31:45 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Linter;
|
|
|
|
|
|
|
|
use Job;
|
2016-12-09 00:05:45 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-11-15 19:31:45 +00:00
|
|
|
use Title;
|
|
|
|
|
|
|
|
class RecordLintJob extends Job {
|
|
|
|
/**
|
|
|
|
* RecordLintJob constructor.
|
|
|
|
* @param Title $title
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function __construct( Title $title, array $params ) {
|
|
|
|
parent::__construct( 'RecordLintJob', $title, $params );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run() {
|
2017-10-06 04:03:30 +00:00
|
|
|
if ( isset( $this->params['revision'] )
|
|
|
|
&& $this->title->getLatestRevID() != $this->params['revision']
|
|
|
|
) {
|
2016-11-15 19:31:45 +00:00
|
|
|
// Outdated now, let a later job handle it
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-18 03:36:06 +00:00
|
|
|
// [ 'id' => LintError ]
|
2016-11-15 19:31:45 +00:00
|
|
|
$errors = [];
|
2016-11-24 03:47:04 +00:00
|
|
|
foreach ( $this->params['errors'] as $errorInfo ) {
|
|
|
|
$error = new LintError(
|
|
|
|
$errorInfo['type'],
|
2016-12-01 02:47:05 +00:00
|
|
|
$errorInfo['location'],
|
2016-11-24 03:47:04 +00:00
|
|
|
$errorInfo['params']
|
2016-11-15 19:31:45 +00:00
|
|
|
);
|
2016-11-24 03:47:04 +00:00
|
|
|
// Use unique id as key to get rid of exact dupes
|
|
|
|
// (e.g. same category of error in same template)
|
2018-08-18 03:36:06 +00:00
|
|
|
$errors[$error->id()] = $error;
|
2016-11-15 19:31:45 +00:00
|
|
|
}
|
|
|
|
$lintDb = new Database( $this->title->getArticleID() );
|
2018-08-18 03:36:06 +00:00
|
|
|
$changes = $lintDb->setForPage( $errors );
|
2017-04-28 20:04:45 +00:00
|
|
|
$this->updateStats( $lintDb, $changes );
|
2016-12-09 00:05:45 +00:00
|
|
|
|
2016-11-15 19:31:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-09 00:05:45 +00:00
|
|
|
/**
|
2017-04-28 20:04:45 +00:00
|
|
|
* Send stats to statsd and update totals cache
|
2016-12-09 00:05:45 +00:00
|
|
|
*
|
|
|
|
* @param Database $lintDb
|
2017-04-28 20:04:45 +00:00
|
|
|
* @param array $changes
|
2016-12-09 00:05:45 +00:00
|
|
|
*/
|
2017-04-28 20:04:45 +00:00
|
|
|
protected function updateStats( Database $lintDb, array $changes ) {
|
2016-12-09 00:05:45 +00:00
|
|
|
global $wgLinterStatsdSampleFactor;
|
|
|
|
|
2017-04-28 20:04:45 +00:00
|
|
|
$mwServices = MediaWikiServices::getInstance();
|
|
|
|
|
|
|
|
$totalsLookup = new TotalsLookup(
|
|
|
|
new CategoryManager(),
|
|
|
|
$mwServices->getMainWANObjectCache()
|
|
|
|
);
|
|
|
|
|
2016-12-09 00:05:45 +00:00
|
|
|
if ( $wgLinterStatsdSampleFactor === false ) {
|
2017-04-28 20:04:45 +00:00
|
|
|
// Don't send to statsd, but update cache with $changes
|
|
|
|
$raw = $changes['added'];
|
|
|
|
foreach ( $changes['deleted'] as $cat => $count ) {
|
|
|
|
if ( isset( $raw[$cat] ) ) {
|
|
|
|
$raw[$cat] -= $count;
|
|
|
|
} else {
|
|
|
|
// Negative value
|
|
|
|
$raw[$cat] = 0 - $count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $raw as $cat => $count ) {
|
|
|
|
if ( $count != 0 ) {
|
|
|
|
// There was a change in counts, invalidate the cache
|
|
|
|
$totalsLookup->touchCategoryCache( $cat );
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 00:05:45 +00:00
|
|
|
return;
|
|
|
|
} elseif ( mt_rand( 1, $wgLinterStatsdSampleFactor ) != 1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$totals = $lintDb->getTotals();
|
2017-04-06 18:21:41 +00:00
|
|
|
$wiki = wfWikiID();
|
2016-12-09 00:05:45 +00:00
|
|
|
|
2017-04-28 20:04:45 +00:00
|
|
|
$stats = $mwServices->getStatsdDataFactory();
|
2016-12-09 00:05:45 +00:00
|
|
|
foreach ( $totals as $name => $count ) {
|
2017-04-06 18:21:41 +00:00
|
|
|
$stats->gauge( "linter.category.$name.$wiki", $count );
|
2016-12-09 00:05:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 18:21:41 +00:00
|
|
|
$stats->gauge( "linter.totals.$wiki", array_sum( $totals ) );
|
2017-04-28 20:04:45 +00:00
|
|
|
|
|
|
|
$totalsLookup->touchAllCategoriesCache();
|
2016-12-09 00:05:45 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:31:45 +00:00
|
|
|
}
|