From be12ebf1ab651d0b689c1bf0494c13d454cad833 Mon Sep 17 00:00:00 2001 From: Krinkle Date: Sat, 31 Dec 2011 21:25:00 +0000 Subject: [PATCH] [Core JS] More fixing of global config variable usage * mw.config is the new way, and global config variable lookups are deprecated * Based on two phase3-wide quick searches: -- of " wg": http://toolserver.org/~krinkle/wikimedia-svn-search/view.php?id=321&hash=81700bf7486e4fee3b7bc1f83eb9eba6 -- of "!wg": http://toolserver.org/~krinkle/wikimedia-svn-search/view.php?id=327&hash=47c9d54a7a1d5d58a724dd834585f40d Related changes: * Changed some php comments mentioning "wg" variables to include the dollar sign, and a typo when the wf function prefix was meant. * Removed TODO comment in wikibits.js and made it use the JS equivalent of wfUrlencode, which we have now, mw.util.wikiUrlencode * SpecialUpload.php: use OutputPage::addJsConfigVars instead of creating a new script tag through OutputPage::addScript(Skin::makeVariablesScript(..)) * Renamed wgUploadSetup in upload.js and made it local. Not used anywhere in ./trunk/phase3 and ./trunk/extensions * Fix OutputPage::addJsConfigVars so that it can actually be called with an array instead of two arguments for key/value * Some minor whitespace/convention stuff around the same line --- docs/hooks.txt | 2 +- includes/HTMLForm.php | 2 +- includes/OutputPage.php | 4 +- includes/logging/LogEventsList.php | 2 +- includes/specials/SpecialUpload.php | 2 +- maintenance/language/messages.inc | 2 +- resources/Resources.php | 4 +- skins/common/ajax.js | 4 +- skins/common/mwsuggest.js | 2 +- skins/common/protect.js | 20 +++++---- skins/common/upload.js | 43 ++++++++++++-------- skins/common/wikibits.js | 7 ++-- tests/phpunit/includes/MWNamespaceTest.php | 2 +- tests/phpunit/includes/upload/UploadTest.php | 2 +- tests/phpunit/skins/SideBarTest.php | 6 +-- 15 files changed, 57 insertions(+), 47 deletions(-) diff --git a/docs/hooks.txt b/docs/hooks.txt index 2d63e8b4bd..ad58d7ec46 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -1221,7 +1221,7 @@ $param: Associative Array with the following additional options: - lim Integer Limit of items to show, default is 50 - conds Array Extra conditions for the query (e.g. "log_action != 'revision'") - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty if set to true (default), "No matching items in log" is displayed if loglist is empty - - msgKey Array If you want a nice box with a message, set this to the key of the message. First element is the message key, additional optional elements are parameters for the key that are processed with wgMsgExt and option 'parse' + - msgKey Array If you want a nice box with a message, set this to the key of the message. First element is the message key, additional optional elements are parameters for the key that are processed with wfMsgExt and option 'parse' - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html (usually something like "<div ...>$1</div>"). - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS) diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index 05597774ad..099b51e9e2 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -423,7 +423,7 @@ class HTMLForm extends ContextSource { } /** - * Display the form (sending to wgOut), with an appropriate error + * Display the form (sending to $wgOut), with an appropriate error * message or stack of messages, and any validation errors, etc. * @param $submitResult Mixed output from HTMLForm::trySubmit() */ diff --git a/includes/OutputPage.php b/includes/OutputPage.php index c83401fa49..84fe1e9ef7 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2750,9 +2750,9 @@ $templates * Add one or more variables to be set in mw.config in JavaScript. * * @param $key {String|Array} Key or array of key/value pars. - * @param $value {Mixed} Value of the configuration variable. + * @param $value {Mixed} [optional] Value of the configuration variable. */ - public function addJsConfigVars( $keys, $value ) { + public function addJsConfigVars( $keys, $value = null ) { if ( is_array( $keys ) ) { foreach ( $keys as $key => $value ) { $this->mJsConfigVars[$key] = $value; diff --git a/includes/logging/LogEventsList.php b/includes/logging/LogEventsList.php index 5458bac189..0da6b0229b 100644 --- a/includes/logging/LogEventsList.php +++ b/includes/logging/LogEventsList.php @@ -614,7 +614,7 @@ class LogEventsList { * if set to true (default), "No matching items in log" is displayed if loglist is empty * - msgKey Array If you want a nice box with a message, set this to the key of the message. * First element is the message key, additional optional elements are parameters for the key - * that are processed with wgMsgExt and option 'parse' + * that are processed with wfMsgExt and option 'parse' * - offset Set to overwrite offset parameter in $wgRequest * set to '' to unset offset * - wrap String Wrap the message in html (usually something like "
$1
"). diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index 5782e462f1..720c8ba447 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -1084,7 +1084,7 @@ class UploadForm extends HTMLForm { ); $out = $this->getOutput(); - $out->addScript( Skin::makeVariablesScript( $scriptVars ) ); + $out->addJsConfigVars( $scriptVars ); $out->addModules( array( diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index 22f725605c..a8030e4c58 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -1015,7 +1015,7 @@ $wgMessageStructure = array( 'email', 'prefs-help-realname', - # 3 messages depending upon wgEmailConfirmToEdit and $wgEnableUserEmail + # 3 messages depending upon $wgEmailConfirmToEdit and $wgEnableUserEmail 'prefs-help-email', 'prefs-help-email-others', 'prefs-help-email-required', diff --git a/resources/Resources.php b/resources/Resources.php index 8edbc24b64..8d73540e0e 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -765,7 +765,7 @@ return array( 'size-gigabytes', 'largefileserver', ), - 'dependencies' => array( 'mediawiki.libs.jpegmeta' ), + 'dependencies' => array( 'mediawiki.libs.jpegmeta', 'mediawiki.util' ), ), /* MediaWiki Legacy */ @@ -774,7 +774,7 @@ return array( 'scripts' => 'common/ajax.js', 'remoteBasePath' => $GLOBALS['wgStylePath'], 'localBasePath' => $GLOBALS['wgStyleDirectory'], - 'dependencies' => 'mediawiki.legacy.wikibits', + 'dependencies' => array( 'mediawiki.util', 'mediawiki.legacy.wikibits' ), ), 'mediawiki.legacy.commonPrint' => array( 'styles' => array( 'common/commonPrint.css' => array( 'media' => 'print' ) ), diff --git a/skins/common/ajax.js b/skins/common/ajax.js index 2a93373f6a..121f9d12f1 100644 --- a/skins/common/ajax.js +++ b/skins/common/ajax.js @@ -84,9 +84,7 @@ window.sajax_do_call = function(func_name, args, target) { var i, x, n; var uri; var post_data; - uri = wgServer + - ( ( wgScript == null ) ? ( wgScriptPath + '/index.php' ) : wgScript ) + - '?action=ajax'; + uri = mw.util.wikiScript() + '?action=ajax'; if ( sajax_request_type == 'GET' ) { if ( uri.indexOf( '?' ) == -1 ) { uri = uri + '?rs=' + encodeURIComponent( func_name ); diff --git a/skins/common/mwsuggest.js b/skins/common/mwsuggest.js index 7045e1bfa6..dac59546ec 100644 --- a/skins/common/mwsuggest.js +++ b/skins/common/mwsuggest.js @@ -478,7 +478,7 @@ window.os_delayedFetch = function() { var query = os_timer.query; os_timer = null; var path = mw.config.get( 'wgMWSuggestTemplate' ).replace( "{namespaces}", os_getNamespaces( r ) ) - .replace( "{dbname}", wgDBname ) + .replace( "{dbname}", mw.config.get( 'wgDBname' ) ) .replace( "{searchTerms}", os_encodeQuery( query ) ); // try to get from cache, if not fetch using ajax diff --git a/skins/common/protect.js b/skins/common/protect.js index c771d57253..a23c0cbd8a 100644 --- a/skins/common/protect.js +++ b/skins/common/protect.js @@ -77,16 +77,20 @@ window.ProtectionForm = { }, /** - * Is this protection level cascadeable? - * @param level String - * - * @return boolean - * + * Checks if a cerain protection level is cascadeable. + * @param level {String} + * @return {Boolean} */ 'isCascadeableLevel': function( level ) { - for (var k = 0; k < wgCascadeableLevels.length; k++) { - if ( wgCascadeableLevels[k] == level ) { - return true; + var cascadeLevels, len, i; + + cascadeLevels = mw.config.get( 'wgCascadeableLevels' ); + // cascadeLevels isn't defined on all pages + if ( cascadeLevels ) { + for ( i = 0, len = cascadeLevels.length; i < len; i += 1 ) { + if ( cascadeLevels[i] === level ) { + return true; + } } } return false; diff --git a/skins/common/upload.js b/skins/common/upload.js index 2e8f54e89a..8e08af31c5 100644 --- a/skins/common/upload.js +++ b/skins/common/upload.js @@ -1,3 +1,7 @@ +( function () { +var ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ), + fileExtensions = mw.config.get( 'wgFileExtensions' ); + window.licenseSelectorCheck = function() { var selector = document.getElementById( "wpLicense" ); var selection = selector.options[selector.selectedIndex].value; @@ -11,7 +15,7 @@ window.licenseSelectorCheck = function() { wgUploadLicenseObj.fetchPreview( selection ); }; -window.wgUploadSetup = function() { +function uploadSetup() { // Disable URL box if the URL copy upload source type is not selected var e = document.getElementById( 'wpSourceTypeurl' ); if( e ) { @@ -36,7 +40,7 @@ window.wgUploadSetup = function() { } // AJAX wpDestFile warnings - if ( wgAjaxUploadDestCheck ) { + if ( ajaxUploadDestCheck ) { // Insert an event handler that fetches upload warnings when wpDestFile // has been changed document.getElementById( 'wpDestFile' ).onchange = function ( e ) { @@ -54,7 +58,7 @@ window.wgUploadSetup = function() { } var wpLicense = document.getElementById( 'wpLicense' ); - if ( wgAjaxLicensePreview && wpLicense ) { + if ( mw.config.get( 'wgAjaxLicensePreview' ) && wpLicense ) { // License selector check wpLicense.onchange = licenseSelectorCheck; @@ -74,8 +78,11 @@ window.wgUploadSetup = function() { // fillDestFile setup - for ( var i = 0; i < wgUploadSourceIds.length; i++ ) - document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) { + var i, + uploadSourceIds = mw.config.get( 'wgUploadSourceIds' ), + len = uploadSourceIds.length; + for ( i = 0; i < len; i += 1 ) + document.getElementById( uploadSourceIds[i] ).onchange = function (e) { fillDestFilename( this.id ); }; }; @@ -89,7 +96,7 @@ window.wgUploadWarningObj = { 'timeoutID': false, 'keypress': function () { - if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return; + if ( !ajaxUploadDestCheck || !sajax_init_object() ) return; // Find file to upload var destFile = document.getElementById('wpDestFile'); @@ -114,7 +121,7 @@ window.wgUploadWarningObj = { }, 'checkNow': function (fname) { - if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return; + if ( !ajaxUploadDestCheck || !sajax_init_object() ) return; if ( this.timeoutID ) { window.clearTimeout( this.timeoutID ); } @@ -123,7 +130,7 @@ window.wgUploadWarningObj = { }, 'timeout' : function() { - if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return; + if ( !ajaxUploadDestCheck || !sajax_init_object() ) return; injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' ); // Get variables into local scope so that they will be preserved for the @@ -168,7 +175,7 @@ window.wgUploadWarningObj = { }; window.fillDestFilename = function(id) { - if (!wgUploadAutoFill) { + if ( !mw.config.get( 'wgUploadAutoFill' ) ) { return; } if (!document.getElementById) { @@ -197,12 +204,12 @@ window.fillDestFilename = function(id) { // Clear the filename if it does not have a valid extension. // URLs are less likely to have a useful extension, so don't include them in the // extension check. - if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) { + if ( mw.config.get( 'wgStrictFileExtensions' ) && fileExtensions && id !== 'wpUploadFileURL' ) { var found = false; - if( fname.lastIndexOf( '.' ) != -1 ) { + if ( fname.lastIndexOf( '.' ) !== -1 ) { var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 ); - for( var i = 0; i < wgFileExtensions.length; i++ ) { - if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) { + for ( var i = 0; i < fileExtensions.length; i += 1 ) { + if ( fileExtensions[i].toLowerCase() === ext.toLowerCase() ) { found = true; break; } @@ -229,7 +236,7 @@ window.fillDestFilename = function(id) { // Replace spaces by underscores fname = fname.replace( / /g, '_' ); // Capitalise first letter if needed - if ( wgCapitalizeUploads ) { + if ( mw.config.get( 'wgCapitalizeUploads' ) ) { fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) ); } @@ -253,7 +260,7 @@ window.wgUploadLicenseObj = { 'responseCache' : { '' : '' }, 'fetchPreview': function( license ) { - if( !wgAjaxLicensePreview ) return; + if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) return; for (cached in this.responseCache) { if (cached == license) { this.showPreview( this.responseCache[license] ); @@ -265,7 +272,7 @@ window.wgUploadLicenseObj = { var title = document.getElementById('wpDestFile').value; if ( !title ) title = 'File:Sample.jpg'; - var url = wgScriptPath + '/api' + wgScriptExtension + var url = mw.util.wikiScript( 'api' ) + '?action=parse&text={{' + encodeURIComponent( license ) + '}}' + '&title=' + encodeURIComponent( title ) + '&prop=text&pst&format=json'; @@ -293,4 +300,6 @@ window.wgUploadLicenseObj = { }; -addOnloadHook( wgUploadSetup ); +$( document ).ready( uploadSetup ); + +}() ); diff --git a/skins/common/wikibits.js b/skins/common/wikibits.js index 4fb94e682a..a619f7193a 100644 --- a/skins/common/wikibits.js +++ b/skins/common/wikibits.js @@ -69,9 +69,8 @@ window.addOnloadHook = function( hookFunct ) { }; window.importScript = function( page ) { - // TODO: might want to introduce a utility function to match wfUrlencode() in PHP - var uri = wgScript + '?title=' + - encodeURIComponent(page.replace(/ /g,'_')).replace(/%2F/ig,'/').replace(/%3A/ig,':') + + var uri = mw.config.get( 'wgScript' ) + '?title=' + + mw.util.wikiUrlencode( page ) + '&action=raw&ctype=text/javascript'; return importScriptURI( uri ); }; @@ -90,7 +89,7 @@ window.importScriptURI = function( url ) { }; window.importStylesheet = function( page ) { - return importStylesheetURI( wgScript + '?action=raw&ctype=text/css&title=' + encodeURIComponent( page.replace(/ /g,'_') ) ); + return importStylesheetURI( mw.config.get( 'wgScript' ) + '?action=raw&ctype=text/css&title=' + mw.util.wikiUrlencode( page ) ); }; window.importStylesheetURI = function( url, media ) { diff --git a/tests/phpunit/includes/MWNamespaceTest.php b/tests/phpunit/includes/MWNamespaceTest.php index a95f08adb6..6b231fc51f 100644 --- a/tests/phpunit/includes/MWNamespaceTest.php +++ b/tests/phpunit/includes/MWNamespaceTest.php @@ -422,7 +422,7 @@ class MWNamespaceTest extends MediaWikiTestCase { $this->assertEquals( array( NS_MAIN, NS_USER, NS_CATEGORY ), MWNamespace::getcontentNamespaces(), - 'NS_MAIN is forced in wgContentNamespaces even if unwanted' + 'NS_MAIN is forced in $wgContentNamespaces even if unwanted' ); # test other cases, return $wgcontentNamespaces as is diff --git a/tests/phpunit/includes/upload/UploadTest.php b/tests/phpunit/includes/upload/UploadTest.php index 38d6aee04f..4293d23b74 100644 --- a/tests/phpunit/includes/upload/UploadTest.php +++ b/tests/phpunit/includes/upload/UploadTest.php @@ -100,7 +100,7 @@ class UploadTest extends MediaWikiTestCase { } /** - * test uploading a 100 bytes file with wgMaxUploadSize = 100 + * test uploading a 100 bytes file with $wgMaxUploadSize = 100 * * This method should be abstracted so we can test different settings. */ diff --git a/tests/phpunit/skins/SideBarTest.php b/tests/phpunit/skins/SideBarTest.php index 441302a549..ed74c6e8f5 100644 --- a/tests/phpunit/skins/SideBarTest.php +++ b/tests/phpunit/skins/SideBarTest.php @@ -136,7 +136,7 @@ class SideBarTest extends MediaWikiLangTestCase { } /** - * Test wgNoFollowLinks in sidebar + * Test $wgNoFollowLinks in sidebar */ function testRespectWgnofollowlinks() { global $wgNoFollowLinks; @@ -145,7 +145,7 @@ class SideBarTest extends MediaWikiLangTestCase { $attribs = $this->getAttribs(); $this->assertArrayNotHasKey( 'rel', $attribs, - 'External URL in sidebar do not have rel=nofollow when wgNoFollowLinks = false' + 'External URL in sidebar do not have rel=nofollow when $wgNoFollowLinks = false' ); // Restore global @@ -153,7 +153,7 @@ class SideBarTest extends MediaWikiLangTestCase { } /** - * Test wgExternaLinkTarget in sidebar + * Test $wgExternaLinkTarget in sidebar */ function testRespectExternallinktarget() { global $wgExternalLinkTarget; -- 2.20.1