r76121 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76120‎ | r76121 | r76122 >
Date:18:39, 5 November 2010
Author:questpc
Status:deferred
Tags:
Comment:
Initial attempt to use ResourseLoader in case it's available (1.17+)
Modified paths:
  • /trunk/extensions/WikiSync/WikiSync.js (modified) (history)
  • /trunk/extensions/WikiSync/WikiSync.php (modified) (history)
  • /trunk/extensions/WikiSync/WikiSyncPage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiSync/WikiSyncPage.php
@@ -160,67 +160,13 @@
161161 );
162162 }
163163
164 - /*
165 - * include stylesheets and scripts; set javascript variables
166 - * @param $outputPage - an instance of OutputPage
167 - * @param $isRTL - whether the current language is RTL
168 - */
169 - static function headScripts( &$outputPage, $isRTL ) {
170 - global $wgJsMimeType;
171 - $outputPage->addLink(
172 - array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => WikiSyncSetup::$ScriptPath . '/WikiSync.css?' . WikiSyncSetup::$version )
173 - );
174 - if ( $isRTL ) {
175 - $outputPage->addLink(
176 - array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => WikiSyncSetup::$ScriptPath . '/WikiSync_rtl.css?' . WikiSyncSetup::$version )
177 - );
178 - }
179 - $outputPage->addScript(
180 - '<script type="' . $wgJsMimeType . '" src="' . WikiSyncSetup::$ScriptPath . '/WikiSync.js?' . WikiSyncSetup::$version . '"></script>
181 - <script type="' . $wgJsMimeType . '" src="' . WikiSyncSetup::$ScriptPath . '/WikiSync_Utils.js?' . WikiSyncSetup::$version . '"></script>
182 - <script type="' . $wgJsMimeType . '">WikiSyncUtils.addEvent(window,"load",WikiSync.onloadHandler);</script>
183 - <script type="' . $wgJsMimeType . '">
184 - WikiSync.setLocalNames( ' .
185 - self::getJsObject( 'wsLocalMessages', 'last_op_error', 'synchronization_confirmation', 'synchronization_success', 'already_synchronized', 'sync_to_itself', 'diff_search', 'revision', 'file_size_mismatch' ) .
186 - ');</script>' . "\n"
187 - );
188 - }
189 -
190 - static function getJsObject( $method_name ) {
191 - $args = func_get_args();
192 - array_shift( $args ); // remove $method_name from $args
193 - $result = '{ ';
194 - $firstElem = true;
195 - foreach ( $args as &$arg ) {
196 - if ( $firstElem ) {
197 - $firstElem = false;
198 - } else {
199 - $result .= ', ';
200 - }
201 - $result .= $arg . ': "' . Xml::escapeJsString( call_user_func( array( 'self', $method_name ), $arg ) ) . '"';
202 - }
203 - $result .= ' }';
204 - return $result;
205 - }
206 -
207 - /*
208 - * currently passed to Javascript:
209 - * localMessages
210 - */
211 - /*
212 - * getJsObject callback
213 - */
214 - static private function wsLocalMessages( $arg ) {
215 - return wfMsg( "wikisync_js_${arg}" );
216 - }
217 -
218164 function __construct() {
219165 parent::__construct( 'WikiSync', 'edit' );
220166 $this->initUser = WikiSyncSetup::initUser();
221167 }
222168
223169 function execute( $param ) {
224 - global $wgOut, $wgContLang;
 170+ global $wgOut;
225171 global $wgUser;
226172 # commented out, ignored by FF 3+ anyway
227173 # $wgOut->enableClientCache( false );
@@ -236,7 +182,6 @@
237183 if ( !$wgUser->isAnon() ) {
238184 WikiSyncSetup::$remote_wiki_user = $wgUser->getName();
239185 }
240 - self::headScripts( $wgOut, $wgContLang->isRTL() );
241186 $wgOut->setPagetitle( wfMsgHtml( 'wikisync' ) );
242187 $this->initSyncDirectionTpl();
243188 $this->initRemoteLoginFormTpl();
Index: trunk/extensions/WikiSync/WikiSync.php
@@ -94,11 +94,23 @@
9595 static $ExtDir; // filesys path with windows path fix
9696 static $ScriptPath; // apache virtual path
9797
 98+ static $jsMessages = array(
 99+ 'wikisync_last_op_error',
 100+ 'wikisync_synchronization_confirmation',
 101+ 'wikisync_synchronization_success',
 102+ 'wikisync_already_synchronized',
 103+ 'wikisync_sync_to_itself',
 104+ 'wikisync_diff_search',
 105+ 'wikisync_revision',
 106+ 'wikisync_file_size_mismatch'
 107+ );
 108+
98109 static function init() {
99110 global $wgScriptPath;
100111 global $wgAutoloadClasses;
101112 global $wgAjaxExportList;
102113 global $wgAPIModules;
 114+ global $wgHooks;
103115
104116 self::$ExtDir = str_replace( "\\", "/", dirname( __FILE__ ) );
105117 $top_dir = explode( '/', self::$ExtDir );
@@ -133,6 +145,8 @@
134146 $wgAjaxExportList[] = 'WikiSyncClient::transferFileBlock';
135147 $wgAjaxExportList[] = 'WikiSyncClient::uploadLocalFile';
136148
 149+ $wgHooks['ResourceLoaderRegisterModules'][] = 'WikiSyncSetup::registerModules';
 150+
137151 if ( ($parsed_url = parse_url( self::$proxy_address )) !== false ) {
138152 if ( isset( $parsed_url['host'] ) ) { self::$proxy_host = $parsed_url['host']; }
139153 if ( isset( $parsed_url['port'] ) ) { self::$proxy_port = $parsed_url['port']; }
@@ -141,16 +155,87 @@
142156 }
143157 }
144158
 159+ /**
 160+ * MW 1.17+ ResourceLoader module hook (JS,CSS)
 161+ */
 162+ static function registerModules( $resourceLoader ) {
 163+ global $wgExtensionAssetsPath;
 164+ $localpath = dirname( __FILE__ );
 165+ $remotepath = "$wgExtensionAssetsPath/WikiSync";
 166+ sdv_debug(__METHOD__,array($localpath,$remotepath),true);
 167+ $resourceLoader->register(
 168+ array(
 169+ 'ext.wikisync' => new ResourceLoaderFileModule(
 170+ array(
 171+ 'scripts' => array( 'WikiSync.js', 'WikiSync_utils.js'),
 172+ 'styles' => 'WikiSync.css',
 173+ 'messages' => self::$jsMessages
 174+ ),
 175+ $localpath,
 176+ $remotepath
 177+ )
 178+ )
 179+ );
 180+ return true;
 181+ }
 182+
 183+ /*
 184+ * include stylesheets and scripts; set javascript variables
 185+ * @param $outputPage - an instance of OutputPage
 186+ * @param $isRTL - whether the current language is RTL
 187+ */
 188+ static function headScripts( &$outputPage, $isRTL ) {
 189+ global $wgJsMimeType;
 190+ if ( class_exists( 'ResourceLoader' ) ) {
 191+# $outputPage->addModules( 'jquery' );
 192+ $outputPage->addModules( 'ext.wikisync' );
 193+ return;
 194+ }
 195+ $outputPage->addLink(
 196+ array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => self::$ScriptPath . '/WikiSync.css?' . self::$version )
 197+ );
 198+ if ( $isRTL ) {
 199+ $outputPage->addLink(
 200+ array( 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => self::$ScriptPath . '/WikiSync_rtl.css?' . self::$version )
 201+ );
 202+ }
 203+ $outputPage->addScript(
 204+ '<script type="' . $wgJsMimeType . '" src="' . self::$ScriptPath . '/WikiSync.js?' . self::$version . '"></script>
 205+ <script type="' . $wgJsMimeType . '" src="' . self::$ScriptPath . '/WikiSync_Utils.js?' . self::$version . '"></script>
 206+ <script type="' . $wgJsMimeType . '">WikiSyncUtils.addEvent(window,"load",WikiSync.onloadHandler);</script>
 207+ <script type="' . $wgJsMimeType . '">
 208+ WikiSync.setLocalMessages( ' .
 209+ self::getJsObject( 'wsLocalMessages', self::$jsMessages ) .
 210+ ');</script>' . "\n"
 211+ );
 212+ }
 213+
 214+ static function getJsObject( $method_name, $jsMessages ) {
 215+ $result = '{ ';
 216+ $firstElem = true;
 217+ foreach ( $jsMessages as &$arg ) {
 218+ if ( $firstElem ) {
 219+ $firstElem = false;
 220+ } else {
 221+ $result .= ', ';
 222+ }
 223+ $result .= $arg . ': "' . Xml::escapeJsString( wfMsg( $arg ) ) . '"';
 224+ }
 225+ $result .= ' }';
 226+ return $result;
 227+ }
 228+
145229 static function checkUserMembership( $groups ) {
146 - global $wgUser, $wgLang;
 230+ global $wgUser;
147231 $ug = $wgUser->getEffectiveGroups();
148232 if ( !$wgUser->isAnon() && !in_array( 'user', $ug ) ) {
149233 $ug[] = 'user';
150234 }
 235+ sdv_debug("ug",$ug,true);
151236 if ( array_intersect( $groups, $ug ) ) {
152237 return true;
153238 }
154 - return wfMsg( 'wikisync_api_result_noaccess', $wgLang->commaList( $groups ) );
 239+ return wfMsg( 'wikisync_api_result_noaccess', implode( $groups, ',' ) );
155240 }
156241
157242 /*
@@ -171,6 +256,7 @@
172257 return self::checkUserMembership( self::$ltr_access_groups );
173258 } elseif ( $direction === null ) {
174259 $groups = array_merge( self::$rtl_access_groups, self::$ltr_access_groups );
 260+ sdv_debug("initUser groups",$groups,true);
175261 return self::checkUserMembership( $groups );
176262 }
177263 return 'Bug: direction should be boolean or null, value (' . $direction . ') given in ' . __METHOD__;
Index: trunk/extensions/WikiSync/WikiSync.js
@@ -97,6 +97,8 @@
9898 },
9999
100100 // localized UI messages
 101+ // warning: do not access directly anymore, because
 102+ // these are not initialized in 1.17+ codepath
101103 localMessages : null,
102104
103105 setLocalNames : function( localMessages ) {
@@ -104,16 +106,28 @@
105107 },
106108
107109 formatMessage : function() {
108 - var formatted = this.localMessages[ arguments[0] ];
 110+ // in case of future ResourceLoader adoption in Extension:CategoryBrowser there
 111+ // should be few methods with different prefixes instead of just one
 112+ var prefix = 'wikisync_';
 113+ if ( typeof mediaWiki === 'object' &&
 114+ typeof mediaWiki.msg === 'function' ) {
 115+ // MW 1.17+
 116+ var args = arguments;
 117+ args[0] = prefix + args[0];
 118+ return mediaWiki.msg.apply( mediaWiki.msg, args );
 119+ }
 120+ var formatted = this.localMessages[ prefix + arguments[0] ];
109121 var indexes = [];
110122 var pos;
111123 var j;
112 - // going in reverse order is very important for the next for loop to be correct
 124+ // gettting $n parameter indexes
 125+ // going in reverse order is very important for the second for loop to be correct
113126 for ( var i = arguments.length - 1; i > 0; i-- ) {
114127 if ( ( pos = formatted.indexOf( '$' + i ) ) !== -1 ) {
115128 indexes.push( pos );
116129 }
117130 }
 131+ // substituting the parameters
118132 for ( i = 0; i < indexes.length; i++ ) {
119133 pos = indexes[i];
120134 j = formatted.charAt( pos + 1 );
@@ -907,7 +921,7 @@
908922 this.srcLastId = this.srcHiId = parseInt( this.popAJAXresult( 'src_rev_last', ['query', 'revisionhistory', 0, 'revid'] ) );
909923 // uncomment next line for "live" debugging
910924 // this.srcLastId = this.srcHiId = 75054;
911 - this.syncPercents.display( { 'desc' : this.localMessages['diff_search'], 'curr' : 0, 'min' : 0, 'max' : Math.ceil( this.mathLogBase( this.srcLastId - this.srcFirstId, 2 ) ) } );
 925+ this.syncPercents.display( { 'desc' : this.formatMessage( 'diff_search' ), 'curr' : 0, 'min' : 0, 'max' : Math.ceil( this.mathLogBase( this.srcLastId - this.srcFirstId, 2 ) ) } );
912926 this.findCommonRev( { 'opcode' : 'start' } );
913927 return;
914928 }

Status & tagging log