Return template contents from a wiki page
From Geohashing
Using MediaWiki api.php
- Hello World!
- Return page titles for a specified category
- Return template contents from a wiki page
- 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)); ?>