2016-10-13 08:14:18 +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 ApiBase;
|
|
|
|
use FormatJson;
|
|
|
|
use IPSet\IPSet;
|
2016-11-15 19:31:45 +00:00
|
|
|
use JobQueueGroup;
|
2017-06-26 22:22:45 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2016-10-13 08:14:18 +00:00
|
|
|
use Title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() ) ) {
|
2017-12-03 01:27:08 +00:00
|
|
|
$this->dieWithError( 'apierror-linter-invalid-ip', 'invalid-ip' );
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$data = FormatJson::decode( $params['data'], true );
|
|
|
|
if ( !is_array( $data ) ) {
|
2017-12-03 01:27:08 +00:00
|
|
|
$this->dieWithError( 'apierror-linter-invalid-data', 'invalid-data' );
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$errors = [];
|
|
|
|
$title = Title::newFromText( $params['page'] );
|
|
|
|
if ( !$title || !$title->getArticleID()
|
|
|
|
|| $title->getLatestRevID() != $params['revision']
|
|
|
|
) {
|
2017-12-03 01:27:08 +00:00
|
|
|
$this->dieWithError( 'apierror-linter-invalid-title', 'invalid-title' );
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
2016-11-22 09:03:02 +00:00
|
|
|
$categoryMgr = new CategoryManager();
|
2016-10-13 08:14:18 +00:00
|
|
|
foreach ( $data as $info ) {
|
2016-11-22 09:03:02 +00:00
|
|
|
if ( !$categoryMgr->isKnownCategory( $info['type'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-26 22:22:45 +00:00
|
|
|
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'],
|
|
|
|
]
|
|
|
|
);
|
2017-06-29 18:23:58 +00:00
|
|
|
continue;
|
2017-06-26 22:22:45 +00:00
|
|
|
}
|
2016-12-01 02:47:05 +00:00
|
|
|
$info['location'] = array_slice( $info['dsr'], 0, 2 );
|
2016-10-13 08:14:18 +00:00
|
|
|
if ( isset( $info['templateInfo'] ) && $info['templateInfo'] ) {
|
|
|
|
$info['params']['templateInfo'] = $info['templateInfo'];
|
|
|
|
}
|
2016-11-15 19:31:45 +00:00
|
|
|
$errors[] = $info;
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:31:45 +00:00
|
|
|
$job = new RecordLintJob( $title, [
|
|
|
|
'errors' => $errors,
|
|
|
|
'revision' => $params['revision'],
|
|
|
|
] );
|
|
|
|
JobQueueGroup::singleton()->push( $job );
|
|
|
|
$this->getResult()->addValue( $this->getModuleName(), 'success', true );
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return [
|
2016-11-22 09:26:38 +00:00
|
|
|
'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,
|
|
|
|
],
|
2016-10-13 08:14:18 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|