User:Technical 13/SandBox/getPageViews.js: Difference between revisions

Content deleted Content added
Technical 13 (talk | contribs)
Okay, now I just need to deal with the synchronous/asynchronous issue...
Technical 13 (talk | contribs)
http: has to be specified to get anything back... I'm set up to warn about mixed content instead of block it... SO, this "should" work.
 
(2 intermediate revisions by the same user not shown)
Line 1:
// Licensed under the MIT license; theopolismewiki@gmail.com
// TODO -- make a [[User:AzaToth/morebits.js]] version of this input sequence
( function ( $, mw ) {
var lang = mw.config.get("wgPageContentLanguage");
function sum ( obj ) {
if(typeof(lang) === "undefined"){
var total;
lang = prompt("Get results in what language?", "en");
for (var prop in obj ) {
if ( obj.hasOwnProperty( prop ) ) {
var relatedTopic = mw.config.get("wgTitle").substr(0, mw.config.get("wgTitle").indexOf("/"));
total += parseInt( obj[prop], 10 );
if(typeof(relatedTopic) === "undefined"){
relatedTopic = prompt("What are these pages related to?", "{{PAGENAME}}");
}
var pageNames = prompt("Page names to get results for?", /*"Augustus Pablo|Avian Java|Bentley Java|Chrysler Java|Coffee|Coffee County, Alabama|HMS Java|Java|Java, Alabama|Java, New York|Java, Ohio|Java, South Dakota|Java, São Tomé and Príncipe|Java, Virginia|Java-eiland|JavaScript|Java (Martin Mystère)|Java (band)|Java (board game)|Java (chicken)|Java (cigarette)|Java (composition)|Java (dance)|Java (programming language)|Java (software platform)|Java (town)|Java District|Java Joel|Java Man|Java Road|Java Sea|Java Trench|Java class cruiser|Java coffee|Java virtual machine|Javanese (disambiguation)|Javanese alphabet|List of The Tribe characters|Metamorpho|SS Java|USS Java (1815)"*/"foo");
if (pageNames.indexOf("|") === -1) {
pageNames = new Array(pageNames);
} else {
pageNames = pageNames.split("|");
}
var pageName = 0;
var rangeToGet = new Array(30, 60, 90);
var pageHits = 0;
var table = "{| class=\"collapsible collapsed sortable\" width=\"95%\" border=\"1\"";
table += "\n|-\n! colspan=\"5\"| Pageview stats for pages related to " + relatedTopic;
table += "\n|-\n! Page Name !! width=\"5%\"| Rank !! width=\"15%\"| Hits in 30 days !! width=\"15%\"| Hits in 60 days !! width=\"15%\"| Hits in 90 days";
for(pageName in pageNames){
table += "\n|- style=\"text-align: center;\"";
table += "\n| style=\"text-align: left; font-weight: bold;\" | [[" + pageNames[pageName] + "]] ";
for(var daysToGet in rangeToGet){
$.ajax({
url: 'http://stats.grok.se/jsonp/' + lang + '/latest' + rangeToGet[daysToGet] + '/' + encodeURIComponent(pageNames[pageName]),
dataType: 'jsonp',
jsonpCallback: 'pageviewsCallback',
success: function(getStats){
for(var date in getStats.daily_views){
pageHits += getStats.daily_views[date];
}
switch(rangeToGet[daysToGet]){
case 30:
if(getStats.rank < 1 || getStats.rank > 1000){
table += "|| Not in the top 1,000";
} else {
table += "|| " + getStats.rank;
}
table += "|| {{Plainlinks|url=http://stats.grok.se/" + lang + "/latest30/" + encodeURIComponent(pageNames[pageName]) + "|name={{SUBST:formatnumber:" + pageHits + "}}}} ";
break;
case 60:
table += "|| {{Plainlinks|url=http://stats.grok.se/" + lang + "/latest60/" + encodeURIComponent(pageNames[pageName]) + "|name={{SUBST:formatnumber:" + pageHits + "}}}} ";
break;
case 90:
table += "|| {{Plainlinks|url=http://stats.grok.se/" + lang + "/latest90/" + encodeURIComponent(pageNames[pageName]) + "|name={{SUBST:formatnumber:" + pageHits + "}}}} ";
break;
default:
break;
}
pageHits = 0;
},
fail: function(errorMsg){
alert("Failed to get results for: http://stats.grok.se/jsonp/" + lang + "/latest" + rangeToGet[daysToGet] + "/" + encodeURIComponent(pageNames[pageName]) + "\nInstead received:" + errorMsg);
}
});
return total;
}
function getPageData( pagename, language ) {
if(pageName === (pageNames.length -1)){
var deferred = $.Deferred(),
table += "\n|}";
request3 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest30/' + pagename ),
alert(table);
request6 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest60/' + pagename ),
request9 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest90/' + pagename );
$.when( request3, request6, request9 ).done( function ( data3, data6, data9 ) {
deferred.resolve( {
title: data3.title,
rank: data3.rank,
hits30: sum( data3.daily_views ),
hits60: sum( data6.daily_views ),
hits90: sum( data9.daily_views )
} );
} );
return deferred;
}
function getPagesTable( pages, language ) {
}
var table = $( '<div>' ), requests = [];
$.each( pages, function ( i, page ) {
requests.push( getPageData( page, language ) );
} );
$.when.apply( $, requests ).done( function () {
$.each( requests, function ( i, request ) {
request.done( function ( data ) {
table.append( $( '<div>' ).text( JSON.stringify( data ) ) );
} );
}, );
} );
}
var pages = prompt( 'Enter page names, separated by pipes', mw.config.get( 'wgTitle' ) ).split( '|' ),
var lang language = mw.config.get(" 'wgPageContentLanguage"' );
$( '#mw-content-text' ).prepend( getPagesTable( pages, language ) );
} )( jQuery, mediaWiki );