From 4cc9407fe943dd9ca754f38aeabe049a8a79f6cc Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 10 Dec 2013 14:36:52 +0100 Subject: [PATCH] (bug 47070) check content model namespace on import. When importing, we need to check that the kind of content we are about to import is actually allowed in the specified location on the local wiki. This change does two things: * Introduce the ContentModelCanBeUsedOn hook which provides control over which kind of content can be used where. * Introduce a check against ContentHandler::canBeUsedOn in the importer, along with an appropriate error message. Change-Id: Ia2ff0b0474f4727c9ebbba3c0a2a111495055f61 --- docs/hooks.txt | 9 +++++++++ includes/Import.php | 12 ++++++++++++ includes/content/ContentHandler.php | 9 ++++++++- languages/messages/MessagesEn.php | 3 ++- languages/messages/MessagesQqq.php | 14 +++++++++++++- maintenance/language/messages.inc | 1 + 6 files changed, 45 insertions(+), 3 deletions(-) diff --git a/docs/hooks.txt b/docs/hooks.txt index e3387b5578..dbeaa6ebc4 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -830,6 +830,15 @@ content model name, but no entry for that model exists in $wgContentHandlers. $modeName: the requested content model name &$handler: set this to a ContentHandler object, if desired. +'ContentModelCanBeUsedOn': Called to determine whether that content model can +be used on a given page. This is especially useful to prevent some content models +to be used in some special location. +$contentModel: ID of the content model in question +$title: the Title in question. +&$ok: Output parameter, whether it is OK to use $contentModel on $title. +Handler functions that modify $ok should generally return false to prevent further +hooks from further modifying $ok. + 'ConvertContent': Called by AbstractContent::convert when a conversion to another content model is requested. $content: The Content object to be converted. diff --git a/includes/Import.php b/includes/Import.php index 8b7af02a0f..2770e470f1 100644 --- a/includes/Import.php +++ b/includes/Import.php @@ -252,6 +252,16 @@ class WikiImporter { * @return bool */ public function importRevision( $revision ) { + if ( !$revision->getContent()->getContentHandler()->canBeUsedOn( $revision->getTitle() ) ) { + $this->notice( 'import-error-bad-location', + $revision->getTitle()->getPrefixedText(), + $revision->getID(), + $revision->getModel(), + $revision->getFormat() ); + + return false; + } + try { $dbw = wfGetDB( DB_MASTER ); return $dbw->deadlockLoop( array( $revision, 'importOldRevision' ) ); @@ -262,6 +272,8 @@ class WikiImporter { $revision->getModel(), $revision->getFormat() ); } + + return false; } /** diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index 1abe1fa7e6..200d453f17 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -677,12 +677,19 @@ abstract class ContentHandler { * typically based on the namespace or some other aspect of the title, such as a special suffix * (e.g. ".svg" for SVG content). * + * @note: this calls the ContentHandlerCanBeUsedOn hook which may be used to override which + * content model can be used where. + * * @param Title $title the page's title. * * @return bool true if content of this kind can be used on the given page, false otherwise. */ public function canBeUsedOn( Title $title ) { - return true; + $ok = true; + + wfRunHooks( 'ContentModelCanBeUsedOn', array( $this->getModelID(), $title, &$ok ) ); + + return $ok; } /** diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 4ec3ee8480..88e68a0d4d 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -3594,7 +3594,7 @@ The file was only partially uploaded.', A temporary folder is missing.', 'import-parse-failure' => 'XML import parse failure', 'import-noarticle' => 'No page to import!', -'import-nonewrevisions' => 'All revisions were previously imported.', +'import-nonewrevisions' => 'No revisions imported (all were either already present, or skipped due to errors).', 'xml-error-string' => '$1 at line $2, col $3 (byte $4): $5', 'import-upload' => 'Upload XML data', 'import-token-mismatch' => 'Loss of session data. @@ -3606,6 +3606,7 @@ Please try again.', 'import-error-special' => 'Page "$1" is not imported because it belongs to a special namespace that does not allow pages.', 'import-error-invalid' => 'Page "$1" is not imported because its name is invalid.', 'import-error-unserialize' => 'Revision $2 of page "$1" could not be unserialized. The revision was reported to use content model $3 serialized as $4.', +'import-error-bad-location' => 'Revision $2 using content model $3 can not be stored on "$1" on this wiki, since that model is not supported on that page.', 'import-options-wrong' => 'Wrong {{PLURAL:$2|option|options}}: $1', 'import-rootpage-invalid' => 'Given root page is an invalid title.', 'import-rootpage-nosubpage' => 'Namespace "$1" of the root page does not allow subpages.', diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index 483fd25bab..b333682165 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -7279,7 +7279,8 @@ See also: See also: * {{msg-mw|Import-noarticle}} * {{msg-mw|Importbadinterwiki}}', -'import-nonewrevisions' => 'Used in [[Special:Import]].', +'import-nonewrevisions' => 'Used in [[Special:Import]] to indicate that no revisions were imported. +This may be due to the revisions already being present, or to errors when importing them.', 'xml-error-string' => 'Parameters: * $1 - Some kind of message, perhaps name of the error? * $2 - line number @@ -7327,6 +7328,17 @@ Parameters: This may happen if the content got corrupted or the serialization format is mis-reported. +Parameters: +* $1 - the name of the page the offending revision belongs to +* $2 - the ID of the offending revision, as reported in the dump that is being imported +* $3 - the content model reported for the offending revision in the dump that is being imported +* $4 - the serialization format reported for the offending revision in the dump that is being imported +{{Related|Import-error}}', +'import-error-bad location' => 'Import error message displayed when trying to import a page to a title that is not allowed for this kind of content on this wiki. + +This may happen for content models bound to specific namespaces or title patterns, if the configuration of the allowed namespaces +or titles differs from the configuration on the source wiki. + Parameters: * $1 - the name of the page the offending revision belongs to * $2 - the ID of the offending revision, as reported in the dump that is being imported diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 40bc31d444..76de144b7e 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -2521,6 +2521,7 @@ $wgMessageStructure = array( 'import-error-special', 'import-error-invalid', 'import-error-unserialize', + 'import-error-bad-location', 'import-options-wrong', 'import-rootpage-invalid', 'import-rootpage-nosubpage', -- 2.20.1