Reduce WikiPage::getContentModel() queries via caching
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 11 Sep 2016 13:13:20 +0000 (06:13 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Sun, 11 Sep 2016 13:13:20 +0000 (06:13 -0700)
Change-Id: I3b25be5e2b456d495cd89da9ef88cf3a2f83cbdf

includes/page/WikiPage.php

index d5dfd3d..b09ef0d 100644 (file)
@@ -488,15 +488,23 @@ class WikiPage implements Page, IDBAccessObject {
         */
        public function getContentModel() {
                if ( $this->exists() ) {
-                       // look at the revision's actual content model
-                       $rev = $this->getRevision();
-
-                       if ( $rev !== null ) {
-                               return $rev->getContentModel();
-                       } else {
-                               $title = $this->mTitle->getPrefixedDBkey();
-                               wfWarn( "Page $title exists but has no (visible) revisions!" );
-                       }
+                       $cache = ObjectCache::getMainWANInstance();
+
+                       return $cache->getWithSetCallback(
+                               $cache->makeKey( 'page', 'content-model', $this->getLatest() ),
+                               $cache::TTL_MONTH,
+                               function () {
+                                       $rev = $this->getRevision();
+                                       if ( $rev ) {
+                                               // Look at the revision's actual content model
+                                               return $rev->getContentModel();
+                                       } else {
+                                               $title = $this->mTitle->getPrefixedDBkey();
+                                               wfWarn( "Page $title exists but has no (visible) revisions!" );
+                                               return $this->mTitle->getContentModel();
+                                       }
+                               }
+                       );
                }
 
                // use the default model for this page