Blog post created or updated.
New page
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:
<syntaxhighlight lang="javascript">
/* 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);
}
});
}
</syntaxhighlight>
Then all there's left is to create the "anchor". For this example, the anchor should look like this:
<syntaxhighlight lang="javascript" line="GESHI_NORMAL_LINE_NUMBERS">
/*(TEMPSTART: foo123)*/
// real function
$("body").css("color", "red");
// termination code
if (1 < 2) {
CodeTerminate.get("User:Penguin-Pal/wikia.js", "foo123");
}
/*(TEMPEND: foo123)*/
</syntaxhighlight>
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,
<syntaxhighlight lang="javascript" line="GESHI_NORMAL_LINE_NUMBERS">
if (new Date().getTime() >= 1385856000000) {
// if today is Dec 1, 2013 or after, do this
}
</syntaxhighlight>
<div class="mw-collapsible mw-collapsed" style="width: 100%; border: 2px solid black;">
So suppose you put it in [[MediaWiki:Wikia.js]]:
<div class="mw-collapsible mw-collapsed" style="width: 100%;">
<div style="font-size: 19px; line-height: 23px; text-align: center;">Before:</div>
<syntaxhighlight lang="javascript">
/* 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"));
</syntaxhighlight>
<div style="font-size: 19px; line-height: 23px; text-align: center;">After:</div>
<syntaxhighlight lang="javascript">
/* 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"));
</syntaxhighlight>
</div>
</div>
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.<br />
[[user:Penguin-Pal|<span style="color: #0e92cf;">Penguin-Pal</span>]] [[user talk:Penguin-Pal|<span style="color: #2e47aa;">(talk)</span>]] 07:29, November 27, 2013 (UTC)
[[Category:Blog posts]]