r69781 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69780‎ | r69781 | r69782 >
Date:09:11, 23 July 2010
Author:peter17
Status:ok (Comments)
Tags:
Comment:
Negative caching of API-retrieved templates (as requested in r69746) + prefixed title without iw prefix (r69730)
Modified paths:
  • /branches/iwtransclusion/phase3/includes/Interwiki.php (modified) (history)
  • /branches/iwtransclusion/phase3/includes/Title.php (modified) (history)

Diff [purge]

Index: branches/iwtransclusion/phase3/includes/Interwiki.php
@@ -277,7 +277,7 @@
278278
279279 } else if( $transAPI !== '' ) {
280280
281 - $fullTitle = $title->getNsText().':'.$title->getText();
 281+ $fullTitle = $title->getSemiPrefixedText( );
282282
283283 $finalText = self::fetchTemplateFromAPI( $wikiID, $transAPI, $fullTitle );
284284
@@ -337,9 +337,13 @@
338338
339339 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $wikiID, $fullTitle );
340340 $text = $wgMemc->get( $key );
341 - if( $text ){
342 - return $text;
343 - }
 341+ if( is_array ( $text )
 342+ && isset ( $text['missing'] )
 343+ && $text['missing'] === true ){
 344+ return false;
 345+ } else if ( $text ) {
 346+ return $text;
 347+ }
344348
345349 $url = wfAppendQuery(
346350 $transAPI,
@@ -354,12 +358,17 @@
355359 $get = Http::get( $url );
356360 $content = FormatJson::decode( $get, true );
357361
358 - if ( ! empty($content['query']['pages']) ) {
359 -
360 - $page = array_pop( $content['query']['pages'] );
361 - $text = $page['revisions'][0]['*'];
362 - $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
363 - return $text;
 362+ if ( isset ( $content['query'] )
 363+ && isset ( $content['query']['pages'] ) ) {
 364+ $page = array_pop( $content['query']['pages'] );
 365+ if ( $page
 366+ && isset( $page['revisions'][0]['*'] ) ) {
 367+ $text = $page['revisions'][0]['*'];
 368+ $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
 369+ return $text;
 370+ } else {
 371+ $wgMemc->set( $key, array ( 'missing' => true ), $wgTranscludeCacheExpiry );
 372+ }
364373 }
365374 return false;
366375 }
@@ -370,10 +379,12 @@
371380 $outdatedTitles = array( );
372381
373382 foreach( $titles as $title ){
374 - $key = wfMemcKey( 'iwtransclustiontext', 'textid', $wikiID, $title['title'] );
375 - $text = $wgMemc->get( $key );
376 - if( !$text ){
377 - $outdatedTitles[] = $title['title'];
 383+ if ( isset ( $title['title'] ) ) {
 384+ $key = wfMemcKey( 'iwtransclustiontext', 'textid', $wikiID, $title['title'] );
 385+ $text = $wgMemc->get( $key );
 386+ if( !$text ){
 387+ $outdatedTitles[] = $title['title'];
 388+ }
378389 }
379390 }
380391
@@ -392,14 +403,18 @@
393404 $get = Http::get( $url );
394405 $content = FormatJson::decode( $get, true );
395406
396 - if ( ! empty($content['query']['pages']) ) {
 407+ if ( isset ( $content['query'] )
 408+ && isset ( $content['query']['pages'] ) ) {
397409 foreach( $content['query']['pages'] as $page ) {
398410 $key = wfMemcKey( 'iwtransclustiontext', 'textid', $wikiID, $page['title'] );
399 - $text = $page['revisions'][0]['*'];
 411+ if ( isset ( $page['revisions'][0]['*'] ) ) {
 412+ $text = $page['revisions'][0]['*'];
 413+ } else {
 414+ $text = array ( 'missing' => true );
 415+ }
400416 $wgMemc->set( $key, $text, $wgTranscludeCacheExpiry );
401417 }
402418 }
403419 }
404420 }
405 -
406421 }
Index: branches/iwtransclusion/phase3/includes/Title.php
@@ -718,6 +718,20 @@
719719 }
720720
721721 /**
 722+ * Return the prefixed title with spaces _without_ the interwiki prefix
 723+ *
 724+ * @return \type{\string} the title, prefixed by the namespace but not by the interwiki prefix, with spaces
 725+ */
 726+ public function getSemiPrefixedText() {
 727+ if ( !isset( $this->mSemiPrefixedText ) ){
 728+ $s = ( $this->mNamespace === NS_MAIN ? '' : $this->getNsText() . ':' ) . $this->mTextform;
 729+ $s = str_replace( '_', ' ', $s );
 730+ $this->mSemiPrefixedText = $s;
 731+ }
 732+ return $this->mSemiPrefixedText;
 733+ }
 734+
 735+ /**
722736 * Get the prefixed title with spaces, plus any fragment
723737 * (part beginning with '#')
724738 *

Follow-up revisions

RevisionCommit summaryAuthorDate
r87106Merge r69745, r69746, r69781, r69783reedy00:28, 29 April 2011
r92987Merge r87106 which is a Merge r69745, r69746, r69781, r69783reedy17:25, 24 July 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r69730iwtransclusion code updated according to the remarks about r69723, except abo...peter1714:14, 22 July 2010
r69746Batch caching for API-retrieved templates, as requested in r69730peter1719:43, 22 July 2010

Comments

#Comment by Peter17 (talk | contribs)   09:14, 23 July 2010

I don't know how to test my code for caching stuff: it seems that $wgMemc is not caching anything on my install... Maybe I have to configure it?

#Comment by Catrope (talk | contribs)   09:19, 23 July 2010

$wgMainCacheType = CACHE_DB; in LocalSettings.php . You can also play around with arbitrary PHP statements with maintenance/eval.php : run that from the command line (php maintenance/eval.php) and you'll basically get an interactive PHP "shell" in which you can use MediaWiki things such as $wgMemc

#Comment by Catrope (talk | contribs)   09:24, 23 July 2010
-			return $text;
+		if ( isset ( $content['query'] )
+			&&  isset ( $content['query']['pages'] ) ) {
+				$page = array_pop( $content['query']['pages'] );

Instead of indenting if blocks with long conditions this way, the following way is preferred:

if ( $someVeryLongCondition &&
		some_other_very_long_condition() &&
		$someMoreStuff ) {
	do_stuff();
}

Status & tagging log