User:Alphax/qd-merged.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.
/*
Installing this (as described on [[MediaWiki talk:Quick-delete.js]]) will give
you links in the toolbox for all pages in the Image: namespace, that say
'Nominate for deletion', 'mark as no source' and 'mark as no license'.
The first one will give you a pop-up that asks for a reason. It inserts this
reason on [[COM:DEL]], marks the image for deletion and notifies the uploader.
The second and third one mark the image as missing source/license (NSD/NLD) and
notify the uploader.

Note: don't abuse this. If you mark 10 images from one user as no source,
don't use this because they will get 10 separate warnings on their talk page!!
(Or if you do, at least clean it up afterwards.)

Please don't copy/paste this into your own monobook.js; if there's one central
copy it's much easier to keep up to date than everyone having their own copy.
If you want to disable the automatic saving, add the following to your
monobook.js somewhere after installing this:

 nfd_autosave = false;
 mnx_autosave = false;

Please note that this has no i18n (yet). Translation efforts should be left on
[[MediaWiki talk:Quick-delete.js]]. Most (but not all) of the translatable
parts are in the "Strings" section below.

DEVELOPMENT VERSION NOTE:
This isn't finished yet, hasn't been fully test, and will probably not work
for Konqueror. It ''should'' work for Firefox, but there are no guarantees.
*/

// Original code by [[:en:User:Jietse Niesen]]
// Adaption for Commons by [[user:pfctdayelise]]
// Cleanup by [[User:Alphax]]

// An en.wp version (based on the old codebase) is available
// at [[w:User:Howcheng/quickimgdelete.js]].

/*
==Automatic 'nominate for deletion', 'mark no license', 'mark no source', 'mark copyvio', 'speedy delete' scripts==
*/
// <pre>
// Configuration

// Should the edits be saved automatically?
if(window.nfd_autosave == false){}else if(window.nfd_autosave){}else{ nfd_autosave = true; }
if(window.mnx_autosave == false){}else if(window.mnx_autosave){}else{ mnx_autosave = true; }
if(window.mcv_autosave == false){}else if(window.mcv_autosave){}else{ mcv_autosave = true; }
if(window.spd_autosave == false){}else if(window.spd_autosave){}else{ spd_autosave = true; }

// String constants

// Strings for NFD - Nominate for deletion
nfd_text = "Nominate for deletion";
nfd_tooltip = "Nominate this image for deletion";
nfd_prompt = "Why do you want to nominate this image for deletion?";
nfd_delReq = "Commons:Deletion_requests";
nfd_deleteTemplate = "delete";
nfd_subpageTemplate = "delete2";
nfd_datepageTemplate = "delete3";
nfd_idwTemplate = "idw";
nfd_imageAction = "mark_delete";
nfd_userAction = "nfd_warn";
nfd_subpageAction = "nfd_create";
nfd_datepageAction = "nfd_include";

// Strings for MNX - Mark no {source|license}
mns_text = "No source";
mns_tooltip = "Mark this image as missing required source information";
mns_imageTemplate = "no source since";
mns_imageAction = "mark_nosource";

mnl_text = "No license";
mnl_tooltip = "Mark this image as missing required licensing information";
mnl_imageTemplate = "no license";
mnl_imageAction = "mark_nolicense";

mnx_lang = "lang?";
mnx_langquery = "In which language should the msg be given?";
mnx_userTemplate = "image source";
mnx_userAction = "mnx_warn";

// Strings for MCV - Mark copyvio

// Strings for SPD - Speedy delete

// Site info
scriptPath = "/w/index.php";
prettyPath = "/wiki/";
wikiHostname = "commons.wikimedia.org";

// Date and page setup
var now = new Date();
var timestamp = now.getUTCFullYear() + "/";
now.getUTCMonth() < 9 ? timestamp += "0" + (now.getUTCMonth() + 1) : timestamp += (now.getUTCMonth() + 1);
timestamp += "/";
now.getUTCDate() < 10 ? timestamp += "0" + now.getUTCDate() : timestamp += now.getUTCDate();
nfd_datePage = nfd_delReq + "/" + timestamp;
var monthsArray = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var timestamp2 = monthsArray[now.getUTCMonth()] + " " + now.getUTCDate();

