Skip to content

Commit

Permalink
Merge branch 'develop' into MAGETWO-46472
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Logvin committed Dec 21, 2015
2 parents 82a47e5 + 77988b6 commit c78a700
Show file tree
Hide file tree
Showing 24 changed files with 768 additions and 119 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: php
php:
- 5.5
- 5.6
- 7.0
env:
- TEST_SUITE=unit
- TEST_SUITE=integration_part_1
Expand Down Expand Up @@ -37,13 +38,7 @@ before_script:
# Mock mail
- sudo service postfix stop
- smtp-sink -d "%d.%H.%M.%S" localhost:2500 1000 &
- echo -e '#!/usr/bin/env bash\nexit 0' | sudo tee /usr/sbin/sendmail
- >
echo 'sendmail_path = "/usr/sbin/sendmail -t -i "'
| sudo tee "/home/travis/.phpenv/versions/`php -i
| grep "PHP Version"
| head -n 1
| grep -o -P '\d+\.\d+\.\d+.*'`/etc/conf.d/sendmail.ini"
- echo 'sendmail_path = "/usr/sbin/sendmail -t -i "' > $(php --ini|grep -m 1 "ini files in:"|cut -d ":" -f 2)/sendmail.ini
# Disable xDebug
- echo '' > ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini
# Install MySQL 5.6, create DB for integration tests
Expand Down
11 changes: 10 additions & 1 deletion app/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@
*/
define('BP', dirname(__DIR__));

$vendorDir = require BP . '/app/etc/vendor_path.php';
define('VENDOR_PATH', BP . '/app/etc/vendor_path.php');

if (!file_exists(VENDOR_PATH)) {
throw new \Exception(
'We can\'t read some files that are required to run the Magento application. '
. 'This usually means file permissions are set incorrectly.'
);
}

$vendorDir = require VENDOR_PATH;
$vendorAutoload = BP . "/{$vendorDir}/autoload.php";

/* 'composer install' validation */
Expand Down
9 changes: 3 additions & 6 deletions app/code/Magento/Backend/Block/Store/Switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@
* See COPYING.txt for license details.
*/

/**
* Store switcher block
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Backend\Block\Store;

/**
* Store switcher block
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Switcher extends \Magento\Backend\Block\Template
{
/**
* URL for store switcher hint
*/
const HINT_URL = 'http://www.magentocommerce.com/knowledge-base/entry/understanding-store-scopes';
const HINT_URL = 'http://docs.magento.com/m2/ce/user_guide/stores/configuration.html';

/**
* Name of website variable
Expand Down
32 changes: 32 additions & 0 deletions app/code/Magento/Search/Api/SynonymAnalyzerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Search\Api;

/**
* @api
*/
interface SynonymAnalyzerInterface
{
/**
* Get synonyms for specified phrase
*
* For phrase: "Elizabeth is the English queen" correct output is an array of arrays containing synonyms for each
* word in the phrase:
*
* [
* 0 => [ 0 => "elizabeth" ],
* 1 => [ 0 => "is" ],
* 2 => [ 0 => "the" ],
* 3 => [ 0 => "british", 1 => "english" ],
* 4 => [ 0 => "queen", 1 => "monarch" ]
* ]
*
* @param string $phrase
* @return array
*/
public function getSynonymsForPhrase($phrase);
}
98 changes: 98 additions & 0 deletions app/code/Magento/Search/Model/ResourceModel/SynonymReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Search\Model\ResourceModel;

use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\Search\Model\SynonymReader as SynReaderModel;
use Magento\Framework\DB\Helper\Mysql\Fulltext;

/**
* Synonym Reader resource model
*/
class SynonymReader extends AbstractDb
{
/**
* @var Fulltext $fullTextSelect
*/
private $fullTextSelect;

/**
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param Fulltext $fulltext
* @param string $connectionName
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
Fulltext $fulltext,
$connectionName = null
) {
parent::__construct($context, $connectionName);
$this->fullTextSelect = $fulltext;
}

/**
* Custom load model: Get data by store view id
*
* @param AbstractModel $object
* @param int $value
* @return $this
*/
public function loadByStoreViewId(AbstractModel $object, $value)
{
$select = $this->getConnection()->select()->from(
$this->getMainTable()
)->where(
'store_id = ?',
$value
);
$data = $this->getConnection()->fetchAll($select);
if ($data) {
$object->setData($data);
$this->_afterLoad($object);
}
return $this;
}

/**
* Custom load model: Get data by user query phrase and store view id
*
* @param SynReaderModel $object
* @param string $value
* @return $this
*/
public function loadByPhrase(SynReaderModel $object, $value)
{
$phrase = strtolower($value);
$matchQuery = $this->fullTextSelect->getMatchQuery(
['synonyms' => 'synonyms'],
$phrase,
Fulltext::FULLTEXT_MODE_BOOLEAN
);
$query = $this->getConnection()->select()->from(
$this->getMainTable()
)->where(
'store_id = ?',
$object->getStoreViewId()
)->where($matchQuery);

$rows = $this->getConnection()->fetchAll($query);
$object->setData($rows);
$this->_afterLoad($object);
return $this;
}

/**
* Init resource data
*
* @return void
*/
protected function _construct()
{
$this->_init('search_synonyms', 'group_id');
}
}
97 changes: 97 additions & 0 deletions app/code/Magento/Search/Model/SynonymAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Search\Model;

use Magento\Search\Api\SynonymAnalyzerInterface;
use Magento\Search\Model\SynonymsReader;

class SynonymAnalyzer implements SynonymAnalyzerInterface
{
/**
* @var SynonymReader $synReaderModel
*/
protected $synReaderModel;

/**
* Constructor
*
* @param SynonymReader $synReader
*/
public function __construct(SynonymReader $synReader)
{
$this->synReaderModel = $synReader;
}

/**
* Returns an array of arrays consisting of the synonyms found for each word in the input phrase
*
* @param string $phrase
* @return array
*/
public function getSynonymsForPhrase($phrase)
{
$synGroups = [];

if (empty($phrase)) {
return $synGroups;
}

// strip off all the white spaces, comma, semicolons, and other such
// "non-word" characters. Then implode it into a single string using white space as delimiter
//$words = preg_split('/\W+/', strtolower($phrase), -1, PREG_SPLIT_NO_EMPTY);
$words = preg_split(
'/[~`!@#$%^&*()_+={}\[\]:"\',\s\.<>?\/\;\\\]+/',
strtolower($phrase),
-1,
PREG_SPLIT_NO_EMPTY
);
$phrase = implode(' ', $words);

$rows = $this->synReaderModel->loadByPhrase($phrase)->getData();
$synonyms = [];
foreach ($rows as $row) {
$synonyms [] = $row['synonyms'];
}

// Go through every returned record looking for presence of the actual phrase. If there were no matching
// records found in DB then create a new entry for it in the returned array
foreach ($words as $w) {
$position = $this->findInArray($w, $synonyms);
if ($position !== false) {
$synGroups[] = explode(',', $synonyms[$position]);
} else {
// No synonyms were found. Return the original word in this position
$synGroups[] = [$w];
}
}
return $synGroups;
}

/**
* Helper method to find the presence of $word in $wordsArray. If found, the particular array index is returned.
* Otherwise false will be returned.
*
* @param string $word
* @param $array $wordsArray
* @return boolean | int
*/
private function findInArray($word, $wordsArray)
{
if (empty($wordsArray)) {
return false;
}
$position = 0;
foreach ($wordsArray as $wordsLine) {
$pattern = '/^' . $word . ',|,' . $word . ',|,' . $word . '$/';
$rv = preg_match($pattern, $wordsLine);
if ($rv != 0) {
return $position;
}
$position++;
}
return false;
}
}
Loading

0 comments on commit c78a700

Please sign in to comment.