From cdc36570eadae18ef367be34a901f3c21b742557 Mon Sep 17 00:00:00 2001 From: "This, that and the other" Date: Sat, 10 Oct 2015 19:51:20 +1100 Subject: [PATCH] Improve error message for missing content model on import I was initially going to refactor out the error message into Import.php, but it quickly became apparent that WikiImporter's error handling needs a LOT of work. In particular, to localise the error message into the user's language is sadly non-trivial. Although not used currently, the MWUnknownContentModelException subclass will help with error handling improvements in the future. Bug: T49270 Change-Id: I9f53c9d6a8a2ea842cb2ba94d4131e10a8b08f5d --- autoload.php | 1 + includes/content/ContentHandler.php | 31 +++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/autoload.php b/autoload.php index a2f432f6fd..caad45f4a8 100644 --- a/autoload.php +++ b/autoload.php @@ -728,6 +728,7 @@ $wgAutoloadLocalClasses = array( 'MWSaltedPassword' => __DIR__ . '/includes/password/MWSaltedPassword.php', 'MWTidy' => __DIR__ . '/includes/parser/MWTidy.php', 'MWTimestamp' => __DIR__ . '/includes/MWTimestamp.php', + 'MWUnknownContentModelException' => __DIR__ . '/includes/content/ContentHandler.php', 'MachineReadableRCFeedFormatter' => __DIR__ . '/includes/rcfeed/MachineReadableRCFeedFormatter.php', 'MagicWord' => __DIR__ . '/includes/MagicWord.php', 'MagicWordArray' => __DIR__ . '/includes/MagicWord.php', diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index 76f2a7b8d9..acaa288f43 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -33,6 +33,32 @@ class MWContentSerializationException extends MWException { } +/** + * Exception thrown when an unregistered content model is requested. This error + * can be triggered by user input, so a separate exception class is provided so + * callers can substitute a context-specific, internationalised error message. + * + * @ingroup Content + * @since 1.27 + */ +class MWUnknownContentModelException extends MWException { + /** @var string The name of the unknown content model */ + private $modelId; + + /** @param string $modelId */ + function __construct( $modelId ) { + parent::__construct( "The content model '$modelId' is not registered on this wiki.\n" . + 'See https://www.mediawiki.org/wiki/Content_handlers to find out which extensions ' . + 'handle this content model.' ); + $this->modelId = $modelId; + } + + /** @return string */ + public function getModelId() { + return $modelId; + } +} + /** * A content handler knows how do deal with a specific type of content on a wiki * page. Content is stored in the database in a serialized form (using a @@ -307,7 +333,8 @@ abstract class ContentHandler { * @param string $modelId The ID of the content model for which to get a * handler. Use CONTENT_MODEL_XXX constants. * - * @throws MWException If no handler is known for the model ID. + * @throws MWException For internal errors and problems in the configuration. + * @throws MWUnknownContentModelException If no handler is known for the model ID. * @return ContentHandler The ContentHandler singleton for handling the model given by the ID. */ public static function getForModelID( $modelId ) { @@ -323,7 +350,7 @@ abstract class ContentHandler { Hooks::run( 'ContentHandlerForModelID', array( $modelId, &$handler ) ); if ( $handler === null ) { - throw new MWException( "No handler for model '$modelId' registered in \$wgContentHandlers" ); + throw new MWUnknownContentModelException( $modelId ); } if ( !( $handler instanceof ContentHandler ) ) { -- 2.20.1