User:Nuno Tavares/validate.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.
/* mediawiki crap */
/*global importScript, wgPageName, wgScript, wgScriptPath, wgUserGroups, wgUserName, addOnloadHook, addPortletLink, wgCanonicalNamespace */

/* supportFuncs */
/*global SimpleAjax, SimpleUI, Join, TaskManager, copySlotsRecursively */
//importScript(''); // [[Benutzer:Codeispoetry/supportFuncs.js]]
mw.loader.load('https://secure.wikimedia.org/wikipedia/de/w/index.php?title=Benutzer:Codeispoetry/supportFuncs.js&action=raw&ctype=text/javascript');

/* api.js */
/*global DOM, Actions */
mw.loader.load('https://secure.wikimedia.org/wikipedia/de/w/index.php?title=Benutzer:D/monobook/api.js&action=raw&ctype=text/javascript'); // [[Benutzer:D/monobook/api.js]]

/* düp-helper */
/*global MiniWiki, MiniUtil */ /* Bei MiniWiki sind's nur noch pageURL und newSection, das sollte doch machbar sein? Wäre eh beides im API nützlich */
mw.loader.load('https://secure.wikimedia.org/wikipedia/de/w/index.php?title=Benutzer:Ireas/düp-helperfunctions-monobook.js&action=raw&ctype=text/javascript'); // [[Benutzer:Ireas/düp-helperfunctions-monobook.js]]

//mw.loader.load('http://www.wikilovesmonuments.org.pt/jscss/jquery-1.4.4.min.js');
//mw.loader.load('http://www.wikilovesmonuments.org.pt/jscss/jquery-ui-1.8.10.min.js');


if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.indexOf(str) == 0;
  };
}

pageContent = null;
pageWlmId = -1;
pageEditToken = mediaWiki.config.get('wikilove-edittoken');
pageName = mediaWiki.config.get('wgPageName');

function fetch(page) { 
	SimpleAjax.get(
	    wgScriptPath + '/api.php?action=query&format=json&prop=revisions&rvprop=content&titles=' + page,
	    null,
	    parseAPIAnswer);
}

function parseVerifyPage(req) {
    var json = req.responseText.parseJSON();
    alert(json.text);
}



function wlmValidate(wlm_id, pageContent, data) {
    alert('validate....');
    newtext = pageContent.replace(/\{\{WLM-PT\|.*\}\}/, '{{WLM-PT|' + wlm_id + '|yes}}');
    console.log('reject: ' + newtext);
}

function wlmReject(wlm_id, pageContent, data) {
    alert('reject....');
    newtext = pageContent.replace(/\{\{WLM-PT\|.*\}\}/, '{{WLM-PT|' + wlm_id + '|no}}');
    console.log('reject: ' + newtext);
}



function jscallback(data) {
 
    //console.log('got data: ' + data.text);
    var newDiv = $("<div>");  
    newDiv.html(data.text).dialog({
	modal: true,
        width: 460,
        title: data.title,
	buttons: {
		"VALIDATE": function() {
			$( this ).dialog( "close" );
                        wlmValidate(pageWlmId, pageContent, data);
		},
		"REJECT": function() {
			$( this ).dialog( "close" );
                        wlmReject(pageWlmId, pageContent, data);
		},
		Cancel: function() {
			$( this ).dialog( "close" );
		},
		"VERIFY": function() {
                        $( this ).dialog( "close" );
			window.open('http://www.wikilovesmonuments.org.pt/tools/verify/index.php?callback=jscallback&debug=0&wlm_id=' + pageWlmId);
		}
	}
    });
}

function callTheJsonp(wlm_id) {
 
    // the url of the script where we send the asynchronous call
    var url = 'http://www.wikilovesmonuments.org.pt/tools/verify/index.json.php?callback=jscallback&debug=0&wlm_id=' + wlm_id;
    // create a new script element
    var script = document.createElement('script');
    // set the src attribute to that url
    script.setAttribute('src', url);
    // insert the script in out page
    document.getElementsByTagName('head')[0].appendChild(script);
}


/**
receives a json api answer
*/
function parseAPIAnswer(req) {
    var json = req.responseText.parseJSON();
    if (!json || !json.query || !json.query.pages) {
        console.log('probremas...: ' + json);
        return;
    }

    console.log('json: ' + json.query.pages);
    var imgpage = null;
    for( pageid in json.query.pages ) {
        console.log('going: ' + pageid);
        if( json.query.pages[pageid].revisions[0]['*'] ) {
            imgpage = json.query.pages[pageid].revisions[0]['*'];
        }
        console.log('content: ' + imgpage);
    }
    var regmatch = imgpage.match( /\{\{WLM-PT\|([^\}\|]+)(.*)\}\}/ );
    var wlm_id = null;
    var wlmsep = null;
    if ( regmatch ) {
        wlm_id = regmatch[1];
        wlmsep = regmatch[2];
    }
    console.log('wlmsep = ' + wlmsep);

    pageContent = imgpage;
    pageWlmId = wlm_id;


    if ( wlmsep.startsWith('|') && wlm_id != null ) {
        alert('already validated: ' + wlm_id);
    } else {
        console.log('got id: ' + wlm_id);
        callTheJsonp(wlm_id);
    }
}

//var a = fetch( pageName );