mediawiki-extensions-Visual.../VisualEditor.hooks.php

157 lines
5.2 KiB
PHP
Raw Normal View History

<?php
/**
* VisualEditor extension hooks
*
* @file
* @ingroup Extensions
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
class VisualEditorHooks {
/** List of skins VisualEditor integration supports */
protected static $supportedSkins = array( 'vector', 'apex', 'monobook' );
/**
* Adds VisualEditor JS to the output if in the correct namespace.
*
* This is attached to the MediaWiki 'BeforePageDisplay' hook.
*
* @param $output OutputPage
* @param $skin Skin
*/
public static function onBeforePageDisplay( &$output, &$skin ) {
global $wgVisualEditorNamespaces;
if (
// Disable on redirect pages until redirects are editable
!$skin->getTitle()->isRedirect() &&
// User has the 'visualeditor-enable' preference set
$skin->getUser()->getOption( 'visualeditor-enable' ) &&
in_array( $skin->getSkinName(), self::$supportedSkins ) &&
(
// Article in the VisualEditor namespace
in_array( $skin->getTitle()->getNamespace(), $wgVisualEditorNamespaces ) ||
// Special page action for an article in the VisualEditor namespace
in_array( $skin->getRelevantTitle()->getNamespace(), $wgVisualEditorNamespaces )
) &&
// Only use VisualEditor if the page is wikitext, not CSS/JS
$skin->getTitle()->getContentModel() === CONTENT_MODEL_WIKITEXT
) {
$output->addModules( array( 'ext.visualEditor.viewPageTarget' ) );
}
return true;
}
public static function onGetPreferences( $user, &$preferences ) {
$preferences['visualeditor-enable'] = array(
'type' => 'toggle',
'label-message' => 'visualeditor-preference-enable',
'section' => 'editing/beta'
);
return true;
}
public static function onListDefinedTags( &$tags ) {
$tags[] = 'visualeditor';
return true;
}
/**
* Adds extra variables to the page config.
*/
public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) {
global $wgStylePath, $wgContLang;
$vars['wgVisualEditor'] = array(
'isPageWatched' => $out->getUser()->isWatched( $out->getTitle() ),
'pageLanguageCode' => $out->getTitle()->getPageLanguage()->getHtmlCode(),
'pageLanguageDir' => $out->getTitle()->getPageLanguage()->getDir(),
'magnifyClipIconURL' => $wgStylePath . '/common/images/magnify-clip' . ( $wgContLang->isRTL() ? '-rtl' : '' ) . '.png' // Same as in Linker.php
);
return true;
}
/**
* Adds extra variables to the global config
*/
public static function onResourceLoaderGetConfigVars( array &$vars ) {
global $wgVisualEditorEnableSectionEditLinks, $wgVisualEditorParsoidProblemReportURL,
$wgVisualEditorParsoidURL, $wgVisualEditorEnableExperimentalCode;
$vars['wgVisualEditorConfig'] = array(
'enableSectionEditLinks' => $wgVisualEditorEnableSectionEditLinks,
'reportProblemURL' => $wgVisualEditorParsoidProblemReportURL !== null ?
$wgVisualEditorParsoidProblemReportURL :
"$wgVisualEditorParsoidURL/_bugs/",
'enableExperimentalCode' => $wgVisualEditorEnableExperimentalCode,
);
return true;
}
public static function onResourceLoaderTestModules( array &$testModules, ResourceLoader &$resourceLoader ) {
$testModules['qunit']['ext.visualEditor.test'] = array(
'scripts' => array(
// QUnit plugin
've.qunit.js',
// VisualEditor Tests
Refactor ve.getHash: Stabilize cross-browser differences; + unit tests * Replaces c8b4a289364966432b58104e975d37cda1fefb84 * Use Object() casting to detect objects instead of .constructor (or instanceof). Both .constructor and instanceof compare by reference the type "Object" which means if the object comes from another window (where there is a different "Object" and "Object.prototype") it will drop out of the system and go freewack. Theory: If a variable casted to an object returns true when strictly compared to the original, the input must be an object. Which is true. It doesn't change the inheritance, it doesn't make it inherit from this window's Object if the object is from another window's object. All it does is cast to an object if not an object already. So e.g. "Object(5) !== 5" because 5 is a primitive value as opposed to an instance of Number. And contrary to "typeof", it doesn't return true for "null". * .constructor also has the problem that it only works this way if the input is a plain object. e.g. a simple construtor function that creates an object also get in the wrong side of the if/else case since it is an instance of Object, but not directly (rather indirectly via another constructor). * Added unit tests for basic getHash usage, as well as regression tests against the above two mentioned problems (these tests fail before this commit). * While at it, also improved other utilities a bit. - Use hasOwnProperty instead of casting to boolean when checking for presence of native support. Thanks to Douglas Crockford for that tip. - Fix documentation for ve.getHash: Parameter is not named "obj". - Add Object-check to ve.getObjectKeys per ES5 Object.keys spec (to match native behavior) - Add Object-check to ve.getObjectValues to match ve.getObjectKeys - Improved performance of ve.getObjectKeys shim. Tried several potential optimizations and compared with jsperf. Using a "static" reference to hasOwn improves performance (by not having to look it up 4 scopes up and 3 property levels deep). Also using [.length] instead of .push() shared off a few ms. - Added unit tests for ve.getObjectValues Change-Id: If24d09405321f201c67f7df75d332bb1171c8a36
2012-08-12 18:27:31 +00:00
've.test.js',
've.example.js',
've.Document.test.js',
've.Element.test.js',
've.Node.test.js',
've.BranchNode.test.js',
've.LeafNode.test.js',
've.Factory.test.js',
// VisualEditor DataModel Tests
'dm/ve.dm.example.js',
'dm/ve.dm.AnnotationSet.test.js',
'dm/ve.dm.NodeFactory.test.js',
'dm/ve.dm.Node.test.js',
'dm/ve.dm.Converter.test.js',
'dm/ve.dm.BranchNode.test.js',
'dm/ve.dm.LeafNode.test.js',
'dm/ve.dm.LinearData.test.js',
'dm/nodes/ve.dm.TextNode.test.js',
'dm/nodes/ve.dm.MWTemplateNode.test.js',
'dm/ve.dm.Document.test.js',
'dm/ve.dm.DocumentSynchronizer.test.js',
'dm/ve.dm.IndexValueStore.test.js',
'dm/ve.dm.Transaction.test.js',
'dm/ve.dm.TransactionProcessor.test.js',
'dm/ve.dm.Surface.test.js',
'dm/ve.dm.SurfaceFragment.test.js',
'dm/ve.dm.ModelRegistry.test.js',
'dm/ve.dm.MetaList.test.js',
'dm/ve.dm.Model.test.js',
'dm/lineardata/ve.dm.ElementLinearData.test.js',
'dm/lineardata/ve.dm.MetaLinearData.test.js',
// VisualEditor ContentEditable Tests
'ce/ve.ce.test.js',
'ce/ve.ce.Document.test.js',
'ce/ve.ce.NodeFactory.test.js',
'ce/ve.ce.Node.test.js',
'ce/ve.ce.BranchNode.test.js',
'ce/ve.ce.ContentBranchNode.test.js',
'ce/ve.ce.LeafNode.test.js',
'ce/nodes/ve.ce.TextNode.test.js',
// VisualEditor Actions Tests
'ui/actions/ve.ui.FormatAction.test.js',
'ui/actions/ve.ui.IndentationAction.test.js',
'ui/actions/ve.ui.ListAction.test.js',
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
// VisualEditor initialization Tests
'init/ve.init.Platform.test.js',
),
'dependencies' => array(
'ext.visualEditor.standalone',
'ext.visualEditor.core',
'ext.visualEditor.experimental',
'ext.visualEditor.viewPageTarget',
),
'localBasePath' => dirname( __FILE__ ) . '/modules/ve/test',
'remoteExtPath' => 'VisualEditor/modules/ve/test',
);
return true;
}
}