mediawiki-extensions-Visual.../VisualEditorDataModule.php

94 lines
2.8 KiB
PHP
Raw Normal View History

<?php
/**
* Resource loader module providing extra data from the server to VisualEditor.
*
* @file
* @ingroup Extensions
* @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
class VisualEditorDataModule extends ResourceLoaderModule {
/* Protected Members */
init.Platform: Refactor parsed messages. Rewrite VisualEditorMessagesModule: * Replace copy-paste dump of user-css module with stuff for VisualEditor (class commend and module::$origin). * Remove duplication between getMessages and getScript. * Actually implement getModifiedTime so that the comment in getMessages() about cache invalidation is actually true Fixes bug 42670: ext.visualEditor.specialMessages cache broken ve.init: * Implement addParsedMessages and getParsedMessage so that we don't mix up plain messages with raw html messages (minoredit was previously overloaded in mw.msg storage with a parsed html message and retrieved though ve.msg, which is documented as retuning plain text, not raw html). This is now separated into a different method. * Improved documentation of the other msg methods to emphasise their differences * Removed redundant code in attachSaveDialog() that was (partially) already done in setupSaveDialog() and moved the remaining bits into it as well. Checked all callers of these and they are both only called from ViewPageTarget.prototype.onLoad * Also implement them in the standalone platform implementation, with the html escaper based on mw.html.escape * Update init.platform.getMessage to use undefined instead of discouraged 'if-in' statement. * Add test suite. demos/test: * Re-run makeStaticLoader.php on test to add ve.init.Platform.test * Re-run makeStaticLoader.php on demos and update i18n caller to use ve.init.platform.addParsedMessages (also moved out of the auto-generated block for easier updating) Change-Id: I7f26b47e9467e850c08b9c217c4f1098590de109
2012-12-04 06:56:41 +00:00
protected $origin = self::ORIGIN_USER_SITEWIDE;
protected $targets = [ 'desktop', 'mobile' ];
/* Methods */
public function getScript( ResourceLoaderContext $context ) {
$msgInfo = $this->getMessageInfo( $context );
$parsedMessages = [];
$textMessages = [];
foreach ( $msgInfo['parse'] as $msgKey => $msgObj ) {
$parsedMessages[ $msgKey ] = $msgObj->parse();
init.Platform: Refactor parsed messages. Rewrite VisualEditorMessagesModule: * Replace copy-paste dump of user-css module with stuff for VisualEditor (class commend and module::$origin). * Remove duplication between getMessages and getScript. * Actually implement getModifiedTime so that the comment in getMessages() about cache invalidation is actually true Fixes bug 42670: ext.visualEditor.specialMessages cache broken ve.init: * Implement addParsedMessages and getParsedMessage so that we don't mix up plain messages with raw html messages (minoredit was previously overloaded in mw.msg storage with a parsed html message and retrieved though ve.msg, which is documented as retuning plain text, not raw html). This is now separated into a different method. * Improved documentation of the other msg methods to emphasise their differences * Removed redundant code in attachSaveDialog() that was (partially) already done in setupSaveDialog() and moved the remaining bits into it as well. Checked all callers of these and they are both only called from ViewPageTarget.prototype.onLoad * Also implement them in the standalone platform implementation, with the html escaper based on mw.html.escape * Update init.platform.getMessage to use undefined instead of discouraged 'if-in' statement. * Add test suite. demos/test: * Re-run makeStaticLoader.php on test to add ve.init.Platform.test * Re-run makeStaticLoader.php on demos and update i18n caller to use ve.init.platform.addParsedMessages (also moved out of the auto-generated block for easier updating) Change-Id: I7f26b47e9467e850c08b9c217c4f1098590de109
2012-12-04 06:56:41 +00:00
}
foreach ( $msgInfo['text'] as $msgKey => $msgObj ) {
$textMessages[ $msgKey ] = $msgObj->text();
}
return 've.init.platform.addParsedMessages(' . FormatJson::encode(
$parsedMessages,
ResourceLoader::inDebugMode()
) . ');'.
've.init.platform.addMessages(' . FormatJson::encode(
$textMessages,
ResourceLoader::inDebugMode()
) . ');';
}
protected function getMessageInfo( ResourceLoaderContext $context ) {
// Messages to be exported as parsed html
$parseMsgs = [
'minoredit' => $context->msg( 'minoredit' ),
'missingsummary' => $context->msg( 'missingsummary' ),
'summary' => $context->msg( 'summary' ),
'watchthis' => $context->msg( 'watchthis' ),
'visualeditor-browserwarning' => $context->msg( 'visualeditor-browserwarning' ),
'visualeditor-wikitext-warning' => $context->msg( 'visualeditor-wikitext-warning' ),
];
// Copyright warning (based on EditPage::getCopyrightWarning)
$rightsText = $this->config->get( 'RightsText' );
if ( $rightsText ) {
$copywarnMsgArgs = [ 'copyrightwarning',
'[[' . $context->msg( 'copyrightpage' )->inContentLanguage()->text() . ']]',
$rightsText ];
} else {
$copywarnMsgArgs = [ 'copyrightwarning2',
'[[' . $context->msg( 'copyrightpage' )->inContentLanguage()->text() . ']]' ];
}
// EditPage supports customisation based on title, we can't support that
$title = Title::newFromText( 'Dwimmerlaik' );
Hooks::run( 'EditPageCopyrightWarning', [ $title, &$copywarnMsgArgs ] );
// Normalise to 'copyrightwarning' so we have a consistent key in the front-end
$parseMsgs[ 'copyrightwarning' ] = call_user_func_array(
[ $context, 'msg' ],
$copywarnMsgArgs
);
// Messages to be exported as text
$textMsgs = [
'visualeditor-feedback-link' => $context->msg( 'visualeditor-feedback-link' )
->inContentLanguage(),
];
return [
'parse' => $parseMsgs,
'text' => $textMsgs,
];
}
public function enableModuleContentVersion() {
return true;
}
public function getDependencies( ResourceLoaderContext $context = null ) {
return [
'ext.visualEditor.base',
'ext.visualEditor.mediawiki',
];
}
}