r58264 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58263‎ | r58264 | r58265 >
Date:17:18, 28 October 2009
Author:skizzerz
Status:deferred
Tags:
Comment:
* update AmazonPlus extension
Modified paths:
  • /trunk/extensions/AmazonPlus/AmazonPlus.php (modified) (history)

Diff [purge]

Index: trunk/extensions/AmazonPlus/AmazonPlus.php
@@ -4,7 +4,7 @@
55 * AmazonPlus extension for MediaWiki -- a highly customizable extension to display Amazon information
66 * Documentation is available at http://www.mediawiki.org/wiki/Extension:AmazonPlus
77 *
8 - * Copyright (c) 2008 Ryan Schmidt (Skizzerz)
 8+ * Copyright (c) 2008-2009 Ryan Schmidt (Skizzerz)
99 *
1010 * This program is free software; you can redistribute it and/or modify
1111 * it under the terms of the GNU General Public License as published by
@@ -22,18 +22,29 @@
2323 * http://www.gnu.org/copyleft/gpl.html
2424 */
2525
 26+if( !function_exists( 'wfIniGetBool' ) ) {
 27+ #perhaps GlobalFunctions wasn't loaded?
 28+ require_once( "$IP/includes/GlobalFunctions.php" );
 29+ if( !function_exists( 'wfIniGetBool' ) ) {
 30+ #unsupported MediaWiki version, exit early so we don't get fatals or whatnot
 31+ echo '<html><head><title>Error</title></head><body>The AmazonPlus extension does not support your version of MediaWiki.
 32+ Please either upgrade MediaWiki or uninstall the AmazonPlus extension.</body></html>';
 33+ die( 1 );
 34+ }
 35+}
 36+
