mediawiki-extensions-Linter/includes/ApiRecordLint.php
Kunal Mehta e41e8e32a4 Drop excess events at the API layer
Originally we were dropping excess events inside the job queue, but that means
all of the events need to be passed into the job queue...which can cause
problems.

So drop them in the API module. The only other place we construct
RecordLintJob is when an article has been deleted, and those jobs have
no errors since they're all being deleted.

Bug: T202179
Change-Id: I61940280e0dfb99398d9f047d0e66007d91a0241
2018-08-17 20:36:06 -07:00

117 lines
3.2 KiB
PHP

<?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 ApiBase;
use FormatJson;
use JobQueueGroup;
use MediaWiki\Logger\LoggerFactory;
use Title;
use Wikimedia\IPSet;
/**
* API module for an external service to record
* a lint error
*/
class ApiRecordLint extends ApiBase {
public function execute() {
global $wgLinterSubmitterWhitelist;
$ipSet = new IPSet(
array_keys( array_filter( $wgLinterSubmitterWhitelist ) )
);
if ( !$ipSet->match( $this->getRequest()->getIP() ) ) {
$this->dieWithError( 'apierror-linter-invalid-ip', 'invalid-ip' );
}
$params = $this->extractRequestParams();
$data = FormatJson::decode( $params['data'], true );
if ( !is_array( $data ) ) {
$this->dieWithError( 'apierror-linter-invalid-data', 'invalid-data' );
}
$errors = [];
$title = Title::newFromText( $params['page'] );
if ( !$title || !$title->getArticleID()
|| $title->getLatestRevID() != $params['revision']
) {
$this->dieWithError( 'apierror-linter-invalid-title', 'invalid-title' );
}
$categoryMgr = new CategoryManager();
$catCounts = [];
foreach ( $data as $info ) {
if ( !$categoryMgr->isKnownCategory( $info['type'] ) ) {
continue;
}
$count = $catCounts[$info['type']] ?? 0;
if ( $count > Database::MAX_PER_CAT ) {
// Drop
continue;
}
$catCounts[$info['type']] = $count + 1;
if ( !isset( $info['dsr'] ) ) {
LoggerFactory::getInstance( 'Linter' )->warning(
'dsr for {page} @ rev {revid}, for lint: {lint} is missing',
[
'page' => $params['page'],
'revid' => $params['revision'],
'lint' => $info['type'],
]
);
continue;
}
$info['location'] = array_slice( $info['dsr'], 0, 2 );
if ( isset( $info['templateInfo'] ) && $info['templateInfo'] ) {
$info['params']['templateInfo'] = $info['templateInfo'];
}
$errors[] = $info;
}
$job = new RecordLintJob( $title, [
'errors' => $errors,
'revision' => $params['revision'],
] );
JobQueueGroup::singleton()->push( $job );
$this->getResult()->addValue( $this->getModuleName(), 'success', true );
}
public function isInternal() {
return true;
}
public function getAllowedParams() {
return [
'data' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'page' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'revision' => [
ApiBase::PARAM_TYPE => 'integer',
ApiBase::PARAM_REQUIRED => true,
],
];
}
}