Jump to content

MediaWiki:Common.js/Special:MovePage

From Wikinews, the free news source you can write!

/* this is just a quick and dirty script. makes a list of double redir on a move page (on MediaWiki:Movepage-moved messaage)

caveats

  • only supports one level of double redirects - triple redirect won't work
  • might break if title contains /
  • /

//

mw.loader.load( '/w/index.php?title=' + 'user:Bawolff/mwapilib.js' + '&action=raw&ctype=text/javascript' ); //note: this loads on every page move, even if we don't always need it. causes some overhead - which is bad
var CheckForDoubleRedir = {};

CheckForDoubleRedir.do = function(pageName,container) {

//var pageName = 'Main Page';
var foo = new Bawolff.mwapi.Request({action:"query", rawcontinue: '', list: "backlinks", blfilterredir: "redirects", blredirect: "true", bllimit: "20", bltitle: pageName});
foo.send(function (doc) {
var out =document.createElement('ul');
var pages, li, a;
//var pages= doc.getElementsByTagName('redirlinks')[0].getElementsByTagName('bl')[0].getAttribute('title');
var redir = doc.getElementsByTagName('redirlinks');
for (var i=0;i < redir.length;i++) {
pages = redir[i].getElementsByTagName('bl');
 for (var j=0;j < pages.length; j++) {
  li = document.createElement('li')
  a= document.createElement('a');
  a.appendChild(document.createTextNode(pages[j].getAttribute('title')));
  a.href= mw.config.get('wgScript') + "?redirect=no&action=edit&title=" + encodeURIComponent(pages[j].getAttribute('title'));
  //FIXME undecode slashes, causes problems
  li.appendChild(a)
  out.appendChild(li);
}

}

if (i===0) {
li = document.createElement('li')
li.appendChild(document.createTextNode('No double redirects were detected'));
out.appendChild(li);
}

if (doc.getElementsByTagName('query-continue').length !==0) {
//>20 double redirects
  li = document.createElement('li')
  a= document.createElement('a');
  a.appendChild(document.createTextNode('Please see [[special:whatlinksherere]] for more.'));
  a.href= mw.config.get('wgScript') + "?hidelinks=1&hidetrans=1&title=" + encodeURIComponent(pageName);
  li.appendChild(a)
  out.appendChild(li);
}

var header =document.createElement('h2');
header.appendChild(document.createTextNode('Please fix the following double redirects so that they point directly to the article, instead of another redirect'));
container.appendChild(header);
container.appendChild(out);
//jsMsg(container); now it is in body as opposed to sitenotice
//doubleRedirInsertPoint

})

}

CheckForDoubleRedir.load = function() {
try {
  var insert = document.getElementById('doubleRedirInsertPoint');
  var page = document.getElementById('destinationPageName').firstChild.firstChild.firstChild.data;
  if (insert && page && Bawolff && Bawolff.mwapi) {
   CheckForDoubleRedir.do(page, insert);
  } else if (insert && page && (!Bawolff && !Bawolff.mwapi)) {
  window.setTimeout(CheckForDoubleRedir.load, 600) //if the lib hasn't loaded yet
  }
 } catch (e) {} //ignore errors. Will get an error on pre-move page
}

$(CheckForDoubleRedir.load);
//

/*

move comment page automatically

Hopefully this will work mostly. Can be disabled by setting

var doNotAutoMoveComments = true;

in special:mypage/vector.js

*/
function moveCommentsPage () {
 //now hopefully this can't be abused...
 if (window.doNotAutoMoveComments) return;
 if (!document.getElementById('move-the-damn-comments-page') || location.search.indexOf('doNotMoveCommentsPage') !== -1) return
 var initial = document.getElementById('initialPageName'); //unsafe(?) should be escaped by Bawolff.mwapi
 var dest = document.getElementById('destinationPageName'); //unsafe(?)
 if (!initial || !dest) return;
 initial = initial.getElementsByTagName('a')[0].firstChild.data;
 dest = dest.getElementsByTagName('a')[0].firstChild.data;
 if (!initial || !dest) return;
 initial = 'Comments:' + initial;
 dest = 'Comments:' + dest;
 if (!confirm('Automatically move associated Comments (Opinions) namespace page from "' + initial + '" to "' + dest + '"?')) return;
 
 Bawolff.mwapi.getToken(function (t) {
  var move = new Bawolff.mwapi.Request({action: 'move', token: t, from: initial, to: dest, reason: 'Automatically moving associated comments namespace page to new name, with [[user talk:Bawolff|JS assistance]]'}, 'POST');
  move.send(function () {
   document.getElementById('move-the-damn-comments-page').innerHTML = '<img height="16" width="16" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/fb/Yes_check.svg/16px-Yes_check.svg.png" alt=""/> <b>Done</b> Comments page moved.';
   }, function (e) {alert('Comments page move failed: ' + e.message + ' (' + e.name + ')' );});
 });
}


$(moveCommentsPage);

/*
  • /