r67786 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r67785‎ | r67786 | r67787 >
Date:08:33, 10 June 2010
Author:daniel
Status:deferred
Tags:
Comment:
generously sprinkle wfDebugLog around DataTransclusion
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
@@ -88,6 +88,7 @@
8989 // find out which data source to use...
9090 if ( empty( $argv['source'] ) ) {
9191 if ( empty( $argv[1] ) ) {
 92+ wfDebugLog( 'DataTransclusion', "no source specified\n" );
9293 return DataTransclusionHandler::errorMessage( 'datatransclusion-missing-source', $asHTML );
9394 } else {
9495 $sourceName = $argv[1];
@@ -98,6 +99,7 @@
99100
100101 $source = DataTransclusionHandler::getDataSource( $sourceName );
101102 if ( empty( $source ) ) {
 103+ wfDebugLog( 'DataTransclusion', "unknown data-source: $sourceName\n" );
102104 return DataTransclusionHandler::errorMessage( 'datatransclusion-unknown-source', $asHTML, $sourceName );
103105 }
104106
@@ -110,6 +112,7 @@
111113
112114 $keyFields = $source->getKeyFields();
113115 if ( ! in_array( $by, $keyFields ) ) {
 116+ wfDebugLog( 'DataTransclusion', "bad 'by' argument: $by (not a known key field)\n" );
114117 return DataTransclusionHandler::errorMessage( 'datatransclusion-bad-argument-by', $asHTML, $sourceName, $by, join( ', ', $keyFields ) );
115118 }
116119
@@ -117,6 +120,7 @@
118121 $key = $argv['key'];
119122 } else if ( $key === null || $key === false ) {
120123 if ( empty( $argv[2] ) ) {
 124+ wfDebugLog( 'DataTransclusion', "missing 'key' argument\n" );
121125 return DataTransclusionHandler::errorMessage( 'datatransclusion-missing-argument-key', $asHTML );
122126 } else {
123127 $key = $argv[2];
@@ -126,6 +130,7 @@
127131 // find out how to render the record
128132 if ( empty( $argv['template'] ) ) {
129133 if ( empty( $argv[3] ) ) {
 134+ wfDebugLog( 'DataTransclusion', "missing 'template' argument\n" );
130135 return DataTransclusionHandler::errorMessage( 'datatransclusion-missing-argument-template', $asHTML );
131136 } else {
132137 $template = $argv[3];
@@ -136,11 +141,15 @@
137142
138143 // load the record
139144 $record = $source->fetchRecord( $by, $key );
140 - if ( empty( $record ) ) return DataTransclusionHandler::errorMessage( 'datatransclusion-record-not-found', $asHTML, $sourceName, $by, $key );
 145+ if ( empty( $record ) ) {
 146+ wfDebugLog( 'DataTransclusion', "no record found matching $by=$key in $sourceName\n" );
 147+ return DataTransclusionHandler::errorMessage( 'datatransclusion-record-not-found', $asHTML, $sourceName, $by, $key );
 148+ }
141149
142150 // render the record into wiki text
143151 $t = Title::newFromText( $template, NS_TEMPLATE );
144152 if ( empty( $t ) ) {
 153+ wfDebugLog( 'DataTransclusion', "illegal template name: $template\n" );
145154 return DataTransclusionHandler::errorMessage( 'datatransclusion-bad-template-name', $asHTML, $template );
146155 }
147156
@@ -153,6 +162,7 @@
154163 $text = $handler->render( $record );
155164
156165 if ( $text === false ) {
 166+ wfDebugLog( 'DataTransclusion', "template not found: $template\n" );
157167 return DataTransclusionHandler::errorMessage( 'datatransclusion-unknown-template', $asHTML, $template );
158168 }
159169
@@ -306,6 +316,7 @@
307317 throw new MWException( "failed to instantiate \$wgDataTransclusionSources['$name'] as new $c." );
308318 }
309319
 320+ wfDebugLog( 'DataTransclusion', "created instance of $c as data-source \"$name\"\n" );
310321 $source = $obj;
311322
312323 if ( isset( $spec[ 'cache' ] ) ) { // check if a cache should be used
@@ -315,15 +326,15 @@
316327 }
317328
318329 $source = new CachingDataTransclusionSource( $obj, $c, @$spec['cache-duration'] ); // apply caching wrapper
 330+
 331+ wfDebugLog( 'DataTransclusion', "wrapped data-source \"$name\" in an instance of CachingDataTransclusionSource\n" );
319332 }
320333
321334 $wgDataTransclusionSources[ $name ] = $source; // replace spec array by actual object, for later re-use
322335 }
323336
324337 if ( !is_object( $source ) ) {
325 - if ( !isset( $source[ 'class' ] ) ) {
326 - throw new MWException( "\$wgDataTransclusionSources['$name'] must be an array or an object." );
327 - }
 338+ throw new MWException( "\$wgDataTransclusionSources['$name'] must be an array or an object." );
328339 }
329340
330341 return $source;
Index: trunk/extensions/DataTransclusion/WebDataTransclusionSource.php
@@ -76,24 +76,29 @@
7777 public function fetchRecord( $field, $value ) {
7878 $raw = $this->loadRecordData( $field, $value ); // TESTME
7979 if ( !$raw ) {
80 - return false; // TODO: log error?
 80+ wfDebugLog( 'DataTransclusion', "failed to fetch data for $field=$value\n" );
 81+ return false;
8182 }
8283
8384 $data = $this->decodeData( $raw, $this->dataFormat );
8485 if ( !$data ) {
85 - return false; // TODO: log error?
 86+ wfDebugLog( 'DataTransclusion', "failed to decode data for $field=$value as {$this->dataFormat}\n" );
 87+ return false;
8688 }
8789
8890 $err = $this->extractError( $data );
8991 if ( $err ) {
90 - return false; // TODO: log error?
 92+ wfDebugLog( 'DataTransclusion', "error message when fetching $field=$value: $err\n" );
 93+ return false;
9194 }
9295
9396 $rec = $this->extractRecord( $data );
9497 if ( !$rec ) {
95 - return false; // TODO: log error?
 98+ wfDebugLog( 'DataTransclusion', "no record found in data for $field=$value\n" );
 99+ return false;
96100 }
97101
 102+ wfDebugLog( 'DataTransclusion', "loaded record for $field=$value from URL\n" );
98103 return $rec;
99104 }
100105
@@ -116,7 +121,18 @@
117122 public function loadRecordData( $field, $value ) {
118123 $u = $this->getRecordURL( $field, $value );
119124
120 - $raw = Http::get( $u, $this->timeout, $this->httpOptions );
 125+ if ( preg_match( '!^https?://!', $u ) ) {
 126+ $raw = Http::get( $u, $this->timeout, $this->httpOptions );
 127+ } else {
 128+ $raw = file_get_contents( $u ); // TESTME
 129+ }
 130+
 131+ if ( $raw ) {
 132+ wfDebugLog( 'DataTransclusion', "loaded " . strlen( $raw ) . " bytes of data from $u\n" );
 133+ } else {
 134+ wfDebugLog( 'DataTransclusion', "failed to load data from $u\n" );
 135+ }
 136+
121137 return $raw;
122138 }
123139
Index: trunk/extensions/DataTransclusion/DataTransclusionSource.php
@@ -76,6 +76,8 @@
7777 function __construct( $spec ) {
7878 $this->name = $spec[ 'name' ];
7979
 80+ wfDebugLog( 'DataTransclusion', "constructing " . get_class( $this ) . " \"{$this->name}\"\n" );
 81+
8082 $this->keyFields = self::splitList( $spec[ 'keyFields' ] );
8183
8284 if ( isset( $spec[ 'fieldNames' ] ) ) {
@@ -132,7 +134,7 @@
133135 return $this->cacheDuration;
134136 }
135137
136 - public function fetchRecord( $key, $value ) {
 138+ public function fetchRecord( $field, $value ) {
137139 throw new MWException( "override fetchRecord()" );
138140 }
139141 }
@@ -180,20 +182,27 @@
181183 return $this->source->getCacheDuration();
182184 }
183185
184 - public function fetchRecord( $key, $value ) {
 186+ public function fetchRecord( $field, $value ) {
185187 global $wgDBname, $wgUser;
186 -
187 - $cacheKey = "$wgDBname:DataTransclusion(" . $this->getName() . ":$key=$value)";
188188
 189+ $cacheKey = "$wgDBname:DataTransclusion(" . $this->getName() . ":$field=$value)";
 190+
189191 $rec = $this->cache->get( $cacheKey );
190 -
 192+
191193 if ( !$rec ) {
192 - $rec = $this->source->fetchRecord( $key, $value );
193 - if ( $rec ) {
194 - $this->cache->set( $cacheKey, $rec, $this->getCacheDuration() ) ; // XXX: also cache negatives??
 194+ wfDebugLog( 'DataTransclusion', "fetching fresh record for $field=$value\n" );
 195+ $rec = $this->source->fetchRecord( $field, $value );
 196+
 197+ if ( $rec ) { // XXX: also cache negatives??
 198+ $duration = $this->getCacheDuration();
 199+
 200+ wfDebugLog( 'DataTransclusion', "caching record for $field=$value for $duration sec\n" );
 201+ $this->cache->set( $cacheKey, $rec, $duration ) ;
195202 }
 203+ } else {
 204+ wfDebugLog( 'DataTransclusion', "using cached record for $field=$value\n" );
196205 }
197 -
 206+
198207 return $rec;
199208 }
200209 }
@@ -240,7 +249,7 @@
241250 }
242251 }
243252
244 - public function fetchRecord( $key, $value ) {
245 - return @$this->lookup[ $key ][ $value ];
 253+ public function fetchRecord( $field, $value ) {
 254+ return @$this->lookup[ $field ][ $value ];
246255 }
247256 }
Index: trunk/extensions/DataTransclusion/DBDataTransclusionSource.php
@@ -116,15 +116,24 @@
117117 $db = wfGetDB( DB_SLAVE );
118118
119119 $sql = $this->getQuery( $field, $value, $db );
 120+ wfDebugLog( 'DataTransclusion', "sql query for $field=$value: $sql\n" );
120121
121122 $rs = $db->query( $sql, "DBDataTransclusionSource(" . $this->getName() . ")::fetchRecord" );
 123+
122124 if ( !$rs ) {
 125+ wfDebugLog( 'DataTransclusion', "sql query failed for $field=$value\n" );
123126 return false;
124127 }
125128
126129 $rec = $db->fetchRow( $rs );
 130+ if ( !$rec ) {
 131+ wfDebugLog( 'DataTransclusion', "no record found matching $field=$value\n" );
 132+ return false;
 133+ }
127134
128135 $db->freeResult( $rs );
 136+
 137+ wfDebugLog( 'DataTransclusion', "loaded record for $field=$value from database\n" );
129138 return $rec;
130139 }
131140 }

Status & tagging log