http://www.mediawiki.org/wiki/Special:Code/MediaWiki/58446
Revision: 58446 Author: yaron Date: 2009-11-02 21:59:26 +0000 (Mon, 02 Nov 2009) Log Message: ----------- New extension Added Paths: ----------- trunk/extensions/TemplateInfo/ trunk/extensions/TemplateInfo/TemplateInfo.API.php trunk/extensions/TemplateInfo/TemplateInfo.classes.php trunk/extensions/TemplateInfo/TemplateInfo.hooks.php trunk/extensions/TemplateInfo/TemplateInfo.i18n.php trunk/extensions/TemplateInfo/TemplateInfo.php Added: trunk/extensions/TemplateInfo/TemplateInfo.API.php =================================================================== --- trunk/extensions/TemplateInfo/TemplateInfo.API.php (rev 0) +++ trunk/extensions/TemplateInfo/TemplateInfo.API.php 2009-11-02 21:59:26 UTC (rev 58446) @@ -0,0 +1,109 @@ +<?php +/** + * Adds the 'templateinfo' action to the MediaWiki API. + * + * @author Yaron Koren + */ + +/** + * Protect against register_globals vulnerabilities. + * This line must be present before any global variable is referenced. + */ +if (!defined('MEDIAWIKI')) die(); + +/** + * @addtogroup API + */ +class TemplateInfoAPI extends ApiBase { + + public function __construct($query, $moduleName) { + parent :: __construct($query, $moduleName); + } + + public function execute() { + global $wgContLang, $wgParser, $egTemplateInfoXML; + + $params = $this->extractRequestParams(); + $template_name = $params['template']; + + if (strlen($template_name) == 0) { + $this->dieUsage("A template name must be specified", 'param_substr'); + } + + $template_title = Title::makeTitleSafe(NS_TEMPLATE, $template_name); + if (! $template_title->exists()) { + $this->dieUsage("A template does not exist by this name", 'param_substr'); + } + $parser_options = new ParserOptions(); + $article = new Article($template_title); + $text = $wgParser->parse($article->getContent(), $template_title, $parser_options); + if (empty($egTemplateInfoXML)) { + $this->dieUsage("This template does not contain an XML definition.", 'param_substr'); + } + + $data = array($egTemplateInfoXML); + + $xmlDTD =<<<END +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE template [ +<!ELEMENT template (description?,params?,data*)> +<!ELEMENT params (param|group)*> +<!ELEMENT param (label?,description?,options?,type?,data*)> +<!ATTLIST param id ID #REQUIRED> +<!ELEMENT group (label?,description?,param*,data*)> +<!ELEMENT label (#PCDATA|msg)*> +<!ELEMENT description (#PCDATA|msg)*> +<!ELEMENT options (option*)> +<!ELEMENT option (#PCDATA|msg)*> +<!ELEMENT type (field*)> +<!ATTLIST type name CDATA #REQUIRED> +<!ELEMENT field EMPTY> +<!ATTLIST field name CDATA #REQUIRED> +<!ATTLIST field value CDATA #REQUIRED> +<!ELEMENT msg (#PCDATA)> +<!ATTLIST msg lang CDATA #REQUIRED> +<!ELEMENT data (field*)> +<!ATTLIST data app CDATA #REQUIRED> +]> + +END; + // hide parsing warnings + libxml_use_internal_errors(true); + $xml_success = simplexml_load_string($xmlDTD . $egTemplateInfoXML); + if (! $xml_success) { + $this->dieUsage("Template contains invalid XML", 'badxml'); + } + + // Set top-level elements + $result = $this->getResult(); + $result->setIndexedTagName($data, 'p'); + $result->addValue(null, $this->getModuleName(), $data); + } + + protected function getAllowedParams() { + return array ( + 'template' => null, + ); + } + + protected function getParamDescription() { + return array ( + 'template' => 'The name of the template to retrieve information for', + ); + } + + protected function getDescription() { + return 'Template information, defined by the Templat Info extension (http://www.mediawiki.org/Extension:Template_Info)'; + } + + protected function getExamples() { + return array ( + 'api.php?action=templateinfo&template=My_template', + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } + +} Added: trunk/extensions/TemplateInfo/TemplateInfo.classes.php =================================================================== --- trunk/extensions/TemplateInfo/TemplateInfo.classes.php (rev 0) +++ trunk/extensions/TemplateInfo/TemplateInfo.classes.php 2009-11-02 21:59:26 UTC (rev 58446) @@ -0,0 +1,20 @@ +<?php +/** + * Classes for TemplateInfo extension + * + * @file + * @ingroup Extensions + */ + +// TemplateInfo class +class TemplateInfo { + + /** + * Display the contents of <templateinfo> in a nicely-formatted way. + */ + public function render( $input ) { + $text = "<p>" . wfMsg('templateinfo-header') . "</p>\n"; + $text .= htmlentities($input); + return $text; + } +} Added: trunk/extensions/TemplateInfo/TemplateInfo.hooks.php =================================================================== --- trunk/extensions/TemplateInfo/TemplateInfo.hooks.php (rev 0) +++ trunk/extensions/TemplateInfo/TemplateInfo.hooks.php 2009-11-02 21:59:26 UTC (rev 58446) @@ -0,0 +1,40 @@ +<?php +/** + * Hooks for TemplateInfo extension + * + * @file + * @ingroup Extensions + */ + +class TemplateInfoHooks { + + /* Functions */ + + // Initialization + public static function register( &$parser ) { + // Register the hook with the parser + $parser->setHook( 'templateinfo', array( 'TemplateInfoHooks', 'render' ) ); + + // Continue + return true; + } + + // Render the displayed XML, if any + public static function render( $input, $args, $parser, $frame ) { + // if this call is contained in a transcluded page or template, + // display nothing + if ($frame->title->getFullText() != $parser->getTitle()->getFullText()) + return; + + // also display nothing if there are no contents + if (empty($input)) { + return; + } + + // Return output + global $egTemplateInfoXML; + $egTemplateInfoXML = $input; + $templateInfo = new TemplateInfo( $parser ); + return $templateInfo->render($input); + } +} Added: trunk/extensions/TemplateInfo/TemplateInfo.i18n.php =================================================================== --- trunk/extensions/TemplateInfo/TemplateInfo.i18n.php (rev 0) +++ trunk/extensions/TemplateInfo/TemplateInfo.i18n.php 2009-11-02 21:59:26 UTC (rev 58446) @@ -0,0 +1,29 @@ +<?php + +/** + * Messages file for the InputBox extension + * + * @addtogroup Extensions + */ + +/** + * Get all extension messages + * + * @return array + */ +$messages = array(); + +$messages['en'] = array( + 'templateinfo-desc' => 'Supports templates defining their data structure via XML markup.', + 'templateinfo-header' => 'The XML definition for this template is:', +); + +/** Message documentation (Message documentation) + * @author Meno25 + * @author SPQRobin + * @author Siebrand + */ +$messages['qqq'] = array( + 'templateinfo-desc' => 'Short description of the Template Info extension, shown on [[Special:Version]].', + 'templateinfo-header' => 'Header to display XML definition in template page', +); Added: trunk/extensions/TemplateInfo/TemplateInfo.php =================================================================== --- trunk/extensions/TemplateInfo/TemplateInfo.php (rev 0) +++ trunk/extensions/TemplateInfo/TemplateInfo.php 2009-11-02 21:59:26 UTC (rev 58446) @@ -0,0 +1,50 @@ +<?php +/** + * TemplateInfo extension + * + * @file + * @ingroup Extensions + * + * This file contains the main include file for the TemplateInfo extension of + * MediaWiki. + * + * Usage: Add the following line in LocalSettings.php: + * require_once( "$IP/extensions/TemplateInfo/TemplateInfo.php" ); + * + * @version 0.1 + */ + +// Check environment +if ( !defined( 'MEDIAWIKI' ) ) { + echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); + die( -1 ); +} + +/* Configuration */ + +// Credits +$wgExtensionCredits['parserhook'][] = array( + 'path' => __FILE__, + 'name' => 'TemplateInfo', + 'author' => 'Yaron Koren', + 'url' => 'http://www.mediawiki.org/wiki/Extension:TemplateInfo', + 'description' => 'Supports templates defining their data structure via XML markup.', + 'descriptionmsg' => 'templateinfo-desc', +); + +// Shortcut to this extension directory +$dir = dirname( __FILE__ ) . '/'; + +// Internationalization +$wgExtensionMessagesFiles['TemplateInfo'] = $dir . 'TemplateInfo.i18n.php'; + +// Register auto load for the special page class +$wgAutoloadClasses['TemplateInfoHooks'] = $dir . 'TemplateInfo.hooks.php'; +$wgAutoloadClasses['TemplateInfo'] = $dir . 'TemplateInfo.classes.php'; +$wgAutoloadClasses['TemplateInfoAPI'] = $dir . 'TemplateInfo.API.php'; + +// Register parser hook +$wgHooks['ParserFirstCallInit'][] = 'TemplateInfoHooks::register'; + +// Register API action +$wgAPIModules['templateinfo'] = 'TemplateInfoAPI'; _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs