Return template contents from a wiki page

From Geohashing
Revision as of 15:02, 17 February 2015 by imported>Sourcerer (Implementation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Using MediaWiki api.php

  1. Hello World!
  2. Return page titles for a specified category
  3. Return template contents from a wiki page
  4. Wiki built-in templates

Get Array of Templates

Provide the page title and get back an array of templates - parsing not yet implemented.

<?php
  // =======================================================================
  // === These constant values must be set before running the script =======
  // =======================================================================
  
  define("API_URL", 'http://wiki.xkcd.com/wgh/api.php');    // Search any wiki page source for "api.php" to find this path on other MediaWiki sites
  define("A_PAGE_TITLE", "Sourcerer_2015");

  // =======================================================================
  // === Provide the page title and 
  // === get the page contents
  // =======================================================================
  function getPage($pageTitle)
  {
    $json     = file_get_contents(API_URL . "?format=json&action=query&titles=$pageTitle&prop=revisions&rvprop=content");
    $readable = json_decode($json, true);

    return $readable;
  }

  // =======================================================================
  // === Provide the page title and 
  // === get the page contents
  // === This will do some parsing NOT IMPLEMENTED YET
  // =======================================================================
  function getTemplatesInPage($pageTitle)
  {
    $templateArray = array();

    $templateArray[] = getPage($pageTitle);
    
    $templateArray[] = "{{Dummy_Template|lat=52|lon=1|user=Sourcerer}}";
    $templateArray[] = "{{Dummy_Template|lat=52|lon=0|user=Sourcerer}}";
    
    return $templateArray;
  }

  // =======================================================================
  // main program
  // =======================================================================
  
  print_r(getTemplatesInPage(A_PAGE_TITLE));
?>