Jump to content

User:Quarl/wikipage.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Quarl (talk | contribs) at 05:36, 23 January 2006 (talkpage). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// User:Quarl/wikipage.js - "WikiPage" class for page name, etc. functionality

// requires: util.js

// Suppose we are editing [[Template talk:Foo bar 'blah']].
//   editingP:             true;

// wikiPage is a pre-initialized global variable using the current page's canonical URL.
// It uses the "Retrieved from" page hyperlink which is robust against redirections, editing, special characters, special pages.
//
//   wikiPage.url:         "http://en.wikipedia.org/wiki/Template_talk:Foo_bar_%27blah%27"
//   wikiPage.qurl:        "http://en.wikipedia.org/w/index.php?title=Template_talk:Foo_bar_%27blah%27"
//   wikiPage.page:        "Template talk:Foo bar 'blah'"
//   wikiPage.article:     "Foo bar 'blah'"
//   wikiPage.namespace:   "Template talk"
//   wikiPage.namespaceNT: "Template"
//   wikiPage.talkP:       true
//   wikiPage.nsTemplateP: true
//   wikiPage.nsMainP:     false
//   wikiPage.nsUserP:     false
//   wikiPage.nsCategoryP: false
//   wikiPage.nsSpecialP:  false
//   wikiPage.nsProjectP:  false    // (namespace "Wikipedia")

// To create new WikiPage object from a URL:
//   var wp = new WikiPage("http://en.wikipedia.org/wiki/Article_Name");
// To create a new WikiPage object from a page name:
//   var wp = new WikiPage(null, 'Article Name');

// <pre><nowiki>

/*
<div class="printfooter">
Retrieved from "<a href="http://en.wikipedia.org/wiki/Albert_Einstein">http://en.wikipedia.org/wiki/Albert_Einstein</a>"</div>
*/

// the "retrieved from" text contains the canonical article URL (even if we're looking at an edit or history page)
function getCanonPageURL0() {
    return getElementsByClass("printfooter", null, 'div')[0].getElementsByTagName('a')[0].href;
}

function getUsername0() {
    // read username from pt-userpage link.
    // <li id="pt-userpage"><a href="/wiki/User:Quarl">Quarl</a></li>
    return document.getElementById('pt-userpage').getElementsByTagName('a')[0].text;
}

function WikiPage(url,page) {
    if (url) {
        this.url = "" + url;
        if (this.url.match(/^(?:http:\/\/en.wikipedia.org)\/wiki\//)) {
            this.pageQuoted = RegExp.rightContext;
        } else if (this.url.match(/^(?:http:\/\/en.wikipedia.org)\/w\/index.php\?title=([^&]+)/)) {
            this.pageQuoted = RegExp.$1;
        } else {
            alert("WikiPage: Couldn't parse page name from url '"+url+"'"); 
            return;
        }
        this.page = unescape(this.pageQuoted).replace(/_/g,' ');
    } else if (page) {
        this.page = page.replace(/_/g, ' ');
        this.pageQuoted = wpaescape(page);
        this.url = 'http://en.wikipedia.org/wiki/' + this.pageQuoted;
    } else {
        alert("WikiPage: must specify url or page"); return; 
    }

    this.qurl = 'http://en.wikipedia.org/w/index.php?title=' + this.pageQuoted;

    if (this.page.match(/:/)) {
        this.namespace = RegExp.leftContext;
        this.article = RegExp.rightContext;
    } else {
        this.namespace = ''; // (main)
        this.article = this.page;
    }

    if (this.namespace == 'Talk') {
        this.talkP = true;
        this.namespaceNT = '';
    } else if (this.namespace.match(/ talk$/)) {
        this.talkP = true;
        this.namespaceNT = RegExp.leftContext;
    } else {
        this.talkP = false;
        this.namespaceNT = this.namespace;
    }

    if (this.article.match(/\//)) {
        this.superarticle = RegExp.leftContext;
        this.subarticle = RegExp.rightContext;
    } else {
        this.superarticle = this.article;
        this.subarticle = '';
    }

    this.nsMainP = (this.namespaceNT == '');
    this.nsSpecialP = (this.namespaceNT == 'Special');
    this.nsUserP = (this.namespaceNT == 'User');
    this.nsTemplateP = (this.namespaceNT == 'Template');
    this.nsCategoryP = (this.namespaceNT == 'Category');
    this.nsProjectP = (this.namespaceNT == 'Wikipedia');
    this.nsHelpP = (this.namespaceNT == 'Help');
    this.nsImageP = (this.namespaceNT == 'Image');
    this.nsCategoryP = (this.namespaceNT == 'Category');

    this.talkPage = function() {
        if (this.talkP) { return this; }
        if (this.namespace == '') { return new WikiPage(null, 'Talk:'+this.page); }
        else { return new WikiPage(null, this.page+' talk:'+this.article); }
    }
}

// from automod.js
function getQueryVars0(){ 
     var res = new Array();
     var pairs = location.search.substring(1).split("&"); 
     for(var i=0; i < pairs.length; i++){ 
         var pair = pairs[i].split("=");
         res[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
     }
     return res; 
}

function initWikiPage() {
    window.wikiPage = new WikiPage(getCanonPageURL0());
    window.username = getUsername0();
    window.editingP = Boolean(document.forms.editform);    //document.title.match(/^Editing /) 
    window.movepageP = Boolean(document.forms.movepage);
    window.previewP = Boolean(document.getElementById("wikiPreview"));
    window.historyP = Boolean(document.getElementById("pagehistory"));
    window.queryVars = getQueryVars0();
}

// DEPRECATED:
function getPname() { return wikiPage.page; }
function getPnameNS() { return wikiPage.namespace; }
function getPnameNoNS() { return wikiPage.article; }
function pageIsSpecial() { return wikiPage.nsSpecialP; }
function get_query_vars() { return queryVars; }

addOnloadHook(initWikiPage);

// obsolete method 1:
/*function getPname0() {
    var z=document.getElementById("content").childNodes;
    for (var n=0;n<z.length;n++) { 
        if (z[n].className=="firstHeading") return z[n].textContent;
    }
}

function getPname1() {
    var t = getPname0();
    t = t.replace(/^Editing /,'');
    t = t.replace(/ \(section\)$/,'');

    return t;
}*/

// obsolete method 2:
/* 
    return document.title.substr(0, document.title.lastIndexOf(' - Wikipedia, the free'));
*/

//</nowiki></pre>