*
* Example config:
*
- * $wgForeignFileRepos[] = array(
+ * $wgForeignFileRepos[] = [
* 'class' => 'ForeignAPIRepo',
* 'name' => 'shared',
* 'apibase' => 'https://en.wikipedia.org/w/api.php',
* 'fetchDescription' => true, // Optional
* 'descriptionCacheExpiry' => 3600,
- * );
+ * ];
*
* @ingroup FileRepo
*/
/** @var array */
protected $mFileExists = [];
- /** @var array */
- private $mQueryCache = [];
+ /** @var string */
+ private $mApiBase;
/**
* @param array|null $info
}
/* 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" );
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;
* @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 );
$status = $req->execute();
if ( $status->isOK() ) {
+ $mtime = wfTimestampOrNull( TS_UNIX, $req->getResponseHeader( 'Last-Modified' ) );
+ $mtime = $mtime ?: 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;
}
}
* @since 1.23
*/
protected static function getIIProps() {
- return join( '|', self::$imageInfoProps );
+ return implode( '|', self::$imageInfoProps );
}
/**
* @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 ) {
$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 ]
+ );
}
/**