X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Ffilerepo%2FForeignAPIRepo.php;h=4176c8240ee892a588dea9ae2de44567ed6a7c96;hp=cc9099c6c05e30bc98146e4fb6226e0e3771e07c;hb=f43fa6f4f0e2cb60b8543daad661b48a3e0653a9;hpb=a5b975edd132e52ee8ce1c725d8195da941c8b7b diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index cc9099c6c0..4176c8240e 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -28,13 +28,13 @@ use MediaWiki\Logger\LoggerFactory; * * Example config: * - * $wgForeignFileRepos[] = array( + * $wgForeignFileRepos[] = [ * 'class' => 'ForeignAPIRepo', * 'name' => 'shared', * 'apibase' => 'https://en.wikipedia.org/w/api.php', * 'fetchDescription' => true, // Optional * 'descriptionCacheExpiry' => 3600, - * ); + * ]; * * @ingroup FileRepo */ @@ -63,8 +63,8 @@ class ForeignAPIRepo extends FileRepo { /** @var array */ protected $mFileExists = []; - /** @var array */ - private $mQueryCache = []; + /** @var string */ + private $mApiBase; /** * @param array|null $info @@ -397,7 +397,8 @@ class ForeignAPIRepo extends FileRepo { } /* There is a new Commons file, or existing thumbnail older than a month */ } - $thumb = self::httpGet( $foreignUrl ); + + $thumb = self::httpGet( $foreignUrl, 'default', [], $mtime ); if ( !$thumb ) { wfDebug( __METHOD__ . " Could not download thumb\n" ); @@ -413,7 +414,11 @@ class ForeignAPIRepo extends FileRepo { return $foreignUrl; } $knownThumbUrls[$sizekey] = $localUrl; - $cache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry ); + + $ttl = $mtime + ? $cache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry ) + : $this->apiThumbCacheExpiry; + $cache->set( $key, $knownThumbUrls, $ttl ); wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); return $localUrl; @@ -506,9 +511,12 @@ class ForeignAPIRepo extends FileRepo { * @param string $url * @param string $timeout * @param array $options + * @param integer|bool &$mtime Resulting Last-Modified UNIX timestamp if received * @return bool|string */ - public static function httpGet( $url, $timeout = 'default', $options = [] ) { + public static function httpGet( + $url, $timeout = 'default', $options = [], &$mtime = false + ) { $options['timeout'] = $timeout; /* Http::get */ $url = wfExpandUrl( $url, PROTO_HTTP ); @@ -524,10 +532,17 @@ class ForeignAPIRepo extends FileRepo { $status = $req->execute(); if ( $status->isOK() ) { + $lmod = $req->getResponseHeader( 'Last-Modified' ); + $mtime = $lmod ? wfTimestamp( TS_UNIX, $lmod ) : false; + return $req->getContent(); } else { $logger = LoggerFactory::getInstance( 'http' ); - $logger->warning( $status->getWikiText(), [ 'caller' => 'ForeignAPIRepo::httpGet' ] ); + $logger->warning( + $status->getWikiText( false, false, 'en' ), + [ 'caller' => 'ForeignAPIRepo::httpGet' ] + ); + return false; } } @@ -545,7 +560,7 @@ class ForeignAPIRepo extends FileRepo { * @param string $target Used in cache key creation, mostly * @param array $query The query parameters for the API request * @param int $cacheTTL Time to live for the memcached caching - * @return null + * @return string|null */ public function httpGetCached( $target, $query, $cacheTTL = 3600 ) { if ( $this->mApiBase ) { @@ -554,28 +569,23 @@ class ForeignAPIRepo extends FileRepo { $url = $this->makeUrl( $query, 'api' ); } - if ( !isset( $this->mQueryCache[$url] ) ) { - $data = ObjectCache::getMainWANInstance()->getWithSetCallback( - $this->getLocalCacheKey( get_class( $this ), $target, md5( $url ) ), - $cacheTTL, - function () use ( $url ) { - return ForeignAPIRepo::httpGet( $url ); + $cache = ObjectCache::getMainWANInstance(); + return $cache->getWithSetCallback( + $this->getLocalCacheKey( get_class( $this ), $target, md5( $url ) ), + $cacheTTL, + function ( $curValue, &$ttl ) use ( $url, $cache ) { + $html = self::httpGet( $url, 'default', [], $mtime ); + if ( $html !== false ) { + $ttl = $mtime ? $cache->adaptiveTTL( $mtime, $ttl ) : $ttl; + } else { + $ttl = $cache->adaptiveTTL( $mtime, $ttl ); + $html = null; // caches negatives } - ); - if ( !$data ) { - return null; - } - - if ( count( $this->mQueryCache ) > 100 ) { - // Keep the cache from growing infinitely - $this->mQueryCache = []; - } - - $this->mQueryCache[$url] = $data; - } - - return $this->mQueryCache[$url]; + return $html; + }, + [ 'pcTTL' => $cache::TTL_PROC_LONG ] + ); } /**