Index: trunk/extensions/AmazonPlus/AmazonPlus.php |
— | — | @@ -4,7 +4,7 @@ |
5 | 5 | * AmazonPlus extension for MediaWiki -- a highly customizable extension to display Amazon information |
6 | 6 | * Documentation is available at http://www.mediawiki.org/wiki/Extension:AmazonPlus |
7 | 7 | * |
8 | | - * Copyright (c) 2008 Ryan Schmidt (Skizzerz) |
| 8 | + * Copyright (c) 2008-2009 Ryan Schmidt (Skizzerz) |
9 | 9 | * |
10 | 10 | * This program is free software; you can redistribute it and/or modify |
11 | 11 | * it under the terms of the GNU General Public License as published by |
— | — | @@ -22,18 +22,29 @@ |
23 | 23 | * http://www.gnu.org/copyleft/gpl.html |
24 | 24 | */ |
25 | 25 | |
| 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 | + |
26 | 37 | # make sure that everything that needs to be set/loaded is that way |
27 | 38 | $err = ''; |
28 | | -if ( !wfIniGetBool( 'allow_url_fopen' ) && !extension_loaded( 'curl' ) ) { |
| 39 | +if( !wfIniGetBool( 'allow_url_fopen' ) && !extension_loaded( 'curl' ) ) { |
29 | 40 | # we need allow_url_fopen or curl to be on in order for the Http::get() call to work on the amazon url |
30 | 41 | $err .= "\n<li>allow_url_fopen or curl must be enabled in php.ini</li>"; |
31 | 42 | } |
32 | | -if ( !extension_loaded( 'simplexml' ) ) { |
| 43 | +if( !extension_loaded( 'simplexml' ) ) { |
33 | 44 | # we need the simplexml extension loaded to parse the xml string |
34 | 45 | $err .= "\n<li>The SimpleXML extension for PHP must be loaded</li>"; |
35 | 46 | } |
36 | 47 | # if there were errors found, die with the messages |
37 | | -if ( $err ) { |
| 48 | +if( $err ) { |
38 | 49 | $html = '<html><head><title>Error</title></head><body> |
39 | 50 | The following errors were discovered with the AmazonPlus extension for MediaWiki: |
40 | 51 | <ul>' . $err . '</ul></body></html>'; |
— | — | @@ -46,19 +57,25 @@ |
47 | 58 | 'name' => 'AmazonPlus', |
48 | 59 | 'description' => 'A highly customizable extension to display Amazon information', |
49 | 60 | 'descriptionmsg' => 'amazonplus-desc', |
50 | | - 'version' => '0.4.0', |
| 61 | + 'version' => '0.5.2', |
51 | 62 | 'url' => 'http://www.mediawiki.org/wiki/Extension:AmazonPlus', |
52 | 63 | 'author' => 'Ryan Schmidt', |
53 | 64 | ); |
54 | 65 | |
55 | 66 | $wgExtensionMessagesFiles['AmazonPlus'] = dirname( __FILE__ ) . '/AmazonPlus.i18n.php'; |
56 | 67 | |
57 | | -$wgHooks['ParserFirstCallInit'][] = 'efAmazonPlusSetup'; |
| 68 | +if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) { |
| 69 | + $wgHooks['ParserFirstCallInit'][] = 'efAmazonPlusSetup'; |
| 70 | +} else { |
| 71 | + $wgExtensionFunctions[] = 'efAmazonPlusSetup'; |
| 72 | +} |
| 73 | + |
58 | 74 | $wgHooks['BeforePageDisplay'][] = 'efAmazonPlusJavascript'; |
59 | 75 | |
60 | 76 | $wgAmazonPlusJSVersion = 1; # Bump the version number every time you change AmazonPlus.js |
61 | 77 | $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 |
63 | 80 | $wgAmazonPlusDefaultSearch = 'Books'; # Default search type, can be Books, DVDs, etc. |
64 | 81 | $wgAmazonPlusDecimal = '.'; # What to seperate the decimals with in currency. e.g. 15.43 or 12,72 |
65 | 82 | |
— | — | @@ -69,8 +86,9 @@ |
70 | 87 | define( 'AMAZONPLUS_USED', 3 ); |
71 | 88 | |
72 | 89 | # 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' ); |
75 | 93 | wfLoadExtensionMessages( 'AmazonPlus' ); |
76 | 94 | return true; |
77 | 95 | } |
— | — | @@ -122,7 +140,6 @@ |
123 | 141 | var $locale, $request, $response, $xml, $input, $error, $valid, $token, $title, $shortReview; |
124 | 142 | |
125 | 143 | function __construct( $title, $args, $input ) { |
126 | | - global $wgAmazonPlusAWS, $wgAmazonPlusAssociates; |
127 | 144 | $this->valid = array( 'us', 'gb', 'ca', 'de', 'fr', 'jp' ); |
128 | 145 | $this->currencies = array(); |
129 | 146 | $this->token = array( 'priceSelect' => rand() . '1', 'shortReview' => rand() . '2' ); |
— | — | @@ -139,10 +156,8 @@ |
140 | 157 | $this->input = $input; |
141 | 158 | $this->request = array( |
142 | 159 | 'Service' => 'AWSECommerceService', |
143 | | - 'AssociateTag' => $wgAmazonPlusAssociates[$this->locale], |
144 | | - 'AWSAccessKeyId' => $wgAmazonPlusAWS, |
145 | 160 | 'Operation' => 'ItemLookup', |
146 | | - 'Version' => '2008-08-19', |
| 161 | + 'Version' => '2009-10-01', |
147 | 162 | 'ItemId' => $id, |
148 | 163 | 'ResponseGroup' => 'Large', |
149 | 164 | ); |
— | — | @@ -159,41 +174,44 @@ |
160 | 175 | function getLocale() { return $this->locale; } |
161 | 176 | |
162 | 177 | function getId( $title, $args ) { |
163 | | - global $wgAmazonPlusAWS, $wgAmazonPlusAssociates, $wgAmazonPlusDefaultSearch; |
| 178 | + global $wgAmazonPlusDefaultSearch; |
164 | 179 | $kw = ( isset( $args['keywords'] ) ) ? $args['keywords'] : $title->getText(); |
165 | 180 | $si = ( isset( $args['search'] ) ) ? $args['search'] : $wgAmazonPlusDefaultSearch; |
166 | 181 | $this->request = array( |
167 | 182 | 'Service' => 'AWSECommerceService', |
168 | 183 | 'Operation' => 'ItemSearch', |
169 | 184 | 'ResponseGroup' => 'ItemIds', |
170 | | - 'Version' => '2008-08-19', |
| 185 | + 'Version' => '2009-10-01', |
171 | 186 | 'Keywords' => $kw, |
172 | | - 'AWSAccessKeyId' => $wgAmazonPlusAWS, |
173 | 187 | 'SearchIndex' => $si, |
174 | | - 'AssociateTag' => $wgAmazonPlusAssociates[$this->locale], |
175 | 188 | ); |
176 | 189 | $this->doRequest(); |
177 | | - if ( !$this->xml->Items->TotalResults ) { |
| 190 | + if( !$this->xml || !$this->xml->Items->TotalResults ) { |
178 | 191 | return false; |
179 | 192 | } |
180 | 193 | return $this->xml->Items->Item[0]->ASIN; |
181 | 194 | } |
182 | 195 | |
183 | 196 | function doRequest() { |
| 197 | + global $wgAmazonPlusSecretKey, $wgAmazonPlusAWS, $wgAmazonPlusAssociates; |
184 | 198 | $tld = $this->localeString(); |
185 | | - $str = "http://ecs.amazonaws.{$tld}/onca/xml"; |
| 199 | + $urlstr = "http://ecs.amazonaws.{$tld}/onca/xml"; |
186 | 200 | $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 ) . '&'; |
195 | 210 | } |
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 ); |
198 | 216 | if ( !$this->response ) { |
199 | 217 | return 'amazonplus-fgcerr'; |
200 | 218 | } |
— | — | @@ -208,6 +226,10 @@ |
209 | 227 | } |
210 | 228 | return true; |
211 | 229 | } |
| 230 | + |
| 231 | + function encode( $str ) { |
| 232 | + return str_ireplace( '%7E', '~', rawurlencode( $str ) ); |
| 233 | + } |
212 | 234 | |
213 | 235 | function getResult() { |
214 | 236 | $item = $this->xml->Items->Item; |
— | — | @@ -280,6 +302,7 @@ |
281 | 303 | 'publishedyear' => $this->parseDate( $attr->PublicationDate, 0 ), |
282 | 304 | 'publishedmonth' => $this->parseDate( $attr->PublicationDate, 1 ), |
283 | 305 | 'publishedday' => $this->parseDate( $attr->PublicationDate, 2 ), |
| 306 | + 'publisher' => $attr->Publisher, |
284 | 307 | 'detailslink' => $links['Technical Details'], |
285 | 308 | /* IDENTIFICATION */ |
286 | 309 | 'isbn' => $attr->ISBN, |