Samwilson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/378506 )

Change subject: [WiP] Remove dependency on phpflickr
......................................................................

[WiP] Remove dependency on phpflickr

This moves the small parts of phpflickr that this extension was
using, into a new Flickr class. This is because that library is
now rather out of date, and was throwing lots of errors from parts
of it that have nothing to do with the simple usage here.

Change-Id: I63352c905c2491200e62c1f6810f0604bb7e8baf
---
M FlickrAPI.hooks.php
D FlickrAPICache.php
M composer.json
M extension.json
A includes/Flickr.php
5 files changed, 143 insertions(+), 83 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/FlickrAPI 
refs/changes/06/378506/1

diff --git a/FlickrAPI.hooks.php b/FlickrAPI.hooks.php
index c2f7332..694dccd 100644
--- a/FlickrAPI.hooks.php
+++ b/FlickrAPI.hooks.php
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\Extension\FlickrApi\Flickr;
+
 /**
  * Hooks for FlickrAPI extension
  *
@@ -137,14 +139,14 @@
         * @throws MWException
         */
        private static function getOutput( $optionsString, Parser $parser ) {
-               global $wgFlickrAPIKey, $wgFlickrAPISecret, $wgUseFileCache, 
$wgFileCacheDirectory;
-
-               wfProfileIn( __METHOD__ );
+               $config = RequestContext::getMain()->getConfig();
+               $apiKey = $config->get( 'FlickrAPIKey' );
+               $apiSecret = $config->get( 'FlickrAPISecret' );
 
                $options = self::extractOptions( $optionsString );
 
                /** @todo i18n these errors? */
-               if ( $wgFlickrAPIKey == '' ) {
+               if ( $apiKey == '' ) {
                        throw new MWException(
                        'Flickr Error ( No API key ): You must set 
$wgFlickrAPIKey!' );
                }
@@ -155,18 +157,10 @@
                        throw new MWException( 'Flickr Error ( Not a valid ID 
): PhotoID not numeric' );
                }
 
-               $phpFlickr = new phpFlickr( $wgFlickrAPIKey, $wgFlickrAPISecret 
);
+               $flickr = new Flickr( $apiKey, $apiSecret );
 
-               // Decide which cache to use
-               if ( $wgUseFileCache ) {
-                       $phpFlickr->enableCache( 'fs', $wgFileCacheDirectory );
-               } else {
-                       $phpFlickr->enableCache( 'custom',
-                               [ 'FlickrAPICache::getCache', 
'FlickrAPICache::setCache' ] );
-               }
-
-               $info = $phpFlickr->photos_getInfo( $options['id'] );
-               $flickrSizes = $phpFlickr->photos_getSizes( $options['id'] );
+               $info = $flickr->photosGetInfo( $options['id'] );
+               $flickrSizes = $flickr->photosGetSizes( $options['id'] );
                if ( !$info || !$flickrSizes ) {
                        throw new MWException( 'Flickr Error ( Photo not found 
): PhotoID ' . $options['id'] );
                }
@@ -191,7 +185,7 @@
 
                $validSizes = self::getValidSizes();
                $handlerParams = [];
-               foreach ( $flickrSizes as $flickrSize ) {
+               foreach ( $flickrSizes['size'] as $flickrSize ) {
                        if ( $flickrSize['label'] === 
$validSizes[$options['size']] ) {
                                $handlerParams['width'] = $flickrSize['width'];
                                $url = $flickrSize['source'];
@@ -206,7 +200,6 @@
 
                $imageLink = FlickrAPIUtils::makeImageLink( $parser, $url, 
$frameParams, $handlerParams );
 
-               wfProfileOut( __METHOD__ );
                return Html::rawElement( 'div', [ 'class' => 'flickrapi' ], 
$imageLink );
        }
 }
diff --git a/FlickrAPICache.php b/FlickrAPICache.php
deleted file mode 100644
index d376edd..0000000
--- a/FlickrAPICache.php
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-
-/**
- * Custom db cache for Flickr API calls
- *
- * @author Ike Hecht
- */
-class FlickrAPICache {
-
-       /**
-        * Get this call from db cache
-        *
-        * @param string $reqhash The cache key.
-        * @return string|bool
-        */
-       public static function getCache( $reqhash ) {
-               $cache = wfGetCache( CACHE_ANYTHING );
-               $key = wfMemcKey( 'flickrapi', $reqhash );
-               $cached = $cache->get( $key );
-               wfDebugLog( "FlickrAPI", __METHOD__ . ": got " . var_export( 
$cached, true ) .
-                       " from cache." );
-               return $cached;
-       }
-
-       /**
-        * Store this call in cache.
-        *
-        * @param string $reqhash The cache key.
-        * @param string $response The response to cache.
-        * @param int $cache_expire Either an interval in seconds or a unix 
timestamp for expiry.
-        * @return bool
-        */
-       public static function setCache( $reqhash, $response, $cache_expire ) {
-               $cache = wfGetCache( CACHE_ANYTHING );
-               $key = wfMemcKey( 'flickrapi', $reqhash );
-               wfDebugLog( "FlickrAPI",
-                       __METHOD__ . ": caching " . var_export( $response, true 
) .
-                       " from Flickr." );
-               return $cache->set( $key, $response, $cache_expire );
-       }
-}
diff --git a/composer.json b/composer.json
index 32c9312..9dc2960 100755
--- a/composer.json
+++ b/composer.json
@@ -1,7 +1,4 @@
 {
-       "require": {
-               "dan-coulter/phpflickr": "dev-master"
-       },
        "require-dev": {
                "jakub-onderka/php-parallel-lint": "0.9.2",
                "mediawiki/mediawiki-codesniffer": "0.12.0",
@@ -15,24 +12,5 @@
                "fix": [
                        "phpcbf"
                ]
-       },
-       "repositories": [
-               {
-                       "type": "package",
-                       "package": {
-                               "name": "dan-coulter/phpflickr",
-                               "version": "dev-master",
-                               "source": {
-                                       "url": 
"https://github.com/dan-coulter/phpflickr.git";,
-                                       "type": "git",
-                                       "reference": 
"bc4f2092b15d347e3d40c19fe0dbff8759fc8e51"
-                               },
-                               "autoload": {
-                                       "classmap": [
-                                               "phpFlickr.php"
-                                       ]
-                               }
-                       }
-               }
-       ]
+       }
 }
diff --git a/extension.json b/extension.json
index 5ca6384..262e8e0 100755
--- a/extension.json
+++ b/extension.json
@@ -16,7 +16,8 @@
        "AutoloadClasses": {
                "FlickrAPIHooks": "FlickrAPI.hooks.php",
                "FlickrAPIUtils": "FlickrAPIUtils.php",
-               "FlickrAPICache": "FlickrAPICache.php"
+               "FlickrAPICache": "FlickrAPICache.php",
+               "MediaWiki\\Extension\\FlickrApi\\Flickr": "includes/Flickr.php"
        },
        "Hooks": {
                "ParserFirstCallInit": "FlickrAPIHooks::onParserFirstCallInit"
@@ -30,6 +31,5 @@
                        "size": "-"
                }
        },
-       "load_composer_autoloader": true,
        "manifest_version": 1
 }
