11c1d842eSAndreas Gohr<?php 238539ccdSAndreas Gohr 338539ccdSAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient; 438539ccdSAndreas Gohruse DOMWrap\Document; 538539ccdSAndreas Gohr 61c1d842eSAndreas Gohr/** 71c1d842eSAndreas Gohr * DokuWiki Plugin amazonlight (Syntax Component) 81c1d842eSAndreas Gohr * 91c1d842eSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 101c1d842eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 111c1d842eSAndreas Gohr */ 12*cc96b9dcSAndreas Gohrclass syntax_plugin_amazonlight extends syntax_plugin_doi_isbn 131c1d842eSAndreas Gohr{ 141c1d842eSAndreas Gohr 151c1d842eSAndreas Gohr /** @var array what regions to use for the different countries */ 161c1d842eSAndreas Gohr const REGIONS = [ 1738539ccdSAndreas Gohr 'us' => 'www.amazon.com', 1838539ccdSAndreas Gohr 'ca' => 'www.amazon.ca', 1938539ccdSAndreas Gohr 'de' => 'www.amazon.de', 2038539ccdSAndreas Gohr 'gb' => 'www.amazon.co.uk', 2138539ccdSAndreas Gohr 'fr' => 'www.amazon.fr', 2238539ccdSAndreas Gohr 'jp' => 'www.amazon.co.jp', 231c1d842eSAndreas Gohr ]; 241c1d842eSAndreas Gohr 25*cc96b9dcSAndreas Gohr public function getConf($setting, $notset = false) 261c1d842eSAndreas Gohr { 27*cc96b9dcSAndreas Gohr $config = parent::getConf($setting); 28*cc96b9dcSAndreas Gohr if(!$config) { 29*cc96b9dcSAndreas Gohr $doi = plugin_load('syntax', 'doi_isbn'); 30*cc96b9dcSAndreas Gohr $config = $doi->getConf($setting, $notset); 311c1d842eSAndreas Gohr } 32*cc96b9dcSAndreas Gohr return $config; 331c1d842eSAndreas Gohr } 341c1d842eSAndreas Gohr 351c1d842eSAndreas Gohr /** 361c1d842eSAndreas Gohr * Connect lookup pattern to lexer. 371c1d842eSAndreas Gohr * 381c1d842eSAndreas Gohr * @param string $mode Parser mode 391c1d842eSAndreas Gohr */ 401c1d842eSAndreas Gohr public function connectTo($mode) 411c1d842eSAndreas Gohr { 421c1d842eSAndreas Gohr $this->Lexer->addSpecialPattern('\{\{amazon>[\w:\\- =]+\}\}', $mode, 'plugin_amazonlight'); 431c1d842eSAndreas Gohr } 441c1d842eSAndreas Gohr 451c1d842eSAndreas Gohr /** @inheritDoc */ 461c1d842eSAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) 471c1d842eSAndreas Gohr { 481c1d842eSAndreas Gohr $match = substr($match, 9, -2); 4974b25907SAndreas Gohr list($ctry, $asin) = sexplode(':', $match, 2); 501c1d842eSAndreas Gohr 511c1d842eSAndreas Gohr // no country given? 521c1d842eSAndreas Gohr if (empty($asin)) { 531c1d842eSAndreas Gohr $asin = $ctry; 541c1d842eSAndreas Gohr $ctry = 'us'; 551c1d842eSAndreas Gohr } 561c1d842eSAndreas Gohr 571c1d842eSAndreas Gohr // default parameters... 581c1d842eSAndreas Gohr $params = array( 591c1d842eSAndreas Gohr 'imgw' => $this->getConf('imgw'), 601c1d842eSAndreas Gohr 'imgh' => $this->getConf('imgh'), 611c1d842eSAndreas Gohr ); 621c1d842eSAndreas Gohr // ...can be overridden 6374b25907SAndreas Gohr list($asin, $more) = sexplode(' ', $asin, 2); 64*cc96b9dcSAndreas Gohr $params['id'] = $asin; 651c1d842eSAndreas Gohr 661c1d842eSAndreas Gohr if (preg_match('/(\d+)x(\d+)/i', $more, $match)) { 671c1d842eSAndreas Gohr $params['imgw'] = $match[1]; 681c1d842eSAndreas Gohr $params['imgh'] = $match[2]; 691c1d842eSAndreas Gohr } 701c1d842eSAndreas Gohr 711c1d842eSAndreas Gohr // correct country given? 721c1d842eSAndreas Gohr if ($ctry === 'uk') $ctry = 'gb'; 731c1d842eSAndreas Gohr if (!preg_match('/^(us|gb|jp|de|fr|ca)$/', $ctry)) { 741c1d842eSAndreas Gohr $ctry = 'us'; 751c1d842eSAndreas Gohr } 761c1d842eSAndreas Gohr $params['country'] = $ctry; 771c1d842eSAndreas Gohr 781c1d842eSAndreas Gohr return $params; 791c1d842eSAndreas Gohr } 801c1d842eSAndreas Gohr 811c1d842eSAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) 821c1d842eSAndreas Gohr { 83*cc96b9dcSAndreas Gohr $region = self::REGIONS[$data['country']]; 84*cc96b9dcSAndreas Gohr $data['url'] = 'https://' . $region . '/dp/' . $data['id']; 85*cc96b9dcSAndreas Gohr if($this->getConf('partner_'.$data['country'])) { 86*cc96b9dcSAndreas Gohr $data['url'] .= '/?tag='.$this->getConf('partner_'.$data['country']); 871c1d842eSAndreas Gohr } 881c1d842eSAndreas Gohr 89*cc96b9dcSAndreas Gohr return parent::render($mode, $renderer, $data); 9038539ccdSAndreas Gohr } 911c1d842eSAndreas Gohr} 921c1d842eSAndreas Gohr 93