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' ),
|
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;
|
|
|
|
|
2014-05-08 21:56:35 +00:00
|
|
|
// Citation tools
|
|
|
|
$msgVals['visualeditor-cite-tool-definition.json'] = json_encode( self::getCitationTools() );
|
|
|
|
|
|
|
|
$msgKeys = array_values( array_unique( array_merge(
|
|
|
|
$msgKeys,
|
|
|
|
array_keys( $msgArgs ),
|
|
|
|
array_keys( $msgVals )
|
|
|
|
) ) );
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'keys' => $msgKeys,
|
|
|
|
'args' => $msgArgs,
|
|
|
|
'vals' => $msgVals,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the list of citation templates that we want to make available in the
|
|
|
|
* VisualEditor toolbar (via the Cite dropdown). These are defined on-wiki at
|
|
|
|
* MediaWiki:Visualeditor-cite-tool-definition.json.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getCitationTools() {
|
Ultra-mega-hyper-citation editing on crack
Objectives:
* Allow users on-wiki to create tools and dialogs for citation templates
of their choosing
* Allow editing of citation templates directly, without having to go
through the reference dialog
* Provide citation template tools within reference editing that use the
same titles and icons as the citation tools do, but don't wrap the
inserted content in a ref tag
Changes:
* Reference list was cloning the DOM element it was inserting into its
view before the generated content node could finish rendering, so it
never ended up showing the finished rendering in the reference list
* Documenting hack about use of reference list node's destroy method,
and how we are depending on destroy not canceling generated content
rendering
* Introduced reference model
* Added saving/updating method to transclusion model
* Added getPartsList method to dm transclusion node, which caches the
result and invalidates the cache on update
* Added citation dialog, which extends transclusion dialog
* Added cite group to toolbars, cite-template in reference dialog toolbar
* Factored out getting the node to edit and saving changes procedures in
transclusion dialog so they could be extended in citation dialog
* Updated uses of autoAdd as per changes in oojs-ui (Ic353f91)
* Renamed MWDialogTool file since there was only one tool in it
* Expanded TransclusionDialogTool file out since there is now more logic
to it
* Switched to using ve.dm.MWReferenceModel instead of plain objects in
reference search widget
Configuration:
If you add to MediaWiki:Visualeditor-cite-tool-definition.json the
following code you will magically be presented with a delightful array
of citation options:
[
{ "name": "web", "icon": "ref-cite-web", "template": "Cite web" },
{ "name": "book", "icon": "ref-cite-book", "template": "Cite book" },
{ "name": "news", "icon": "ref-cite-news", "template": "Cite news" },
{ "name": "journal", "icon": "ref-cite-journal", "template": "Cite journal" }
]
...or...
[
{
"name": "any-name",
"icon": "any-ooui-icon",
"template": "Any template",
"title": "Any title text"
}
]
The title text is derived either from the title property or from the name
property by pre-pending the string 'visualeditor-cite-tool-name-' to
generate a message key. Titles for 'web', 'book', 'news' and 'journal' are
provided. The icon is a normal oo-ui-icon name, and more icons can be
added, as usual, by adding a class called .oo-ui-icon-{icon name} to
MediaWiki:Common.css. 'ref-cite-web', 'ref-cite-book', 'ref-cite-news'
and 'ref-cite-journal' are provided. The template name is simply the name
of the template without its namespace prefix.
Depends on Ic353f91 in oojs-ui
Bug: 50110
Bug: 50768
Change-Id: Id401d973b8d5fe2faec481cc777c17a24fd19dd4
2014-03-21 18:56:46 +00:00
|
|
|
$citationDefinition = json_decode(
|
|
|
|
wfMessage( 'visualeditor-cite-tool-definition.json' )->plain()
|
|
|
|
);
|
|
|
|
$citationTools = array();
|
|
|
|
if ( is_array( $citationDefinition ) ) {
|
|
|
|
foreach ( $citationDefinition as $tool ) {
|
|
|
|
if ( !isset( $tool->title ) ) {
|
|
|
|
$tool->title =
|
|
|
|
wfMessage( 'visualeditor-cite-tool-name-' . $tool->name )->text();
|
|
|
|
$msgKeys[] = $tool->title;
|
|
|
|
}
|
|
|
|
$citationTools[] = $tool;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 21:56:35 +00:00
|
|
|
return $citationTools;
|
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
|
|
|
}
|