From 796da84576415445c83ed7b147eccc8ef488f26c Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Thu, 21 Apr 2016 12:40:42 +0100 Subject: [PATCH] Allow extensions to support extra namespaces and content models Change-Id: If67bab93da355da609b6df274514b5c72a6ed91a --- ApiVisualEditor.php | 11 +++++++++++ VisualEditor.hooks.php | 10 ++++------ extension.json | 1 + .../targets/ve.init.mw.DesktopArticleTarget.init.js | 4 ++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ApiVisualEditor.php b/ApiVisualEditor.php index ab62ee0748..5676b26de8 100644 --- a/ApiVisualEditor.php +++ b/ApiVisualEditor.php @@ -655,7 +655,18 @@ class ApiVisualEditor extends ApiBase { $canonicalName = MWNamespace::getCanonicalName( $namespaceId ); return ( isset( $availableNamespaces[$namespaceId] ) && $availableNamespaces[$namespaceId] ) || ( isset( $availableNamespaces[$canonicalName] ) && $availableNamespaces[$canonicalName] ); + } + /** + * Check if the configured allowed content models include the specified content model + * + * @param Config $config Configuration object + * @param string $contentModel Content model ID + * @return boolean + */ + public static function isAllowedContentType( Config $config, $contentModel ) { + $availableContentModels = $config->get( 'VisualEditorAvailableContentModels' ); + return isset( $availableContentModels[ $contentModel ] ); } /** diff --git a/VisualEditor.hooks.php b/VisualEditor.hooks.php index cd87cedd99..30145ed873 100644 --- a/VisualEditor.hooks.php +++ b/VisualEditor.hooks.php @@ -124,7 +124,6 @@ class VisualEditorHooks { } $title = $article->getTitle(); - $params = $req->getValues(); if ( isset( $params['venoscript'] ) ) { @@ -143,7 +142,7 @@ class VisualEditorHooks { self::getUserEditor( $user, $req ) === 'wikitext' || !$title->quickUserCan( 'edit' ) || !ApiVisualEditor::isAllowedNamespace( $veConfig, $title->getNamespace() ) || - $title->getContentModel() !== CONTENT_MODEL_WIKITEXT || + !ApiVisualEditor::isAllowedContentType( $veConfig, $title->getContentModel() ) || // Known parameters that VE does not handle // TODO: Other params too? See identical list in ve.init.mw.DesktopArticleTarget.init.js isset( $params['undo'] ) || @@ -273,12 +272,10 @@ class VisualEditorHooks { $title = $skin->getRelevantTitle(); $namespaceEnabled = ApiVisualEditor::isAllowedNamespace( $config, $title->getNamespace() ); $pageContentModel = $title->getContentModel(); + $contentModelEnabled = ApiVisualEditor::isAllowedContentType( $veConfig, $pageContentModel ); // Don't exit if this page isn't VE-enabled, since we should still // change "Edit" to "Edit source". - $isAvailable = ( - $namespaceEnabled && - $pageContentModel === CONTENT_MODEL_WIKITEXT - ); + $isAvailable = $namespaceEnabled && $contentModelEnabled; // HACK: Exit if we're in the Education Program namespace (even though it's content) if ( defined( 'EP_NS' ) && $title->inNamespace( EP_NS ) ) { @@ -682,6 +679,7 @@ class VisualEditorHooks { 'disableForAnons' => $veConfig->get( 'VisualEditorDisableForAnons' ), 'preferenceModules' => $veConfig->get( 'VisualEditorPreferenceModules' ), 'namespaces' => $enabledNamespaces, + 'contentModels' => $veConfig->get( 'VisualEditorAvailableContentModels' ), 'signatureNamespaces' => array_values( array_filter( $enabledNamespaces, 'MWNamespace::wantSignatures' ) ), diff --git a/extension.json b/extension.json index 3223ee6bc6..c700131832 100644 --- a/extension.json +++ b/extension.json @@ -103,6 +103,7 @@ "Category": true, "_merge_strategy": "array_plus" }, + "VisualEditorAvailableContentModels": [ "wikitext" ], "VisualEditorSkinToolbarScrollOffset": [], "VisualEditorParsoidTimeout": 100, "VisualEditorUseSingleEditTab": false, diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js index 40ec83f767..cd4446f295 100644 --- a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js +++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js @@ -669,8 +669,8 @@ // Not on pages which are outputs of the Page Translation feature mw.config.get( 'wgTranslatePageTranslation' ) !== 'translation' && - // Only for pages with a wikitext content model - mw.config.get( 'wgPageContentModel' ) === 'wikitext' + // Only for pages with a supported content model + conf.contentModels.indexOf( mw.config.get( 'wgPageContentModel' ) ) !== -1 ); // FIXME: We should do this more elegantly