From 444d88ed4a5636195099cacfee0376fb28648b34 Mon Sep 17 00:00:00 2001 From: lens0021 Date: Sun, 10 Jul 2022 18:25:05 +0900 Subject: [PATCH] Use PageStoreRecord::REQUIRED_FIELDS for fields (#99) (cherry picked from commit 6c61256a5b5d5fa31bb28dc90676396116658e11) --- includes/HookHandlers/RelatedArticles.php | 88 ++++++++++------------- 1 file changed, 36 insertions(+), 52 deletions(-) diff --git a/includes/HookHandlers/RelatedArticles.php b/includes/HookHandlers/RelatedArticles.php index bc45bd4..3df0c9f 100644 --- a/includes/HookHandlers/RelatedArticles.php +++ b/includes/HookHandlers/RelatedArticles.php @@ -5,6 +5,7 @@ use Config; use ExtensionRegistry; use MediaWiki\MediaWikiServices; +use MediaWiki\Page\PageStoreRecord; use Title; use Wikimedia\Rdbms\DBConnRef; use Wikimedia\Rdbms\ILoadBalancer; @@ -138,19 +139,7 @@ private function makeTitlesFromHereSQL( Title $title, DBConnRef $dbr, $targetNam // Hide redirects 'rd_from' => null, ] ) - ->fields( [ - 'page_namespace', - 'page_title', - 'page_touched', - // Used by LinkCache::addGoodLinkObjFromRow() - 'page_id', - 'page_len', - 'page_is_redirect', - 'page_latest', - 'page_content_model', - 'page_lang', - 'page_restrictions', - ] ) + ->fields( self::getSelectFields() ) ->getSQL(); } @@ -184,19 +173,7 @@ private function makeRedirectedTitlesFromHereSQL( Title $title, DBConnRef $dbr, // Only redirects 'rd_from != 0', ] ) - ->fields( [ - 'page_namespace' => 'target.page_namespace', - 'page_title' => 'target.page_title', - 'page_touched' => 'target.page_touched', - // Used by LinkCache::addGoodLinkObjFromRow() - 'page_id' => 'target.page_id', - 'page_len' => 'target.page_len', - 'page_is_redirect' => 'target.page_is_redirect', - 'page_latest' => 'target.page_latest', - 'page_content_model' => 'target.page_content_model', - 'page_lang' => 'target.page_lang', - 'page_restrictions' => 'target.page_restrictions', - ] ) + ->fields( self::getSelectFields( 'target' ) ) ->getSQL(); } @@ -226,19 +203,7 @@ private function makeTitlesToHereSQL( Title $title, DBConnRef $dbr, $targetNames ->conds( $targetNamespaces ? [ 'page_namespace' => $targetNamespaces, ] : [] ) - ->fields( [ - 'page_namespace', - 'page_title', - 'page_touched', - // Used by LinkCache::addGoodLinkObjFromRow() - 'page_id', - 'page_len', - 'page_is_redirect', - 'page_latest', - 'page_content_model', - 'page_lang', - 'page_restrictions', - ] ) + ->fields( self::getSelectFields() ) ->getSQL(); } @@ -276,19 +241,38 @@ private function makeTitlesToRedirectsOfHereSQL( Title $title, DBConnRef $dbr, $ ->conds( $targetNamespaces ? [ 'redirect_from.page_namespace' => $targetNamespaces, ] : [] ) - ->fields( [ - 'page_namespace' => 'redirect_from.page_namespace', - 'page_title' => 'redirect_from.page_title', - 'page_touched' => 'redirect_from.page_touched', - // Used by LinkCache::addGoodLinkObjFromRow() - 'page_id' => 'redirect_from.page_id', - 'page_len' => 'redirect_from.page_len', - 'page_is_redirect' => 'redirect_from.page_is_redirect', - 'page_latest' => 'redirect_from.page_latest', - 'page_content_model' => 'redirect_from.page_content_model', - 'page_lang' => 'redirect_from.page_lang', - 'page_restrictions' => 'redirect_from.page_restrictions', - ] ) + ->fields( self::getSelectFields( 'redirect_from' ) ) ->getSQL(); } + + /** + * @param string|null $table + * @return array + */ + public static function getSelectFields( $table = null ) { + $pageLanguageUseDB = MediaWikiServices::getInstance()->getMainConfig()->get( 'PageLanguageUseDB' ); + + $fields = array_merge( + PageStoreRecord::REQUIRED_FIELDS, + [ + 'page_len', + 'page_restrictions', + 'page_content_model', + ] + ); + + if ( $pageLanguageUseDB ) { + $fields[] = 'page_lang'; + } + + if ( $table ) { + $fieldMap = []; + foreach ( $fields as $f ) { + $fieldMap[$f] = "$table.$f"; + } + return $fieldMap; + } + + return $fields; + } }