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

Content deleted Content added
Technical 13 (talk | contribs)
Not sure why I'm getting a foo is not defined error... Trying to make this work with callbacks to deal with async 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.
 
(One intermediate revision 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 createTable( $, mw ) {
function sum ( obj ) {
var table = "{| class=\"collapsible collapsed sortable\" width=\"95%\" border=\"1\"";
var total;
table += "\n|-\n! colspan=\"5\"| Pageview stats for pages related to " + relatedTopic;
for (var prop in obj ) {
table += "\n|-\n! Page Name !! width=\"15%\"| Hits in 30 days !! width=\"15%\"| Hits in 60 days !! width=\"15%\"| Hits in 90 days !! width=\"5%\"| Rank";
if ( obj.hasOwnProperty( prop ) ) {
for(var page in allData){
total += parseInt( obj[prop], 10 );
table += "\n|- style=\"text-align: center;\"";
}
table += "\n| style=\"text-align: left; font-weight: bold;\" | [[" + pageNames[pageName] + "]] ";
table += "|| {{Plainlinks|url=http://stats.grok.se/" + lang + "/latest30/" + encodeURIComponent(pageNames[pageName]) + "|name={{SUBST:formatnumber:" + allData[foo].hits30 + "}}}} ";
table += "|| {{Plainlinks|url=http://stats.grok.se/" + lang + "/latest60/" + encodeURIComponent(pageNames[pageName]) + "|name={{SUBST:formatnumber:" + allData[foo].hits60 + "}}}} ";
table += "|| {{Plainlinks|url=http://stats.grok.se/" + lang + "/latest90/" + encodeURIComponent(pageNames[pageName]) + "|name={{SUBST:formatnumber:" + allData[foo].hits90 + "}}}} ";
if(allData[foo].rank < 1 || allData[foo].rank > 1000){
table += "|| Not in the top 1,000";
} else {
table += "|| " + allData[foo].rank;
}
return total;
}
function getPageData( pagename, language ) {
table += "\n|}";
var deferred = $.Deferred(),
alert(table);
request3 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest30/' + pagename ),
}
request6 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest60/' + pagename ),
//var lang = mw.config.get("wgPageContentLanguage");
request9 = $.getJSON( 'http://stats.grok.se/json/' + language + '/latest90/' + pagename );
if(typeof(lang) === "undefined"){
$.when( request3, request6, request9 ).done( function ( data3, data6, data9 ) {
lang = prompt("Get results in what language?", "en");
deferred.resolve( {
}
title: data3.title,
//var relatedTopic = mw.config.get("wgTitle").substr(0, mw.config.get("wgTitle").indexOf("/"));
rank: data3.rank,
if(typeof(relatedTopic) === "undefined" || relatedTopic === ""){
hits30: sum( data3.daily_views ),
relatedTopic = prompt("What are these pages related to?", "{{PAGENAME}}");
hits60: sum( data6.daily_views ),
}
hits90: sum( data9.daily_views )
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);
return deferred;
} else {
}
pageNames = pageNames.split("|");
function getPagesTable( pages, language ) {
}
var table = $( '<div>' ), requests = [];
var rangeToGet = new Array(30, 60, 90);
$.each( pages, function ( i, page ) {
var allData = [];
requests.push( getPageData( page, language ) );
var hits30 = 0;
} );
var hits60 = 0;
$.when.apply( $, requests ).done( function () {
var hits90 = 0;
$.each( requests, function ( i, request ) {
for(var pageName in pageNames){
request.done( function ( data ) {
for(var daysToGet in rangeToGet){
table.append( $( '<div>' ).text( JSON.stringify( data ) ) );
$.ajax({
} );
url: 'http://stats.grok.se/jsonp/' + lang + '/latest' + rangeToGet[daysToGet] + '/' + encodeURIComponent(pageNames[pageName]),
} );
dataType: 'jsonp',
} );
jsonpCallback: 'pageviewsCallback',
success: function(getStats){
var pageHits = 0;
for(var date in getStats.daily_views){
pageHits += getStats.daily_views[date];
}
switch(rangeToGet[daysToGet]){
case 30:
allData[foo].rank = getStats.rank;
allData[foo].hits30 = pageHits;
break;
case 60:
allData[foo].hits60 = pageHits;
break;
case 90:
allData[foo].hits90 = pageHits;
break;
default:
break;
}
},
fail: function(errorMsg){
alert("Failed to get results for: http://stats.grok.se/jsonp/" + lang + "/latest" + rangeToGet[daysToGet] + "/" + encodeURIComponent(pageNames[pageName]) + "\nInstead received:" + errorMsg);
}
});
}
var pages = prompt( 'Enter page names, separated by pipes', mw.config.get( 'wgTitle' ) ).split( '|' ),
}
language = mw.config.get( 'wgPageContentLanguage' );
$( '#mw-content-text' ).prepend( getPagesTable( pages, language ) );
} )( jQuery, mediaWiki );