User:Guandalug/addOTRS.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.
// This script has shamelessly been stolen from [[:de:User:DerHexer/addotrs.js]] and only saw minor adjustments to work on commons.
// blame him if it doesn't work :D

if(!usersignature) var usersignature = "-- \~\~\~\~";


if(mw.config.get('wgNamespaceNumber')==6) 
{
  $(function() 
  { 
	var node = document.getElementById('t-whatlinkshere');

    if (mw.config.get('skin') == "vector")
      mw.util.addPortletLink('p-cactions', "javascript:add_otrs();", "OTRS", 't-otrs', 'add OTRS permission', '');
    else
	mw.util.addPortletLink('p-tb', "javascript:add_otrs();", "OTRS", 't-otrs', 'add OTRS permission', '', node);

	var wgAction = mw.config.get('wgAction');
    if ((wgAction == 'edit')  || (wgAction=='submit'))
    {
		mw.util.addPortletLink('p-tb', "javascript:add_template();","{{Information|..}}", 't-otrs', 'add image information template', '',  node);
		mw.util.addPortletLink('p-tb', "javascript:add_license('cc-by-sa-3.0');", "+CC-by-sa", 't-ccbysa', 'Lizenz CC-by-sa eintragen', '',  node);
		mw.util.addPortletLink('p-tb', "javascript:add_license('GFDL');", "+GFDL", 't-bildgfdl', 'Lizenz Bild-GFDL-Neu eintragen', '',  node);
    }
  });

  if ((mw.util.getParamValue("dootrs")== "true") && (wgAction=='edit')) 
    window.setTimeout('add_otrs2()', 500);
}

function add_otrs() 
{

	var wgAction = mw.config.get('wgAction');
  if ((wgAction!='edit') && (wgAction !='submit'))
  {
    location.href = "http://commons.wikimedia.org/w/index.php?title=" + encodeURIComponent(mw.config.get('wgPageName')) + "&action=edit&dootrs=true";
  } else {
   add_otrs2();
  }
}
 
function add_otrs2() 
{
  var counter = 0;
  while(counter != 3) 
  {
    var ticketnummer=prompt('Ticket number:','');
    if (ticketnummer == null)
    {
      counter = 3;
    } else if (ticketnummer.search(/[^\d ]/) == -1) 
    {
      counter = 3;

      otrs_text = "\{\{PermissionOTRS\|id=" + ticketnummer.trim() +"\|" + usersignature + "\}\}";
      document.editform.wpSummary.value = "OTRS-Permission (using [[User:Guandalug/addOTRS.js|addOTRS.js]])";
  
      var text = document.editform.wpTextbox1.value;

      text = text.replace(/\=\= Summary \=\=/g, '== {{int:filedesc}} ==');
      text = text.replace(/\=\= Licensing \=\=/g, '== {{int:license}} ==');

      text = text.replace(/\{\{OTRS[ _][Pp]ending[^\}\n]*\}\}/g, '');
      text = text.replace(/\{\{no[ _]license[^\}\n]*\}\}/g, '');
      text = text.replace(/\{\{no[ _]permission[^\}\n]*\}\}/g, '');
      text = text.replace(/\{\{no[ _]OTRS[ _]permission[^\}\n]*\}\}/g, '');
      text = text.replace(/\{\{OTRS[ _]received[^\}\n]*\}\}/g, '');
      text = text.replace(/\{\{delete[^\n]*\(CE[S]?T\)\s*/g, '');
      text = text.replace(/\{\{delete[^\}\n]*\}\}\b/g, '');

      replacedlicenceMatch = text.match(/\|Permission\s?\=([^\n]*)(\n)/);
      text = text.replace(/\|Permission(\s*)\=[^\n]*/i, "|permission$1= " + otrs_text)
      if (replacedlicenceMatch)
        text += "\r\n" + replacedlicenceMatch[2].toString()+replacedlicenceMatch[1].toString();

      document.editform.wpTextbox1.value = text;
    } else {
       counter = counter + 1;
    }
  } 	
}

function add_template() 
{
  document.editform.wpTextbox1.value = 
     "== {{int:filedesc}} ==\n{{Information\n|Description=\n|Source=\n|Author=\n|Date=\n|Permission=\n|other_versions=\n}}\n\n== {{int:license-header}} =="+
     document.editform.wpTextbox1.value;
}

function add_license(vorlage)
{
  if (document.editform.wpTextbox1.value.search(/\=\=.* \{\{int:license(-header)?\}\}/)>=0)
    document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.replace(/\=\=.* \{\{int:license(-header)?\}\}[^\n]*/,"== {{int:license-header}} ==\n\{\{"+vorlage+"\}\}");
  else
    document.editform.wpTextbox1.value += "== {{int:license-header}} ==\n\{\{"+vorlage+"\}\}";
}