User:Ladsgroup/AHK.js: Difference between revisions

From Wikimedia Foundation Governance Wiki
Content deleted Content added
Ladsgroup (talk | contribs)
Created page with "→‎A tool to add information template Author: Amir (User:Ladsgroup): (function ($, mw) { 'use strict'; function setwindow (text) { var uri = new mw..."
 
Ladsgroup (talk | contribs)
No edit summary
 
Line 27: Line 27:
}
}
var author = document.getElementById('mw-imagepage-section-filehistory').lastElementChild.lastElementChild.lastElementChild.children[4].firstElementChild.href.replace('&action=edit&redlink=1','').split('User:')[1];
var author = document.getElementById('mw-imagepage-section-filehistory').lastElementChild.lastElementChild.lastElementChild.children[4].firstElementChild.href.replace('&action=edit&redlink=1','').split('User:')[1];
info = '{{Information\n |description = $2\n |date = ' + date + '\n |source = {{own}}\n |author = [[User:' + author + ']]\n |permission = \n |other_versions = \n}}\n';
info = '{{Information\n |Description = $2\n |Date = ' + date + '\n |Source = {{own}}\n |Author = [[User:' + author + ']]\n |Permission = \n |Other_versions = \n}}\n';
text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{[Ss]elf.+?\}\})/, "$1" + info + "$3");
text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{[Ss]elf.+?\}\})/, "$1" + info + "$3");
text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');
text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');
callback(text);
callback(text);
} else {
} else {
info = '{{Information\n |description = $2\n |date = \n |source = \n |author = \n |permission = \n |other_versions = \n}}\n';
info = '{{Information\n |Description = $2\n |Date = \n |Source = \n |Author = \n |Permission = \n |Other_versions = \n}}\n';
text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{)/, "$1" + info + "$3");
text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{)/, "$1" + info + "$3");
text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');
text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');

Latest revision as of 19:36, 13 February 2015

/* 
    A tool to add information template
    Author: Amir (User:Ladsgroup)
*/
(function ($, mw) {
    'use strict';

    function setwindow (text) {
        var uri = new mw.Uri();
        uri.query = {
            action: 'edit',
            withJS: 'MediaWiki:AHK.js',
            text: text,
            summary: 'Adding Information template using [[User:Ladsgroup/AHK.js|tool]]',
        };
        window.location = uri.toString();
    }

    function check (text, callback) {
        if (text.search(/\{\{[Ss]elf/) !== -1) {
            var date = '';
            var info = '';
            try {
                date = '{{According to EXIF data|' + document.getElementsByClassName('exif-datetimeoriginal')[0].lastElementChild.innerHTML.split(", ")[1] + '}}';
            } catch (err) {
                date = '';
            }
            var author = document.getElementById('mw-imagepage-section-filehistory').lastElementChild.lastElementChild.lastElementChild.children[4].firstElementChild.href.replace('&action=edit&redlink=1','').split('User:')[1];
            info = '{{Information\n |Description = $2\n |Date = ' + date + '\n |Source = {{own}}\n |Author = [[User:' + author + ']]\n |Permission = \n |Other_versions = \n}}\n';
            text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{[Ss]elf.+?\}\})/, "$1" + info + "$3");
            text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');
            callback(text);
        } else {
            info = '{{Information\n |Description = $2\n |Date = \n |Source = \n |Author = \n |Permission = \n |Other_versions = \n}}\n';
            text = text.replace(/(^(?:\=\= *?\{\{int\:filedesc\}\} *?\=\=\n)?)([\s\S]*?)\n*?((?:\=\=[\s\S]+?)?\{\{)/, "$1" + info + "$3");
            text = text.replace(/\n?\[\[Category\:Media missing infobox template\]\]/, '');
            callback(text);
        }

    }

    function loadPage(title, callback) {
        new mw.Api().get({
            action: 'query',
            prop: 'revisions',
            titles: title,
            rvprop: 'content',
            format: 'json'
        }).done(function (data) {
        try {
            callback($.map(data.query.pages, function (value) {
                return value;
            })[0].revisions[0]['*'], setwindow);
        }
        catch (exception) {
            callback('', setwindow)}
        }).fail(function (data) {
            callback($.map(data.query.pages, function (value) {
                return value;
            })[0].revisions[0], setwindow);
        });
    }
    if (mw.config.get('wgNamespaceNumber') === 6 && !(document.getElementById("fileinfotpl_desc"))) {
        $(function () {
            $(mw.util.addPortletLink('p-views', '#', 'Add info', 'ca-ahk', 'Adding Information template'))
                .click(function (e) {
                    loadPage(mw.config.get('wgPageName'), check);
            });
        });
    }
}(jQuery, mediaWiki));