From be89ff97d3d2f12693008d8c04c90e7735195e64 Mon Sep 17 00:00:00 2001 From: Scott Tester Date: Wed, 21 May 2014 20:44:15 +1000 Subject: [PATCH 1/7] fixed normalizePath to keep a '.' path when necessary. --- framework/helpers/BaseFileHelper.php | 9 +++++++-- tests/unit/framework/helpers/FileHelperTest.php | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/framework/helpers/BaseFileHelper.php b/framework/helpers/BaseFileHelper.php index 0974d8d315..881934d33c 100644 --- a/framework/helpers/BaseFileHelper.php +++ b/framework/helpers/BaseFileHelper.php @@ -49,13 +49,18 @@ class BaseFileHelper $parts = []; foreach (explode($ds, $path) as $part) { if ($part === '..' && !empty($parts) && end($parts) !== '..') { - array_pop($parts); - } elseif ($part === '.' || $part === '' && !empty($parts)) { + if (array_pop($parts) === ".") { + $parts[] = '..'; + } + } elseif (( $part === '.' || $part === '' ) && !empty($parts)) { continue; } else { $parts[] = $part; } } + if (count($parts)>1 && $parts[0] === '.') { + array_shift($parts); + } return implode($ds, $parts); } diff --git a/tests/unit/framework/helpers/FileHelperTest.php b/tests/unit/framework/helpers/FileHelperTest.php index b8822dfb0e..c62f4abe40 100644 --- a/tests/unit/framework/helpers/FileHelperTest.php +++ b/tests/unit/framework/helpers/FileHelperTest.php @@ -369,10 +369,16 @@ class FileHelperTest extends TestCase $this->assertEquals("..{$ds}c", FileHelper::normalizePath('//a/.\\b//..//..//../../c')); // relative paths + $this->assertEquals(".", FileHelper::normalizePath('.')); + $this->assertEquals(".", FileHelper::normalizePath('./')); + $this->assertEquals("a", FileHelper::normalizePath('.\\a')); + $this->assertEquals("a/b", FileHelper::normalizePath('./a\\b')); + $this->assertEquals(".", FileHelper::normalizePath('./a\\../')); $this->assertEquals("..{$ds}..{$ds}a", FileHelper::normalizePath('../..\\a')); $this->assertEquals("..{$ds}..{$ds}a", FileHelper::normalizePath('../..\\a/../a')); $this->assertEquals("..{$ds}..{$ds}b", FileHelper::normalizePath('../..\\a/../b')); $this->assertEquals("..{$ds}a", FileHelper::normalizePath('./..\\a')); + $this->assertEquals("..{$ds}a", FileHelper::normalizePath('././..\\a')); $this->assertEquals("..{$ds}a", FileHelper::normalizePath('./..\\a/../a')); $this->assertEquals("..{$ds}b", FileHelper::normalizePath('./..\\a/../b')); } From de484b5e76510c29e34d41f01311650c2c2cba2b Mon Sep 17 00:00:00 2001 From: Scott Tester Date: Wed, 21 May 2014 21:02:38 +1000 Subject: [PATCH 2/7] Added BUG #3522 to the changelog. --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 998e025ae6..31d2ce1020 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -132,6 +132,7 @@ Yii Framework 2 Change Log - Bug #2848: Individual queries should be enclosed within parenthesis in a UNION query (qiangxue) - Bug #2862: Using `DbCache` while enabling schema caching may cause infinite loops (qiangxue) - Bug #3052: Fixed the issue that cache dependency data is not reused when `reusable` is set true (qiangxue) +- Bug #3522: Fixed BaseFileHelper::normalizePath to allow a (.) for the current path. - Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark) - Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark) - Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe) From 39265da4f530b35569c1d859c81dfab651ef96e6 Mon Sep 17 00:00:00 2001 From: Scott Tester Date: Wed, 21 May 2014 22:24:49 +1000 Subject: [PATCH 3/7] Code style fix & CHANGELOG typo fix for bug #3522 --- framework/CHANGELOG.md | 2 +- framework/helpers/BaseFileHelper.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 31d2ce1020..b46bdf837d 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -28,6 +28,7 @@ Yii Framework 2 Change Log - Bug #3431: Allow using extended ErrorHandler class from the app namespace (cebe) - Bug #3436: Fixed the issue that `ServiceLocator` still returns the old component after calling `set()` with a new definition (qiangxue) - Bug #3458: Fixed the bug that the image rendered by `CaptchaAction` was using a wrong content type (MDMunir, qiangxue) +- Bug #3522: Fixed BaseFileHelper::normalizePath to allow a (.) for the current path. (skotos) - Bug: Fixed inconsistent return of `\yii\console\Application::runAction()` (samdark) - Enh #2264: `CookieCollection::has()` will return false for expired or removed cookies (qiangxue) - Enh #2435: `yii\db\IntegrityException` is now thrown on database integrity errors instead of general `yii\db\Exception` (samdark) @@ -132,7 +133,6 @@ Yii Framework 2 Change Log - Bug #2848: Individual queries should be enclosed within parenthesis in a UNION query (qiangxue) - Bug #2862: Using `DbCache` while enabling schema caching may cause infinite loops (qiangxue) - Bug #3052: Fixed the issue that cache dependency data is not reused when `reusable` is set true (qiangxue) -- Bug #3522: Fixed BaseFileHelper::normalizePath to allow a (.) for the current path. - Bug: Fixed `Call to a member function registerAssetFiles() on a non-object` in case of wrong `sourcePath` for an asset bundle (samdark) - Bug: Fixed incorrect event name for `yii\jui\Spinner` (samdark) - Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe) diff --git a/framework/helpers/BaseFileHelper.php b/framework/helpers/BaseFileHelper.php index 881934d33c..e37c1d0e9c 100644 --- a/framework/helpers/BaseFileHelper.php +++ b/framework/helpers/BaseFileHelper.php @@ -58,7 +58,7 @@ class BaseFileHelper $parts[] = $part; } } - if (count($parts)>1 && $parts[0] === '.') { + if (count($parts) > 1 && $parts[0] === '.') { array_shift($parts); } return implode($ds, $parts); From 330f4609bfe879af4e0d6e0a454afe0a1e772e6b Mon Sep 17 00:00:00 2001 From: Scott Tester Date: Thu, 22 May 2014 08:23:26 +1000 Subject: [PATCH 4/7] Implemented qiangxue's solution as it is much simpler. --- framework/helpers/BaseFileHelper.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/framework/helpers/BaseFileHelper.php b/framework/helpers/BaseFileHelper.php index e37c1d0e9c..edf81a1df0 100644 --- a/framework/helpers/BaseFileHelper.php +++ b/framework/helpers/BaseFileHelper.php @@ -49,19 +49,15 @@ class BaseFileHelper $parts = []; foreach (explode($ds, $path) as $part) { if ($part === '..' && !empty($parts) && end($parts) !== '..') { - if (array_pop($parts) === ".") { - $parts[] = '..'; - } - } elseif (( $part === '.' || $part === '' ) && !empty($parts)) { + array_pop($parts); + } elseif ($part === '.' || $part === '' && !empty($parts)) { continue; } else { $parts[] = $part; } } - if (count($parts) > 1 && $parts[0] === '.') { - array_shift($parts); - } - return implode($ds, $parts); + $path = implode($ds, $parts); + return $path === '' ? '.' : $path; } /** From 390004a8992788c4a2de29035524a2e91b38f4f0 Mon Sep 17 00:00:00 2001 From: Scott Tester Date: Thu, 22 May 2014 09:34:10 +1000 Subject: [PATCH 5/7] Fixed path in unit test to use the directory separator variable. --- tests/unit/framework/helpers/FileHelperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/framework/helpers/FileHelperTest.php b/tests/unit/framework/helpers/FileHelperTest.php index c62f4abe40..3eb8f35e1c 100644 --- a/tests/unit/framework/helpers/FileHelperTest.php +++ b/tests/unit/framework/helpers/FileHelperTest.php @@ -372,7 +372,7 @@ class FileHelperTest extends TestCase $this->assertEquals(".", FileHelper::normalizePath('.')); $this->assertEquals(".", FileHelper::normalizePath('./')); $this->assertEquals("a", FileHelper::normalizePath('.\\a')); - $this->assertEquals("a/b", FileHelper::normalizePath('./a\\b')); + $this->assertEquals("a{$ds}b", FileHelper::normalizePath('./a\\b')); $this->assertEquals(".", FileHelper::normalizePath('./a\\../')); $this->assertEquals("..{$ds}..{$ds}a", FileHelper::normalizePath('../..\\a')); $this->assertEquals("..{$ds}..{$ds}a", FileHelper::normalizePath('../..\\a/../a')); From 2e7ae3801e1981ce18e831d891e03c7d6267a20c Mon Sep 17 00:00:00 2001 From: Borales Date: Thu, 22 May 2014 11:40:33 +0300 Subject: [PATCH 6/7] [WIP] #3527 Improving Elasticsearch extension --- extensions/elasticsearch/ActiveRecord.php | 12 ++++++++++++ extensions/elasticsearch/CHANGELOG.md | 3 ++- extensions/elasticsearch/Command.php | 5 +++++ extensions/elasticsearch/Query.php | 17 +++++++++++++++++ extensions/elasticsearch/QueryBuilder.php | 4 ++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index 6ff1095d4f..be6a7451dd 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -42,6 +42,8 @@ use yii\helpers\StringHelper; * * @property float $score Returns the score of this record when it was retrieved via a [[find()]] query. This * property is read-only. + * @property array $highlight Returns a list of arrays with highlighted excerpts indexed by field names. This + * property is read-only. * * @author Carsten Brandt * @since 2.0 @@ -51,6 +53,7 @@ class ActiveRecord extends BaseActiveRecord private $_id; private $_score; private $_version; + private $_highlight; /** * Returns the database connection used by this AR class. @@ -175,6 +178,14 @@ class ActiveRecord extends BaseActiveRecord return $this->_score; } + /** + * @return array|null A list of arrays with highlighted excerpts indexed by field names. + */ + public function getHighlight() + { + return $this->_highlight; + } + /** * Sets the primary key * @param mixed $value @@ -302,6 +313,7 @@ class ActiveRecord extends BaseActiveRecord if ($pk === '_id') { $record->_id = $row['_id']; } + $record->_highlight = isset($row['highlight']) ? $row['highlight'] : null; $record->_score = isset($row['_score']) ? $row['_score'] : null; $record->_version = isset($row['_version']) ? $row['_version'] : null; // TODO version should always be available... } diff --git a/extensions/elasticsearch/CHANGELOG.md b/extensions/elasticsearch/CHANGELOG.md index 3710c9708c..92595c4693 100644 --- a/extensions/elasticsearch/CHANGELOG.md +++ b/extensions/elasticsearch/CHANGELOG.md @@ -5,8 +5,9 @@ Yii Framework 2 elasticsearch extension Change Log -------------------------- - Chg: asArray in ActiveQuery is now equal to using the normal Query. This means, that the output structure has changed and `with` is supported anymore. (cebe) -- Chg: Deletion of a record is now also considered successfull if the record did not exist. (cebe) +- Chg: Deletion of a record is now also considered successful if the record did not exist. (cebe) - Chg: Requirement changes: Yii now requires elasticsearch version 1.0 or higher (cebe) +- Enh #3527: Added `highlight` property to Query and ActiveRecord. (Borales) 2.0.0-beta April 13, 2014 diff --git a/extensions/elasticsearch/Command.php b/extensions/elasticsearch/Command.php index f1313ad15b..7528f0f7af 100644 --- a/extensions/elasticsearch/Command.php +++ b/extensions/elasticsearch/Command.php @@ -38,6 +38,11 @@ class Command extends Component * @var array list of arrays or json strings that become parts of a query */ public $queryParts; + /** + * @var array list of arrays to highlight search results on one or more fields + * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html + */ + public $highlight; public $options = []; diff --git a/extensions/elasticsearch/Query.php b/extensions/elasticsearch/Query.php index 535e1c83eb..9c4f8e6c59 100644 --- a/extensions/elasticsearch/Query.php +++ b/extensions/elasticsearch/Query.php @@ -132,6 +132,11 @@ class Query extends Component implements QueryInterface * the elasticsearch [Query DSL](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html). */ public $filter; + /** + * @var array The highlight part of this search query. This is an array that allows to highlight search results + * on one or more fields. + */ + public $highlight; public $facets = []; @@ -525,6 +530,18 @@ class Query extends Component implements QueryInterface return $this; } + /** + * Sets a highlight parameters to retrieve from the documents. + * @param array $highlight array of parameters to highlight results. + * @return static the query object itself + * @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-highlighting.html + */ + public function highlight($highlight) + { + $this->highlight = $highlight; + return $this; + } + /** * Sets the source filtering, specifying how the `_source` field of the document should be returned. * @param array $source the source patterns to be selected. diff --git a/extensions/elasticsearch/QueryBuilder.php b/extensions/elasticsearch/QueryBuilder.php index 951e6f6d17..96aa374d43 100644 --- a/extensions/elasticsearch/QueryBuilder.php +++ b/extensions/elasticsearch/QueryBuilder.php @@ -97,6 +97,10 @@ class QueryBuilder extends \yii\base\Object $parts['filter'] = $whereFilter; } + if($query->highlight) { + $parts['highlight'] = $query->highlight; + } + $sort = $this->buildOrderBy($query->orderBy); if (!empty($sort)) { $parts['sort'] = $sort; From fda64b4b8fbf575c5acf9e07df915fd9003edb19 Mon Sep 17 00:00:00 2001 From: Borales Date: Thu, 22 May 2014 11:53:24 +0300 Subject: [PATCH 7/7] [WIP] #3527 Fixing spaces --- extensions/elasticsearch/ActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index be6a7451dd..b4b7abd5b5 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -53,7 +53,7 @@ class ActiveRecord extends BaseActiveRecord private $_id; private $_score; private $_version; - private $_highlight; + private $_highlight; /** * Returns the database connection used by this AR class.