mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Linter
synced 2024-11-23 23:44:17 +00:00
Merge "Handle non-existent categories in the database better"
This commit is contained in:
commit
c78e1c8d75
|
@ -62,6 +62,9 @@ class ApiQueryLintErrors extends ApiQueryBase {
|
|||
$count = 0;
|
||||
foreach ( $rows as $row ) {
|
||||
$lintError = Database::makeLintError( $row );
|
||||
if ( !$lintError ) {
|
||||
continue;
|
||||
}
|
||||
$count++;
|
||||
if ( $count > $params['limit'] ) {
|
||||
$this->setContinueEnumParameter( 'from', $lintError->lintId );
|
||||
|
|
|
@ -132,7 +132,7 @@ class CategoryManager {
|
|||
|
||||
/**
|
||||
* @param int $id
|
||||
* @throws \RuntimeException if we can't find the name for the id
|
||||
* @throws MissingCategoryException if we can't find the name for the id
|
||||
* @return string
|
||||
*/
|
||||
public function getCategoryName( $id ) {
|
||||
|
@ -141,7 +141,7 @@ class CategoryManager {
|
|||
return $flip[$id];
|
||||
}
|
||||
|
||||
throw new \RuntimeException( "Could not find name for id $id" );
|
||||
throw new MissingCategoryException( "Could not find name for id $id" );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,13 +162,13 @@ class CategoryManager {
|
|||
*
|
||||
* @param string $name
|
||||
* @return int
|
||||
* @throws \RuntimeException if we can't find the id for the name
|
||||
* @throws MissingCategoryException if we can't find the id for the name
|
||||
*/
|
||||
public function getCategoryId( $name ) {
|
||||
if ( isset( $this->categoryIds[$name] ) ) {
|
||||
return $this->categoryIds[$name];
|
||||
}
|
||||
|
||||
throw new \RuntimeException( "Cannot find id for '$name'" );
|
||||
throw new MissingCategoryException( "Cannot find id for '$name'" );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
namespace MediaWiki\Linter;
|
||||
|
||||
use FormatJson;
|
||||
use MWExceptionHandler;
|
||||
|
||||
/**
|
||||
* Database logic
|
||||
|
@ -77,11 +78,17 @@ class Database {
|
|||
* Turn a database row into a LintError object
|
||||
*
|
||||
* @param \stdClass $row
|
||||
* @return LintError
|
||||
* @return LintError|bool false on error
|
||||
*/
|
||||
public static function makeLintError( $row ) {
|
||||
try {
|
||||
$name = ( new CategoryManager() )->getCategoryName( $row->linter_cat );
|
||||
} catch ( MissingCategoryException $e ) {
|
||||
MWExceptionHandler::logException( $e );
|
||||
return false;
|
||||
}
|
||||
return new LintError(
|
||||
( new CategoryManager() )->getCategoryName( $row->linter_cat ),
|
||||
$name,
|
||||
[ (int)$row->linter_start, (int)$row->linter_end ],
|
||||
$row->linter_params,
|
||||
(int)$row->linter_id
|
||||
|
@ -106,6 +113,9 @@ class Database {
|
|||
$result = [];
|
||||
foreach ( $rows as $row ) {
|
||||
$error = $this->makeLintError( $row );
|
||||
if ( !$error ) {
|
||||
continue;
|
||||
}
|
||||
$result[$error->id()] = $error;
|
||||
}
|
||||
|
||||
|
@ -236,7 +246,12 @@ class Database {
|
|||
// Initialize zero values
|
||||
$ret = array_fill_keys( $this->categoryManager->getVisibleCategories(), 0 );
|
||||
foreach ( $rows as $row ) {
|
||||
$ret[$this->categoryManager->getCategoryName( $row->linter_cat )] = (int)$row->count;
|
||||
try {
|
||||
$catName = $this->categoryManager->getCategoryName( $row->linter_cat );
|
||||
} catch ( MissingCategoryException $e ) {
|
||||
continue;
|
||||
}
|
||||
$ret[$catName] = (int)$row->count;
|
||||
}
|
||||
|
||||
return $ret;
|
||||
|
|
|
@ -102,6 +102,9 @@ class LintErrorsPager extends TablePager {
|
|||
$row = $this->mCurrentRow;
|
||||
$row->linter_cat = $this->categoryId;
|
||||
$lintError = Database::makeLintError( $row );
|
||||
if ( !$lintError ) {
|
||||
return '';
|
||||
}
|
||||
if ( $this->haveParserMigrationExt &&
|
||||
$this->categoryManager->needsParserMigrationEdit( $name )
|
||||
) {
|
||||
|
|
29
includes/MissingCategoryException.php
Normal file
29
includes/MissingCategoryException.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
/**
|
||||
* Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace MediaWiki\Linter;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Exception to throw if a category cannot be found
|
||||
*/
|
||||
class MissingCategoryException extends Exception {
|
||||
}
|
Loading…
Reference in a new issue