From db51c53b8fdbe27a3d082bf625ec4dc420d085e5 Mon Sep 17 00:00:00 2001 From: Alex Monk Date: Sun, 26 May 2013 17:25:30 +0100 Subject: [PATCH] Replace remaining sajax use Bug: 40785 Change-Id: I4a0af8986f924cd127a73828e72da6998f28536c --- includes/Setup.php | 4 -- includes/api/ApiQueryImageInfo.php | 23 +++++++++--- includes/specials/SpecialUpload.php | 24 ------------ resources/Resources.php | 2 + skins/common/upload.js | 57 +++++++++++++++-------------- 5 files changed, 48 insertions(+), 62 deletions(-) diff --git a/includes/Setup.php b/includes/Setup.php index 4f5ac92b23..cfe5f1c69b 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -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'; diff --git a/includes/api/ApiQueryImageInfo.php b/includes/api/ApiQueryImageInfo.php index 40041d86ff..0ea286847b 100644 --- a/includes/api/ApiQueryImageInfo.php +++ b/includes/api/ApiQueryImageInfo.php @@ -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', ); } diff --git a/includes/specials/SpecialUpload.php b/includes/specials/SpecialUpload.php index 98d0c9acc5..efde5cb213 100644 --- a/includes/specials/SpecialUpload.php +++ b/includes/specials/SpecialUpload.php @@ -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 = ' '; - if ( $file ) { - $exists = UploadBase::getExistsWarning( $file ); - $warning = self::getExistsWarning( $exists ); - if ( $warning !== '' ) { - $s = "
$warning
"; - } - } - return $s; - } - /** * Construct a warning and a gallery from an array of duplicate files. * @param $dupes array diff --git a/resources/Resources.php b/resources/Resources.php index 6352843fd8..6f0d3beafe 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1080,6 +1080,8 @@ return array( 'remoteBasePath' => $GLOBALS['wgStylePath'], 'localBasePath' => $GLOBALS['wgStyleDirectory'], 'dependencies' => array( + 'mediawiki.api', + 'mediawiki.Title', 'mediawiki.legacy.wikibits', 'mediawiki.util', ), diff --git a/skins/common/upload.js b/skins/common/upload.js index 4246e82ea9..2c2203c753 100644 --- a/skins/common/upload.js +++ b/skins/common/upload.js @@ -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 == ' ' ) { + 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 ) { -- 2.20.1