MediaWiki:VIhelper.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 vihelper =
{
 doneRE : /\{\{VICbotMove\|(.+)\|([^\^|}]+)\}\}/,
 //doneRE : /\{\{VICbotMove\|([^\}]+)\}\}/,
 imfileRE : /^\s*([Ii]mage:|[Ff]ile:)(.*)$/,
 oldonsubmit : null,
 categories : [
  '- select -',
  'Activities/Sports and Games',
  'Activities/Arts',
  'Activities/Techniques',
  'Activities/Occupations',
  'Activities/Other',
  'Companies, organizations and institutions',
  'Concepts and ideas/Religious',
  'Concepts and ideas/Social and cultural',
  'Concepts and ideas/Other',
  'Events',
  'Food and drink',
  'Historical/Culture',
  'Historical/Events',
  'Historical/Wars, revolutions and battles',
  'Historical/Documents',
  'Historical/Other',
  'Life forms/Amoeboids',
  'Life forms/Amoeboids/Mycetozoa',
  'Life forms/Animals/Cnidaria',
  'Life forms/Animals/Molluscs',
  'Life forms/Animals/Segmented worms (Annelida)',
  'Life forms/Animals/Arthropods/Spiders, mites and horseshoe crabs (Chelicerata)',
  'Life forms/Animals/Arthropods/Millipedes and centipedes (Myriapoda)',
  'Life forms/Animals/Arthropods/Mayflies (Ephemeroptera)',
  'Life forms/Animals/Arthropods/Dragonflies and damselflies (Odonata)',
  'Life forms/Animals/Arthropods/Grasshoppers, crickets and locusts (Orthoptera)',
  'Life forms/Animals/Arthropods/Mantids (Mantodea)',
  'Life forms/Animals/Arthropods/Bugs (Hemiptera)',
  'Life forms/Animals/Arthropods/Ants, bees and wasps (Hymenoptera)',
  'Life forms/Animals/Arthropods/Beetles (Coleoptera)',
  'Life forms/Animals/Arthropods/Raphidioptera',
  'Life forms/Animals/Arthropods/Mecoptera',
  'Life forms/Animals/Arthropods/Flies and mosquitoes (Diptera)',
  'Life forms/Animals/Arthropods/Butterflies and moths (Lepidoptera)',
  'Life forms/Animals/Arthropods/Other insects',
  'Life forms/Animals/Arthropods/Crustaceans',
  'Life forms/Animals/Echinoderms',
  'Life forms/Animals/Fish',
  'Life forms/Animals/Amphibians',
  'Life forms/Animals/Reptiles',
  'Life forms/Animals/Birds',
  'Life forms/Animals/Mammals/Domesticated',
  'Life forms/Animals/Mammals/Wild',
  'Life forms/Animals/Other',
  'Life forms/Animals/Terminology',
  'Life forms/Fungi/Moulds and Mushrooms',
  'Life forms/Fungi/Lichen',
  'Life forms/Plants/Ferns and club mosses (Pteridophyta)',
  'Life forms/Plants/Green algae',
  'Life forms/Plants/Mosses and Liverworts (Bryophyta)',
  'Life forms/Plants/Seed plants (Spermatophyta)/Other (Ginkophyta, Cycadophyta and Gnetophyta)',
  'Life forms/Plants/Seed plants (Spermatophyta)/Conifers (Pinophyta)',
  'Life forms/Plants/Seed plants (Spermatophyta)/Flowering plants (Magnoliophyta)',
  'Life forms/Plants/Terminology',
  'Natural phenomena',
  'Objects/Cameras, optics and microscopes',
  'Objects/Electronics and electrical',
  'Objects/Geological objects/Fossils',
  'Objects/Geological objects/Rocks, minerals and elements',
  'Objects/Household items',
  'Objects/Industrial',
  'Objects/Musical',
  'Objects/Transport and vehicles/Automobiles',
  'Objects/Transport and vehicles/Balloons, aeroplanes, helicopters, etc.',
  'Objects/Transport and vehicles/Boats and ships',
  'Objects/Transport and vehicles/Cycles and motorcycles',
  'Objects/Transport and vehicles/Railway',
  'Objects/Transport and vehicles/Other',
  'Objects/Other',
  'People/Artists',
  'People/Athletes',
  'People/Leaders',
  'People/Religious',
  'People/Scientists and experts',
  'People/Other',
  'Places/Countries and regions',
  'Places/Cities and towns',
  'Places/Buildings/Castles, palaces and fortresses',
  'Places/Buildings/Cultural',
  'Places/Buildings/Industrial buildings',
  'Places/Buildings/Institutional',
  'Places/Buildings/Ports, airports and railway stations',
  'Places/Buildings/Religious buildings and shrines',
  'Places/Buildings/Skyscrapers and towers',
  'Places/Buildings/Bridges',
  'Places/Buildings/Other',
  'Places/Natural/Mountain sites',
  'Places/Natural/Oceans and seas',
  'Places/Natural/Rivers and lakes',
  'Places/Natural/Islands and shore sites',
  'Places/Natural/Other',
  'Places/Other',
  'Science/Astronomy',
  'Science/Engineering',
  'Science/Ethnology',
  'Science/Mathematics',
  'Science/Medicine, biology and biotechnology',
  'Science/Physics and chemistry',
  'Science/Computer science',
  'Works of art/Paintings and pictorial arts',
  'Works of art/Statues, monuments and plaques',
  'Works of art/Music',
  'Works of art/Architecture',
  'Works of art/Other'
 ],

 install : function()
 {
  var editform = document.getElementById( 'editform' );
  if( editform !== null )
  {
   vihelper.oldonsubmit = editform.onsubmit;
   editform.onsubmit = vihelper.commitChanges;
   vihelper.buildGallery();
  }
 },

 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( 'vigallery' );
  if( preview === null )
  {
   preview = document.createElement( 'DIV' );
   preview.id = 'vigallery';
   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 = vihelper.imfileRE.exec( line ); 
    if( m && 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 = '//commons.wikimedia.org/wiki/File:' + file;
     img = document.createElement( 'IMG' );
     img.src = '//commons.wikimedia.org/w/index.php?width=150&title=Special:FilePath/' + file;
     link.appendChild( img );
     div.appendChild( link );
     div.appendChild( document.createElement( 'BR' ) );
     if( vihelper.doneRE.test( desc ) )
     {
      vihelper.doneRE.exec( desc );
      scope = RegExp.$1;
      if (scope == '')
      {
        noScope = document.createElement('b');
        noScope.appendChild(document.createTextNode('NO VALID SCOPE!'));
        div.appendChild(noScope);
        dropdown = vihelper.buildDropdown( RegExp.$2 );
        dropdown.disabled = true;
        div.appendChild(dropdown);
      }
      else
      {
        scopePrefix = document.createElement( 'B' );
        scopePrefix.appendChild( document.createTextNode('VI scope: '));
        div.appendChild(scopePrefix);
        div.appendChild( document.createTextNode( scope ) );
        div.appendChild( vihelper.buildDropdown( RegExp.$2 ) );
        div.style.border = '1px solid gray';
      }
     }
     else //then scope consists in all the image description
     {
      scope = desc;
      if (scope == '')
      {
        noScope = document.createElement('b');
        noScope.appendChild(document.createTextNode('NO VALID SCOPE!'));
        div.appendChild(noScope);
        dropdown = vihelper.buildDropdown( '' );
        dropdown.disabled = true;
        div.appendChild(dropdown);
      }
      else
      {
        scopePrefix = document.createElement( 'B' );
        scopePrefix.appendChild( document.createTextNode('VI scope: '));
        div.appendChild(scopePrefix);
        div.appendChild( document.createTextNode( scope ) );
        div.appendChild( vihelper.buildDropdown( '' ) );
        div.style.border = '2px solid red';
      }
     }
     div.vi_image = file;
     preview.appendChild( div );
    }
   }
  }
  /*
  var button = document.createElement('INPUT');
  button.onclick = vihelper.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 vihelper.categories )
  {
   option = document.createElement( 'OPTION' );
   option.appendChild( document.createTextNode( vihelper.categories[key] ) );
   if( text == vihelper.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].vi_image )
   {
    results[divs[i].vi_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 = vihelper.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 = '';
     }
     // Extract scope
     if ( vihelper.doneRE.test( desc ) )
     {
      vihelper.doneRE.exec(desc);
      scope = RegExp.$1;
     }
    else
    {
      scope = desc;
    }
     
     // already tagged?
     if( results[file] && results[file] != '- select -' )
     {
      line = 'File:' + file.replace( /[\n\r]/, '' ) + '|' + '{{VICbotMove|' + scope + '|' + results[file] + '}}';// + desc.replace( vihelper.doneRE, '' );
     }
    }
   }
   newwikitext += line + "\n";
  }
  textbox.value = newwikitext;
  if( vihelper.oldonsubmit ) vihelper.oldonsubmit();
 }
}

mwSetupToolbar = function() { return false; };

if( mw.config.get('wgAction') == "edit" && mw.config.get('wgPageName') == "Commons:Valued_images/Recently_promoted" ) 
 $(vihelper.install);