FANDOM


Hi there,

If you're looking for a way of creating a script that not only uses conditions to decide whether or not to apply it but are also looking for a method of completely removing it from a certain script page, maybe you'd find this useful.

Basically the syntax of this code isn't too long. It's made of:

  • A function to begine with, to make it work.
  • Wrapping "tags" to be used as some sort of anchors, to determine what part of the code to remove.
  • A certain code to do, placed between the wrapping tags.
  • A condition, also trapped between the "anchors". If it's true then trigger the self-destruction code.

When and if the condition is true, it then would use $.getJSON to get the content of the JS page on which the code is found, remove the requested "anchor" from it- containing the code and the if condition- and then use MediaWiki's API to save the changes.

Here's an example for a syntax for the basic code:

/* terminator */
// create object
CodeTerminate = {};
// edit API function
CodeTerminate.save = function( content, page ) {
	$.ajax({
		url: mw.util.wikiScript( 'api' ),
		data: {
			format: 'json',
			action: 'edit',
			title: page.replace(/ /g, '_'),
			summary: 'Code removal via API',
			text: content,
			token: mw.user.tokens.get( 'editToken' )
		},
		dataType: 'json',
		type: 'POST',
		success: function( data ) {
			if ( data && data.edit && data.edit.result == 'Success' ) {
				console.log( 'Successfully saved page "' + page + '" using API' );
			} else if ( data && data.error ) {
				console.log( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
			} else {
				console.log( 'Error: Unknown result from API.' );
			}
		},
		error: function( xhr ) {
			console.log( 'Error: Request failed.' );
		}
	});
}
// get content using $.getJSON() and terminate
CodeTerminate.get = function(page, partToRemove) {
	$.getJSON("/api.php?action=query&format=json&prop=revisions&titles=" + page.replace(/ /g, '_') + "&rvprop=content&cb=" + new Date().getTime(), function(data) {
		var tempContent = data.query.pages;
		for (var pageid in tempContent) {
			var content = tempContent[pageid].revisions[0]["*"],
				send = content.split("/*(TEMPSTART: " + partToRemove + ")*/")[0] + content.split("/*(TEMPEND: " + partToRemove + ")*/")[1];
			CodeTerminate.save(send, page);
		}
	});
}

Then all there's left is to create the "anchor". For this example, the anchor should look like this:

  1. /*(TEMPSTART: foo123)*/
  2. // real function
  3. $("body").css("color", "red");
  4. // termination code
  5. if (1 < 2) {
  6. 	CodeTerminate.get("User:Penguin-Pal/wikia.js", "foo123");
  7. }
  8. /*(TEMPEND: foo123)*/

On the example above:

  • Line 1 is the anchor's opening. Line 8 is its closing.
  • foo123 is the name of the anchor that is used.
  • User:Penguin-Pal/wikia.js is just my personal JS page, but of ourse any JS page can be used.
  • Line 3 contains the actual wanted code (on this example, make text on page red).
  • Lines 5-7 contain the condition.

On this example, 1 < 2 will always be true, but using another condition, say,

  1. if (new Date().getTime() >= 1385856000000) {
  2. 	// if today is Dec 1, 2013 or after, do this
  3. }

So suppose you put it in MediaWiki:Wikia.js:

Before:
/* import scripts */
 
importArticles({
	type: "script",
	articles: [
		"w:c:dev:EditcountTag/code.js",
		"w:c:dev:WallGreetingButton/code.js"
		"w:c:dev:Countdown/code.js"
	]
});
 
 
/* terminator */
// create object
CodeTerminate = {};
// edit API function
CodeTerminate.save = function( content, page ) {
	$.ajax({
		url: mw.util.wikiScript( 'api' ),
		data: {
			format: 'json',
			action: 'edit',
			title: page.replace(/ /g, '_'),
			summary: 'Code removal via API',
			text: content,
			token: mw.user.tokens.get( 'editToken' )
		},
		dataType: 'json',
		type: 'POST',
		success: function( data ) {
			if ( data && data.edit && data.edit.result == 'Success' ) {
				console.log( 'Successfully saved page "' + page + '" using API' );
			} else if ( data && data.error ) {
				console.log( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
			} else {
				console.log( 'Error: Unknown result from API.' );
			}
		},
		error: function( xhr ) {
			console.log( 'Error: Request failed.' );
		}
	});
}
// get content using $.getJSON() and terminate
CodeTerminate.get = function(page, partToRemove) {
	$.getJSON("/api.php?action=query&format=json&prop=revisions&titles=" + page.replace(/ /g, '_') + "&rvprop=content&cb=" + new Date().getTime(), function(data) {
		var tempContent = data.query.pages;
		for (var pageid in tempContent) {
			var content = tempContent[pageid].revisions[0]["*"],
				send = content.split("/*(TEMPSTART: " + partToRemove + ")*/")[0] + content.split("/*(TEMPEND: " + partToRemove + ")*/")[1];
			CodeTerminate.save(send, page);
		}
	});
}
 
 
/* create array of admins */
 
wikiAdmins = [
	"Drakemaster 101",
	"Michael Jackson",
	"Syster",
	"Wikiabot",
	"Jake25",
].sort();
 
 
/*(TEMPSTART: foo123)*/
/* give wiki halloween theme from [[MediaWiki:Halloween theme.js]] */
// real function
importArticle({
    type: "script",
    article: "MediaWiki:Halloween_theme.js"
});
// termination code
if (1 < 2) {
	CodeTerminate.get("MediaWiki:Wikia.js", "foo123");
}
/*(TEMPEND: foo123)*/
 
 
/* log use rights */
 
console.log(mw.config.get("wgUserGroups"));
After:
/* import scripts */
 
importArticles({
	type: "script",
	articles: [
		"w:c:dev:EditcountTag/code.js",
		"w:c:dev:WallGreetingButton/code.js"
		"w:c:dev:Countdown/code.js"
	]
});
 
 
/* terminator */
// create object
CodeTerminate = {};
// edit API function
CodeTerminate.save = function( content, page ) {
	$.ajax({
		url: mw.util.wikiScript( 'api' ),
		data: {
			format: 'json',
			action: 'edit',
			title: page.replace(/ /g, '_'),
			summary: 'Code removal via API',
			text: content,
			token: mw.user.tokens.get( 'editToken' )
		},
		dataType: 'json',
		type: 'POST',
		success: function( data ) {
			if ( data && data.edit && data.edit.result == 'Success' ) {
				console.log( 'Successfully saved page "' + page + '" using API' );
			} else if ( data && data.error ) {
				console.log( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
			} else {
				console.log( 'Error: Unknown result from API.' );
			}
		},
		error: function( xhr ) {
			console.log( 'Error: Request failed.' );
		}
	});
}
// get content using $.getJSON() and terminate
CodeTerminate.get = function(page, partToRemove) {
	$.getJSON("/api.php?action=query&format=json&prop=revisions&titles=" + page.replace(/ /g, '_') + "&rvprop=content&cb=" + new Date().getTime(), function(data) {
		var tempContent = data.query.pages;
		for (var pageid in tempContent) {
			var content = tempContent[pageid].revisions[0]["*"],
				send = content.split("/*(TEMPSTART: " + partToRemove + ")*/")[0] + content.split("/*(TEMPEND: " + partToRemove + ")*/")[1];
			CodeTerminate.save(send, page);
		}
	});
}
 
 
/* create array of admins */
 
wikiAdmins = [
	"Drakemaster 101",
	"Michael Jackson",
	"Syster",
	"Wikiabot",
	"Jake25",
].sort();
 
 
 
 
 
/* log use rights */
 
console.log(mw.config.get("wgUserGroups"));

There's no reason why this shouldn't work in applying changes on multiple pages. I don't really see a reason why to use this sort of script rather than using only if conditions from the first place, but maybe some of you could find a possible usage for this somewhere.
Penguin-Pal (talk) 07:29, November 27, 2013 (UTC)

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.