r53147 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53146‎ | r53147 | r53148 >
Date:21:16, 12 July 2009
Author:btongminh
Status:reverted
Tags:
Comment:
Refactored the querying code out of the display code, to allow a future API module for query pages. No user visible changes.
Modified paths:
  • /trunk/phase3/includes/QueryPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/QueryPage.php
@@ -172,6 +172,18 @@
173173 function formatResult( $skin, $result ) {
174174 return '';
175175 }
 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+ }
176188
177189 /**
178190 * The content returned by this function will be output before any result
@@ -278,21 +290,94 @@
279291 function doQuery( $offset, $limit, $shownavigation=true ) {
280292 global $wgUser, $wgOut, $wgLang, $wgContLang;
281293
 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+
282368 $this->offset = $offset;
283369 $this->limit = $limit;
284370
285 - $sname = $this->getName();
286 - $fname = get_class($this) . '::doQuery';
 371+ $fname = get_class($this) . '::reallyDoQuery';
287372 $dbr = wfGetDB( DB_SLAVE );
288373
289 - $wgOut->setSyndicated( $this->isSyndicated() );
290374
291375 if ( !$this->isCached() ) {
292376 $sql = $this->getSQL();
 377+ $result['cached'] = false;
293378 } else {
294379 # Get the cached result
295380 $querycache = $dbr->tableName( 'querycache' );
296 - $type = $dbr->strencode( $sname );
 381+ $type = $dbr->strencode( $this->getName() );
297382 $sql =
298383 "SELECT qc_type as type, qc_namespace as namespace,qc_title as title, qc_value as value
299384 FROM $querycache WHERE qc_type='$type'";
@@ -304,21 +389,17 @@
305390 $tRow = $dbr->fetchObject( $tRes );
306391
307392 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;
312394 } else {
313 - $wgOut->addWikiMsg( 'perfcached' );
 395+ $result['cached'] = true;
314396 }
315397
316398 # If updates on this page have been disabled, let the user know
317399 # that the data set won't be refreshed for now
318400 global $wgDisableQueryPageUpdate;
319401 if( is_array( $wgDisableQueryPageUpdate ) && in_array( $this->getName(), $wgDisableQueryPageUpdate ) ) {
320 - $wgOut->addWikiMsg( 'querypage-no-updates' );
 402+ $result['disabled'] = true;
321403 }
322 -
323404 }
324405
325406 }
@@ -329,45 +410,11 @@
330411 $num = $dbr->numRows($res);
331412
332413 $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;
372419 }
373420
374421 /**
@@ -391,7 +438,7 @@
392439
393440 # $res might contain the whole 1,000 rows, so we read up to
394441 # $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++ ) {
396443 $line = $this->formatResult( $skin, $row );
397444 if( $line ) {
398445 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
@@ -428,6 +475,7 @@
429476 }
430477 }
431478
 479+
432480 function openList( $offset ) {
433481 return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n";
434482 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r53167Revert r53147, r53149 and r53163 ("Add API module for QueryPage-based special...catrope12:40, 13 July 2009
r53216Revert r53147 properly; was supposed to be reverted in r53167, but I wasn't f...catrope08:44, 14 July 2009

Status & tagging log