diff --git a/TemplateData.php b/TemplateData.php index 30085ffc..c83a5af0 100644 --- a/TemplateData.php +++ b/TemplateData.php @@ -107,6 +107,7 @@ $wgResourceModules['ext.templateDataGenerator.ui'] = array( 'messages' => array( 'comma-separator', 'templatedata-doc-no-params-set', + 'templatedata-exists-on-related-page', 'templatedata-modal-button-add-language', 'templatedata-modal-button-addparam', 'templatedata-modal-button-apply', @@ -157,7 +158,7 @@ $wgResourceModules['ext.templateDataGenerator.ui'] = array( 'templatedata-modal-title-paramorder', 'templatedata-modal-title-templatedesc', 'templatedata-modal-title-templateparam-details', - 'templatedata-modal-title-templateparams', + 'templatedata-modal-title-templateparams' ) ); diff --git a/i18n/en.json b/i18n/en.json index 497cee92..2cd1741b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -28,6 +28,7 @@ "templatedata-doc-params": "Template parameters", "templatedata-editbutton": "Manage TemplateData", "templatedata-errormsg-jsonbadformat": "Bad JSON format. Either correct it, or delete the current tags and try again.", + "templatedata-exists-on-related-page": "Please note: there is already a TemplateData block on the related page '[[$1]]'.", "templatedata-helplink": "Information about TemplateData", "templatedata-helplink-target": "//www.mediawiki.org/wiki/Special:MyLanguage/Help:TemplateData", "templatedata-invalid-duplicate-value": "Property \"$1\" (\"$3\") is a duplicate of \"$2\".", diff --git a/i18n/qqq.json b/i18n/qqq.json index f6b1661e..9e7ab3c6 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -35,6 +35,7 @@ "templatedata-doc-params": "Used as caption for the table which has the following headings:\n* {{msg-mw|Templatedata-doc-param-name}}\n* {{msg-mw|Templatedata-doc-param-desc}}\n* {{msg-mw|Templatedata-doc-param-default}}\n* {{msg-mw|Templatedata-doc-param-status}}\n{{Identical|Template parameter}}", "templatedata-editbutton": "The label of the button to manage templatedata, appearing above the editor field.", "templatedata-errormsg-jsonbadformat": "Error message that appears in case the JSON string is not possible to parse. The user is asked to either correct the json syntax or delete the values between the <templatedata> tags and try again.", + "templatedata-exists-on-related-page": "A messaged that is displayed when there already is a templatedata string in a related page. $1: The page where templatedata already exists.", "templatedata-helplink": "The label of the link to the TemplateData documentation, appearing above the editor field.", "templatedata-helplink-target": "{{ignore}} The target of the link to the TemplateData documentation", "templatedata-invalid-duplicate-value": "Displayed when an array that must only contain unique values contains a duplicate.\n* $1 - name of property containing the duplicate\n* $2 - name of property with first occurrence of value\n* $3 - the value being duplicated", diff --git a/modules/ext.templateDataGenerator.data.js b/modules/ext.templateDataGenerator.data.js index bd4fa94a..bd11b6e4 100644 --- a/modules/ext.templateDataGenerator.data.js +++ b/modules/ext.templateDataGenerator.data.js @@ -61,17 +61,28 @@ OO.mixinClass( mw.TemplateData.Model, OO.EventEmitter ); /** * Get information from the mediaWiki API * @param {string} page Page name + * @param {boolean} [getTemplateData] Fetch the templatedata in the page. * @return {jQuery.Promise} API promise */ -mw.TemplateData.Model.static.getApi = function ( page ) { - var api = new mw.Api(); - return api.get( { - action: 'query', - prop: 'revisions', - rvprop: 'content', - indexpageids: '1', - titles: page - } ); +mw.TemplateData.Model.static.getApi = function ( page, getTemplateData ) { + var config, + api = new mw.Api(), + baseConfig = { + action: getTemplateData ? 'templatedata' : 'query', + titles: page + }; + if ( getTemplateData ) { + config = $.extend( baseConfig, { + redirects: '1' + } ); + } else { + config = $.extend( baseConfig, { + prop: 'revisions', + rvprop: 'content', + indexpageids: '1' + } ); + } + return api.get( config ); }; /** diff --git a/modules/ext.templateDataGenerator.editPage.js b/modules/ext.templateDataGenerator.editPage.js index 6703b643..580d6602 100644 --- a/modules/ext.templateDataGenerator.editPage.js +++ b/modules/ext.templateDataGenerator.editPage.js @@ -8,19 +8,32 @@ 'use strict'; $( function () { - var config = { + var pieces, isDocPage, + pageName = mw.config.get( 'wgPageName' ), + config = { + pageName: pageName, isPageSubLevel: false }, - $textbox = $( '#wpTextbox1' ), - pageName = mw.config.get( 'wgPageName' ); + $textbox = $( '#wpTextbox1' ); // Check if there's an editor textarea and if we're in the proper namespace if ( $textbox.length && mw.config.get( 'wgCanonicalNamespace' ) === 'Template' ) { - if ( pageName.indexOf( '/' ) > -1 ) { - config.parentPage = pageName.substr( 0, pageName.indexOf( '/' ) ); - config.isPageSubLevel = pageName.indexOf( '/' ) > -1; - } + pieces = pageName.split( '/' ); + isDocPage = pieces.length > 1 && pieces[ pieces.length - 1 ] === 'doc'; + config = { + pageName: pageName, + isPageSubLevel: pieces.length > 1, + parentPage: pageName, + isDocPage: isDocPage + }; + + // Only if we are in a doc page do we set the parent page to + // the one above. Otherwise, all parent pages are current pages + if ( isDocPage ) { + pieces.pop(); + config.parentPage = pieces.join( '/' ); + } // Prepare the editor mw.libs.tdgUi.init( $( '#mw-content-text' ), $textbox, config ); } diff --git a/modules/ext.templateDataGenerator.ui.js b/modules/ext.templateDataGenerator.ui.js index 5ca52519..0c886cb7 100644 --- a/modules/ext.templateDataGenerator.ui.js +++ b/modules/ext.templateDataGenerator.ui.js @@ -10,6 +10,8 @@ 'use strict'; mw.libs.tdgUi = ( function () { var isPageSubLevel, + isDocPage, + pageName, parentPage, $textbox, /** @@ -28,13 +30,20 @@ * Display error message in the edit window * @param {string} msg Message to display * @param {string} type Message type 'notice' or 'error' + * @param {boolean} [parseHTML] The message should be parsed */ - setNoticeMessage: function ( msg, type ) { + setNoticeMessage: function ( msg, type, parseHTML ) { type = type || 'error'; editNoticeLabel.$element.toggleClass( 'errorbox', type === 'error' ); + if ( parseHTML ) { + // OOUI's label elements do not parse strings and display them + // as-is. If the message contains html that should be parsed, + // we have to transform it into a jQuery object + msg = $( '' ).append( $.parseHTML( msg ) ); + } editNoticeLabel.setLabel( msg ); - editNoticeLabel.$element.show(); + editNoticeLabel.toggle( true ); }, /** @@ -42,7 +51,7 @@ */ resetNoticeMessage: function () { editNoticeLabel.setLabel( '' ); - editNoticeLabel.$element.hide(); + editNoticeLabel.toggle( false ); } }, @@ -114,13 +123,15 @@ * current wikitext from. */ init: function ( $container, $editorTextbox, userConfig ) { - var editHelpButtonWidget, + var editHelpButtonWidget, relatedPage, config = userConfig; $textbox = $editorTextbox; + pageName = config.pageName; parentPage = config.parentPage; isPageSubLevel = !!config.isPageSubLevel; + isDocPage = !!config.isDocPage; editOpenDialogButton = new OO.ui.ButtonWidget( { label: mw.msg( 'templatedata-editbutton' ) @@ -144,6 +155,26 @@ tdgDialog = new mw.TemplateData.Dialog( config ); windowManager.addWindows( [ tdgDialog ] ); + // Check if there's already a templatedata in a related page + relatedPage = isDocPage ? parentPage : pageName + '/doc'; + editOpenDialogButton.setDisabled( true ); + mw.TemplateData.Model.static.getApi( relatedPage, true ) + .then( function ( result ) { + var msg; + if ( !$.isEmptyObject( result.pages ) ) { + // HACK: Setting a link in the messages doesn't work. The bug report offers + // a somewhat hacky work around that includes setting a separate message + // to be parsed. + // https://phabricator.wikimedia.org/T49395#490610 + msg = mw.message( 'templatedata-exists-on-related-page', relatedPage ).plain(); + mw.messages.set( { 'templatedata-string-exists-hack-message': msg } ); + msg = mw.message( 'templatedata-string-exists-hack-message' ).parse(); + + editArea.setNoticeMessage( msg, 'error', true ); + } + editOpenDialogButton.setDisabled( false ); + } ); + // Prepend to container $container .prepend(