2012-06-21 20:39:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2013-08-30 00:55:31 +00:00
|
|
|
* Resource loader module providing extra data from the server to VisualEditor.
|
2012-06-21 20:39:27 +00:00
|
|
|
*
|
|
|
|
* @file
|
2012-07-19 00:11:26 +00:00
|
|
|
* @ingroup Extensions
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2012-07-19 00:11:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
2012-06-21 20:39:27 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-30 00:55:31 +00:00
|
|
|
class VisualEditorDataModule extends ResourceLoaderModule {
|
2012-06-21 20:39:27 +00:00
|
|
|
|
|
|
|
/* Protected Members */
|
|
|
|
|
2012-12-04 06:56:41 +00:00
|
|
|
protected $origin = self::ORIGIN_USER_SITEWIDE;
|
2013-08-29 22:53:33 +00:00
|
|
|
protected $gitInfo;
|
|
|
|
protected $gitHeadHash;
|
2013-10-11 18:42:46 +00:00
|
|
|
protected $targets = array( 'desktop', 'mobile' );
|
2012-06-21 20:39:27 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2013-08-29 22:53:33 +00:00
|
|
|
public function __construct () {
|
|
|
|
$this->gitInfo = new GitInfo( __DIR__ );
|
|
|
|
}
|
|
|
|
|
2012-06-21 20:39:27 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
2013-08-29 22:53:33 +00:00
|
|
|
// Messages
|
2012-12-06 05:27:44 +00:00
|
|
|
$msgInfo = $this->getMessageInfo();
|
2013-12-09 23:00:43 +00:00
|
|
|
$parsedMessages = array();
|
2013-05-18 17:34:25 +00:00
|
|
|
$messages = array();
|
2012-12-06 05:27:44 +00:00
|
|
|
foreach ( $msgInfo['args'] as $msgKey => $msgArgs ) {
|
2013-12-09 23:00:43 +00:00
|
|
|
$parsedMessages[ $msgKey ] = call_user_func_array( 'wfMessage', $msgArgs )
|
2012-12-06 05:27:44 +00:00
|
|
|
->inLanguage( $context->getLanguage() )
|
|
|
|
->parse();
|
2012-12-04 06:56:41 +00:00
|
|
|
}
|
2013-05-18 17:34:25 +00:00
|
|
|
foreach ( $msgInfo['vals'] as $msgKey => $msgVal ) {
|
|
|
|
$messages[ $msgKey ] = $msgVal;
|
|
|
|
}
|
2013-08-30 00:55:31 +00:00
|
|
|
|
2013-08-29 22:53:33 +00:00
|
|
|
// Version information
|
|
|
|
$language = Language::factory( $context->getLanguage() );
|
|
|
|
|
2013-09-18 01:40:00 +00:00
|
|
|
$hash = $this->getGitHeadHash();
|
|
|
|
$id = $hash ? substr( $this->getGitHeadHash(), 0, 7 ) : false;
|
2013-08-29 22:53:33 +00:00
|
|
|
$url = $this->gitInfo->getHeadViewUrl();
|
|
|
|
$date = $this->gitInfo->getHeadCommitDate();
|
|
|
|
$dateString = $date ? $language->timeanddate( $date, true ) : '';
|
|
|
|
|
2013-05-18 17:34:25 +00:00
|
|
|
return
|
2013-08-30 00:55:31 +00:00
|
|
|
've.init.platform.addParsedMessages(' . FormatJson::encode(
|
2013-12-09 23:00:43 +00:00
|
|
|
$parsedMessages,
|
2013-08-30 00:55:31 +00:00
|
|
|
ResourceLoader::inDebugMode()
|
|
|
|
) . ');'.
|
|
|
|
've.init.platform.addMessages(' . FormatJson::encode(
|
|
|
|
$messages,
|
|
|
|
ResourceLoader::inDebugMode()
|
2013-08-29 22:53:33 +00:00
|
|
|
) . ');'.
|
2013-09-18 01:40:00 +00:00
|
|
|
// Documented in .docs/external.json
|
2013-08-29 22:53:33 +00:00
|
|
|
've.version = ' . FormatJson::encode(
|
|
|
|
array(
|
|
|
|
'id' => $id,
|
|
|
|
'url' => $url,
|
|
|
|
'timestamp' => $date,
|
|
|
|
'dateString' => $dateString,
|
|
|
|
), ResourceLoader::inDebugMode()
|
|
|
|
) . ';';
|
2012-06-21 20:39:27 +00:00
|
|
|
}
|
|
|
|
|
2012-12-06 05:27:44 +00:00
|
|
|
protected function getMessageInfo() {
|
|
|
|
$msgKeys = array();
|
|
|
|
|
2013-05-17 14:27:33 +00:00
|
|
|
// Messages that just require simple parsing
|
2012-12-06 05:27:44 +00:00
|
|
|
$msgArgs = array(
|
|
|
|
'minoredit' => array( 'minoredit' ),
|
2013-07-13 11:44:25 +00:00
|
|
|
'missingsummary' => array( 'missingsummary' ),
|
2013-07-18 22:34:16 +00:00
|
|
|
'summary' => array( 'summary' ),
|
2012-12-06 05:27:44 +00:00
|
|
|
'watchthis' => array( 'watchthis' ),
|
2013-05-26 15:23:03 +00:00
|
|
|
'visualeditor-browserwarning' => array( 'visualeditor-browserwarning' ),
|
2012-12-11 23:19:21 +00:00
|
|
|
'visualeditor-report-notice' => array( 'visualeditor-report-notice' ),
|
2013-07-13 11:44:25 +00:00
|
|
|
'visualeditor-wikitext-warning' => array( 'visualeditor-wikitext-warning' ),
|
2012-12-06 05:27:44 +00:00
|
|
|
);
|
|
|
|
|
2013-05-18 17:34:25 +00:00
|
|
|
// Override message value
|
|
|
|
$msgVals = array(
|
2013-05-30 11:00:36 +00:00
|
|
|
'visualeditor-feedback-link' => wfMessage( 'visualeditor-feedback-link' )
|
|
|
|
->inContentLanguage()
|
|
|
|
->text(),
|
2013-05-18 17:34:25 +00:00
|
|
|
);
|
|
|
|
|
2012-12-06 05:27:44 +00:00
|
|
|
// Copyright warning (based on EditPage::getCopyrightWarning)
|
|
|
|
global $wgRightsText;
|
|
|
|
if ( $wgRightsText ) {
|
|
|
|
$copywarnMsg = array( 'copyrightwarning',
|
|
|
|
'[[' . wfMessage( 'copyrightpage' )->inContentLanguage()->text() . ']]',
|
|
|
|
$wgRightsText );
|
|
|
|
} else {
|
|
|
|
$copywarnMsg = array( 'copyrightwarning2',
|
|
|
|
'[[' . wfMessage( 'copyrightpage' )->inContentLanguage()->text() . ']]' );
|
|
|
|
}
|
|
|
|
// EditPage supports customisation based on title, we can't support that here
|
|
|
|
// since these messages are cached on a site-level. $wgTitle is likely set to null.
|
|
|
|
$title = Title::newFromText( 'Dwimmerlaik' );
|
|
|
|
wfRunHooks( 'EditPageCopyrightWarning', array( $title, &$copywarnMsg ) );
|
|
|
|
|
|
|
|
// Keys used in copyright warning
|
|
|
|
$msgKeys[] = 'copyrightpage';
|
|
|
|
$msgKeys[] = $copywarnMsg[0];
|
|
|
|
// Normalise to 'copyrightwarning' so we have a consistent key in the front-end.
|
|
|
|
$msgArgs[ 'copyrightwarning' ] = $copywarnMsg;
|
|
|
|
|
2013-05-30 11:00:36 +00:00
|
|
|
$msgKeys = array_values( array_unique( array_merge(
|
|
|
|
$msgKeys,
|
|
|
|
array_keys( $msgArgs ),
|
|
|
|
array_keys( $msgVals )
|
|
|
|
) ) );
|
2012-12-06 05:27:44 +00:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'keys' => $msgKeys,
|
|
|
|
'args' => $msgArgs,
|
2013-05-18 17:34:25 +00:00
|
|
|
'vals' => $msgVals,
|
2012-12-06 05:27:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-06-21 20:39:27 +00:00
|
|
|
public function getMessages() {
|
2012-12-06 05:27:44 +00:00
|
|
|
// We don't actually use the client-side message system for these messages.
|
|
|
|
// But we're registering them in this standardised method to make use of the
|
|
|
|
// getMsgBlobMtime utility to make cache invalidation work out-of-the-box.
|
|
|
|
|
|
|
|
$msgInfo = $this->getMessageInfo();
|
|
|
|
return $msgInfo['keys'];
|
2012-06-21 20:39:27 +00:00
|
|
|
}
|
2012-07-20 23:59:59 +00:00
|
|
|
|
2012-06-21 20:39:27 +00:00
|
|
|
public function getDependencies() {
|
|
|
|
return array( 'ext.visualEditor.base' );
|
|
|
|
}
|
2012-12-04 06:56:41 +00:00
|
|
|
|
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
return max(
|
2013-08-29 22:53:33 +00:00
|
|
|
$this->getGitHeadModifiedTime( $context ),
|
2012-12-04 06:56:41 +00:00
|
|
|
$this->getMsgBlobMtime( $context->getLanguage() ),
|
|
|
|
// Also invalidate this module if this file changes (i.e. when messages were
|
2013-08-30 00:55:31 +00:00
|
|
|
// added or removed, or when the Javascript invocation in getScript is changed).
|
2013-05-30 11:00:36 +00:00
|
|
|
// Use 1 because 0 = now, would invalidate continously
|
|
|
|
file_exists( __FILE__ ) ? filemtime( __FILE__ ) : 1
|
2012-12-04 06:56:41 +00:00
|
|
|
);
|
|
|
|
}
|
2013-08-29 22:53:33 +00:00
|
|
|
|
|
|
|
protected function getGitHeadModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
$cache = wfGetCache( CACHE_ANYTHING );
|
|
|
|
$key = wfMemcKey( 'resourceloader', 'vedatamodule', 'changeinfo' );
|
|
|
|
|
|
|
|
$hash = $this->getGitHeadHash();
|
|
|
|
|
|
|
|
$result = $cache->get( $key );
|
|
|
|
if ( is_array( $result ) && $result['hash'] === $hash ) {
|
|
|
|
return $result['timestamp'];
|
|
|
|
}
|
|
|
|
$timestamp = wfTimestamp();
|
|
|
|
$cache->set( $key, array(
|
|
|
|
'hash' => $hash,
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
) );
|
|
|
|
return $timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getGitHeadHash() {
|
2013-09-18 01:40:00 +00:00
|
|
|
if ( $this->gitHeadHash === null ) {
|
2013-08-29 22:53:33 +00:00
|
|
|
$this->gitHeadHash = $this->gitInfo->getHeadSHA1();
|
|
|
|
}
|
|
|
|
return $this->gitHeadHash;
|
|
|
|
}
|
2012-06-21 20:39:27 +00:00
|
|
|
}
|