Quick refactor of the http logic in ForeignAPIRepo
authorMark Holmquist <mtraceur@member.fsf.org>
Tue, 8 Oct 2013 20:11:54 +0000 (13:11 -0700)
committerMarkTraceur <mtraceur@member.fsf.org>
Tue, 8 Oct 2013 21:15:29 +0000 (21:15 +0000)
Automatic caching, will enable slightly smaller patches elsewhere.

Resetting after failing at a rebase.

Change-Id: I6849107a3af773687a31155b23a2d7b4d7a60645

includes/filerepo/ForeignAPIRepo.php

index 02d83bb..eb16093 100644 (file)
@@ -159,33 +159,18 @@ class ForeignAPIRepo extends FileRepo {
                                'action' => 'query',
                                'redirects' => 'true'
                        ) );
+
                if ( !isset( $query['uselang'] ) ) { // uselang is unset or null
                        $query['uselang'] = $wgLanguageCode;
                }
-               if ( $this->mApiBase ) {
-                       $url = wfAppendQuery( $this->mApiBase, $query );
-               } else {
-                       $url = $this->makeUrl( $query, 'api' );
-               }
 
-               if ( !isset( $this->mQueryCache[$url] ) ) {
-                       $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
-                       $data = $wgMemc->get( $key );
-                       if ( !$data ) {
-                               $data = self::httpGet( $url );
-                               if ( !$data ) {
-                                       return null;
-                               }
-                               $wgMemc->set( $key, $data, 3600 );
-                       }
+               $data = $this->httpGetCached( 'Metadata', $query );
 
-                       if ( count( $this->mQueryCache ) > 100 ) {
-                               // Keep the cache from growing infinitely
-                               $this->mQueryCache = array();
-                       }
-                       $this->mQueryCache[$url] = $data;
+               if ( $data ) {
+                       return FormatJson::decode( $data, true );
+               } else {
+                       return null;
                }
-               return FormatJson::decode( $this->mQueryCache[$url], true );
        }
 
        /**
@@ -466,6 +451,46 @@ class ForeignAPIRepo extends FileRepo {
                }
        }
 
+       /**
+        * HTTP GET request to a mediawiki API (with caching)
+        * @param $target string Used in cache key creation, mostly
+        * @param $query array The query parameters for the API request
+        * @param $cacheTTL int Time to live for the memcached caching
+        */
+       public function httpGetCached( $target, $query, $cacheTTL = 3600 ) {
+               if ( $this->mApiBase ) {
+                       $url = wfAppendQuery( $this->mApiBase, $query );
+               } else {
+                       $url = $this->makeUrl( $query, 'api' );
+               }
+
+               if ( !isset( $this->mQueryCache[$url] ) ) {
+                       global $wgMemc;
+
+                       $key = $this->getLocalCacheKey( get_class( $this ), $target, md5( $url ) );
+                       $data = $wgMemc->get( $key );
+
+                       if ( !$data ) {
+                               $data = self::httpGet( $url );
+
+                               if ( !$data ) {
+                                       return null;
+                               }
+
+                               $wgMemc->set( $key, $data, $cacheTTL );
+                       }
+
+                       if ( count( $this->mQueryCache ) > 100 ) {
+                               // Keep the cache from growing infinitely
+                               $this->mQueryCache = array();
+                       }
+
+                       $this->mQueryCache[$url] = $data;
+               }
+
+               return $this->mQueryCache[$url];
+       }
+
        /**
         * @param $callback Array|string
         * @throws MWException