r67750 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67749‎ | r67750 | r67751 >
Date:19:50, 9 June 2010
Author:daniel
Status:deferred
Tags:
Comment:
make more friendy to unit testing
Modified paths:
  • /trunk/extensions/DataTransclusion/DBDataTransclusionSource.php (modified) (history)
  • /trunk/extensions/DataTransclusion/DataTransclusionHandler.php (modified) (history)
  • /trunk/extensions/DataTransclusion/DataTransclusionSource.php (modified) (history)
  • /trunk/extensions/DataTransclusion/WebDataTransclusionSource.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DataTransclusion/DataTransclusionHandler.php
@@ -58,15 +58,17 @@
5959 }
6060
6161 static function errorMessage( $key, $asHTML ) {
62 - $params = func_get_args();
63 - array_shift( $params ); // $key
64 - array_shift( $params ); // $asHTML
 62+ $params = func_get_args();
 63+ array_shift( $params ); // $key
 64+ array_shift( $params ); // $asHTML
6565
66 - if ( $asHTML ) $mode = 'parseinline';
67 - else $mode = 'parsemag';
 66+ if ( $asHTML ) $mode = 'parseinline';
 67+ else $mode = 'parsemag';
6868
69 - $m = wfMsgExt( $key, $mode, $params );
70 - return "<span class=\"error\">$m</span>";
 69+ $m = wfMsgExt( $key, $mode, $params );
 70+
 71+ //NOTE: using the key as a css class is done mainely for testing/debugging, but could be useful for scripting and styling too.
 72+ return "<span class=\"error $key\">$m</span>";
7173 }
7274
7375 /**
@@ -80,7 +82,7 @@
8183 * Fetches a records and renders it, according to the given array of parameters.
8284 * Common implementation for parser tag and parser function.
8385 */
84 - static function handleRecordTransclusion( $key, $argv, $parser, $asHTML ) {
 86+ static function handleRecordTransclusion( $key, $argv, $parser, $asHTML, $templateText = null ) {
8587 //find out which data source to use...
8688 if ( empty( $argv['source'] ) ) {
8789 if ( empty( $argv[1] ) ) return DataTransclusionHandler::errorMessage( 'datatransclusion-missing-source', $asHTML ); //TESTME
@@ -124,7 +126,7 @@
125127 //FIXME: log the template we used into the parser output, like regular template use
126128 // (including templates used by the template, etc)
127129
128 - $handler = new DataTransclusionHandler( $parser, $source, $t );
 130+ $handler = new DataTransclusionHandler( $parser, $source, $t, $templateText );
129131
130132 $record = $handler->normalizeRecord( $record );
131133 $text = $handler->render( $record );
@@ -233,11 +235,13 @@
234236 '!^;!m' => '&#59;',
235237 '![\r\n]!' => ' ',
236238 '!^ !m' => '&#32;',
237 - '!^-!m' => '&#45;',
238 - '!^=!m' => '&#61;',
 239+ '!^-!m' => '&#45;',
 240+ '!^=!m' => '&#61;',
239241 );
240242
241243 static function sanitizeValue( $v ) {
 244+ //TODO: would be nicer to use <nowiki> - or better, insert substitution chunks directly into the parser state. would still need html escpaing though
 245+
242246 $find = array_keys( self::$sanitizerSubstitution );
243247 $subst = array_values( self::$sanitizerSubstitution );
244248
Index: trunk/extensions/DataTransclusion/WebDataTransclusionSource.php
@@ -81,7 +81,7 @@
8282 return $rec;
8383 }
8484
85 - protected function getRecordURL( $field, $value ) {
 85+ public function getRecordURL( $field, $value ) {
8686 $u = $this->url;
8787
8888 if ( strpos( $u, '?' ) === false ) $u .= '?';
@@ -94,14 +94,14 @@
9595 return $u;
9696 }
9797
98 - protected function loadRecordData( $field, $value ) {
 98+ public function loadRecordData( $field, $value ) {
9999 $u = $this->getRecordURL( $field, $value );
100100
101101 $raw = Http::get( $u, $this->timeout, $this->httpOptions );
102102 return $raw;
103103 }
104104
105 - protected function decodeData( $raw, $format = 'php' ) {
 105+ public function decodeData( $raw, $format = 'php' ) {
106106 if ( $format == 'json' ) return FormatJson::decode( $raw, true ); //TESTME
107107 if ( $format == 'wddx' ) return wddx_unserialize( $raw ); //TESTME
108108 if ( $format == 'php' ) return unserialize( $raw ); //TESTME
@@ -109,15 +109,15 @@
110110 return false;
111111 }
112112
113 - protected function extractError( $data ) {
 113+ public function extractError( $data ) {
114114 return $this->extractField( $data, $this->errorPath );
115115 }
116116
117 - protected function extractRecord( $data ) {
 117+ public function extractRecord( $data ) {
118118 return $this->extractField( $data, $this->dataPath );
119119 }
120120
121 - protected function extractField( $data, $path ) {
 121+ public function extractField( $data, $path ) {
122122 if ( $path == null ) return $data;
123123 if ( is_string( $path ) ) return @$data[ $path ];
124124
Index: trunk/extensions/DataTransclusion/DataTransclusionSource.php
@@ -59,7 +59,7 @@
6060 if ( $s === null || $s === false ) return $s;
6161 if ( !is_string( $s ) ) return $s;
6262
63 - $list = preg_split( '!\s*[,;|]\s*!', $s );
 63+ $list = preg_split( '!\s*[,;|/]\s*!', $s );
6464 return $list;
6565 }
6666
@@ -71,8 +71,12 @@
7272 $this->name = $spec[ 'name' ];
7373
7474 $this->keyFields = self::splitList( $spec[ 'keyFields' ] );
75 - $this->fieldNames = self::splitList( $spec[ 'fieldNames' ] );
7675
 76+ if ( isset( $spec[ 'fieldNames' ] ) )
 77+ $this->fieldNames = self::splitList( $spec[ 'fieldNames' ] );
 78+ else
 79+ $this->fieldNames = $this->keyFields;
 80+
7781 if ( !empty( $spec[ 'defaultKey' ] ) ) $this->defaultKey = $spec[ 'defaultKey' ];
7882 else $this->defaultKey = $this->keyFields[ 0 ];
7983
Index: trunk/extensions/DataTransclusion/DBDataTransclusionSource.php
@@ -68,9 +68,8 @@
6969 else return (string)$value;
7070 }
7171
72 - public function fetchRecord( $field, $value ) {
73 - $db = wfGetDB( DB_SLAVE );
74 -
 72+ public function getQuery( $field, $value, $db = null ) {
 73+ if ( !$db ) $db = wfGetDB( DB_SLAVE );
7574 if ( !preg_match( '/\w+[\w\d]+/', $field ) ) return false; // redundant, but make extra sure we don't get anythign evil here //TESTME
7675
7776 $value = $this->convertKey( $field, $value ); //TESTME
@@ -85,6 +84,14 @@
8685
8786 if ( $this->querySuffix ) $sql = $sql . ' ' . $this->querySuffix;
8887
 88+ return $sql;
 89+ }
 90+
 91+ public function fetchRecord( $field, $value ) {
 92+ $db = wfGetDB( DB_SLAVE );
 93+
 94+ $sql = $this->getQuery( $field, $value, $db );
 95+
8996 $rs = $db->query( $sql, "DBDataTransclusionSource(" . $this->getName() . ")::fetchRecord" );
9097 if ( !$rs ) return false;
9198

Status & tagging log