User:Bawolff/stash.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// Super hacky script to add some useful links to Special:UploadStash. Mostly useful for debugging.
// Specifically
//   * Adds a link to 100px thumbnail (in order to get around 1mb download limit of stashed files)
//   * Adds a link to the file metadata so you can verify its the file you think it is (By looking at file size, and sha1 hash)
//   * Adds a publish link fso that a user can take a file out of their upload stash on onto the wiki (bare bones, no option for filling out description, etc)

// To use, add to [[special:MyPage/common.js]]
//   importScript( 'User:Bawolff/stash.js' );

$(function() {
if ( wgCanonicalSpecialPageName === 'UploadStash' ) {
 $( '#mw-content-text li' ).each( function () {
  var li = $(this );
  li.find( 'a' ).first().each( function () {
   var fileid = this.href.replace( /^.*\/([^/]+)$/, '$1' );
   var ext = fileid.replace( /^.*\.([^\.]*)$/, '$1' );
   var url;
   if ( ext === 'png' || ext === 'svg' || ext === 'gif' || ext === 'jpg' ) {
    url = wgScriptPath + '/index.php?title=Special:UploadStash/thumb/' + encodeURIComponent( fileid + '/100px-' + fileid );
    li.append( ' (<a href="' +url + '">thumb</a>, ' );
   } else if( ext === 'ogv' || ext === 'webm' || ext === 'ogx' || ext === 'ogg' ) {
    // meh, this will probably fail for audio files non-nicely.
    url = wgScriptPath + '/index.php?title=Special:UploadStash/thumb/' + encodeURIComponent( fileid + '/100px--' + fileid );
    li.append( ' (<a href="' +url + '">thumb</a>, ' );
   } else {
    li.append( ' (' );
   }

   var meta = $( '<a href="#">metadata</a>' ).click( function() {
    var opt = {
     action: 'query',
     prop: 'stashimageinfo',
     siifilekey: fileid,
     siiprop: 'size|sha1|mime|metadata',
     format: 'json'
    };
    var cb = function( data ) { alert( data ) };
    $.post( wgScriptPath + "/api.php", opt , cb, "text" );
    return false;
   });
   li.append( meta );
   li.append( ', ' );

   var pub = $( '<a href="#">publish</a>' ).click( function () {
    var fileName = prompt( "Filename to use on wiki, including extension, not including 'File:' prefix. Empty to cancel", fileid );
    if ( !fileName ) return;
    var opt = {
     filekey: fileid,
     ignorewarnings: 1,
     comment: "Rescued from upload stash using [[User:Bawolff/stash.js]]",
     text: '{<nowiki></nowiki>{subst:nld}}\n\nThis file was uploaded by a stupid script, and needs a real editor to add an appropriate description',
     token: mw.user.tokens.get( 'editToken' ),
     async: 1,
     filename: fileName,
     format: 'json',
     action: 'upload'
    };
    var cb = function( data, status ) {
     alert( "Trying to publish file. Please wait ~10 minutes, and see if it works by checking [[Special:AllMyUploads]].\n\n-----------\n" + status + "\n\n" + data );
    }
    $.post( wgScriptPath + "/api.php", opt , cb, "text" );
    return false;
   });
   li.append( pub );
   li.append( ')' );
  });
 }); 
}

});