r58478 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58477‎ | r58478 | r58479 >
Date:14:15, 3 November 2009
Author:catrope
Status:deferred
Tags:
Comment:
Overhaul TemplateInfo big time:
* Don't use an $eg global to store the XML in, that's ugly as hell. Instead, store it in the page_props table, which also makes for quicker querying on the API side
* Remove useless TemplateInfo.classes.php file that contained only two lines of code
* Totally rewrite API module
** Make it prop=templateinfo instead of action=templateinfo
** Accept multiple titles, which don't necessarily have to be in the template namespace
** Query from page_props instead of running the template through the parser
** Don't process XML in the API module; maybe do some processing before storing it in the page_props table
Modified paths:
  • /trunk/extensions/TemplateInfo/ApiQueryTemplateInfo.php (added) (history)
  • /trunk/extensions/TemplateInfo/TemplateInfo.API.php (deleted) (history)
  • /trunk/extensions/TemplateInfo/TemplateInfo.classes.php (deleted) (history)
  • /trunk/extensions/TemplateInfo/TemplateInfo.hooks.php (modified) (history)
  • /trunk/extensions/TemplateInfo/TemplateInfo.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TemplateInfo/TemplateInfo.API.php
@@ -1,109 +0,0 @@
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
@@ -1,20 +0,0 @@
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.hooks.php
@@ -22,19 +22,17 @@
2323 // Render the displayed XML, if any
2424 public static function render( $input, $args, $parser, $frame ) {
2525 // if this call is contained in a transcluded page or template,
26 - // display nothing
27 - if ($frame->title->getFullText() != $parser->getTitle()->getFullText())
 26+ // or if the inpur is empty, display nothing
 27+ if ( !$frame->title->equals( $parser->getTitle() ) || $input == '' )
2828 return;
 29+
 30+ // Store XML in the page_props table
 31+ // TODO: Do processing here, like parse to an array
 32+ $parser->getOutput()->setProperty( 'templateinfo', $input );
2933
30 - // also display nothing if there are no contents
31 - if (empty($input)) {
32 - return;
33 - }
34 -
3534 // Return output
36 - global $egTemplateInfoXML;
37 - $egTemplateInfoXML = $input;
38 - $templateInfo = new TemplateInfo( $parser );
39 - return $templateInfo->render($input);
 35+ $text = "<p>" . wfMsg( 'templateinfo-header' ) . "</p>\n";
 36+ $text .= htmlspecialchars( $input, ENT_QUOTES );
 37+ return $text;
4038 }
4139 }
Index: trunk/extensions/TemplateInfo/ApiQueryTemplateInfo.php
@@ -0,0 +1,74 @@
 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 APIQueryTemplateInfo extends ApiQueryBase {
 19+
 20+ public function __construct( $query, $moduleName ) {
 21+ parent :: __construct( $query, $moduleName, 'ti' );
 22+ }
 23+
 24+ public function execute() {
 25+ $params = $this->extractRequestParams();
 26+ $titles = $this->getPageSet()->getGoodTitles();
 27+ $this->addTables( 'page_props' );
 28+ $this->addFields( array( 'pp_page', 'pp_value' ) );
 29+ $this->addWhere( array( 'pp_propname' => 'templateinfo' ) );
 30+ if ( !is_null( $params['continue'] ) )
 31+ {
 32+ $fromid = intval( $params['continue'] );
 33+ $this->addWhere( "pp_page >= $fromid" );
 34+ }
 35+ $this->addOption( 'ORDER BY', 'pp_page' );
 36+
 37+ $res = $this->select(__METHOD__);
 38+ while ( $row = $this->getDB()->fetchObject( $res ) ) {
 39+ $vals = array( );
 40+ ApiResult::setContent( $vals, $row->pp_value );
 41+ $fit = $this->addPageSubItems( $row->pp_page, $vals );
 42+ if( !$fit ) {
 43+ $this->setContinueEnumParameter( 'continue', $row->pp_page );
 44+ break;
 45+ }
 46+ }
 47+ }
 48+
 49+ public function getAllowedParams() {
 50+ return array (
 51+ 'continue' => null,
 52+ );
 53+ }
 54+
 55+ public function getParamDescription() {
 56+ return array (
 57+ 'continue' => 'When more results are available, use this to continue',
 58+ );
 59+ }
 60+
 61+ public function getDescription() {
 62+ return 'Template information, defined by the Template Info extension (http://www.mediawiki.org/Extension:Template_Info)';
 63+ }
 64+
 65+ protected function getExamples() {
 66+ return array (
 67+ 'api.php?action=query&prop=templateinfo&titles=Template:Foo|Template:Bar',
 68+ );
 69+ }
 70+
 71+ public function getVersion() {
 72+ return __CLASS__ . ': $Id$';
 73+ }
 74+
 75+}
Property changes on: trunk/extensions/TemplateInfo/ApiQueryTemplateInfo.php
___________________________________________________________________
Name: svn:mergeinfo
176 +
Index: trunk/extensions/TemplateInfo/TemplateInfo.php
@@ -26,7 +26,7 @@
2727 $wgExtensionCredits['parserhook'][] = array(
2828 'path' => __FILE__,
2929 'name' => 'TemplateInfo',
30 - 'author' => 'Yaron Koren',
 30+ 'author' => array( 'Yaron Koren', 'Roan Kattouw' ),
3131 'url' => 'http://www.mediawiki.org/wiki/Extension:TemplateInfo',
3232 'description' => 'Supports templates defining their data structure via XML markup.',
3333 'descriptionmsg' => 'templateinfo-desc',
@@ -40,11 +40,10 @@
4141
4242 // Register auto load for the special page class
4343 $wgAutoloadClasses['TemplateInfoHooks'] = $dir . 'TemplateInfo.hooks.php';
44 -$wgAutoloadClasses['TemplateInfo'] = $dir . 'TemplateInfo.classes.php';
45 -$wgAutoloadClasses['TemplateInfoAPI'] = $dir . 'TemplateInfo.API.php';
 44+$wgAutoloadClasses['ApiQueryTemplateInfo'] = $dir . 'ApiQueryTemplateInfo.php';
4645
4746 // Register parser hook
4847 $wgHooks['ParserFirstCallInit'][] = 'TemplateInfoHooks::register';
4948
5049 // Register API action
51 -$wgAPIModules['templateinfo'] = 'TemplateInfoAPI';
 50+$wgAPIPropModules['templateinfo'] = 'ApiQueryTemplateInfo';

Status & tagging log