User:Neolexx/ExCommons.JS

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

/** exCommons - copy files to individual Wikis before deleting them on Commons (c) 2014 Magnus Manske Released under GPL v2+

  • /

$(document).ready ( function () {

var exCommons = {

sites : {} , no_local_upload : [ 'es.wikipedia.org' ] , auto_check : [ 'en.wikipedia.org' ] , empty : true , oauth : 'https://tools.wmflabs.org/magnustools/oauth_uploader.php?callback=?' , authorized : false ,

init : function () { if ( wgAction !== 'delete' ) return ; // not deleting if ( wgNamespaceNumber !== 6 ) return ; // not a file

var h = "

Checking OAuth...
Finding global usage of this file...

" ;

$('#mw-content-text').prepend ( h ) ; this.checkOAuth() ; this.checkGlobalUsage() ; } ,

checkGlobalUsage : function ( gucontinue ) { var self = this ; var params = { action:'query', rawcontinue:, prop:'globalusage', gulimit:500, gufilterlocal:1, guprop:'namespace', format:'json' , titles:wgPageName } ; if ( typeof gucontinue !== 'undefined' ) params.gucontinue = gucontinue ; $.post ( '/w/api.php' , params , function ( d ) { $.each ( ((d.query||{}).pages||{}) , function ( file_page_id , v ) { $.each ( (v.globalusage||[]) , function ( k2 , v2 ) { self.empty = false ; if ( v2.ns === 0 ) { self.sites[v2.wiki] = true ; // NS 0 } else { if ( typeof self.sites[v2.wiki] === 'undefined' ) self.sites[v2.wiki] = false ; // Other namespace } } ) ; } ) ;

if ( typeof d['query-continue'] !== 'undefined' ) { self.checkGlobalUsage ( d['query-continue'].globalusage.gucontinue ) ; return ; }

self.showResults() ; } , 'json' ) ; } ,

showResults : function () { var self = this ; if ( self.empty ) { $('#ex_commons_main').html ( "This file is not used in any other project." ) ; return ; }

var h = "

This file is used on other Wikimedia projects

" ; h += "

Note: It appears that you need to be an admin on the target wiki itself to upload a file if the same name exists on Commons. Fix will be supplied over the weekend.

" ; h += "

" ;

$.each ( self.sites , function ( site , has_ns0 ) { var text, siteEscaped = mw.html.escape( site || );

if ( has_ns0 ) text = "" + siteEscaped + "" ; else text = "" + siteEscaped + "" ;

h += "
" ;

if ( -1 !== $.inArray ( site , self.no_local_upload ) ) { h += "<input type='checkbox' disabled /> " + text + " no local upload available" ; } else { h += "<label><input type='checkbox' class='ex_commons_cb' site='" + siteEscaped + "' " ; if ( has_ns0 && -1 !== $.inArray ( site , self.auto_check ) ) h += " checked " ; h += " /> " ; h += text ; h += "</label>" ; } h += " " ;

h += "
" ;

} ) ;

h += "

" ;

h += "<input type='button' id='ex_commons_run' value='Copy file to selected wikis' />" ;

// Get file description $.get ( '/w/api.php' , { action:'parse', page:wgPageName, format:'json', prop:'wikitext' } , function ( d ) { self.desc = d.parse.wikitext['*']|| ; self.desc = self.desc.replace ( /\{\{delete.+?\}\}/i , ) ; self.desc = "Template:From Commons\n\n" + self.desc ;

$('#ex_commons_main').html ( h ) ; $('#ex_commons_run').click ( self.run ) ; } , 'json' ) ; } ,

run : function () { var self = exCommons ; if ( !self.authorized ) { alert ( "OAuth is required for this to work. Please click on the link on the right and authorize, then reload this page." ) ; return ; } var targets = [] ; $('#ex_commons_main input:checked').each ( function () { targets.push ( $(this).attr('site') ) ; } ) ;

var running = targets.length ; if ( running === 0 ) { alert ( "No wikis selected!" ) ; return ; }

$.get ( '/w/api.php' , { action:'query', titles:wgPageName, prop:'imageinfo', format:'json', iiprop:'url' } , function ( d0 ) { var url ; $.each ( (d0.query.pages||{}) , function ( pageid , v ) { url = v.imageinfo[0].url ; } ) ; if ( typeof url === 'undefined' ) { alert ( "Can not find URL for this file; deleted already?" ) ; return ; }

$.each ( targets , function ( dummy , site ) { $.getJSON ( self.oauth , { action:'upload', site:site, url:url, newfile:wgTitle, desc:self.desc, ignorewarnings:1, comment:'File is about to be deleted on Commons, evacuating to individual wikis', botmode:1 } , function ( d ) { var o = $('span.ex_commons_result[site="'+site+'"]') ;

console.log ( d ) ; if ( d.error === 'OK' ) { o.html ( "OK" ) ; } else { o.html ( ""+d.res.error.info+"" ) ; }

running-- ; if ( running === 0 ) self.finish() ; // console.log ( d ) ; } ) ; } ) ; } , 'json' ) ;

} ,

finish : function () { // TODO } ,

checkOAuth : function () { var self = this ; $.getJSON ( self.oauth , { action:'checkauth', botmode:1 } , function ( d ) { if ( d.error === 'OK' ) { $('#ex_commons_oauth').html ( "OAuth OK!" ) ; self.authorized = true ; } else { $('#ex_commons_oauth').html ( "<a target='_blank' href='https://tools.wmflabs.org/magnustools/oauth_uploader.php?action=authorize'>Log into OAuth!</a>" ) ; } } ) ; } ,

fin : } ;


exCommons.init() ; } ) ;