Difference between revisions of "Return template contents from a wiki page"

From Geohashing
imported>Sourcerer
(Implementation)
 
imported>Sourcerer
m (Sourcerer's Implementation)
 
Line 3: Line 3:
 
== Get Array of Templates ==
 
== Get Array of Templates ==
  
Provide the page title and get back an array of templates - parsing not yet implemented.
+
Provide the page title and get back an array of templates.
  
 
  <nowiki><?php
 
  <nowiki><?php
Line 11: Line 11:
 
    
 
    
 
   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("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");
+
   define("A_PAGE_TITLE", "Sourcerer_2015");                 // Which wiki page to process
  
 
   // =======================================================================
 
   // =======================================================================
Line 19: Line 19:
 
   function getPage($pageTitle)
 
   function getPage($pageTitle)
 
   {
 
   {
     $json    = file_get_contents(API_URL . "?format=json&action=query&titles=$pageTitle&prop=revisions&rvprop=content");
+
     return file_get_contents(API_URL . "?format=json&action=query&titles=$pageTitle&prop=revisions&rvprop=content");
    $readable = json_decode($json, true);
 
 
 
    return $readable;
 
 
   }
 
   }
  
Line 32: Line 29:
 
   function getTemplatesInPage($pageTitle)
 
   function getTemplatesInPage($pageTitle)
 
   {
 
   {
 +
    $txt = getPage($pageTitle);
 +
    $txt = str_replace('\n', '', $txt);
 +
    $inTemplate    = false;
 +
    $currTemplate  = '';
 
     $templateArray = array();
 
     $templateArray = array();
 +
 
 +
    for ($ii = 1; $ii < strlen($txt);  $ii++)
 +
    {
 +
      if (($txt[$ii - 1] == '{') && ($txt[$ii] == '{'))    // Template start
 +
      {
 +
        $inTemplate = true;
 +
      }
 +
      else
 +
      if (($txt[$ii - 1] == '}') && ($txt[$ii] == '}'))    // Template end
 +
      {
 +
        $inTemplate = false;
 +
        if (trim($currTemplate) != '')
 +
        {
 +
          $templateArray[] = explode('|', $currTemplate);
 +
        }
 +
        $currTemplate = '';
 +
      }
 +
      else   
 +
      if ($txt[$ii] == '{')
 +
      {
 +
          // Skip it
 +
      }
 +
      else   
 +
      if ($txt[$ii] == '}')
 +
      {
 +
          // Skip it
 +
      }
 +
      else
 +
      if ($inTemplate)
 +
      {
 +
        $currTemplate .= $txt[$ii];
 +
      }
 +
    }
 +
 +
    if ($inTemplate)
 +
    {
 +
      $inTemplate = false;
 +
      if (trim($currTemplate) != '')
 +
      {
 +
        $templateArray[] = explode('|', $currTemplate);
 +
      }
 +
      $currTemplate = '';
 +
    }
  
    $templateArray[] = getPage($pageTitle);
 
   
 
    $templateArray[] = "{{Dummy_Template|lat=52|lon=1|user=Sourcerer}}";
 
    $templateArray[] = "{{Dummy_Template|lat=52|lon=0|user=Sourcerer}}";
 
   
 
 
     return $templateArray;
 
     return $templateArray;
 
   }
 
   }

Latest revision as of 13:06, 19 February 2015

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.

<?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");                 // Which wiki page to process

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

  // =======================================================================
  // === Provide the page title and 
  // === get the page contents
  // === This will do some parsing NOT IMPLEMENTED YET
  // =======================================================================
  function getTemplatesInPage($pageTitle)
  {
    $txt = getPage($pageTitle);
    $txt = str_replace('\n', '', $txt);
    $inTemplate    = false;
    $currTemplate  = '';
    $templateArray = array();
  
    for ($ii = 1; $ii < strlen($txt);  $ii++)
    {
      if (($txt[$ii - 1] == '{') && ($txt[$ii] == '{'))    // Template start
      {
        $inTemplate = true;
      }
      else
      if (($txt[$ii - 1] == '}') && ($txt[$ii] == '}'))    // Template end
      {
        $inTemplate = false;
        if (trim($currTemplate) != '')
        {
          $templateArray[] = explode('|', $currTemplate);
        }
        $currTemplate = '';
      }
      else    
      if ($txt[$ii] == '{')
      {
          // Skip it
      }
      else    
      if ($txt[$ii] == '}')
      {
          // Skip it
      }
      else
      if ($inTemplate)
      {
        $currTemplate .= $txt[$ii];
      }
    }

    if ($inTemplate)
    {
      $inTemplate = false;
      if (trim($currTemplate) != '')
      {
        $templateArray[] = explode('|', $currTemplate);
      }
      $currTemplate = '';
    }

    return $templateArray;
  }

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