diff --git a/includes/Flickr.php b/includes/Flickr.php
new file mode 100644
index 0000000..e915b79
--- /dev/null
+++ b/includes/Flickr.php
@@ -0,0 +1,130 @@
+<?php
+
+namespace MediaWiki\Extension\FlickrApi;
+
+use MediaWiki\MediaWikiServices;
+use MultiHttpClient;
+use MWException;
+use stdClass;
+
+class Flickr {
+
+       /** @var string */
+       protected $restEndpoint = 'https://api.flickr.com/services/rest/';
+
+       /** @var string */
+       protected $uploadEndpoint = 'https://up.flickr.com/services/upload/';
+
+       /** @var string */
+       protected $replaceEndpoint = 'https://up.flickr.com/services/replace/';
+
+       /** @var string */
+       protected $apiKey;
+
+       /** @var string */
+       protected $apiSecret;
+
+       /**
+        * Flickr constructor.
+        * @param string $apiKey The wiki application's API key.
+        * @param string $apiSecret The wiki application's API secret.
+        */
+       public function __construct( $apiKey, $apiSecret ) {
+               $this->apiKey = $apiKey;
+               $this->apiSecret = $apiSecret;
+       }
+
+       /**
+        * Get information about a photo.
+        * @link https://www.flickr.com/services/api/flickr.photos.getInfo.html
+        * @param int $photoId The ID of the photo to get information for.
+        * @param string $secret The secret for the photo. If the correct 
secret is passed then
+        * permissions checking is skipped. This enables the 'sharing' of 
individual photos by passing
+        * around the id and secret.
+        * @return stdClass
+        */
+       public function photosGetInfo( $photoId, $secret = null ) {
+               $info = $this->call( 'flickr.photos.getInfo', [
+                       'photo_id' => $photoId,
+                       'secret' => $secret,
+               ] );
+               return $info;
+       }
+
+       /**
+        * Returns the available sizes for a photo.
+        * @link https://www.flickr.com/services/api/flickr.photos.getSizes.html
+        * @param int $photoId The ID of the photo to fetch size information 
for.
+        * @return stdClass
+        */
+       public function photosGetSizes( $photoId ) {
+               $sizes = $this->request( 'flickr.photos.getSizes', [ 'photo_id' 
=> $photoId ] );
+               return $sizes['sizes'];
+       }
+
+       /**
+        * Call the API (returning a cached value if possible).
+        * @param string $method The Flickr API method.
+        * @param string[] $arguments The method's arguments.
+        * @return string[]
+        */
+       protected function call( $method, $arguments ) {
+               // Clean up the inputs.
+               foreach ( $arguments as $key => $value ) {
+                       if ( is_null( $value ) ) {
+                               unset( $arguments[$key] );
+                       }
+               }
+
+               // Set up the cache.
+               $cache = 
MediaWikiServices::getInstance()->getMainWANObjectCache();
+               $cacheKey = $cache->makeKey( $method, serialize( $arguments ) );
+
+               // Check the cache.
+               $cachedResponse = $cache->get( $cacheKey );
+               if ( is_array( $cachedResponse ) ) {
+                       return $cachedResponse;
+               }
+
+               // Run the request and cache the response.
+               $response = $this->request( $method, $arguments );
+               $cache->set( $cacheKey, $response, $cache::TTL_MINUTE * 10 );
+               return $response;
+       }
+
+       /**
+        * Send an API request to Flickr.
+        * @param string $method The Flickr API method.
+        * @param string[] $arguments The method's arguments.
+        * @return string[]
+        * @throws MWException
+        */
+       protected function request( $method, $arguments = [] ) {
+               $arguments = array_merge( [
+                       "method" => $method,
+                       "format" => "json",
+                       "nojsoncallback" => "1",
+                       "api_key" => $this->apiKey,
+               ], $arguments );
+               ksort( $arguments );
+               $auth_sig = "";
+               foreach ( $arguments as $key => $data ) {
+                       if ( is_null( $data ) ) {
+                               unset( $arguments[$key] );
+                               continue;
+                       }
+                       $auth_sig .= $key . $data;
+               }
+               $client = new MultiHttpClient( [] );
+               $response = $client->run( [
+                       'method' => 'POST',
+                       'url' => $this->restEndpoint,
+                       'body' => $arguments,
+               ] );
+               $body = json_decode( $response['body'], true );
+               if ( $body['stat'] !== 'ok' ) {
+                       throw new MWException( $body['message'], $body['code'] 
);
+               }
+               return $body;
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I63352c905c2491200e62c1f6810f0604bb7e8baf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/FlickrAPI
Gerrit-Branch: master
Gerrit-Owner: Samwilson <s...@samwilson.id.au>

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

Reply via email to