vendor/pimcore/pimcore/lib/Google/Cse.php line 442

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Google;
  15. use Google\Service\CustomSearchAPI;
  16. use Google\Service\CustomSearchAPI\Result;
  17. use Google\Service\CustomSearchAPI\Search;
  18. use Pimcore\Cache;
  19. use Pimcore\Google\Cse\Item;
  20. use Pimcore\Localization\LocaleServiceInterface;
  21. use Pimcore\Model;
  22. use Pimcore\Model\Paginator\PaginateListingInterface;
  23. class Cse implements PaginateListingInterface
  24. {
  25.     /**
  26.      * @param string $query
  27.      * @param int $offset
  28.      * @param int $perPage
  29.      * @param array $config
  30.      * @param string|null $facet
  31.      *
  32.      * @return Cse
  33.      */
  34.     public static function search($query$offset 0$perPage 10, array $config = [], $facet null)
  35.     {
  36.         $list = new self();
  37.         $list->setConfig($config);
  38.         $list->setOffset($offset);
  39.         $list->setPerPage($perPage);
  40.         $list->setQuery($query);
  41.         if (!empty($facet)) {
  42.             $list->setQuery($list->getQuery() . ' more:' $facet);
  43.         }
  44.         return $list;
  45.     }
  46.     public function load()
  47.     {
  48.         $client Api::getSimpleClient();
  49.         $config $this->getConfig();
  50.         $perPage $this->getPerPage();
  51.         $offset $this->getOffset();
  52.         $query $this->getQuery();
  53.         if ($client) {
  54.             $search = new CustomSearchAPI($client);
  55.             // determine language
  56.             $language \Pimcore::getContainer()->get(LocaleServiceInterface::class)->findLocale();
  57.             if ($position strpos($language'_')) {
  58.                 $language substr($language0$position);
  59.             }
  60.             if (!array_key_exists('hl'$config) && !empty($language)) {
  61.                 $config['hl'] = $language;
  62.             }
  63.             if (!array_key_exists('lr'$config) && !empty($language)) {
  64.                 $config['lr'] = 'lang_' $language;
  65.             }
  66.             if ($query) {
  67.                 if ($offset) {
  68.                     $config['start'] = $offset 1;
  69.                 }
  70.                 if (empty($perPage)) {
  71.                     $perPage 10;
  72.                 }
  73.                 $config['num'] = $perPage;
  74.                 $config['q'] = $query;
  75.                 $cacheKey 'google_cse_' md5($query serialize($config));
  76.                 // this is just a protection so that no query get's sent twice in a request (loops, ...)
  77.                 if (\Pimcore\Cache\Runtime::isRegistered($cacheKey)) {
  78.                     $result \Pimcore\Cache\Runtime::get($cacheKey);
  79.                 } else {
  80.                     if (!$result Cache::load($cacheKey)) {
  81.                         $result $search->cse->listCse($config);
  82.                         Cache::save($result$cacheKey, ['google_cse'], 3600999);
  83.                         \Pimcore\Cache\Runtime::set($cacheKey$result);
  84.                     }
  85.                 }
  86.                 $this->readGoogleResponse($result);
  87.                 return $this->getResults(false);
  88.             }
  89.             return [];
  90.         } else {
  91.             throw new \Exception('Google Simple API Key is not configured in System-Settings.');
  92.         }
  93.     }
  94.     /**
  95.      * @var Item[]
  96.      */
  97.     public $results = [];
  98.     /**
  99.      * @var int
  100.      */
  101.     public $total 0;
  102.     /**
  103.      * @var int
  104.      */
  105.     public $offset 0;
  106.     /**
  107.      * @var int
  108.      */
  109.     public $perPage 10;
  110.     /**
  111.      * @var array
  112.      */
  113.     public $config = [];
  114.     /**
  115.      * @var string
  116.      */
  117.     public $query '';
  118.     /**
  119.      * @var array
  120.      */
  121.     public $raw = [];
  122.     /**
  123.      * @var array
  124.      */
  125.     public $facets = [];
  126.     /**
  127.      * @param null|mixed $googleResponse
  128.      */
  129.     public function __construct($googleResponse null)
  130.     {
  131.         if ($googleResponse) {
  132.             $this->readGoogleResponse($googleResponse);
  133.         }
  134.     }
  135.     /**
  136.      * @param Search $googleResponse
  137.      */
  138.     public function readGoogleResponse(Search $googleResponse)
  139.     {
  140.         $items = [];
  141.         $this->setRaw($googleResponse);
  142.         // set search results
  143.         $total = (int)$googleResponse->getSearchInformation()->getTotalResults();
  144.         if ($total 100) {
  145.             $total 100;
  146.         }
  147.         $this->setTotal($total);
  148.         /** @var Result[] $results */
  149.         $results $googleResponse->getItems();
  150.         if (is_array($results)) {
  151.             foreach ($results as $item) {
  152.                 $pimcoreResultItem = new Item($item);
  153.                 // check for relation to document or asset
  154.                 // first check for an image
  155.                 $pagemap $item->getPagemap();
  156.                 if (is_array($pagemap)) {
  157.                     if (array_key_exists('cse_image'$item['pagemap']) && is_array($item['pagemap']['cse_image'])) {
  158.                         if ($item['pagemap']['cse_image'][0]) {
  159.                             // try to get the asset id
  160.                             $id false;
  161.                             $regexes = [
  162.                                 '/image-thumb__([0-9]+)__/',
  163.                                 '/([0-9]+)\/thumb__/',
  164.                                 '/thumb_([0-9]+)__/',
  165.                             ];
  166.                             foreach ($regexes as $regex) {
  167.                                 if (preg_match($regex$item['pagemap']['cse_image'][0]['src'], $matches)) {
  168.                                     if ($id $matches[1]) {
  169.                                         break;
  170.                                     }
  171.                                 }
  172.                             }
  173.                             if ($id) {
  174.                                 if ($image Model\Asset::getById($id)) {
  175.                                     if ($image instanceof Model\Asset\Image) {
  176.                                         $pimcoreResultItem->setImage($image);
  177.                                     }
  178.                                 }
  179.                             }
  180.                             $pimcoreResultItem->setImage($item['pagemap']['cse_image'][0]['src']);
  181.                         }
  182.                     }
  183.                 }
  184.                 // now a document
  185.                 $urlParts parse_url($item->getLink());
  186.                 if ($document Model\Document::getByPath($urlParts['path'])) {
  187.                     $pimcoreResultItem->setDocument($document);
  188.                 }
  189.                 $pimcoreResultItem->setType('searchresult');
  190.                 $items[] = $pimcoreResultItem;
  191.             }
  192.         }
  193.         $this->setResults($items);
  194.     }
  195.     /**
  196.      * @param int $offset
  197.      *
  198.      * @return $this
  199.      */
  200.     public function setOffset($offset)
  201.     {
  202.         $this->offset $offset;
  203.         return $this;
  204.     }
  205.     /**
  206.      * @return int
  207.      */
  208.     public function getOffset()
  209.     {
  210.         return $this->offset;
  211.     }
  212.     /**
  213.      * @param array $raw
  214.      *
  215.      * @return $this
  216.      */
  217.     public function setRaw($raw)
  218.     {
  219.         $this->raw $raw;
  220.         return $this;
  221.     }
  222.     /**
  223.      * @return array
  224.      */
  225.     public function getRaw()
  226.     {
  227.         return $this->raw;
  228.     }
  229.     /**
  230.      * @param int $total
  231.      *
  232.      * @return $this
  233.      */
  234.     public function setTotal($total)
  235.     {
  236.         $this->total $total;
  237.         return $this;
  238.     }
  239.     /**
  240.      * @return int
  241.      */
  242.     public function getTotal()
  243.     {
  244.         return $this->total;
  245.     }
  246.     /**
  247.      * @param int $perPage
  248.      *
  249.      * @return $this
  250.      */
  251.     public function setPerPage($perPage)
  252.     {
  253.         $this->perPage $perPage;
  254.         return $this;
  255.     }
  256.     /**
  257.      * @return int
  258.      */
  259.     public function getPerPage()
  260.     {
  261.         return $this->perPage;
  262.     }
  263.     /**
  264.      * @param array $config
  265.      *
  266.      * @return $this
  267.      */
  268.     public function setConfig($config)
  269.     {
  270.         $this->config $config;
  271.         return $this;
  272.     }
  273.     /**
  274.      * @return array
  275.      */
  276.     public function getConfig()
  277.     {
  278.         return $this->config;
  279.     }
  280.     /**
  281.      * @param string $query
  282.      *
  283.      * @return $this
  284.      */
  285.     public function setQuery($query)
  286.     {
  287.         $this->query $query;
  288.         return $this;
  289.     }
  290.     /**
  291.      * @return string
  292.      */
  293.     public function getQuery()
  294.     {
  295.         return $this->query;
  296.     }
  297.     /**
  298.      * @param Item[] $results
  299.      *
  300.      * @return $this
  301.      */
  302.     public function setResults($results)
  303.     {
  304.         $this->results $results;
  305.         return $this;
  306.     }
  307.     /**
  308.      * @param bool $retry
  309.      *
  310.      * @return Item[]
  311.      */
  312.     public function getResults($retry true)
  313.     {
  314.         if (empty($this->results) && $retry) {
  315.             $this->load();
  316.         }
  317.         return $this->results;
  318.     }
  319.     /**
  320.      * @param array $facets
  321.      *
  322.      * @return $this
  323.      */
  324.     public function setFacets($facets)
  325.     {
  326.         $this->facets $facets;
  327.         return $this;
  328.     }
  329.     /**
  330.      * @return array
  331.      */
  332.     public function getFacets()
  333.     {
  334.         return $this->facets;
  335.     }
  336.     /**
  337.      * Methods for PaginateListingInterface
  338.      */
  339.     /**
  340.      * @return int
  341.      */
  342.     public function count()
  343.     {
  344.         $this->getResults();
  345.         return $this->getTotal();
  346.     }
  347.     /**
  348.      * @param int $offset
  349.      * @param int $itemCountPerPage
  350.      *
  351.      * @return array
  352.      */
  353.     public function getItems($offset$itemCountPerPage)
  354.     {
  355.         $this->setOffset($offset);
  356.         $this->setPerPage($itemCountPerPage);
  357.         $items $this->load();
  358.         return $items;
  359.     }
  360.     /**
  361.      * Methods for Iterator
  362.      */
  363.     public function rewind()
  364.     {
  365.         reset($this->results);
  366.     }
  367.     /**
  368.      * @return mixed
  369.      */
  370.     public function current()
  371.     {
  372.         $this->getResults();
  373.         $var current($this->results);
  374.         return $var;
  375.     }
  376.     /**
  377.      * @return mixed
  378.      */
  379.     public function key()
  380.     {
  381.         $this->getResults();
  382.         $var key($this->results);
  383.         return $var;
  384.     }
  385.     /**
  386.      * @return mixed
  387.      */
  388.     public function next()
  389.     {
  390.         $this->getResults();
  391.         $var next($this->results);
  392.         return $var;
  393.     }
  394.     /**
  395.      * @return bool
  396.      */
  397.     public function valid()
  398.     {
  399.         $this->getResults();
  400.         $var $this->current() !== false;
  401.         return $var;
  402.     }
  403. }