Index: trunk/extensions/DataTransclusion/DataTransclusionHandler.php |
— | — | @@ -88,6 +88,7 @@ |
89 | 89 | // find out which data source to use... |
90 | 90 | if ( empty( $argv['source'] ) ) { |
91 | 91 | if ( empty( $argv[1] ) ) { |
| 92 | + wfDebugLog( 'DataTransclusion', "no source specified\n" ); |
92 | 93 | return DataTransclusionHandler::errorMessage( 'datatransclusion-missing-source', $asHTML ); |
93 | 94 | } else { |
94 | 95 | $sourceName = $argv[1]; |
— | — | @@ -98,6 +99,7 @@ |
99 | 100 | |
100 | 101 | $source = DataTransclusionHandler::getDataSource( $sourceName ); |
101 | 102 | if ( empty( $source ) ) { |
| 103 | + wfDebugLog( 'DataTransclusion', "unknown data-source: $sourceName\n" ); |
102 | 104 | return DataTransclusionHandler::errorMessage( 'datatransclusion-unknown-source', $asHTML, $sourceName ); |
103 | 105 | } |
104 | 106 | |
— | — | @@ -110,6 +112,7 @@ |
111 | 113 | |
112 | 114 | $keyFields = $source->getKeyFields(); |
113 | 115 | if ( ! in_array( $by, $keyFields ) ) { |
| 116 | + wfDebugLog( 'DataTransclusion', "bad 'by' argument: $by (not a known key field)\n" ); |
114 | 117 | return DataTransclusionHandler::errorMessage( 'datatransclusion-bad-argument-by', $asHTML, $sourceName, $by, join( ', ', $keyFields ) ); |
115 | 118 | } |
116 | 119 | |
— | — | @@ -117,6 +120,7 @@ |
118 | 121 | $key = $argv['key']; |
119 | 122 | } else if ( $key === null || $key === false ) { |
120 | 123 | if ( empty( $argv[2] ) ) { |
| 124 | + wfDebugLog( 'DataTransclusion', "missing 'key' argument\n" ); |
121 | 125 | return DataTransclusionHandler::errorMessage( 'datatransclusion-missing-argument-key', $asHTML ); |
122 | 126 | } else { |
123 | 127 | $key = $argv[2]; |
— | — | @@ -126,6 +130,7 @@ |
127 | 131 | // find out how to render the record |
128 | 132 | if ( empty( $argv['template'] ) ) { |
129 | 133 | if ( empty( $argv[3] ) ) { |
| 134 | + wfDebugLog( 'DataTransclusion', "missing 'template' argument\n" ); |
130 | 135 | return DataTransclusionHandler::errorMessage( 'datatransclusion-missing-argument-template', $asHTML ); |
131 | 136 | } else { |
132 | 137 | $template = $argv[3]; |
— | — | @@ -136,11 +141,15 @@ |
137 | 142 | |
138 | 143 | // load the record |
139 | 144 | $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 | + } |
141 | 149 | |
142 | 150 | // render the record into wiki text |
143 | 151 | $t = Title::newFromText( $template, NS_TEMPLATE ); |
144 | 152 | if ( empty( $t ) ) { |
| 153 | + wfDebugLog( 'DataTransclusion', "illegal template name: $template\n" ); |
145 | 154 | return DataTransclusionHandler::errorMessage( 'datatransclusion-bad-template-name', $asHTML, $template ); |
146 | 155 | } |
147 | 156 | |
— | — | @@ -153,6 +162,7 @@ |
154 | 163 | $text = $handler->render( $record ); |
155 | 164 | |
156 | 165 | if ( $text === false ) { |
| 166 | + wfDebugLog( 'DataTransclusion', "template not found: $template\n" ); |
157 | 167 | return DataTransclusionHandler::errorMessage( 'datatransclusion-unknown-template', $asHTML, $template ); |
158 | 168 | } |
159 | 169 | |
— | — | @@ -306,6 +316,7 @@ |
307 | 317 | throw new MWException( "failed to instantiate \$wgDataTransclusionSources['$name'] as new $c." ); |
308 | 318 | } |
309 | 319 | |
| 320 | + wfDebugLog( 'DataTransclusion', "created instance of $c as data-source \"$name\"\n" ); |
310 | 321 | $source = $obj; |
311 | 322 | |
312 | 323 | if ( isset( $spec[ 'cache' ] ) ) { // check if a cache should be used |
— | — | @@ -315,15 +326,15 @@ |
316 | 327 | } |
317 | 328 | |
318 | 329 | $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" ); |
319 | 332 | } |
320 | 333 | |
321 | 334 | $wgDataTransclusionSources[ $name ] = $source; // replace spec array by actual object, for later re-use |
322 | 335 | } |
323 | 336 | |
324 | 337 | 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." ); |
328 | 339 | } |
329 | 340 | |
330 | 341 | return $source; |
Index: trunk/extensions/DataTransclusion/WebDataTransclusionSource.php |
— | — | @@ -76,24 +76,29 @@ |
77 | 77 | public function fetchRecord( $field, $value ) { |
78 | 78 | $raw = $this->loadRecordData( $field, $value ); // TESTME |
79 | 79 | if ( !$raw ) { |
80 | | - return false; // TODO: log error? |
| 80 | + wfDebugLog( 'DataTransclusion', "failed to fetch data for $field=$value\n" ); |
| 81 | + return false; |
81 | 82 | } |
82 | 83 | |
83 | 84 | $data = $this->decodeData( $raw, $this->dataFormat ); |
84 | 85 | 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; |
86 | 88 | } |
87 | 89 | |
88 | 90 | $err = $this->extractError( $data ); |
89 | 91 | if ( $err ) { |
90 | | - return false; // TODO: log error? |
| 92 | + wfDebugLog( 'DataTransclusion', "error message when fetching $field=$value: $err\n" ); |
| 93 | + return false; |
91 | 94 | } |
92 | 95 | |
93 | 96 | $rec = $this->extractRecord( $data ); |
94 | 97 | if ( !$rec ) { |
95 | | - return false; // TODO: log error? |
| 98 | + wfDebugLog( 'DataTransclusion', "no record found in data for $field=$value\n" ); |
| 99 | + return false; |
96 | 100 | } |
97 | 101 | |
| 102 | + wfDebugLog( 'DataTransclusion', "loaded record for $field=$value from URL\n" ); |
98 | 103 | return $rec; |
99 | 104 | } |
100 | 105 | |
— | — | @@ -116,7 +121,18 @@ |
117 | 122 | public function loadRecordData( $field, $value ) { |
118 | 123 | $u = $this->getRecordURL( $field, $value ); |
119 | 124 | |
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 | + |
121 | 137 | return $raw; |
122 | 138 | } |
123 | 139 | |
Index: trunk/extensions/DataTransclusion/DataTransclusionSource.php |
— | — | @@ -76,6 +76,8 @@ |
77 | 77 | function __construct( $spec ) { |
78 | 78 | $this->name = $spec[ 'name' ]; |
79 | 79 | |
| 80 | + wfDebugLog( 'DataTransclusion', "constructing " . get_class( $this ) . " \"{$this->name}\"\n" ); |
| 81 | + |
80 | 82 | $this->keyFields = self::splitList( $spec[ 'keyFields' ] ); |
81 | 83 | |
82 | 84 | if ( isset( $spec[ 'fieldNames' ] ) ) { |
— | — | @@ -132,7 +134,7 @@ |
133 | 135 | return $this->cacheDuration; |
134 | 136 | } |
135 | 137 | |
136 | | - public function fetchRecord( $key, $value ) { |
| 138 | + public function fetchRecord( $field, $value ) { |
137 | 139 | throw new MWException( "override fetchRecord()" ); |
138 | 140 | } |
139 | 141 | } |
— | — | @@ -180,20 +182,27 @@ |
181 | 183 | return $this->source->getCacheDuration(); |
182 | 184 | } |
183 | 185 | |
184 | | - public function fetchRecord( $key, $value ) { |
| 186 | + public function fetchRecord( $field, $value ) { |
185 | 187 | global $wgDBname, $wgUser; |
186 | | - |
187 | | - $cacheKey = "$wgDBname:DataTransclusion(" . $this->getName() . ":$key=$value)"; |
188 | 188 | |
| 189 | + $cacheKey = "$wgDBname:DataTransclusion(" . $this->getName() . ":$field=$value)"; |
| 190 | + |
189 | 191 | $rec = $this->cache->get( $cacheKey ); |
190 | | - |
| 192 | + |
191 | 193 | 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 ) ; |
195 | 202 | } |
| 203 | + } else { |
| 204 | + wfDebugLog( 'DataTransclusion', "using cached record for $field=$value\n" ); |
196 | 205 | } |
197 | | - |
| 206 | + |
198 | 207 | return $rec; |
199 | 208 | } |
200 | 209 | } |
— | — | @@ -240,7 +249,7 @@ |
241 | 250 | } |
242 | 251 | } |
243 | 252 | |
244 | | - public function fetchRecord( $key, $value ) { |
245 | | - return @$this->lookup[ $key ][ $value ]; |
| 253 | + public function fetchRecord( $field, $value ) { |
| 254 | + return @$this->lookup[ $field ][ $value ]; |
246 | 255 | } |
247 | 256 | } |
Index: trunk/extensions/DataTransclusion/DBDataTransclusionSource.php |
— | — | @@ -116,15 +116,24 @@ |
117 | 117 | $db = wfGetDB( DB_SLAVE ); |
118 | 118 | |
119 | 119 | $sql = $this->getQuery( $field, $value, $db ); |
| 120 | + wfDebugLog( 'DataTransclusion', "sql query for $field=$value: $sql\n" ); |
120 | 121 | |
121 | 122 | $rs = $db->query( $sql, "DBDataTransclusionSource(" . $this->getName() . ")::fetchRecord" ); |
| 123 | + |
122 | 124 | if ( !$rs ) { |
| 125 | + wfDebugLog( 'DataTransclusion', "sql query failed for $field=$value\n" ); |
123 | 126 | return false; |
124 | 127 | } |
125 | 128 | |
126 | 129 | $rec = $db->fetchRow( $rs ); |
| 130 | + if ( !$rec ) { |
| 131 | + wfDebugLog( 'DataTransclusion', "no record found matching $field=$value\n" ); |
| 132 | + return false; |
| 133 | + } |
127 | 134 | |
128 | 135 | $db->freeResult( $rs ); |
| 136 | + |
| 137 | + wfDebugLog( 'DataTransclusion', "loaded record for $field=$value from database\n" ); |
129 | 138 | return $rec; |
130 | 139 | } |
131 | 140 | } |