2637 # make sure that everything that needs to be set/loaded is that way
2738 $err = '';
28 -if ( !wfIniGetBool( 'allow_url_fopen' ) && !extension_loaded( 'curl' ) ) {
 39+if( !wfIniGetBool( 'allow_url_fopen' ) && !extension_loaded( 'curl' ) ) {
2940 # we need allow_url_fopen or curl to be on in order for the Http::get() call to work on the amazon url
3041 $err .= "\n<li>allow_url_fopen or curl must be enabled in php.ini</li>";
3142 }
32 -if ( !extension_loaded( 'simplexml' ) ) {
 43+if( !extension_loaded( 'simplexml' ) ) {
3344 # we need the simplexml extension loaded to parse the xml string
3445 $err .= "\n<li>The SimpleXML extension for PHP must be loaded</li>";
3546 }
3647 # if there were errors found, die with the messages
37 -if ( $err ) {
 48+if( $err ) {
3849 $html = '<html><head><title>Error</title></head><body>
3950 The following errors were discovered with the AmazonPlus extension for MediaWiki:
4051 <ul>' . $err . '</ul></body></html>';
@@ -46,19 +57,25 @@
4758 'name' => 'AmazonPlus',
4859 'description' => 'A highly customizable extension to display Amazon information',
4960 'descriptionmsg' => 'amazonplus-desc',
50 - 'version' => '0.4.0',
 61+ 'version' => '0.5.2',
5162 'url' => 'http://www.mediawiki.org/wiki/Extension:AmazonPlus',
5263 'author' => 'Ryan Schmidt',
5364 );
5465
5566 $wgExtensionMessagesFiles['AmazonPlus'] = dirname( __FILE__ ) . '/AmazonPlus.i18n.php';
5667
57 -$wgHooks['ParserFirstCallInit'][] = 'efAmazonPlusSetup';
 68+if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
 69+ $wgHooks['ParserFirstCallInit'][] = 'efAmazonPlusSetup';
 70+} else {
 71+ $wgExtensionFunctions[] = 'efAmazonPlusSetup';
 72+}
 73+
5874 $wgHooks['BeforePageDisplay'][] = 'efAmazonPlusJavascript';
5975
6076 $wgAmazonPlusJSVersion = 1; # Bump the version number every time you change AmazonPlus.js
6177 $wgAmazonPlusAWS = ''; # Amazon AWS Id. Required
62 -$wgAmazonPlusAssociates = array(); # Amazon Associates IDs, per locale.Example: array( 'us' => 'myamazonid', 'fr' => 'myfrenchid' ); Required
 78+$wgAmazonPlusSecretKey = ''; # Amazon Secret Key. Required
 79+$wgAmazonPlusAssociates = array(); # Amazon Associates IDs, per locale. Example: array( 'us' => 'myamazonid', 'fr' => 'myfrenchid' ); Required
6380 $wgAmazonPlusDefaultSearch = 'Books'; # Default search type, can be Books, DVDs, etc.
6481 $wgAmazonPlusDecimal = '.'; # What to seperate the decimals with in currency. e.g. 15.43 or 12,72
6582
@@ -69,8 +86,9 @@
7087 define( 'AMAZONPLUS_USED', 3 );
7188
7289 # Set up the tag extension
73 -function efAmazonPlusSetup( &$parser ) {
74 - $parser->setHook( 'amazon', 'efAmazonPlusRender' );
 90+function efAmazonPlusSetup() {
 91+ global $wgParser;
 92+ $wgParser->setHook( 'amazon', 'efAmazonPlusRender' );
7593 wfLoadExtensionMessages( 'AmazonPlus' );
7694 return true;
7795 }
@@ -122,7 +140,6 @@
123141 var $locale, $request, $response, $xml, $input, $error, $valid, $token, $title, $shortReview;
124142
125143 function __construct( $title, $args, $input ) {
126 - global $wgAmazonPlusAWS, $wgAmazonPlusAssociates;
127144 $this->valid = array( 'us', 'gb', 'ca', 'de', 'fr', 'jp' );
128145 $this->currencies = array();
129146 $this->token = array( 'priceSelect' => rand() . '1', 'shortReview' => rand() . '2' );
@@ -139,10 +156,8 @@
140157 $this->input = $input;
141158 $this->request = array(
142159 'Service' => 'AWSECommerceService',
143 - 'AssociateTag' => $wgAmazonPlusAssociates[$this->locale],
144 - 'AWSAccessKeyId' => $wgAmazonPlusAWS,
145160 'Operation' => 'ItemLookup',
146 - 'Version' => '2008-08-19',
 161+ 'Version' => '2009-10-01',
147162 'ItemId' => $id,
148163 'ResponseGroup' => 'Large',
149164 );
@@ -159,41 +174,44 @@
160175 function getLocale() { return $this->locale; }
161176
162177 function getId( $title, $args ) {
163 - global $wgAmazonPlusAWS, $wgAmazonPlusAssociates, $wgAmazonPlusDefaultSearch;
 178+ global $wgAmazonPlusDefaultSearch;
164179 $kw = ( isset( $args['keywords'] ) ) ? $args['keywords'] : $title->getText();
165180 $si = ( isset( $args['search'] ) ) ? $args['search'] : $wgAmazonPlusDefaultSearch;
166181 $this->request = array(
167182 'Service' => 'AWSECommerceService',
168183 'Operation' => 'ItemSearch',
169184 'ResponseGroup' => 'ItemIds',
170 - 'Version' => '2008-08-19',
 185+ 'Version' => '2009-10-01',
171186 'Keywords' => $kw,
172 - 'AWSAccessKeyId' => $wgAmazonPlusAWS,
173187 'SearchIndex' => $si,
174 - 'AssociateTag' => $wgAmazonPlusAssociates[$this->locale],
175188 );
176189 $this->doRequest();
177 - if ( !$this->xml->Items->TotalResults ) {
 190+ if( !$this->xml || !$this->xml->Items->TotalResults ) {
178191 return false;
179192 }
180193 return $this->xml->Items->Item[0]->ASIN;
181194 }
182195
183196 function doRequest() {
 197+ global $wgAmazonPlusSecretKey, $wgAmazonPlusAWS, $wgAmazonPlusAssociates;
184198 $tld = $this->localeString();
185 - $str = "http://ecs.amazonaws.{$tld}/onca/xml";
 199+ $urlstr = "http://ecs.amazonaws.{$tld}/onca/xml";
186200 $i = false;
187 - foreach ( $this->request as $key => $value ) {
188 - if ( $i ) {
189 - $str .= '&';
190 - } else {
191 - $str .= '?';
192 - $i = true;
193 - }
194 - $str .= wfUrlencode( $key ) . '=' . wfUrlencode( $value );
 201+ // generate the Signature
 202+ $this->request['Timestamp'] = wfTimestamp( TS_ISO_8601, time() );
 203+ $this->request['AWSAccessKeyId'] = $wgAmazonPlusAWS;
 204+ $this->request['AssociateTag'] = $wgAmazonPlusAssociates[$this->locale];
 205+ ksort( $this->request );
 206+ $prestr = "GET\necs.amazonaws.{$tld}\n/onca/xml\n";
 207+ $str = '';
 208+ foreach( $this->request as $key => $value ) {
 209+ $str .= $this->encode( $key ) . '=' . $this->encode( $value ) . '&';
195210 }
196 -
197 - $this->response = Http::get( $str );
 211+ $str = rtrim( $str, '&' );
 212+ $signature = $this->encode( base64_encode( hash_hmac( 'sha256', $prestr . $str, $wgAmazonPlusSecretKey, true ) ) );
 213+ $urlstr .= '?' . $str . '&Signature=' . $signature;
 214+ // do the query
 215+ $this->response = Http::get( $urlstr );
198216 if ( !$this->response ) {
199217 return 'amazonplus-fgcerr';
200218 }
@@ -208,6 +226,10 @@
209227 }
210228 return true;
211229 }
 230+
 231+ function encode( $str ) {
 232+ return str_ireplace( '%7E', '~', rawurlencode( $str ) );
 233+ }
212234
213235 function getResult() {
214236 $item = $this->xml->Items->Item;
@@ -280,6 +302,7 @@
281303 'publishedyear' => $this->parseDate( $attr->PublicationDate, 0 ),
282304 'publishedmonth' => $this->parseDate( $attr->PublicationDate, 1 ),
283305 'publishedday' => $this->parseDate( $attr->PublicationDate, 2 ),
 306+ 'publisher' => $attr->Publisher,
284307 'detailslink' => $links['Technical Details'],
285308 /* IDENTIFICATION */
286309 'isbn' => $attr->ISBN,

Status & tagging log