User:Enterprisey/AFCRHS.js

//

(function() {

if (mw.config.get("wgPageName").indexOf('Wikipedia:Articles_for_creation/Redirects') === -1 && mw.config.get("wgPageName").indexOf('Wikipedia:Articles_for_creation/Categories') === -1) {

return;

}

var afcHelper_RedirectPageName = mw.config.get("wgPageName").replace(/_/g, ' ');

var afcHelper_RedirectSubmissions = new Array();

var afcHelper_RedirectSections = new Array();

var afcHelper_advert = ' (AFCRHS.js)';

var afcHelper_numTotal = 0;

var afcHelper_AJAXnumber = 0;

var afcHelper_Submissions = new Array();

var needsupdate = new Array();

var afcHelper_redirectDecline_reasonhash = {

'exists': 'The title you suggested already exists on Wikipedia',

'blank': 'We cannot accept empty submissions',

'notarget': ' A redirect cannot be created unless the target is an existing article. Either you have not specified the target, or the target does not exist',

'unlikely': 'The title you suggested seems unlikely. Could you provide a source showing that it is a commonly used alternate name?',

'notredirect': 'This request is not a redirect request',

'custom': ''

};

var afcHelper_categoryDecline_reasonhash = {

'exists': 'The category you suggested already exists on Wikipedia',

'blank': 'We cannot accept empty submissions',

'unlikely': 'It seems unlikely that there are enough pages to support this category',

'notcategory': 'This request is not a category request',

'custom': ''

};

function afcHelper_redirect_init() {

pagetext = afcHelper_getPageText(afcHelper_RedirectPageName);

// cleanup the wikipedia links for preventing stuff like https://en.wikipedia.org/w/index.php?diff=576244067&oldid=576221437

pagetext = afcHelper_cleanuplinks(pagetext);

// first, strip out the parts before the first section.

var section_re = /==.*?==/;

pagetext = pagetext.substring(pagetext.search(section_re));

// then split it into the rest of the sections

afcHelper_RedirectSections = pagetext.match(/^==.*?==$((\r?\n?)(?!==[^=]).*)*/img);

// parse the sections.

for (var i = 0; i < afcHelper_RedirectSections.length; i++) {

var closed = /(\{\{\s*afc(?!\s+comment)|This is an archived discussion)/i.test(afcHelper_RedirectSections[i]);

if (!closed) {

// parse.

var header = afcHelper_RedirectSections[i].match(section_re)[0];

if (header.search(/Redirect request/i) !== -1) {

var wikilink_re = /\[\[(\s*[^=]*?)*?\]\]/g;

var links = header.match(wikilink_re);

if (!links) continue;

for (var l = 0; l < links.length; l++) {

links[l] = links[l].replace(/[\[\]]/g, '');

if (links[l].charAt(0) === ':') links[l] = links[l].substring(1);

}

var re = /Target of redirect:\s*\[\[([^\[\]]*)\]\]/i;

re.test(afcHelper_RedirectSections[i]);

var to = $.trim(RegExp.$1);

var reasonRe = /Reason:[ \t]*?(.+)/i;

var reasonMatch = reasonRe.exec(afcHelper_RedirectSections[i]);

var reason = reasonMatch && reasonMatch[1].trim() ? reasonMatch[1] : null;

var sourceRe = /Source.*?:[ \t]*?(.+)/i;

var sourceMatch = sourceRe.exec(afcHelper_RedirectSections[i]);

var source = sourceMatch && sourceMatch[1].trim() ? sourceMatch[1] : null;

var submission = {

type: 'redirect',

from: new Array(),

section: i,

to: to,

title: to,

reason: reason,

source: source

};

for (var j = 0; j < links.length; j++) {

var sub = {

type: 'redirect',

to: to,

id: afcHelper_numTotal,

title: links[j],

action: ''

};

submission.from.push(sub);

afcHelper_Submissions.push(sub);

afcHelper_numTotal++;

}

afcHelper_RedirectSubmissions.push(submission);

} else if (header.search(/Category request/i) !== -1) {

// Find a wikilink in the header, and assume it's the category to create

var category_name = /\[\^\[\+\]\]/.exec(header);

if (!category_name) continue;

category_name = category_name[0];

category_name = category_name.replace(/[\[\]]/g, '');

category_name = category_name.replace(/Category\s*:\s*/gi, 'Category:');

if (category_name.charAt(0) === ':') category_name = category_name.substring(1);

// Figure out the parent categories

var request_text = afcHelper_RedirectSections[i].substring(header.length);

// We only want categories listed under the "Parent category/categories" heading,

// *NOT* any categories listed under "Example pages which belong to this category".

var idx_of_parent_heading = request_text.indexOf('Parent category/categories');

if (idx_of_parent_heading >= 0 ) {

request_text = request_text.substring(idx_of_parent_heading);

}

var parent_cats = [];

var parent_cat_match = null;

var parent_cat_regex = /\[\[\s*:\s*(Category:[^\]\[]*)\]\]/ig;

do {

parent_cat_match = parent_cat_regex.exec(request_text);

if (parent_cat_match) {

parent_cats.push(parent_cat_match[1]);

}

} while (parent_cat_match);

var submission = {

type: 'category',

title: category_name,

section: i,

id: afcHelper_numTotal,

action: '',

parents: parent_cats.join(',')

};

afcHelper_numTotal++;

afcHelper_RedirectSubmissions.push(submission);

afcHelper_Submissions.push(submission);

}

} // end if (!closed)

} // end loop over sections

// Build the form

var $form = $('

Reviewing AfC redirect requests

');

displayMessage($form);

var $messageDiv = $form.parent();

// now layout the text.

var afcHelper_Redirect_empty = 1;

var ACTIONS = [

{label: 'Accept', value: 'accept'},

{label: 'Decline', value: 'decline'},

{label: 'Comment',value: 'comment'},

{label: 'None', selected: true, value: 'none'}

];

for (var k = 0; k < afcHelper_RedirectSubmissions.length; k++) {

if (afcHelper_RedirectSubmissions[k].to !== undefined)

var submissionname = afcHelper_RedirectSubmissions[k].to.replace(/\s/g,'');

else

var submissionname = "";

var $thisSubList = $('

    ');

    var $thisSubListElement = $('

  • ');

    if (afcHelper_RedirectSubmissions[k].type === 'redirect') {

    $thisSubListElement.append('Redirect(s) to ');

    if (!submissionname) {

    for (var i = afcHelper_RedirectSubmissions[k].from.length - 1; i >= 0; i--) {

    needsupdate.push({

    id: afcHelper_RedirectSubmissions[k].from[i].id,

    reason: 'notarget'

    });

    }

    } else if (!afcHelper_RedirectSubmissions[k].to) {

    for (var i = afcHelper_RedirectSubmissions[k].from.length - 1; i >= 0; i--) {

    needsupdate.push({

    id: afcHelper_RedirectSubmissions[k].from[i].id,

    reason: 'notredirect'

    });

    }

    }

    if (afcHelper_RedirectSubmissions[k] === '' || afcHelper_RedirectSubmissions[k] === ' ') {

    $thisSubListElement.append('Empty submission \#' + afcHelper_Redirect_empty);

    afcHelper_Redirect_empty++;

    } else {

    if (submissionname.length > 0) {

    $thisSubListElement.append($('')

    .attr('href', mw.config.get("wgArticlePath").replace("$1", encodeURIComponent(afcHelper_RedirectSubmissions[k].to)))

    .attr('target', '_blank')

    .text(afcHelper_RedirectSubmissions[k].to));

    } else {

    $thisSubListElement.append('no target given: ');

    }

    }

    var $fromList = $('

      ').appendTo($thisSubListElement);

      for (var l = 0; l < afcHelper_RedirectSubmissions[k].from.length; l++) {

      var from = afcHelper_RedirectSubmissions[k].from[l];

      var toarticle = from.title;

      if (toarticle.replace(/\s*/gi, "").length == 0) toarticle = "no title specified, check the request details";

      var reasonAndSource = $('

        ');

        if(afcHelper_RedirectSubmissions[k].reason)

        reasonAndSource.append('

      • Reason: ' + afcHelper_RedirectSubmissions[k].reason + '
      • ');

        if(afcHelper_RedirectSubmissions[k].source)

        reasonAndSource.append('

      • Source: ' + afcHelper_RedirectSubmissions[k].source + '
      • ');

        var googleSearchUrl = 'http://www.google.com/search?q="' +

        encodeURIComponent(toarticle) + '"+-wikipedia.org';

        $fromList.append($('

      • ')

        .append('From: ' + toarticle + ' (' +

        'Google · what links here)
        ')

        .append(reasonAndSource)

        .append($('

        .append(afcHelper_generateSelectObject('afcHelper_redirect_action_' + from.id, ACTIONS,

        afcHelper_redirect_makeActionChange(from.id)))

        .append($('

        ').attr('id', 'afcHelper_redirect_extra_' + from.id)));

        }

        } else {

        var subId = afcHelper_RedirectSubmissions[k].id;

        $thisSubListElement.append('Category submission: ')

        .append($('')

        .attr('href', '/wiki/' + afcHelper_RedirectSubmissions[k].title)

        .attr('title', afcHelper_RedirectSubmissions[k].title)

        .text(afcHelper_RedirectSubmissions[k].title))

        .append('
        ')

        .append($('

        .append(afcHelper_generateSelectObject('afcHelper_redirect_action_' + subId, ACTIONS,

        afcHelper_redirect_makeActionChange(subId)))

        .append($('

        ').attr('id', 'afcHelper_redirect_extra_' + subId));

        }

        $thisSubList.append($thisSubListElement);

        $messageDiv.append($thisSubList);

        } // end loop over sections

        $messageDiv.append($('