r58587 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58586‎ | r58587 | r58588 >
Date:16:36, 5 November 2009
Author:yaron
Status:ok (Comments)
Tags:
Comment:
Moving XML validation from API querying to parsing
Modified paths:
  • /trunk/extensions/TemplateInfo/ApiQueryTemplateInfo.php (modified) (history)
  • /trunk/extensions/TemplateInfo/TemplateInfo.hooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TemplateInfo/TemplateInfo.hooks.php
@@ -19,6 +19,39 @@
2020 return true;
2121 }
2222
 23+ public static function validateXML( $xml ) {
 24+ $xmlDTD =<<<END
 25+<?xml version="1.0" encoding="utf-8"?>
 26+<!DOCTYPE template [
 27+<!ELEMENT template (description?,params?,data*)>
 28+<!ELEMENT params (param|group)*>
 29+<!ELEMENT param (label?,description?,options?,type?,data*)>
 30+<!ATTLIST param id ID #REQUIRED>
 31+<!ELEMENT group (label?,description?,param*,data*)>
 32+<!ELEMENT label (#PCDATA|msg)*>
 33+<!ELEMENT description (#PCDATA|msg)*>
 34+<!ELEMENT options (option*)>
 35+<!ELEMENT option (#PCDATA|msg)*>
 36+<!ELEMENT type (field*)>
 37+<!ATTLIST type name CDATA #REQUIRED>
 38+<!ELEMENT field EMPTY>
 39+<!ATTLIST field name CDATA #REQUIRED>
 40+<!ATTLIST field value CDATA #REQUIRED>
 41+<!ELEMENT msg (#PCDATA)>
 42+<!ATTLIST msg lang CDATA #REQUIRED>
 43+<!ELEMENT data (field*)>
 44+<!ATTLIST data app CDATA #REQUIRED>
 45+]>
 46+
 47+END;
 48+ // we are using the SimpleXML library to do the XML validation
 49+ // for now - this may change later
 50+ // hide parsing warnings
 51+ libxml_use_internal_errors(true);
 52+ $xml_success = simplexml_load_string($xmlDTD . $xml);
 53+ return $xml_success;
 54+ }
 55+
2356 // Render the displayed XML, if any
2457 public static function render( $input, $args, $parser, $frame ) {
2558 // if this call is contained in a transcluded page or template,
@@ -28,7 +61,10 @@
2962
3063 // Store XML in the page_props table
3164 // TODO: Do processing here, like parse to an array
32 - $parser->getOutput()->setProperty( 'templateinfo', $input );
 65+ if ( TemplateInfoHooks::validateXML( $input ) )
 66+ $parser->getOutput()->setProperty( 'templateinfo', $input );
 67+ else
 68+ $parser->getOutput()->setProperty( 'templateinfo', "Error: Invalid XML" );
3369
3470 // Return output
3571 $text = "<p>" . wfMsg( 'templateinfo-header' ) . "</p>\n";
Index: trunk/extensions/TemplateInfo/ApiQueryTemplateInfo.php
@@ -20,39 +20,6 @@
2121 parent :: __construct( $query, $moduleName, 'ti' );
2222 }
2323
24 - private function validateXML( $xml ) {
25 - $xmlDTD =<<<END
26 -<?xml version="1.0" encoding="utf-8"?>
27 -<!DOCTYPE template [
28 -<!ELEMENT template (description?,params?,data*)>
29 -<!ELEMENT params (param|group)*>
30 -<!ELEMENT param (label?,description?,options?,type?,data*)>
31 -<!ATTLIST param id ID #REQUIRED>
32 -<!ELEMENT group (label?,description?,param*,data*)>
33 -<!ELEMENT label (#PCDATA|msg)*>
34 -<!ELEMENT description (#PCDATA|msg)*>
35 -<!ELEMENT options (option*)>
36 -<!ELEMENT option (#PCDATA|msg)*>
37 -<!ELEMENT type (field*)>
38 -<!ATTLIST type name CDATA #REQUIRED>
39 -<!ELEMENT field EMPTY>
40 -<!ATTLIST field name CDATA #REQUIRED>
41 -<!ATTLIST field value CDATA #REQUIRED>
42 -<!ELEMENT msg (#PCDATA)>
43 -<!ATTLIST msg lang CDATA #REQUIRED>
44 -<!ELEMENT data (field*)>
45 -<!ATTLIST data app CDATA #REQUIRED>
46 -]>
47 -
48 -END;
49 - // we are using the SimpleXML library to do the XML validation
50 - // for now - this may change later
51 - // hide parsing warnings
52 - libxml_use_internal_errors(true);
53 - $xml_success = simplexml_load_string($xmlDTD . $xml);
54 - return $xml_success;
55 - }
56 -
5724 public function execute() {
5825 $params = $this->extractRequestParams();
5926 $titles = $this->getPageSet()->getGoodTitles();
@@ -72,11 +39,7 @@
7340 $res = $this->select(__METHOD__);
7441 while ( $row = $this->getDB()->fetchObject( $res ) ) {
7542 $vals = array( );
76 - if ($this->validateXML( $row->pp_value )) {
77 - ApiResult::setContent( $vals, $row->pp_value );
78 - } else {
79 - ApiResult::setContent( $vals, "Error! Invalid XML" );
80 - }
 43+ ApiResult::setContent( $vals, $row->pp_value );
8144 $fit = $this->addPageSubItems( $row->pp_page, $vals );
8245 if( !$fit ) {
8346 $this->setContinueEnumParameter( 'continue', $row->pp_page );

Comments

#Comment by Catrope (talk | contribs)   20:51, 5 November 2009

Putting the text "Error: invalid XML" in the page_props table seems like a bad idea to me. Instead, just put an empty string there or something, and check for it in the API module and add the invalid => attribute when appropriate.

#Comment by Yaron Koren (talk | contribs)   06:37, 9 November 2009

That's a good idea - I'm on vacation now, but I'll fix this when I get back.

#Comment by Siebrand (talk | contribs)   16:30, 17 January 2010

Was fixed in r58478. Setting to new.

Status & tagging log