Introducing ContentHandler::canBeUsedOn()
authordaniel <daniel.kinzler@wikimedia.de>
Tue, 11 Sep 2012 13:39:52 +0000 (15:39 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Tue, 11 Sep 2012 13:39:57 +0000 (15:39 +0200)
ContentHandler::canBeUsedOn() is called by WikiPage::doEditContent
to determine whether the requested kind of content can be used
on the present page.

Change-Id: I37b0e42eea2376b7485581f779b2520caa3b7fe2

includes/ContentHandler.php
includes/WikiPage.php
languages/messages/MessagesEn.php
languages/messages/MessagesQqq.php

index 6140824..584a6ad 100644 (file)
@@ -612,6 +612,23 @@ abstract class ContentHandler {
                return $pageLang;
        }
 
+       /**
+        * Determines whether the content type handled by this ContentHandler
+        * can be used on the given page.
+        *
+        * This default implementation always returns true.
+        * Subclasses may override this to restrict the use of this content model to specific locations,
+        * typically based on the namespace or some other aspect of the title, such as a special suffix
+        * (e.g. ".svg" for SVG content).
+        *
+        * @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;
+       }
+
        /**
         * Returns the name of the diff engine to use.
         *
index 65ccc0b..6c7b335 100644 (file)
@@ -1616,6 +1616,13 @@ class WikiPage extends Page implements IDBAccessObject {
 
                wfProfileIn( __METHOD__ );
 
+               if ( !$content->getContentHandler()->canBeUsedOn( $this->getTitle() ) ) {
+                       wfProfileOut( __METHOD__ );
+                       return Status::newFatal( 'content-not-allowed-here',
+                               ContentHandler::getLocalizedName( $content->getModel() ),
+                               $this->getTitle()->getPrefixedText() );
+               }
+
                $user = is_null( $user ) ? $wgUser : $user;
                $status = Status::newGood( array() );
 
index acad936..2b2a884 100644 (file)
@@ -1488,6 +1488,7 @@ It already exists.',
 'addsection-editintro'             => '', # do not translate or duplicate this message to other languages
 'defaultmessagetext'               => 'Default message text',
 'invalid-content-data'             => 'Invalid content data',
+'content-not-allowed-here'         => '"$1" content is not allowed on page [[$2]]',
 
 # Parser/template warnings
 'expensive-parserfunction-warning'        => "'''Warning:''' This page contains too many expensive parser function calls.
index 3dda8ee..49cf0d7 100644 (file)
@@ -1054,6 +1054,9 @@ Please report at [[Support]] if you are unable to properly translate this messag
 'edit-conflict' => "An 'Edit conflict' happens when more than one edit is being made to a page at the same time. This would usually be caused by separate individuals working on the same page. However, if the system is slow, several edits from one individual could back up and attempt to apply simultaneously - causing the conflict.",
 'defaultmessagetext' => 'Caption above the default message text shown on the left-hand side of a diff displayed after clicking “Show changes” when creating a new page in the MediaWiki: namespace',
 'invalid-content-data'             => 'Error message indicating that the page\'s content can not be saved because it is invalid. This may occurr for some non-text content types.',
+'content-not-allowed-here'         => 'Error message indicating that the desired content model is not supported in given localtion.
+* $1 is the human readable name of the content model
+* $1 is the title of the page in question.',
 
 # Parser/template warnings
 'expensive-parserfunction-warning' => 'On some (expensive) [[MetaWikipedia:Help:ParserFunctions|parser functions]] (e.g. <code><nowiki>{{#ifexist:}}</nowiki></code>) there is a limit of how many times it may be used. This is an error message shown when the limit is exceeded.