User:Jitrixis/termALL.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.
( function ( mw, $ ) {
	/*
	 * Start the application (only if it's a category)
	 */
	if(mw.config.values.wgCanonicalNamespace != "Category"){
		return;
	}
	$( document ).ready( termall );

	/*
	 * INIT : main variable
	 */
	var pageName = mw.config.values.wgPageName;
	var contentShown = true;
	var basetimestamp = 0;

	function termall() {
		var portletId, portletLink;

		switch(mw.config.get( 'skin' )){
			case "cologneblue":
				portletId = 'p-pageoptions'; 
				break;
			case "modern":
			case "monobook":
			case "vector":
				portletId = 'p-tb';
				break;
		}

		portletLink = mw.util.addPortletLink( portletId, '#', "Re-licensing", 't-termall' );		
		$( portletLink ).click( function () {
			if(contentShown){
				hideContent();
			}else{
				showContent();
			}
			return false;
		} );

		switch(mw.config.get( 'skin' )){
			case "cologneblue":
				document.getElementById( 'mw-content-text' ).parentNode.innerHTML += '<div id="ta-content"></div>';
				break;
			case "modern":
				document.getElementById( 'mw_contentholder' ).parentNode.innerHTML += '<div id="ta-content"></div>';
				break;
			case "monobook":
			case "vector":
				document.getElementById( 'bodyContent' ).parentNode.innerHTML += '<div id="ta-content"></div>';
				break;
        }
        var taContent = document.getElementById( 'ta-content' );
        if (taContent) {
            taContent.style.display = 'none';
        }
	}

	function hideContent(){
		switch(mw.config.get( 'skin' )){
			case "cologneblue":
				$( document.getElementById( 'mw-content-text' ) ).fadeOut(); 
				break;
			case "modern":
				$( document.getElementById( 'mw_contentholder' ) ).fadeOut();
				break;
			case "monobook":
			case "vector":
				$( document.getElementById( 'bodyContent' ) ).fadeOut(); 
				break;
		}
		$( document.getElementById( 'ta-content' ) ).fadeIn();
		contentShown = false; 
		insertContent();
	}

	function showContent(){
		switch(mw.config.get( 'skin' )){
			case "cologneblue":
				$( document.getElementById( 'mw-content-text' ) ).fadeIn(); 
				break;
			case "modern":
				$( document.getElementById( 'mw_contentholder' ) ).fadeIn();
				break;
			case "monobook":
			case "vector":
				$( document.getElementById( 'bodyContent' ) ).fadeIn(); 
				break;
		}
		$( document.getElementById( 'ta-content' ) ).fadeOut();
		contentShown = true;
	}

	function insertContent(){
		var inner = '<a href="#" id="ta-close" style="float: left;width: 20px;padding-right: 10px" title="close" ><img src="//upload.wikimedia.org/wikipedia/commons/3/35/Button_normal.svg" alt="close" title="close" style="width: 100%"/></a>'; 
		inner += '<div id="ta-qabox"><a href="#" id="ta-start">Search a license : </a> {{<input type="text" id="ta-answer"/>}} </div>';
		inner += '<div id="ta-data"></div>';
		document.getElementById( 'ta-content' ).innerHTML = inner;
		$( document.getElementById( 'ta-close' ) ).click( function () {
			showContent();
			contentShown = true;
			return false;
		} );
		$( document.getElementById( 'ta-start' ) ).click( function () {
			generateContent();
		} );
	}

	function generateContent(){
		document.getElementById( 'ta-data' ).innerHTML = '<ul id="ta-data-list"></ul>';

		///w/api.php?action=query&list=categorymembers&format=json&cmtitle=Category:LOL&cmprop=ids&cmnamespace=6&cmlimit=100
		$.ajax( {
			url: mw.util.wikiScript( 'api' ),
			data: {
				format: 'json',
				action: 'query',
				list: 'categorymembers',
				cmtitle: pageName,
				cmprop: 'ids',
				cmnamespace: '6',
				cmlimit: '200'
			},
			async: false
		} )
		.done( function ( data ) {
			for(var id in data.query.categorymembers){
				if(data.query.categorymembers.hasOwnProperty( id )){
					var innerData = '<li id="li-' + data.query.categorymembers[id].pageid + '"><input type="checkbox" id="cb-' + data.query.categorymembers[id].pageid + '" class="ta-checkbox"/><strong>' + data.query.categorymembers[id].pageid + '</strong> : <em id="em-' + data.query.categorymembers[id].pageid +'"></em><ul id="ul-' + data.query.categorymembers[id].pageid +'" class="ta-more"></ul>';
					innerData += '<span style="display: none" id="ct-' + data.query.categorymembers[id].pageid + '"></span></li>';
					document.getElementById( 'ta-data-list' ).innerHTML += innerData;
				}
			}
		} )
		.fail( function () {
			mw.notify( 'API Error', {
				title: 'termALL.js',
				tag: 'termALL'
			} );
		} );

		$( "ul#ta-data-list li" ).each(function() {
			getFileData( $( this ).prop('id').substr(3) );
		});


		var inner = '<a href="#" id="ta-start">Search a license : </a> {{<input type="text" id="ta-answer" value="' + document.getElementById("ta-answer").value + '"/>}} | <a href="#" id="ta-send">Replace by : </a> {{<input type="text" id="ta-answer2" value="' + document.getElementById("ta-answer").value + '"/>}} | Search a username : <input type="text" id="ta-username" value="' + mw.config.values.wgUserName + '"/> | Summary (why) : <input type="text" id="ta-summary"/>';
		document.getElementById( 'ta-qabox' ).innerHTML = inner;
		$( document.getElementById( 'ta-send' ) ).click( function () {
			sendNewLicense();
		} );
		$( document.getElementById( 'ta-start' ) ).click( function () {
			generateContent();
		} );
		$( document.getElementById( 'ta-username' ) ).on('input', function () {
			$( ".contributors" ).each(function() {
				$( this ).context.innerHTML = $( this ).context.innerHTML.replace('<strong style="background-color:yellow">', "");
				$( this ).context.innerHTML = $( this ).context.innerHTML.replace("</strong>", "");
				$( this ).context.innerHTML = $( this ).context.innerHTML.replace(document.getElementById("ta-username").value, '<strong style="background-color:yellow">' + document.getElementById("ta-username").value + "</strong>");
			});
		} );

	}

	function getFileData( pageid ){
		///w/api.php?action=query&prop=revisions&format=json&rvprop=content&pageids=33559691
		$.ajax( {
			url: mw.util.wikiScript( 'api' ),
			data: {
				format: 'json',
				action: 'query',
				prop: 'revisions',
				rvprop: 'content',
				pageids: pageid
			},
			async: true
		} )
		.done( function ( data ) {
			document.getElementById( 'em-' + pageid ).innerHTML = data.query.pages[pageid].title;
			var hastemplate = data.query.pages[pageid].revisions[0]['*'].search("{{" + document.getElementById("ta-answer").value + "}}");
			if(hastemplate >= 0){
				document.getElementById( 'ul-' + pageid ).innerHTML += '<li class="hasTemplate" style="color:green">License found</li>';
				document.getElementById( 'cb-' + pageid ).checked = "checked";
			}else{				
				document.getElementById( 'ul-' + pageid ).innerHTML += '<li class="hasTemplate" style="color:red">License not found</li>';
				document.getElementById( 'cb-' + pageid ).disabled = "disabled";
			}

			$("li.hasTemplate").css({ display: "inline" , "padding-right": "20px", "font-size": "0.7em", float: "left" , width: "100px" });

			document.getElementById( 'ct-' + pageid ).innerHTML += data.query.pages[pageid].revisions[0]['*'];

			var date = new Date();
			var bts = date.getUTCFullYear() * 10000000000 + (date.getUTCMonth() + 1) * 100000000  + date.getUTCDate() * 1000000 + date.getUTCHours() * 10000 + date.getUTCMinutes() * 100 + date.getUTCSeconds();
			console.log(bts);
			if(basetimestamp < bts){
				basetimestamp = bts;
			}
		} )
		.fail( function () {
			mw.notify( 'API Error', {
				title: 'termALL.js',
				tag: 'termALL'
			} );
		} );

		///w/api.php?action=query&prop=contributors&format=json&pcexcludegroup=bot&pclimit=10&pageids=33559691
		$.ajax( {
			url: mw.util.wikiScript( 'api' ),
			data: {
				format: 'json',
				action: 'query',
				prop: 'contributors',
				pcexcludegroup: 'bot',
				pageids: pageid,
				pclimit: '10'
			},
			async: true
		} )
		.done( function ( data ) {
			var innerData = '<li class="contributors">Contributors : ';
			for(var id in data.query.pages[pageid].contributors){
				if(id != 0){
					innerData += " ; ";
				}
				if( data.query.pages[pageid].contributors[id].name == mw.config.values.wgUserName){
					innerData += '<strong style="background-color:yellow">';
				}
				innerData += data.query.pages[pageid].contributors[id].name;
				if( data.query.pages[pageid].contributors[id].name == mw.config.values.wgUserName){
					innerData += "</strong>";
				}
			}
			document.getElementById( 'ul-' + pageid ).innerHTML += innerData + '</li>';			

			$("ul.ta-more li").css({ display: "inline" , "padding-right": "20px", "font-size": "0.7em" });
		} )
		.fail( function () {
			mw.notify( 'API Error', {
				title: 'termALL.js',
				tag: 'termALL'
			} );
		} );

	}

	function sendNewLicense(){
		$( ".ta-checkbox" ).each(function() {
			var pageid = $( this ).prop("id").substr(3);
			if(document.getElementById( "cb-" + pageid ).checked && !document.getElementById( "cb-" + pageid ).disabled){
				$.ajax( {
					type: 'POST',
					url: mw.util.wikiScript( 'api' ),
					data: {
						format: 'json',
						action: 'edit',
						pageid: pageid,
						token: mw.user.tokens.get( 'editToken' ),
						text: document.getElementById("ct-" + pageid).innerHTML.replace("{{" + document.getElementById("ta-answer").value + "}}", "{{" + document.getElementById("ta-answer2").value + "}}"),
						summary: '[[User:Jitrixis/termALL.js|termALL]] : replace license (' + document.getElementById("ta-summary").value + ')',
						"basetimestamp": basetimestamp
					}
				} )
				.done( function ( data ) {
					if ( data.hasOwnProperty( 'error' ) ) {
						$( '#ul-' + pageid + " .hasTemplate" ).empty().append('License unchanged');
						$( '#ul-' + pageid + " .hasTemplate" ).css("color", "orange");
					}else{
						document.getElementById( 'cb-' + pageid ).disabled = "disabled";
						$( '#ul-' + pageid + " .hasTemplate" ).empty().append('License changed');
						$( '#ul-' + pageid + " .hasTemplate" ).css("color", "blue");
					}
				} )
				.fail( function () {
					$( '#ul-' + pageid + " .hasTemplate" ).empty().append('License unchanged');
					$( '#ul-' + pageid + " .hasTemplate" ).css("color", "orange");
					mw.notify( 'API Error', {
						title: 'labelLister.js',
						tag: 'labelLister'
					} );
				} );
				//console.log(document.getElementById("ct-" + pageid).innerHTML.replace("{{" + document.getElementById("ta-answer").value + "}}", "{{" + document.getElementById("ta-answer2").value + "}}"));
			}
		});
	

		var inner = '<a href="#" id="ta-start">Search a license : </a> {{<input type="text" id="ta-answer"/>}}';
		document.getElementById( 'ta-qabox' ).innerHTML = inner;
		$( document.getElementById( 'ta-start' ) ).click( function () {
			generateContent();
		} );
	}

} ( mediaWiki, jQuery ) );