jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/387616 )

Change subject: Handle non-existent categories in the database better
......................................................................


Handle non-existent categories in the database better

If a newer version of MediaWiki gets rolled back, it's possible for
there to be lint entries in the database that don't exist according to
CategoryManager.

Instead of showing an error to the user, just silently hide those rows.

All callers to CategoryManager::getCategoryId() already check the
category exists. The callers for CategoryManager::getCategoryName() will
catch the MissingCategoryException, and log it if necessary. Notably
LinterError::makeLintError() will return false on invalid rows, and all
callers have been updated to handle that.

Bug: T179423
Change-Id: Ia5f56f18a51fa871511b02410222a6079efbfff6
---
M includes/ApiQueryLintErrors.php
M includes/CategoryManager.php
M includes/Database.php
M includes/LintErrorsPager.php
A includes/MissingCategoryException.php
5 files changed, 57 insertions(+), 7 deletions(-)

Approvals:
  Chad: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/ApiQueryLintErrors.php b/includes/ApiQueryLintErrors.php
index da2f164..adebc18 100644
--- a/includes/ApiQueryLintErrors.php
+++ b/includes/ApiQueryLintErrors.php
@@ -62,6 +62,9 @@
                $count = 0;
                foreach ( $rows as $row ) {
                        $lintError = Database::makeLintError( $row );
+                       if ( !$lintError ) {
+                               continue;
+                       }
                        $count++;
                        if ( $count > $params['limit'] ) {
                                $this->setContinueEnumParameter( 'from', 
$lintError->lintId );
diff --git a/includes/CategoryManager.php b/includes/CategoryManager.php
index 3e57251..066e40c 100644
--- a/includes/CategoryManager.php
+++ b/includes/CategoryManager.php
@@ -132,7 +132,7 @@
 
        /**
         * @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 @@
                        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 @@
         *
         * @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'" );
        }
 }
diff --git a/includes/Database.php b/includes/Database.php
index edeb9fe..2f9c778 100644
--- a/includes/Database.php
+++ b/includes/Database.php
@@ -21,6 +21,7 @@
 namespace MediaWiki\Linter;
 
 use FormatJson;
+use MWExceptionHandler;
 
 /**
  * Database logic
@@ -77,11 +78,17 @@
         * 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 @@
                $result = [];
                foreach ( $rows as $row ) {
                        $error = $this->makeLintError( $row );
+                       if ( !$error ) {
+                               continue;
+                       }
                        $result[$error->id()] = $error;
                }
 
@@ -236,7 +246,12 @@
                // 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;
diff --git a/includes/LintErrorsPager.php b/includes/LintErrorsPager.php
index f7ee521..05b4813 100644
--- a/includes/LintErrorsPager.php
+++ b/includes/LintErrorsPager.php
@@ -102,6 +102,9 @@
                $row = $this->mCurrentRow;
                $row->linter_cat = $this->categoryId;
                $lintError = Database::makeLintError( $row );
+               if ( !$lintError ) {
+                       return '';
+               }
                if ( $this->haveParserMigrationExt &&
                        $this->categoryManager->needsParserMigrationEdit( $name 
)
                ) {
diff --git a/includes/MissingCategoryException.php 
b/includes/MissingCategoryException.php
new file mode 100644
index 0000000..68c915e
--- /dev/null
+++ b/includes/MissingCategoryException.php
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Copyright (C) 2017 Kunal Mehta <lego...@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 {
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/387616
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia5f56f18a51fa871511b02410222a6079efbfff6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Linter
Gerrit-Branch: master
Gerrit-Owner: Legoktm <lego...@member.fsf.org>
Gerrit-Reviewer: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Subramanya Sastry <ssas...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to