Wiki report/javascript

From Freephile Wiki
Jump to: navigation, search

I need to come try some of this and document it. Some clientside JavaScript to talk to the mwApi.

$(document).ready(function() {
  $.ajax({
    url: "https://freephile.org/w/api.php",
    // url: "http://en.banglapedia.org/api.php",
    jsonp: "callback",
    dataType: "jsonp",
    data: {
      action: "query",
      meta: "siteinfo",
      format: "json",
      siprop: "general|extensions|statistics"
      // siprop: "general|extensions|statistics"
    },
    success: function( data ) {
      // console.log(Object.keys(data));
      var text = '';
      var myObj = data.query.general;
      Object.getOwnPropertyNames(myObj).every(function(val, idx, array) {
        if ( (myObj[val] == '') || (typeof myObj[val] == 'undefined') ) {
          return false;
        } else {
          console.log( val + ' -> ' + myObj[val]);
          text +=  '<li>' + val + ' -> ' + myObj[val] + "</li>\n";
          return true;
        }
      });
      $( "#wikigeneral" ).html(text);
      //alert(text);
      delete myObj;
      var text = '';
      var myObj = data.query.extensions;
      Object.getOwnPropertyNames(myObj).every(function(val, idx, array) {
        if ( (myObj[val] == '') || (typeof myObj[val] == 'undefined') ) {
          return false;
        } else if (typeof myObj[val] == 'object') {
          console.log( val + ' -> nested ');
          text +=  '<li>' + val + ' -> nested ';
          nestedObj = myObj[val];
          Object.getOwnPropertyNames(nestedObj).every(function(val, idx, array) {
            console.log( val + ' -> ' + nestedObj[val]);
            text +=  val + ' -> ' + nestedObj[val] + "</li>\n";
          });
        } else {
          console.log( val + ' -> ' + myObj[val]);
          text +=  '<li>' + val + ' -> ' + myObj[val] + "</li>\n";
          return true;
        }
      });
      $( "#wikiextensions" ).html(text);
      delete myObj;
      var text = '';
      var myObj = data.query.statistics;
      // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
      Object.getOwnPropertyNames(myObj).forEach(function(val, idx, array) {
        console.log( val + ' -> ' + myObj[val]);
        text +=  '<li>' + val + ' -> ' + myObj[val] + "</li>\n";
      });
      $( "#wikistatistics" ).html(text);

    }
  })
});    
    

      $(document).ready(function() {
        var url = $( "#url" ).val();
        var fullUrl = url +  apiQuery;
        $.ajax({
          url: fullUrl
        })
        .done(function( data ) {
          if ( console && console.log ) {
            console.log( "Sample of data:" +  data);
          }
        });
      });
    
    function makeQueryUrl (url, apiQuery) {
      if ( (url == '') || (typeof url == 'undefined') ) {
        url = $( "#url" ).val();
      }
      if ( (apiQuery == '') || (typeof apiQuery == 'undefined') ) {
        apiQuery = '/api.php?action=query&meta=siteinfo&format=json&siprop=general|extensions|statistics';
      }
      return url + apiQuery;
    }

    $( "#wr" ).submit(function( event ) {
        var apiQuery = '/api.php?action=query&meta=siteinfo&format=json&siprop=general|extensions|statistics';
        var fullUrl = $( "#url" ).val() + apiQuery;
        $( "#wikigeneral" ).text( fullUrl ).show().fadeIn( 100 );
        event.preventDefault();
    });