Replace remaining sajax use
authorAlex Monk <krenair@gmail.com>
Sun, 26 May 2013 16:25:30 +0000 (17:25 +0100)
committerTim Starling <tstarling@wikimedia.org>
Tue, 13 Aug 2013 02:29:31 +0000 (02:29 +0000)
Bug: 40785
Change-Id: I4a0af8986f924cd127a73828e72da6998f28536c

includes/Setup.php
includes/api/ApiQueryImageInfo.php
includes/specials/SpecialUpload.php
resources/Resources.php
skins/common/upload.js

index 4f5ac92..cfe5f1c 100644 (file)
@@ -377,10 +377,6 @@ if ( $wgInvalidateCacheOnLocalSettingsChange ) {
        $wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( "$IP/LocalSettings.php" ) ) );
 }
 
-if ( $wgAjaxUploadDestCheck ) {
-       $wgAjaxExportList[] = 'SpecialUpload::ajaxGetExistsWarning';
-}
-
 if ( $wgNewUserLog ) {
        # Add a new log type
        $wgLogTypes[] = 'newusers';
index 40041d8..0ea2868 100644 (file)
@@ -82,12 +82,17 @@ class ApiQueryImageInfo extends ApiQueryBase {
                                $start = $title === $fromTitle ? $fromTimestamp : $params['start'];
 
                                if ( !isset( $images[$title] ) ) {
-                                       $result->addValue(
-                                               array( 'query', 'pages', intval( $pageId ) ),
-                                               'imagerepository', ''
-                                       );
-                                       // The above can't fail because it doesn't increase the result size
-                                       continue;
+                                       if ( isset( $prop['uploadwarning'] ) ) {
+                                               // Uploadwarning needs info about non-existing files
+                                               $images[$title] = wfLocalFile( $title );
+                                       } else {
+                                               $result->addValue(
+                                                       array( 'query', 'pages', intval( $pageId ) ),
+                                                       'imagerepository', ''
+                                               );
+                                               // The above can't fail because it doesn't increase the result size
+                                               continue;
+                                       }
                                }
 
                                /** @var $img File */
@@ -358,6 +363,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                $mediatype = isset( $prop['mediatype'] );
                $archive = isset( $prop['archivename'] );
                $bitdepth = isset( $prop['bitdepth'] );
+               $uploadwarning = isset( $prop['uploadwarning'] );
 
                if ( ( $url || $sha1 || $meta || $mime || $mediatype || $archive || $bitdepth )
                                && $file->isDeleted( File::DELETED_FILE ) ) {
@@ -427,6 +433,10 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        $vals['bitdepth'] = $file->getBitDepth();
                }
 
+               if ( $uploadwarning ) {
+                       $vals['html'] = SpecialUpload::getExistsWarning( UploadBase::getExistsWarning( $file ) );
+               }
+
                return $vals;
        }
 
@@ -556,6 +566,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        'metadata' =>       ' metadata      - Lists Exif metadata for the version of the image',
                        'archivename' =>    ' archivename   - Adds the file name of the archive version for non-latest versions',
                        'bitdepth' =>       ' bitdepth      - Adds the bit depth of the version',
+                       'uploadwarning' =>  ' uploadwarning - Used by the Special:Upload page to get information about an existing file. Not intended for use outside MediaWiki core',
                );
        }
 
index 98d0c9a..efde5cb 100644 (file)
@@ -672,30 +672,6 @@ class SpecialUpload extends SpecialPage {
                return $warning;
        }
 
