Index: trunk/phase3/includes/QueryPage.php |
— | — | @@ -172,6 +172,18 @@ |
173 | 173 | function formatResult( $skin, $result ) { |
174 | 174 | return ''; |
175 | 175 | } |
| 176 | + /** |
| 177 | + * Formats the result as something that can be understood by the API. |
| 178 | + * Defaults to setting id, ns and title |
| 179 | + */ |
| 180 | + function formatApiResult( $result ) { |
| 181 | + $title = Title::makeTitle( $row->namespace, $row->title ); |
| 182 | + return array( |
| 183 | + 'pageid' => intval( $row->id ), |
| 184 | + 'ns' => intval( $title->getNamespace() ), |
| 185 | + 'title' => $title->getPrefixedText(), |
| 186 | + ); |
| 187 | + } |
176 | 188 | |
177 | 189 | /** |
178 | 190 | * The content returned by this function will be output before any result |
— | — | @@ -278,21 +290,94 @@ |
279 | 291 | function doQuery( $offset, $limit, $shownavigation=true ) { |
280 | 292 | global $wgUser, $wgOut, $wgLang, $wgContLang; |
281 | 293 | |
| 294 | + $wgOut->setSyndicated( $this->isSyndicated() ); |
| 295 | + |
| 296 | + # Really really do the query now |
| 297 | + $result = $this->reallyDoQuery( $offset, $limit ); |
| 298 | + |
| 299 | + # Tell about cachiness |
| 300 | + if ( $result['cached'] !== false ) { |
| 301 | + if ( $result['cached'] === true ) { |
| 302 | + $wgOut->addWikiMsg( 'perfcached' ); |
| 303 | + } else { |
| 304 | + $updated = $wgLang->timeAndDate( $result['cached'], true, true ); |
| 305 | + $wgOut->addMeta( 'Data-Cache-Time', $result['cached'] ); |
| 306 | + $wgOut->addInlineScript( "var dataCacheTime = '{$result['cached']}';" ); |
| 307 | + $wgOut->addWikiMsg( 'perfcachedts', $updated ); |
| 308 | + } |
| 309 | + } |
| 310 | + if ( $result['disabled'] ) { |
| 311 | + # If updates on this page have been disabled, let the user know |
| 312 | + # that the data set won't be refreshed for now |
| 313 | + |
| 314 | + $wgOut->addWikiMsg( 'querypage-no-updates' ); |
| 315 | + } |
| 316 | + |
| 317 | + $wgOut->addHTML( XML::openElement( 'div', array('class' => 'mw-spcontent') ) ); |
| 318 | + |
| 319 | + # Top header and navigation |
| 320 | + if( $shownavigation ) { |
| 321 | + $wgOut->addHTML( $this->getPageHeader() ); |
| 322 | + if( $result['count'] > 0 ) { |
| 323 | + $wgOut->addHTML( '<p>' . wfShowingResults( $offset, $result['count'] ) . '</p>' ); |
| 324 | + # Disable the "next" link when we reach the end |
| 325 | + $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $this->getName() ), |
| 326 | + wfArrayToCGI( $this->linkParameters() ), ( $result['count'] < $limit ) ); |
| 327 | + $wgOut->addHTML( '<p>' . $paging . '</p>' ); |
| 328 | + } else { |
| 329 | + # No results to show, so don't bother with "showing X of Y" etc. |
| 330 | + # -- just let the user know and give up now |
| 331 | + $wgOut->addHTML( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' ); |
| 332 | + $wgOut->addHTML( XML::closeElement( 'div' ) ); |
| 333 | + return; |
| 334 | + } |
| 335 | + } |
| 336 | + |
| 337 | + # The actual results; specialist subclasses will want to handle this |
| 338 | + # with more than a straight list, so we hand them the info, plus |
| 339 | + # an OutputPage, and let them get on with it |
| 340 | + $this->outputResults( $wgOut, |
| 341 | + $wgUser->getSkin(), |
| 342 | + $result['dbr'], # Should use a ResultWrapper for this |
| 343 | + $result['result'], |
| 344 | + $result['count'], |
| 345 | + $offset ); |
| 346 | + |
| 347 | + # Repeat the paging links at the bottom |
| 348 | + if( $shownavigation ) { |
| 349 | + $wgOut->addHTML( '<p>' . $paging . '</p>' ); |
| 350 | + } |
| 351 | + |
| 352 | + $wgOut->addHTML( XML::closeElement( 'div' ) ); |
| 353 | + |
| 354 | + return $result['count']; |
| 355 | + } |
| 356 | + |
| 357 | + /** |
| 358 | + * Really really do the query. Returns an array with: |
| 359 | + * 'disabled' => true if the data will not be further refreshed, |
| 360 | + * 'cached' => false if uncached, the timestamp of the last cache if known, else simply true, |
| 361 | + * 'result' => the real result object, |
| 362 | + * 'count' => number of results, |
| 363 | + * 'dbr' => the database used for fetching the data |
| 364 | + */ |
| 365 | + protected function reallyDoQuery( $offset, $limit ) { |
| 366 | + $result = array( 'disabled' => false ); |
| 367 | + |
282 | 368 | $this->offset = $offset; |
283 | 369 | $this->limit = $limit; |
284 | 370 | |
285 | | - $sname = $this->getName(); |
286 | | - $fname = get_class($this) . '::doQuery'; |
| 371 | + $fname = get_class($this) . '::reallyDoQuery'; |
287 | 372 | $dbr = wfGetDB( DB_SLAVE ); |
288 | 373 | |
289 | | - $wgOut->setSyndicated( $this->isSyndicated() ); |
290 | 374 | |
291 | 375 | if ( !$this->isCached() ) { |
292 | 376 | $sql = $this->getSQL(); |
| 377 | + $result['cached'] = false; |
293 | 378 | } else { |
294 | 379 | # Get the cached result |
295 | 380 | $querycache = $dbr->tableName( 'querycache' ); |
296 | | - $type = $dbr->strencode( $sname ); |
| 381 | + $type = $dbr->strencode( $this->getName() ); |
297 | 382 | $sql = |
298 | 383 | "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value |
299 | 384 | FROM $querycache WHERE qc_type='$type'"; |
— | — | @@ -304,21 +389,17 @@ |
305 | 390 | $tRow = $dbr->fetchObject( $tRes ); |
306 | 391 | |
307 | 392 | if( $tRow ) { |
308 | | - $updated = $wgLang->timeAndDate( $tRow->qci_timestamp, true, true ); |
309 | | - $wgOut->addMeta( 'Data-Cache-Time', $tRow->qci_timestamp ); |
310 | | - $wgOut->addInlineScript( "var dataCacheTime = '{$tRow->qci_timestamp}';" ); |
311 | | - $wgOut->addWikiMsg( 'perfcachedts', $updated ); |
| 393 | + $result['cached'] = $tRow->qci_timestamp; |
312 | 394 | } else { |
313 | | - $wgOut->addWikiMsg( 'perfcached' ); |
| 395 | + $result['cached'] = true; |
314 | 396 | } |
315 | 397 | |
316 | 398 | # If updates on this page have been disabled, let the user know |
317 | 399 | # that the data set won't be refreshed for now |
318 | 400 | global $wgDisableQueryPageUpdate; |
319 | 401 | if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) { |
320 | | - $wgOut->addWikiMsg( 'querypage-no-updates' ); |
| 402 | + $result['disabled'] = true; |
321 | 403 | } |
322 | | - |
323 | 404 | } |
324 | 405 | |
325 | 406 | } |
— | — | @@ -329,45 +410,11 @@ |
330 | 411 | $num = $dbr->numRows($res); |
331 | 412 | |
332 | 413 | $this->preprocessResults( $dbr, $res ); |
333 | | - |
334 | | - $wgOut->addHTML( XML::openElement( 'div', array('class' => 'mw-spcontent') ) ); |
335 | | - |
336 | | - # Top header and navigation |
337 | | - if( $shownavigation ) { |
338 | | - $wgOut->addHTML( $this->getPageHeader() ); |
339 | | - if( $num > 0 ) { |
340 | | - $wgOut->addHTML( '<p>' . wfShowingResults( $offset, $num ) . '</p>' ); |
341 | | - # Disable the "next" link when we reach the end |
342 | | - $paging = wfViewPrevNext( $offset, $limit, $wgContLang->specialPage( $sname ), |
343 | | - wfArrayToCGI( $this->linkParameters() ), ( $num < $limit ) ); |
344 | | - $wgOut->addHTML( '<p>' . $paging . '</p>' ); |
345 | | - } else { |
346 | | - # No results to show, so don't bother with "showing X of Y" etc. |
347 | | - # -- just let the user know and give up now |
348 | | - $wgOut->addHTML( '<p>' . wfMsgHtml( 'specialpage-empty' ) . '</p>' ); |
349 | | - $wgOut->addHTML( XML::closeElement( 'div' ) ); |
350 | | - return; |
351 | | - } |
352 | | - } |
353 | | - |
354 | | - # The actual results; specialist subclasses will want to handle this |
355 | | - # with more than a straight list, so we hand them the info, plus |
356 | | - # an OutputPage, and let them get on with it |
357 | | - $this->outputResults( $wgOut, |
358 | | - $wgUser->getSkin(), |
359 | | - $dbr, # Should use a ResultWrapper for this |
360 | | - $res, |
361 | | - $dbr->numRows( $res ), |
362 | | - $offset ); |
363 | | - |
364 | | - # Repeat the paging links at the bottom |
365 | | - if( $shownavigation ) { |
366 | | - $wgOut->addHTML( '<p>' . $paging . '</p>' ); |
367 | | - } |
368 | | - |
369 | | - $wgOut->addHTML( XML::closeElement( 'div' ) ); |
370 | | - |
371 | | - return $num; |
| 414 | + |
| 415 | + $result['result'] = $res; |
| 416 | + $result['count'] = $num; |
| 417 | + $result['dbr'] = $dbr; |
| 418 | + return $result; |
372 | 419 | } |
373 | 420 | |
374 | 421 | /** |
— | — | @@ -391,7 +438,7 @@ |
392 | 439 | |
393 | 440 | # $res might contain the whole 1,000 rows, so we read up to |
394 | 441 | # $num [should update this to use a Pager] |
395 | | - for( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++ ) { |
| 442 | + for ( $i = 0; $i < $num && $row = $dbr->fetchObject( $res ); $i++ ) { |
396 | 443 | $line = $this->formatResult( $skin, $row ); |
397 | 444 | if( $line ) { |
398 | 445 | $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 ) |
— | — | @@ -428,6 +475,7 @@ |
429 | 476 | } |
430 | 477 | } |
431 | 478 | |
| 479 | + |
432 | 480 | function openList( $offset ) { |
433 | 481 | return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n"; |
434 | 482 | } |