Allow Parsoid to provide category ID hints

This eases deployment dependencies by allowing Parsoid to supply an
appropriate database category ID so that new lint categories can be
appropriately stored during the interval between adding a new lint
category to Parsoid and deploying an Extension:Linter patch to
describe it.

Change-Id: Ib7b2342168fa53ca2abac7d5f54fe313be341eb7
This commit is contained in:
C. Scott Ananian 2017-12-07 17:30:14 -05:00
parent 40f0b3cef9
commit 551a1fb398
7 changed files with 34 additions and 7 deletions

View file

@ -167,14 +167,25 @@ class CategoryManager {
* Get the int id for the category in lint_categories table * Get the int id for the category in lint_categories table
* *
* @param string $name * @param string $name
* @param int|null $hint An optional hint, passed along from Parsoid.
* If the hint contains a suggested category ID but the Linter
* extension doesn't (yet) have one, use the ID from Parsoid's hint.
* This allows decoupling the Parsoid deploy of a new category
* from the corresponding Linter extension deploy.
* @return int * @return int
* @throws MissingCategoryException if we can't find the id for the name * @throws MissingCategoryException if we can't find the id for the name
* and there is no hint from Parsoid
*/ */
public function getCategoryId( $name ) { public function getCategoryId( $name, $hint = null ) {
if ( isset( $this->categoryIds[$name] ) ) { if ( isset( $this->categoryIds[$name] ) ) {
return $this->categoryIds[$name]; return $this->categoryIds[$name];
} }
// Use hint from Parsoid, if available.
if ( $hint !== null ) {
return $hint;
}
throw new MissingCategoryException( "Cannot find id for '$name'" ); throw new MissingCategoryException( "Cannot find id for '$name'" );
} }
} }

View file

@ -94,6 +94,7 @@ class Database {
$name, $name,
[ (int)$row->linter_start, (int)$row->linter_end ], [ (int)$row->linter_start, (int)$row->linter_end ],
$row->linter_params, $row->linter_params,
$row->linter_cat,
(int)$row->linter_id (int)$row->linter_id
); );
} }
@ -135,7 +136,7 @@ class Database {
private function serializeError( LintError $error ) { private function serializeError( LintError $error ) {
return [ return [
'linter_page' => $this->pageId, 'linter_page' => $this->pageId,
'linter_cat' => $this->categoryManager->getCategoryId( $error->category ), 'linter_cat' => $this->categoryManager->getCategoryId( $error->category, $error->catId ),
'linter_params' => FormatJson::encode( $error->params, false, FormatJson::ALL_OK ), 'linter_params' => FormatJson::encode( $error->params, false, FormatJson::ALL_OK ),
'linter_start' => $error->location[0], 'linter_start' => $error->location[0],
'linter_end' => $error->location[1], 'linter_end' => $error->location[1],

View file

@ -159,7 +159,9 @@ class Hooks {
$categoryMgr = new CategoryManager(); $categoryMgr = new CategoryManager();
$catCounts = []; $catCounts = [];
foreach ( $data as $info ) { foreach ( $data as $info ) {
if ( !$categoryMgr->isKnownCategory( $info['type'] ) ) { if ( $categoryMgr->isKnownCategory( $info['type'] ) ) {
$info[ 'dbid' ] = null;
} elseif ( !isset( $info[ 'dbid' ] ) ) {
continue; continue;
} }
$count = $catCounts[$info['type']] ?? 0; $count = $catCounts[$info['type']] ?? 0;

View file

@ -51,14 +51,25 @@ class LintError {
*/ */
public $templateInfo; public $templateInfo;
/*
* Optional hint for the linter category ID.
* Passed through from Parsoid when a new category has been added
* that isn't known to PHP yet.
* @var int|null
*/
public $catId;
/** /**
* @param string $category * @param string $category
* @param int[] $location [ start, end ] * @param int[] $location [ start, end ]
* @param string|array $params JSON string or already decoded array * @param string|array $params JSON string or already decoded array
* @param int $lintId linter_id * @param int|null $catId Optional category ID hint
* @param int $lintId Optional linter_id
*/ */
public function __construct( $category, $location, $params, $lintId = 0 ) { public function __construct( $category, $location, $params, $catId = null, $lintId = 0 ) {
$this->category = $category; $this->category = $category;
$this->catId = $catId;
if ( is_string( $params ) ) { if ( is_string( $params ) ) {
$params = FormatJson::decode( $params, true ); $params = FormatJson::decode( $params, true );
} }
@ -92,5 +103,4 @@ class LintError {
return $params; return $params;
} }
} }

View file

@ -48,7 +48,8 @@ class RecordLintJob extends Job {
$error = new LintError( $error = new LintError(
$errorInfo['type'], $errorInfo['type'],
$errorInfo['location'], $errorInfo['location'],
$errorInfo['params'] $errorInfo['params'],
$errorInfo['dbid']
); );
// Use unique id as key to get rid of exact dupes // Use unique id as key to get rid of exact dupes
// (e.g. same category of error in same template) // (e.g. same category of error in same template)

View file

@ -43,6 +43,7 @@ class LintErrorTest extends \MediaWikiTestCase {
'obsolete-tag', 'obsolete-tag',
[ 10, 20 ], [ 10, 20 ],
[ 'name' => 'big' ], [ 'name' => 'big' ],
null,
5 5
); );
$this->assertInstanceOf( LintError::class, $error2 ); $this->assertInstanceOf( LintError::class, $error2 );

View file

@ -50,6 +50,7 @@ class RecordLintJobTest extends \MediaWikiTestCase {
'type' => 'fostered', 'type' => 'fostered',
'location' => [ 0, 10 ], 'location' => [ 0, 10 ],
'params' => [], 'params' => [],
'dbid' => null,
]; ];
$job = new RecordLintJob( $this->getMockTitle(), [ $job = new RecordLintJob( $this->getMockTitle(), [
'errors' => [ $error ], 'errors' => [ $error ],