-       /**
-        * Get a list of warnings
-        *
-        * @param string $filename local filename, e.g. 'file exists', 'non-descriptive filename'
-        * @return Array: list of warning messages
-        */
-       public static function ajaxGetExistsWarning( $filename ) {
-               $file = wfFindFile( $filename );
-               if ( !$file ) {
-                       // Force local file so we have an object to do further checks against
-                       // if there isn't an exact match...
-                       $file = wfLocalFile( $filename );
-               }
-               $s = '&#160;';
-               if ( $file ) {
-                       $exists = UploadBase::getExistsWarning( $file );
-                       $warning = self::getExistsWarning( $exists );
-                       if ( $warning !== '' ) {
-                               $s = "<div>$warning</div>";
-                       }
-               }
-               return $s;
-       }
-
        /**
         * Construct a warning and a gallery from an array of duplicate files.
         * @param $dupes array
index 6352843..6f0d3be 100644 (file)
@@ -1080,6 +1080,8 @@ return array(
                'remoteBasePath' => $GLOBALS['wgStylePath'],
                'localBasePath' => $GLOBALS['wgStyleDirectory'],
                'dependencies' => array(
+                       'mediawiki.api',
+                       'mediawiki.Title',
                        'mediawiki.legacy.wikibits',
                        'mediawiki.util',
                ),
index 4246e82..2c2203c 100644 (file)
@@ -96,7 +96,7 @@ window.wgUploadWarningObj = {
        'timeoutID': false,
 
        'keypress': function () {
-               if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
+               if ( !ajaxUploadDestCheck ) return;
 
                // Find file to upload
                var destFile = document.getElementById('wpDestFile');
@@ -121,7 +121,7 @@ window.wgUploadWarningObj = {
        },
 
        'checkNow': function (fname) {
-               if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
+               if ( !ajaxUploadDestCheck ) return;
                if ( this.timeoutID ) {
                        window.clearTimeout( this.timeoutID );
                }
@@ -130,25 +130,29 @@ window.wgUploadWarningObj = {
        },
 
        'timeout' : function() {
-               if ( !ajaxUploadDestCheck || !sajax_init_object() ) return;
+               if ( !ajaxUploadDestCheck || this.nameToCheck === '' ) return;
                injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
 
-               // Get variables into local scope so that they will be preserved for the
-               // anonymous callback. fileName is copied so that multiple overlapping
-               // ajax requests can be supported.
-               var obj = this;
-               var fileName = this.nameToCheck;
-               sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
-                       function (result) {
-                               obj.processResult(result, fileName)
+               var uploadWarningObj = this;
+               ( new mw.Api ).get( {
+                       action: 'query',
+                       titles: ( new mw.Title( this.nameToCheck, mw.config.get( 'wgNamespaceIds' ).file ) ).getPrefixedText(),
+                       prop: 'imageinfo',
+                       iiprop: 'uploadwarning',
+                       indexpageids: '',
+               } ).done( function ( result ) {
+                       var result = '';
+                       if ( result.query ) {
+                               result = result.query.pages[result.query.pageids[0]].imageinfo[0];
                        }
-               );
+                       uploadWarningObj.processResult( result, uploadWarningObj.nameToCheck );
+               } );
        },
 
-       'processResult' : function (result, fileName) {
+       'processResult' : function ( result, fileName ) {
                removeSpinner( 'destcheck' );
-               this.setWarning(result.responseText);
-               this.responseCache[fileName] = result.responseText;
+               this.setWarning( result.html );
+               this.responseCache[fileName] = result.html;
        },
 
        'setWarning' : function (warning) {
@@ -159,7 +163,7 @@ window.wgUploadWarningObj = {
 
                // Set a value in the form indicating that the warning is acknowledged and
                // doesn't need to be redisplayed post-upload
-               if ( warning == '' || warning == '&nbsp;' ) {
+               if ( warning == '' ) {
                        ackElt[0].value = '';
                } else {
                        ackElt[0].value = '1';
@@ -279,18 +283,15 @@ window.wgUploadLicenseObj = {
                var title = document.getElementById('wpDestFile').value;
                if ( !title ) title = 'File:Sample.jpg';
 
-               var url = mw.util.wikiScript( 'api' )
-                       + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
-                       + '&title=' + encodeURIComponent( title )
-                       + '&prop=text&pst&format=json';
-
-               var req = sajax_init_object();
-               req.onreadystatechange = function() {
-                       if ( req.readyState == 4 && req.status == 200 )
-                               wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
-               };
-               req.open( 'GET', url, true );
-               req.send( '' );
+               ( new mw.Api ).get( {
+                       action: 'parse',
+                       text: '{{' + license + '}}',
+                       title: title,
+                       prop: 'text',
+                       pst: ''
+               } ).done( function ( result ) {
+                       wgUploadLicenseObj.processResult( result, license );
+               } );
        },
 
        'processResult' : function( result, license ) {