mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Linter
synced 2024-11-27 17:20:12 +00:00
Implement ParserLogLinterData hook
Bug: T238456 Change-Id: I3e05d1c113b18c83db02c7e500d5834e931afb7f
This commit is contained in:
parent
9916d63656
commit
40f0b3cef9
|
@ -29,7 +29,8 @@
|
|||
"BeforePageDisplay": "MediaWiki\\Linter\\Hooks::onBeforePageDisplay",
|
||||
"APIQuerySiteInfoGeneralInfo": "MediaWiki\\Linter\\Hooks::onAPIQuerySiteInfoGeneralInfo",
|
||||
"InfoAction": "MediaWiki\\Linter\\Hooks::onInfoAction",
|
||||
"WikiPageDeletionUpdates": "MediaWiki\\Linter\\Hooks::onWikiPageDeletionUpdates"
|
||||
"WikiPageDeletionUpdates": "MediaWiki\\Linter\\Hooks::onWikiPageDeletionUpdates",
|
||||
"ParserLogLinterData": "MediaWiki\\Linter\\Hooks::onParserLogLinterData"
|
||||
},
|
||||
"APIModules": {
|
||||
"record-lint": "MediaWiki\\Linter\\ApiRecordLint"
|
||||
|
|
|
@ -22,9 +22,6 @@ namespace MediaWiki\Linter;
|
|||
|
||||
use ApiBase;
|
||||
use FormatJson;
|
||||
use JobQueueGroup;
|
||||
use MediaWiki\Logger\LoggerFactory;
|
||||
use Title;
|
||||
use Wikimedia\IPSet;
|
||||
|
||||
/**
|
||||
|
@ -35,7 +32,6 @@ class ApiRecordLint extends ApiBase {
|
|||
|
||||
public function execute() {
|
||||
global $wgLinterSubmitterWhitelist;
|
||||
|
||||
$ipSet = new IPSet(
|
||||
array_keys( array_filter( $wgLinterSubmitterWhitelist ) )
|
||||
);
|
||||
|
@ -47,51 +43,13 @@ class ApiRecordLint extends ApiBase {
|
|||
if ( !is_array( $data ) ) {
|
||||
$this->dieWithError( 'apierror-linter-invalid-data', 'invalid-data' );
|
||||
}
|
||||
'@phan-var array[] $data';
|
||||
|
||||
$errors = [];
|
||||
$title = Title::newFromText( $params['page'] );
|
||||
if ( !$title || !$title->getArticleID()
|
||||
|| $title->getLatestRevID() != $params['revision']
|
||||
) {
|
||||
if ( Hooks::onParserLogLinterData(
|
||||
$params['page'], $params['revision'], $data
|
||||
) ) {
|
||||
$this->getResult()->addValue( $this->getModuleName(), 'success', true );
|
||||
} else {
|
||||
$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() {
|
||||
|
|
|
@ -27,6 +27,9 @@ use IContextSource;
|
|||
use MWCallableUpdate;
|
||||
use OutputPage;
|
||||
use WikiPage;
|
||||
use JobQueueGroup;
|
||||
use MediaWiki\Logger\LoggerFactory;
|
||||
use Title;
|
||||
|
||||
class Hooks {
|
||||
/**
|
||||
|
@ -131,4 +134,62 @@ class Hooks {
|
|||
$pageInfo['linter'][] = [ $context->msg( "linter-category-$name" ), htmlspecialchars( $count ) ];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook: ParserLogLinterData
|
||||
*
|
||||
* To record a lint errors.
|
||||
*
|
||||
* @param string $page
|
||||
* @param int $revision
|
||||
* @param array[] $data
|
||||
* @return bool
|
||||
*/
|
||||
public static function onParserLogLinterData(
|
||||
string $page, int $revision, array $data
|
||||
): bool {
|
||||
$errors = [];
|
||||
$title = Title::newFromText( $page );
|
||||
if (
|
||||
!$title || !$title->getArticleID() ||
|
||||
$title->getLatestRevID() != $revision
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
$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' => $page,
|
||||
'revid' => $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' => $revision,
|
||||
] );
|
||||
JobQueueGroup::singleton()->push( $job );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue