r78186 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78185‎ | r78186 | r78187 >
Date:14:27, 10 December 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Reduce query bloat
Modified paths:
  • /trunk/extensions/Translate/tag/PageTranslationHooks.php (modified) (history)
  • /trunk/extensions/Translate/tag/TranslatablePage.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/tag/TranslatablePage.php
@@ -448,27 +448,34 @@
449449
450450 // Returns false if not found
451451 protected function getTag( $tag, $dbt = DB_SLAVE ) {
452 - $db = wfGetDB( $dbt );
453452
454 - $id = $this->getTagId( $tag );
455 -
456453 if ( !$this->getTitle()->exists() ) {
457454 return false;
458455 }
459456
 457+ static $cache = array();
 458+ $aid = $this->getTitle()->getArticleId();
 459+
 460+ if ( isset( $cache[$aid][$tag] ) ) {
 461+ return $cache[$aid][$tag];
 462+ }
 463+
 464+ $db = wfGetDB( $dbt );
 465+ $id = $this->getTagId( $tag );
 466+
460467 $fields = 'rt_revision';
461468 $conds = array(
462 - 'rt_page' => $this->getTitle()->getArticleId(),
 469+ 'rt_page' => $aid,
463470 'rt_type' => $id,
464471 );
465472
466473 $options = array( 'ORDER BY' => 'rt_revision DESC' );
467474
468 - $tag = $db->selectField( 'revtag', $fields, $conds, __METHOD__, $options );
469 - if ( $tag !== false ) {
470 - return intval( $tag );
 475+ $tagRevision = $db->selectField( 'revtag', $fields, $conds, __METHOD__, $options );
 476+ if ( $tagRevision !== false ) {
 477+ return $cache[$aid][$tag] = intval( $tagRevision );
471478 } else {
472 - return false;
 479+ return $cache[$aid][$tag] = false;
473480 }
474481 }
475482
@@ -634,7 +641,7 @@
635642 return $db->selectField( 'revtag', $fields, $conds, __METHOD__, $options );
636643 }
637644
638 - protected function getTagId( $tag ) {
 645+ protected static function getTagId( $tag ) {
639646 $validTags = array( 'tp:mark', 'tp:tag', 'tp:transver' );
640647 if ( !in_array( $tag, $validTags ) ) {
641648 throw new MWException( "Invalid tag $tag requested" );
@@ -699,6 +706,45 @@
700707 protected static function changeTitleText( Title $title, $text ) {
701708 return Title::makeTitleSafe( $title->getNamespace(), $text );
702709 }
 710+
 711+ public static function isSourcePage( Title $title ) {
 712+ static $cache = null;
 713+
 714+ $cacheObj = wfGetCache( CACHE_ANYTHING );
 715+ $cacheKey = wfMemcKey( 'pagetranslation', 'sourcepages' );
 716+
 717+ if ( $cache === null ) {
 718+ $cache = $cacheObj->get( $cacheKey );
 719+ }
 720+ if ( !is_array( $cache ) ) {
 721+ $cache = self::getTranslatablePages();
 722+ $cacheObj->set( $cacheKey, $cache, 60 * 5 );
 723+ }
 724+
 725+ return in_array( $title->getArticleId(), $cache );
 726+ }
 727+
 728+ /// List of page ids where the latest revision is either tagged or marked
 729+ public static function getTranslatablePages() {
 730+ $dbr = wfGetDB( DB_SLAVE );
 731+
 732+ $tables = array( 'revtag', 'page' );
 733+ $fields = 'rt_page';
 734+ $conds = array(
 735+ 'rt_page = page_id',
 736+ 'rt_revision = page_latest',
 737+ 'rt_type' => array( self::getTagId( 'tp:mark' ), self::getTagId( 'tp:tag' ) ),
 738+ );
 739+ $options = array( 'GROUP BY' => 'rt_page' );
 740+
 741+ $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options );
 742+ $results = array();
 743+ foreach ( $res as $row ) {
 744+ $results[] = $row->rt_page;
 745+ }
 746+
 747+ return $results;
 748+ }
703749 }
704750
705751 /**
Index: trunk/extensions/Translate/tag/PageTranslationHooks.php
@@ -373,20 +373,21 @@
374374 }
375375
376376 public static function header( Title $title ) {
377 - $page = TranslatablePage::newFromTitle( $title );
378 - $marked = $page->getMarkedTag();
379 - $ready = $page->getReadyTag();
380 -
381 - if ( $marked || $ready ) {
382 - self::sourcePageHeader( $page, $marked, $ready );
383 - } else {
 377+ if ( TranslatablePage::isSourcePage( $title ) ) {
 378+ self::sourcePageHeader( $title );
 379+ } elseif ( TranslatablePage::isTranslationPage( $title ) ) {
384380 self::translationPageHeader( $title );
385381 }
386382 }
387383
388 - protected static function sourcePageHeader( TranslatablePage $page, $marked, $ready ) {
 384+ protected static function sourcePageHeader( Title $title ) {
389385 global $wgUser, $wgLang;
390386
 387+ $page = TranslatablePage::newFromTitle( $title );
 388+
 389+ $marked = $page->getMarkedTag();
 390+ $ready = $page->getReadyTag();
 391+
391392 $title = $page->getTitle();
392393 $sk = $wgUser->getSkin();
393394

Status & tagging log