MediaWiki:QIhelper.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.
var qihelper =
{
 tempRE : /\{\{QICbotMove\|([^\}]+)\}\}/,
 imfileRE : /^\s*([Ii]mage:|[Ff]ile:)(.*)$/,
 oldonsubmit : null,
 categories : [
  '- select -',
  'Subject/Amoeboids',
  'Subject/Animals/Amphibians',
  'Subject/Animals/Annelida',
  'Subject/Animals/Platyhelminthes',
  'Subject/Animals/Arthropods/Spiders, Mites, Horseshoe crabs (Chelicerata)',
  'Subject/Animals/Arthropods/Dragonflies and Damselflies (Odonata)',
  'Subject/Animals/Arthropods/Grasshoppers, Crickets, Mantis, Bugs etc (Hemipterodea)',
  'Subject/Animals/Arthropods/Beetles (Coleoptera)',
  'Subject/Animals/Arthropods/Ants, Bees & Wasps (Hymenoptera)',
  'Subject/Animals/Arthropods/Butterflies and Moths (Lepidoptera)',
  'Subject/Animals/Arthropods/Flies & Mosquitoes (Diptera)',
  'Subject/Animals/Arthropods/Mayflies (Ephemeroptera)',
  'Subject/Animals/Arthropods/Mecoptera',
  'Subject/Animals/Arthropods/Raphidioptera',
  'Subject/Animals/Arthropods/Other insects',
  'Subject/Animals/Arthropods/Myriapoda',
  'Subject/Animals/Arthropods/Crustaceans',
  'Subject/Animals/Birds',
  'Subject/Animals/Cnidaria',
  'Subject/Animals/Echinoderms',
  'Subject/Animals/Fish',
  'Subject/Animals/Mammals/Domesticated',
  'Subject/Animals/Mammals/Wild',
  'Subject/Animals/Mammals/Zoo',
  'Subject/Animals/Molluscs',
  'Subject/Animals/Reptiles',
  'Subject/Animated',
  'Subject/Astronomy',
  'Subject/Events',
  'Subject/Food and drink',
  'Subject/Fungi/Fungi',
  'Subject/Fungi/Lichen',
  'Subject/Microscopic',
  'Subject/Natural phenomena',
  'Subject/Non photographic media',
  'Subject/Objects/Cameras, Optics and Microscopes',
  'Subject/Objects/Closeups of Structures',
  'Subject/Objects/Electronics & electrical',
  'Subject/Objects/Geological objects',
  'Subject/Objects/Geological objects/Fossils',
  'Subject/Objects/Geological objects/Rocks, Minerals, Elements',
  'Subject/Objects/Household Items',
  'Subject/Objects/Industrial',
  'Subject/Objects/Other',
  'Subject/Objects/Statues, Monuments and Plaques',
  'Subject/Objects/Transport and Vehicles/Aerial Trams',
  'Subject/Objects/Transport and Vehicles/Automobiles',
  'Subject/Objects/Transport and Vehicles/Balloons, Aeroplanes, Helicopters etc',
  'Subject/Objects/Transport and Vehicles/Boats and Ships',
  'Subject/Objects/Transport and Vehicles/Cycles and Motorcycles',
  'Subject/Objects/Transport and Vehicles/Other vehicles',
  'Subject/Objects/Transport and Vehicles/Railway',
  'Subject/Objects/Transport and Vehicles/Steam Powered',
  'Subject/People',
  'Subject/Places/Man made structures',
  'Subject/Places/Man made structures/Buildings, Exterior',
  'Subject/Places/Man made structures/Buildings, Interior',
  'Subject/Places/Man made structures/Panorama',
  'Subject/Places/Mixed',
  'Subject/Places/Mixed/Panorama',
  'Subject/Places/Natural structures',
  'Subject/Places/Natural structures/Panorama',
  'Subject/Plant life/Ferns and Horsetails',
  'Subject/Plant life/Flowers',
  'Subject/Plant life/Foliage etc',
  'Subject/Plant life/Fruit, berries, seeds etc',
  'Subject/Plant life/Mosses and Liverworts',
  'Subject/Plant life/Trees',
  'Subject/Sports',
  'Subject/Sunsets',
  'Subject/Works of art',
  'Technical/Color',
  'Technical/Composition',
  'Technical/Depth of field',
  'Technical/Exposure',
  'Technical/Movement control',
  'Technical/Perspective',
  'Technical/Proportion'
 ],

 install : function()
 {
  var editform = document.getElementById( 'editform' );
  if( editform !== null )
  {
   qihelper.oldonsubmit = editform.onsubmit;
   editform.onsubmit = qihelper.commitChanges;
   qihelper.buildGallery();
   $('#wpSummary').val('Categorizing new QIs using [[MediaWiki:QIhelper.js]]');
  }
 },

 buildGallery : function()
 {
  var textbox = document.getElementById('wpTextbox1');
  var wppreview = document.getElementById('wikiPreview')
  var toolbar = document.getElementById('toolbar');
  var copywarn = document.getElementById('editpage-copywarn');

  if( toolbar !== null ) toolbar.style.display = 'none';
  if( copywarn !== null ) copywarn.style.display = 'none';
  if( wppreview === null ) return;
  textbox.style.display = 'none';

  var preview = document.getElementById( 'qigallery' );
  if( preview === null )
  {
   preview = document.createElement( 'DIV' );
   preview.id = 'qigallery';
   wppreview.appendChild( preview );
  }
  else
  {
   // delete everything
  }

  var wikitext = textbox.value;
  var lines = wikitext.split( "\n" );
  var line, lline, pipe, file, desc, img, div, link;
  var inGallery = false;
  var m, filematch;

  for( var key in lines )
  {
   line = lines[key];
   lline = line.toLowerCase();

   if( lline.search('<gallery') > -1 ) inGallery = true;
   else if( lline.search('</gallery>') > -1 ) inGallery = false;
   else if( inGallery )
   {
    m = qihelper.imfileRE.exec( line ); 
    if( m != null && m.length == 3 )
    {
     filematch = m[2];
     pipe = filematch.indexOf('|');
     if( pipe > 0 ) 
     {
      file = filematch.substring( 0, pipe );
      desc = filematch.substring( pipe + 1 );
     }
     else
     {
      file = filematch;
      desc = '';
     }

     div = document.createElement( 'DIV' );
     div.style.width = '300px';
     div.style.float = 'left';
     div.style.textAlign = 'center';
     div.style.margin = '2px';
     div.style.padding = '2px';
     
     link = document.createElement( 'A' );
     link.href = 'http://commons.wikimedia.org/wiki/File:' + file;
     img = document.createElement( 'IMG' );
     img.src = 'http://commons.wikimedia.org/wiki/Special:Redirect/file/' + encodeURIComponent(file) + '?width=150';
     link.appendChild( img );
     div.appendChild( link );
     div.appendChild( document.createElement( 'BR' ) );
     if( qihelper.tempRE.test( desc ) )
     {
      qihelper.tempRE.exec( desc );
      div.appendChild( qihelper.buildDropdown( RegExp.$1 ) );
      div.style.border = '1px solid gray';
     }
     else 
     {
      div.appendChild( qihelper.buildDropdown( '' ) );
      div.style.border = '2px solid red';
     }
     div.qi_image = file;
     preview.appendChild( div );
    }
   }
  }
  /*
  var button = document.createElement('INPUT');
  button.onclick = qihelper.commitChanges;
  button.type = "submit";
  button.value = "Commit";
  preview.appendChild( button );
  */

  wppreview.style.display = 'block';
 },
 
 buildDropdown : function( text )
 {
  var option;
  var select = document.createElement( 'SELECT' );
  for( var key in qihelper.categories )
  {
   option = document.createElement( 'OPTION' );
   option.appendChild( document.createTextNode( qihelper.categories[key] ) );
   if( text == qihelper.categories[key] ) option.selected = true;
   select.appendChild( option );
  }
  select.style.width = "100%";
  return select;
 },

 commitChanges : function()
 {
  var divs = document.getElementsByTagName('DIV');
  var textbox = document.getElementById('wpTextbox1');
  var wikitext = textbox.value;
  var newwikitext = "";
  var lines = wikitext.split( "\n" );
  var line, lline, pipe, file, desc;
  var inGallery = false;
  var results = {};
  var m, filematch;

  for( var i = 0; i < divs.length; i++ )
  {
   if( divs[i].qi_image )
   {
    results[divs[i].qi_image] = divs[i].getElementsByTagName('SELECT')[0].value;
   }
  }

  for( var key in lines )
  {
   line = lines[key];
   lline = line.toLowerCase();

   if( lline.search('<gallery') > -1 ) inGallery = true;
   else if( lline.search('</gallery>') > -1 ) inGallery = false;
   else if( inGallery )
   {
    m = qihelper.imfileRE.exec( line ); 
    if( m != null && m.length == 3 )
    {
     filematch = m[2];
     pipe = filematch.indexOf('|');
     if( pipe > 0 ) 
     {
      file = filematch.substring( 0, pipe );
      desc = filematch.substring( pipe + 1 );
     }
     else
     {
      file = filematch;
      desc = '';
     }
     // already tagged?
     if( results[file] && results[file] != '- select -' )
     {
      line = 'File:' + file.replace( /[\n\r]/, '' ) + '|' + '{{QICbotMove|' + results[file] + '}}' + desc.replace( qihelper.tempRE, '' );
     }
    }
   }
   newwikitext += line + "\n";
  }
  textbox.value = newwikitext;
  if( qihelper.oldonsubmit ) qihelper.oldonsubmit();
 }
  
}
mwSetupToolbar = function() { return false; };
if( mw.config.get('wgAction') == "edit" && mw.config.get('wgPageName') == "Commons:Quality_images/Recently_promoted" ) 
  $(qihelper.install);