Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/308326

Change subject: Add caching to Title::loadRestrictions()
......................................................................

Add caching to Title::loadRestrictions()

Change-Id: Id9dac9b67cf969cddaaa8d412cb7fd033146f17d
---
M includes/Title.php
1 file changed, 41 insertions(+), 42 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/26/308326/1

diff --git a/includes/Title.php b/includes/Title.php
index 2021e0a..0674c38 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -2849,23 +2849,6 @@
        }
 
        /**
-        * 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.
         * Public for usage by LiquidThreads.
@@ -2948,36 +2931,52 @@
         *   restrictions from page table (pre 1.10)
         */
        public function loadRestrictions( $oldFashionedRestrictions = null ) {
-               if ( !$this->mRestrictionsLoaded ) {
-                       $dbr = wfGetDB( DB_SLAVE );
-                       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();
+               $dbr = wfGetDB( DB_SLAVE );
 
-                               if ( $title_protection ) {
-                                       $now = wfTimestampNow();
-                                       $expiry = $dbr->decodeExpiry( 
$title_protection['expiry'] );
+               $id = $this->getArticleID();
+               if ( $id ) {
+                       $cache = ObjectCache::getMainWANInstance();
+                       $rows = $cache->getWithSetCallback(
+                               $cache->makeKey( 'page-restrictions', $id, 
$this->getTouched() ),
+                               $cache::TTL_DAY,
+                               function ( $curValue, &$ttl, array &$setOpts ) 
use ( $dbr ) {
+                                       $setOpts += 
Database::getCacheSetOptions( $dbr );
 
-                                       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';
+                                       return iterator_to_array(
+                                               $dbr->select(
+                                                       'page_restrictions',
+                                                       [ 'pr_type', 
'pr_expiry', 'pr_level', 'pr_cascade' ],
+                                                       [ 'pr_page' => 
$this->getArticleID() ],
+                                                       __METHOD__
+                                               )
+                                       );
                                }
-                               $this->mRestrictionsLoaded = true;
+                       );
+
+                       $this->loadRestrictionsFromRows( $rows, 
$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';
                        }
+                       $this->mRestrictionsLoaded = true;
                }
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/308326
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id9dac9b67cf969cddaaa8d412cb7fd033146f17d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to