From 62b9d21ccc534fc5350b6ae18525eecb127beaad Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sat, 3 Sep 2016 05:56:12 -0700 Subject: [PATCH] Add caching to Title::loadRestrictions() Change-Id: Id9dac9b67cf969cddaaa8d412cb7fd033146f17d --- includes/Title.php | 88 +++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/includes/Title.php b/includes/Title.php index 24bad815ea..8d305b7e78 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -2848,23 +2848,6 @@ class Title implements LinkTarget { return $this->mCascadeRestriction; } - /** - * Loads a string into mRestrictions array - * - * @param ResultWrapper $res Resource restrictions as an SQL result. - * @param string $oldFashionedRestrictions Comma-separated list of page - * restrictions from page table (pre 1.10) - */ - private function loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions = null ) { - $rows = []; - - foreach ( $res as $row ) { - $rows[] = $row; - } - - $this->loadRestrictionsFromRows( $rows, $oldFashionedRestrictions ); - } - /** * Compiles list of active page restrictions from both page table (pre 1.10) * and page_restrictions table for this existing page. @@ -2948,36 +2931,53 @@ class Title implements LinkTarget { * restrictions from page table (pre 1.10) */ public function loadRestrictions( $oldFashionedRestrictions = null ) { - if ( !$this->mRestrictionsLoaded ) { - $dbr = wfGetDB( DB_REPLICA ); - if ( $this->exists() ) { - $res = $dbr->select( - 'page_restrictions', - [ 'pr_type', 'pr_expiry', 'pr_level', 'pr_cascade' ], - [ 'pr_page' => $this->getArticleID() ], - __METHOD__ - ); + if ( $this->mRestrictionsLoaded ) { + return; + } - $this->loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions ); - } else { - $title_protection = $this->getTitleProtection(); - - if ( $title_protection ) { - $now = wfTimestampNow(); - $expiry = $dbr->decodeExpiry( $title_protection['expiry'] ); - - if ( !$expiry || $expiry > $now ) { - // Apply the restrictions - $this->mRestrictionsExpiry['create'] = $expiry; - $this->mRestrictions['create'] = explode( ',', trim( $title_protection['permission'] ) ); - } else { // Get rid of the old restrictions - $this->mTitleProtection = false; - } - } else { - $this->mRestrictionsExpiry['create'] = 'infinity'; + $id = $this->getArticleID(); + if ( $id ) { + $cache = ObjectCache::getMainWANInstance(); + $rows = $cache->getWithSetCallback( + // Page protections always leave a new null revision + $cache->makeKey( 'page-restrictions', $id, $this->getLatestRevID() ), + $cache::TTL_DAY, + function ( $curValue, &$ttl, array &$setOpts ) { + $dbr = wfGetDB( DB_REPLICA ); + + $setOpts += Database::getCacheSetOptions( $dbr ); + + return iterator_to_array( + $dbr->select( + 'page_restrictions', + [ 'pr_type', 'pr_expiry', 'pr_level', 'pr_cascade' ], + [ 'pr_page' => $this->getArticleID() ], + __METHOD__ + ) + ); + } + ); + + $this->loadRestrictionsFromRows( $rows, $oldFashionedRestrictions ); + } else { + $title_protection = $this->getTitleProtection(); + + if ( $title_protection ) { + $now = wfTimestampNow(); + $expiry = wfGetDB( DB_REPLICA )->decodeExpiry( $title_protection['expiry'] ); + + if ( !$expiry || $expiry > $now ) { + // Apply the restrictions + $this->mRestrictionsExpiry['create'] = $expiry; + $this->mRestrictions['create'] = + explode( ',', trim( $title_protection['permission'] ) ); + } else { // Get rid of the old restrictions + $this->mTitleProtection = false; } - $this->mRestrictionsLoaded = true; + } else { + $this->mRestrictionsExpiry['create'] = 'infinity'; } + $this->mRestrictionsLoaded = true; } } -- 2.20.1