Index: trunk/extensions/TemplateInfo/TemplateInfo.hooks.php |
— | — | @@ -0,0 +1,40 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Hooks for TemplateInfo extension |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +class TemplateInfoHooks { |
| 11 | + |
| 12 | + /* Functions */ |
| 13 | + |
| 14 | + // Initialization |
| 15 | + public static function register( &$parser ) { |
| 16 | + // Register the hook with the parser |
| 17 | + $parser->setHook( 'templateinfo', array( 'TemplateInfoHooks', 'render' ) ); |
| 18 | + |
| 19 | + // Continue |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + // Render the displayed XML, if any |
| 24 | + public static function render( $input, $args, $parser, $frame ) { |
| 25 | + // if this call is contained in a transcluded page or template, |
| 26 | + // display nothing |
| 27 | + if ($frame->title->getFullText() != $parser->getTitle()->getFullText()) |
| 28 | + return; |
| 29 | + |
| 30 | + // also display nothing if there are no contents |
| 31 | + if (empty($input)) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + // Return output |
| 36 | + global $egTemplateInfoXML; |
| 37 | + $egTemplateInfoXML = $input; |
| 38 | + $templateInfo = new TemplateInfo( $parser ); |
| 39 | + return $templateInfo->render($input); |
| 40 | + } |
| 41 | +} |
Index: trunk/extensions/TemplateInfo/TemplateInfo.API.php |
— | — | @@ -0,0 +1,109 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Adds the 'templateinfo' action to the MediaWiki API. |
| 5 | + * |
| 6 | + * @author Yaron Koren |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Protect against register_globals vulnerabilities. |
| 11 | + * This line must be present before any global variable is referenced. |
| 12 | + */ |
| 13 | +if (!defined('MEDIAWIKI')) die(); |
| 14 | + |
| 15 | +/** |
| 16 | + * @addtogroup API |
| 17 | + */ |
| 18 | +class TemplateInfoAPI extends ApiBase { |
| 19 | + |
| 20 | + public function __construct($query, $moduleName) { |
| 21 | + parent :: __construct($query, $moduleName); |
| 22 | + } |
| 23 | + |
| 24 | + public function execute() { |
| 25 | + global $wgContLang, $wgParser, $egTemplateInfoXML; |
| 26 | + |
| 27 | + $params = $this->extractRequestParams(); |
| 28 | + $template_name = $params['template']; |
| 29 | + |
| 30 | + if (strlen($template_name) == 0) { |
| 31 | + $this->dieUsage("A template name must be specified", 'param_substr'); |
| 32 | + } |
| 33 | + |
| 34 | + $template_title = Title::makeTitleSafe(NS_TEMPLATE, $template_name); |
| 35 | + if (! $template_title->exists()) { |
| 36 | + $this->dieUsage("A template does not exist by this name", 'param_substr'); |
| 37 | + } |
| 38 | + $parser_options = new ParserOptions(); |
| 39 | + $article = new Article($template_title); |
| 40 | + $text = $wgParser->parse($article->getContent(), $template_title, $parser_options); |
| 41 | + if (empty($egTemplateInfoXML)) { |
| 42 | + $this->dieUsage("This template does not contain an XML definition.", 'param_substr'); |
| 43 | + } |
| 44 | + |
| 45 | + $data = array($egTemplateInfoXML); |
| 46 | + |
| 47 | + $xmlDTD =<<<END |
| 48 | +<?xml version="1.0" encoding="utf-8"?> |
| 49 | +<!DOCTYPE template [ |
| 50 | +<!ELEMENT template (description?,params?,data*)> |
| 51 | +<!ELEMENT params (param|group)*> |
| 52 | +<!ELEMENT param (label?,description?,options?,type?,data*)> |
| 53 | +<!ATTLIST param id ID #REQUIRED> |
| 54 | +<!ELEMENT group (label?,description?,param*,data*)> |
| 55 | +<!ELEMENT label (#PCDATA|msg)*> |
| 56 | +<!ELEMENT description (#PCDATA|msg)*> |
| 57 | +<!ELEMENT options (option*)> |
| 58 | +<!ELEMENT option (#PCDATA|msg)*> |
| 59 | +<!ELEMENT type (field*)> |
| 60 | +<!ATTLIST type name CDATA #REQUIRED> |
| 61 | +<!ELEMENT field EMPTY> |
| 62 | +<!ATTLIST field name CDATA #REQUIRED> |
| 63 | +<!ATTLIST field value CDATA #REQUIRED> |
| 64 | +<!ELEMENT msg (#PCDATA)> |
| 65 | +<!ATTLIST msg lang CDATA #REQUIRED> |
| 66 | +<!ELEMENT data (field*)> |
| 67 | +<!ATTLIST data app CDATA #REQUIRED> |
| 68 | +]> |
| 69 | + |
| 70 | +END; |
| 71 | + // hide parsing warnings |
| 72 | + libxml_use_internal_errors(true); |
| 73 | + $xml_success = simplexml_load_string($xmlDTD . $egTemplateInfoXML); |
| 74 | + if (! $xml_success) { |
| 75 | + $this->dieUsage("Template contains invalid XML", 'badxml'); |
| 76 | + } |
| 77 | + |
| 78 | + // Set top-level elements |
| 79 | + $result = $this->getResult(); |
| 80 | + $result->setIndexedTagName($data, 'p'); |
| 81 | + $result->addValue(null, $this->getModuleName(), $data); |
| 82 | + } |
| 83 | + |
| 84 | + protected function getAllowedParams() { |
| 85 | + return array ( |
| 86 | + 'template' => null, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + protected function getParamDescription() { |
| 91 | + return array ( |
| 92 | + 'template' => 'The name of the template to retrieve information for', |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + protected function getDescription() { |
| 97 | + return 'Template information, defined by the Templat Info extension (http://www.mediawiki.org/Extension:Template_Info)'; |
| 98 | + } |
| 99 | + |
| 100 | + protected function getExamples() { |
| 101 | + return array ( |
| 102 | + 'api.php?action=templateinfo&template=My_template', |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public function getVersion() { |
| 107 | + return __CLASS__ . ': $Id$'; |
| 108 | + } |
| 109 | + |
| 110 | +} |
Index: trunk/extensions/TemplateInfo/TemplateInfo.classes.php |
— | — | @@ -0,0 +1,20 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Classes for TemplateInfo extension |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +// TemplateInfo class |
| 11 | +class TemplateInfo { |
| 12 | + |
| 13 | + /** |
| 14 | + * Display the contents of <templateinfo> in a nicely-formatted way. |
| 15 | + */ |
| 16 | + public function render( $input ) { |
| 17 | + $text = "<p>" . wfMsg('templateinfo-header') . "</p>\n"; |
| 18 | + $text .= htmlentities($input); |
| 19 | + return $text; |
| 20 | + } |
| 21 | +} |
Index: trunk/extensions/TemplateInfo/TemplateInfo.i18n.php |
— | — | @@ -0,0 +1,29 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Messages file for the InputBox extension |
| 6 | + * |
| 7 | + * @addtogroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Get all extension messages |
| 12 | + * |
| 13 | + * @return array |
| 14 | + */ |
| 15 | +$messages = array(); |
| 16 | + |
| 17 | +$messages['en'] = array( |
| 18 | + 'templateinfo-desc' => 'Supports templates defining their data structure via XML markup.', |
| 19 | + 'templateinfo-header' => 'The XML definition for this template is:', |
| 20 | +); |
| 21 | + |
| 22 | +/** Message documentation (Message documentation) |
| 23 | + * @author Meno25 |
| 24 | + * @author SPQRobin |
| 25 | + * @author Siebrand |
| 26 | + */ |
| 27 | +$messages['qqq'] = array( |
| 28 | + 'templateinfo-desc' => 'Short description of the Template Info extension, shown on [[Special:Version]].', |
| 29 | + 'templateinfo-header' => 'Header to display XML definition in template page', |
| 30 | +); |
Index: trunk/extensions/TemplateInfo/TemplateInfo.php |
— | — | @@ -0,0 +1,50 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * TemplateInfo extension |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + * |
| 9 | + * This file contains the main include file for the TemplateInfo extension of |
| 10 | + * MediaWiki. |
| 11 | + * |
| 12 | + * Usage: Add the following line in LocalSettings.php: |
| 13 | + * require_once( "$IP/extensions/TemplateInfo/TemplateInfo.php" ); |
| 14 | + * |
| 15 | + * @version 0.1 |
| 16 | + */ |
| 17 | + |
| 18 | +// Check environment |
| 19 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 20 | + echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); |
| 21 | + die( -1 ); |
| 22 | +} |
| 23 | + |
| 24 | +/* Configuration */ |
| 25 | + |
| 26 | +// Credits |
| 27 | +$wgExtensionCredits['parserhook'][] = array( |
| 28 | + 'path' => __FILE__, |
| 29 | + 'name' => 'TemplateInfo', |
| 30 | + 'author' => 'Yaron Koren', |
| 31 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:TemplateInfo', |
| 32 | + 'description' => 'Supports templates defining their data structure via XML markup.', |
| 33 | + 'descriptionmsg' => 'templateinfo-desc', |
| 34 | +); |
| 35 | + |
| 36 | +// Shortcut to this extension directory |
| 37 | +$dir = dirname( __FILE__ ) . '/'; |
| 38 | + |
| 39 | +// Internationalization |
| 40 | +$wgExtensionMessagesFiles['TemplateInfo'] = $dir . 'TemplateInfo.i18n.php'; |
| 41 | + |
| 42 | +// Register auto load for the special page class |
| 43 | +$wgAutoloadClasses['TemplateInfoHooks'] = $dir . 'TemplateInfo.hooks.php'; |
| 44 | +$wgAutoloadClasses['TemplateInfo'] = $dir . 'TemplateInfo.classes.php'; |
| 45 | +$wgAutoloadClasses['TemplateInfoAPI'] = $dir . 'TemplateInfo.API.php'; |
| 46 | + |
| 47 | +// Register parser hook |
| 48 | +$wgHooks['ParserFirstCallInit'][] = 'TemplateInfoHooks::register'; |
| 49 | + |
| 50 | +// Register API action |
| 51 | +$wgAPIModules['templateinfo'] = 'TemplateInfoAPI'; |