Make EditPage::getTemplates avoid page table query spam
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 15 Mar 2019 09:24:31 +0000 (02:24 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 15 Mar 2019 09:34:00 +0000 (02:34 -0700)
This changes TemplatesOnThisPageFormatter to use LinkBatch
and LinkBatch/LinkCache to manage the field.

Change-Id: I523158cdffc599d4d29bab91c98e55085130cee2

includes/TemplatesOnThisPageFormatter.php
includes/Title.php
includes/cache/LinkCache.php

index 494c7bf..bca1ef6 100644 (file)
@@ -66,11 +66,7 @@ class TemplatesOnThisPageFormatter {
                }
 
                # Do a batch existence check
-               $batch = new LinkBatch;
-               foreach ( $templates as $title ) {
-                       $batch->addObj( $title );
-               }
-               $batch->execute();
+               ( new LinkBatch( $templates ) )->execute();
 
                # Construct the HTML
                $outText = '<div class="mw-templatesUsedExplanation">';
index cf76c0d..88a7efb 100644 (file)
@@ -3255,8 +3255,9 @@ class Title implements LinkTarget, IDBAccessObject {
                }
 
                if ( $this->mOldRestrictions === false ) {
-                       $this->mOldRestrictions = $dbr->selectField( 'page', 'page_restrictions',
-                               [ 'page_id' => $this->getArticleID() ], __METHOD__ );
+                       $linkCache = MediaWikiServices::getInstance()->getLinkCache();
+                       $linkCache->addLinkObj( $this ); # in case we already had an article ID
+                       $this->mOldRestrictions = $linkCache->getGoodLinkFieldObj( $this, 'restrictions' );
                }
 
                if ( $this->mOldRestrictions != '' ) {
index b3dc004..fb416f0 100644 (file)
@@ -141,6 +141,7 @@ class LinkCache {
                        'revision' => (int)$revision,
                        'model' => $model ? (string)$model : null,
                        'lang' => $lang ? (string)$lang : null,
+                       'restrictions' => null
                ] );
        }
 
@@ -158,8 +159,15 @@ class LinkCache {
                        'length' => intval( $row->page_len ),
                        'redirect' => intval( $row->page_is_redirect ),
                        'revision' => intval( $row->page_latest ),
-                       'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
-                       'lang' => !empty( $row->page_lang ) ? strval( $row->page_lang ) : null,
+                       'model' => !empty( $row->page_content_model )
+                               ? strval( $row->page_content_model )
+                               : null,
+                       'lang' => !empty( $row->page_lang )
+                               ? strval( $row->page_lang )
+                               : null,
+                       'restrictions' => !empty( $row->page_restrictions )
+                               ? strval( $row->page_restrictions )
+                               : null
                ] );
        }
 
@@ -198,7 +206,13 @@ class LinkCache {
        public static function getSelectFields() {
                global $wgContentHandlerUseDB, $wgPageLanguageUseDB;
 
-               $fields = [ 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ];
+               $fields = [
+                       'page_id',
+                       'page_len',
+                       'page_is_redirect',
+                       'page_latest',
+                       'page_restrictions'
+               ];
                if ( $wgContentHandlerUseDB ) {
                        $fields[] = 'page_content_model';
                }