// From [[en:Wikipedia:WikiProject User scripts/Scripts/addLink]]
function addLink(where, url1, name1, id, title, key, after, url2, name2)
{
    //* where is the id of the toolbar where the button should be added;
    //   i.e. one of "p-cactions", "p-personal", "p-navigation", or "p-tb".
    //
    //* url1 is the URL which will be called when the button is clicked.
    //   javascript: urls can be used to do more complex things.
    //
    //* name1 is what will appear as the name of the button.
    //
    //* id is the id of the button; it's best to define one.  
    //   Use a prefix to make sure its unique. Optional.
    //
    //* title is the tooltip title that gives a longer description 
    //   of the button; if you define a accesskey, mention it here. Optional.
    //
    //* key is the char you want for the accesskey. Optional.
    //
    //* after is the id of the button you want to follow this one. Optional.
    //
    //* url2 is a second url to add. Optional
    //
    //*name2 is the name of the second url; defaults to name1. Optional
    var na = document.createElement('a');
    na.href = url1;
    na.appendChild(document.createTextNode(name1));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);

    if (url2) //Another link, brother of the previous.
    {
       var na = document.createElement('a');
       na.href = url2;       
       na.appendChild(document.createTextNode(name2 ? name2 : name1));
       li.appendChild(document.createTextNode(" · ")); //separate them a bit
       li.appendChild(na);
    }

    var tabs = document.getElementById(where).getElementsByTagName('ul')[0];
    if(after)
    {
        tabs.insertBefore(li,document.getElementById(after));
    }
    else
    {
        tabs.appendChild(li);
    }
    if(id)
    {
        if(key && title)
        {
            ta[id] = [key, title];
        }
        else if(key)
        {
            ta[id] = [key, ''];
        }
        else if(title)
        {
            ta[id] = ['', title];
        }
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

function openWindow(url) {
  var res = window.open(url, '_blank');
  if (!res) alert("openWindow: window.open() returned null");
}

function getUploader() {
  // Get uploader from first point in the list under "File history"
  // Uploader is stored in second A tag in UL tag under "File history"
  // Returns title of user page (without name space) in URL form
  var el = document.getElementById('filehistory')
  if (!el) {
    alert("getUploader: Cannot find filehistory ... exiting");
    return null;
  }
  while (el.nextSibling) {
    el = el.nextSibling;
    if (el.tagName && el.tagName.toLowerCase() == 'ul') 
      break;
  }
  if (!el) {
    alert("getUploader: Cannot find UL tag ... exiting");
    return null;
  }
  var el = el.getElementsByTagName('li')[0];
  if (!el) {
    alert("getUploader: Cannot find LI tag ... exiting");
    return null;
  }
  var as = el.getElementsByTagName('a');

  var re1 = new RegExp('http://' + (wikiHostname + prettyPath).replace(/\./g, '\\.') + 'User:(.*)$');
  var re2 = new RegExp('http://' + (wikiHostname + scriptPath).replace(/\./g, '\\.') + '\\?title=User:([^&]*)');
  var m;
  for (var k=0; k<as.length; k++) {
     m = re1.exec(as[k].href);
     if (m) return m[1];
     m = re2.exec(as[k].href);
     if (m) return m[1];
  }
  alert("getUploader: Cannot find uploader ... exiting");
  return null;
}

/**** New bit starts here ****/

function mark_image(image_action, usertalk_action)
{
    var pagename = encodeURIComponent(mw.config.get('wgPageName'));
    var uploader = getUploader();
    if (!uploader) return;
    
    if(image_action == nfd_imageAction)
    {
        // needs cleanup
        var reason = prompt(nfd_prompt, '');
        if (!reason) return;
        // redundant - already checking for this
        // if (!uploader) return;
        // should assert that usertalk_action == nfd_userAction or ignore it entirely
        openWindow(scriptPath + '?title=User_talk:' + uploader
         + '&action=edit&fakeaction=' + nfd_userAction + '&target=' + pagename);
        openWindow(scriptPath + '?title=' + nfd_delReq + '/' + pagename 
         + '&action=edit&fakeaction=' + nfd_subpageAction + '&target=' + pagename + '&reason='
         + encodeURIComponent(reason));
        openWindow(scriptPath + '?title=' + nfd_datePage +
         '&action=edit&fakeaction=' + nfd_datepageAction + '&target=' + pagename);
        var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
        document.location = editlk + '&fakeaction=' + image_action + '&reason='
         + encodeURIComponent(reason);
    }
    else if(image_action == mns_imageAction || image_action == mnl_imageAction)
    {
        if(usertalk_action == "mnx_ask_lang")
        {
            var message_lang = prompt(mnx_langquery, wgUserLanguage);
            if (!message_lang) return; //User pressed cancel
        }
        else
        {
            message_lang = "en";
        }
        if (!message_lang) return; //User pressed cancel
        usertalk_action = mnx_userAction; // Override any previous mnx_ask_lang
        openWindow(scriptPath + '?title=User_talk:' + uploader
         + '&action=edit&fakeaction=' + usertalk_action + '&target=' + pagename + '&mnx_lang=' + message_lang);
        var editlk = document.getElementById('ca-edit').getElementsByTagName('a')[0].href;
        document.location = editlk + '&fakeaction=' + image_action + '&mnx_lang=' + message_lang;
    }
    else if(image_action == "mark_copyvio")
    {
        // experimental, unfinished, etc.
        var copyvio_source = prompt("Source?", "");
        if (!copyvio_source) return;
    }
    else if(image_action == "mark_speedy")
    {
        // experimental, unfinished, etc.
    }
}

function addTemplate(template_name)
{
    var txt = "{{";
    if(wgCanonicalNamespace == "Image")
    {
        if(template_name == nfd_deleteTemplate)
        {
            // no subst:
            txt += template_name + "|"
             + decodeURIComponent(getParamValue("reason")) + "|day="
             + now.getUTCDate() + "|month=" + monthsArray[now.getUTCMonth()]
             + "|year=" + now.getUTCFullYear() + "}}";
        }
        else if((template_name == mns_imageTemplate) ||
            (template_name == mnl_imageTemplate))
        {
            // no subst: needed unless this goes back to nsd/nld
            txt += template_name + "/" + getParamValue("mnx_lang") +
                "|month={{subst:CURRENTMONTHNAME}}|day={{subst:CURRENTDAY}}|" +
                "year={{subst:CURRENTYEAR}}" + "}}";
        }
        // potential expansion for copyvio, etc.
        else
        {
            alert("Warning! Invalid template!");
            return null;
        }
    }
    else if(wgCanonicalNamespace == "User_talk")
    {
        if((template_name == mnx_userTemplate) ||
            (template_name == nfd_idwTemplate))
        {
            txt += "subst:" + template_name + "/" + getParamValue("mnx_lang") +
                "|" + decodeURIComponent(getParamValue("target")) + "}}~~" +
                "~~\n";
        }
        // potential expansion for copyvio, etc.
        else
        {
            alert("Warning! Invalid template!");
            return null;
        }
    }
    else if(wgCanonicalNamespace == "Project")
    {
        // check if we're at a deletion requests subpage
        if((wgPageName.split("/")[0] == nfd_delReq) &&
            (wgPageName.split("/")[1] != null) &&
            (wgPageName.split("/")[2] == null))
        {
            txt += "subst:" + template_name + "|";
            if(template_name == nfd_subpageTemplate)
            {
                txt += "image=" + decodeURIComponent(getParamValue("target")) +
                    "|reason=" + decodeURIComponent(getParamValue("reason")) +
                    "-- ~~" + "~~}}";
            }
            else
            {
                alert("Warning! Invalid template!");
                return null;
            }
        }
        else if(wgPageName == nfd_datePage)
        {
            if(template_name == nfd_datepageTemplate)
            {
                txt += "pg=" + decodeURIComponent(getParamValue("target")) +
                "}}";
            }
            // potential expansion for copyvio, etc.
            else
            {
                alert("Warning! Invalid template!");
                return null;
            }
        }
        // potential expansion for copvyio, etc.
        else
        {
            alert("Warning! Invalid page!");
            return null;
        }
    }
    else
    {
        alert("Warning! Invalid page!");
        return null;
    }
    
    // Work out where to put stuff
    if(template_name == nfd_deleteTemplate)
    {
        document.editform.wpTextbox1.value = txt + "\n" +
            document.editform.wpTextbox1.value;
        document.editform.wpSummary.value = txt;
        if(nfd_autosave)
        {
            document.editform.wpSave.click();
        }
    }
    else if(template_name == nfd_subpageTemplate)
    {
        document.editform.wpTextbox1.value +=
        (document.editform.wpTextbox1.value.length > 0 ? "\n" : "") + txt;
        document.editform.wpSummary.value = "Nominating [[" +
            decodeURIComponent(getParamValue("target")) + "]]";
        if(nfd_autosave)
        {
            document.editform.wpSave.click();
        }
    }
    else if(template_name == nfd_datepageTemplate)
    {
        document.editform.wpTextbox1.value += 
        (document.editform.wpTextbox1.value.length > 0 ? '\n' : '==' +
            timestamp2 + '==\n') + txt;
        document.editform.wpSummary.value = "Nominating [[" +
            decodeURIComponent(getParamValue("target")) + "]]";
        if(nfd_autosave)
        {
            document.editform.wpSave.click();
        }
    }
    else if(template_name == nfd_idwTemplate)
    {
        document.editform.wpTextbox1.value +=
        (document.editform.wpTextbox1.value.length > 0 ? "\n" : "") + txt;
        document.editform.wpSummary.value = txt;
        if(nfd_autosave)
        {
            document.editform.wpSave.click();
        }
    }
    else if(template_name == mnx_userTemplate)
    {
        document.editform.wpTextbox1.value +=
        (document.editform.wpTextbox1.value.length > 0 ? "\n" : "") + txt;
        document.editform.wpSummary.value = "Warning: image missing source " +
            "or licensing information.";
        if(mnx_autosave)
        {
            document.editform.wpSave.click();
        }
    }
    else if((template_name == mns_imageTemplate) ||
        (template_name == mnl_imageTemplate))
    {
        document.editform.wpTextbox1.value = txt + "\n" +
            document.editform.wpTextbox1.value;
        var sorl = ((template_name == mns_imageTemplate) ? "source" : 
            ((template_name == mnl_imageTemplate) ? "license" : 
            alert("Error in addTemplate: template_name has changed")));
        if(sorl == null)
        {
            return null;
        }
        document.editform.wpSummary.value = "Marking image as missing " +
            "essential " + sorl + " information. If this is not fixed this " +
            "image might be deleted after 7 days.";
        if(mnx_autosave)
        {
            document.editform.wpSave.click();
        }
    }
    // potential expansion for copvyio, etc.
    else
    {
        alert("Warning: invalid template!");
        return null;
    }
}

function qd_onload()
{
    if(wgCanonicalNamespace == "Image")
    {
        addLink('p-tb', 'javascript:mark_image(nfd_imageAction)', nfd_text, 'nom-for-del', nfd_tooltip);
        addLink('p-tb', 'javascript:mark_image(mns_imageAction)', mns_text, 'mark-no-source', mns_tooltip,
            null, null, 'javascript:mark_image(mns_imageAction, "mnx_ask_lang")', mnx_lang);
        addLink('p-tb', 'javascript:mark_image(mnl_imageAction)', mnl_text, 'mark-no-license', mnl_tooltip,
            null, null, 'javascript:mark_image(mnl_imageAction, "mnx_ask_lang")', mnx_lang);
        // potential expansion for copyvio etc.
    }
    
    var fakeaction = getParamValue('fakeaction');
    
    // Get the right template for the right fakeaction
    if(wgCanonicalNamespace == "Image")
    {
        if(fakeaction == mns_imageAction)
        {
            addTemplate(mns_imageTemplate);
        }
        else if(fakeaction == mnl_imageAction)
        {
            addTemplate(mnl_imageTemplate);
        }
        else if(fakeaction == nfd_imageAction)
        {
            addTemplate(nfd_deleteTemplate)
        }
        // potential expansion for copyvio etc.
    }
    else if(wgCanonicalNamespace == "User_talk")
    {
        if(fakeaction == mnx_userAction)
        {
            addTemplate(mnx_userTemplate);
        }
        else if(fakeaction == nfd_userAction)
        {
            addTemplate(nfd_idwTemplate);
        }
        // potential expansion for copyvio etc.
    }
    else if(wgCanonicalNamespace == "Project")
    {    
        if(fakeaction == nfd_subpageAction)
        {
            addTemplate(nfd_subpageTemplate);
        }
        else if(fakeaction == nfd_datepageAction)
        {
            addTemplate(nfd_datepageTemplate);
        }
        // potential expansion for copyvio etc.
    }
}

$(qd_onload);
// </pre>