jenkins-bot has submitted this change and it was merged. Change subject: Declare SimpleKeywordFeature with an array instead of a regex ......................................................................
Declare SimpleKeywordFeature with an array instead of a regex This should make it easier to detect conflicts when we'll add a hook to allow extensions to register their own keywords. It also makes SimpleKeyword features independent from regex and makes them "more re-useable" by a parser not "regex-based". Change-Id: I14228bbb1e6f80ac95a933599eccc7f6a548fd10 --- M includes/Query/BoostTemplatesFeature.php M includes/Query/FileNumericFeature.php M includes/Query/FileTypeFeature.php M includes/Query/GeoFeature.php M includes/Query/HasTemplateFeature.php M includes/Query/InCategoryFeature.php M includes/Query/InTitleFeature.php M includes/Query/LanguageFeature.php M includes/Query/LinksToFeature.php M includes/Query/SimpleInSourceFeature.php M includes/Query/SimpleKeywordFeature.php M tests/unit/Query/SimpleKeywordFeatureTest.php 12 files changed, 44 insertions(+), 39 deletions(-) Approvals: EBernhardson: Looks good to me, approved jenkins-bot: Verified Objections: Cindy-the-browser-test-bot: There's a problem with this change, please improve diff --git a/includes/Query/BoostTemplatesFeature.php b/includes/Query/BoostTemplatesFeature.php index 5e0cf99..8577b84 100644 --- a/includes/Query/BoostTemplatesFeature.php +++ b/includes/Query/BoostTemplatesFeature.php @@ -18,10 +18,10 @@ */ class BoostTemplatesFeature extends SimpleKeywordFeature { /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'boost-templates'; + protected function getKeywords() { + return ['boost-templates']; } /** diff --git a/includes/Query/FileNumericFeature.php b/includes/Query/FileNumericFeature.php index 1789155..594e728 100644 --- a/includes/Query/FileNumericFeature.php +++ b/includes/Query/FileNumericFeature.php @@ -14,10 +14,10 @@ */ class FileNumericFeature extends SimpleKeywordFeature { /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'file(size|bits|h|w|height|width|res)'; + protected function getKeywords() { + return ['filesize', 'filebits', 'fileh', 'filew', 'fileheight', 'filewidth', 'fileres']; } /** @@ -120,4 +120,4 @@ return $query; } -} \ No newline at end of file +} diff --git a/includes/Query/FileTypeFeature.php b/includes/Query/FileTypeFeature.php index e46940f..fd9731f 100644 --- a/includes/Query/FileTypeFeature.php +++ b/includes/Query/FileTypeFeature.php @@ -13,10 +13,10 @@ */ class FileTypeFeature extends SimpleKeywordFeature { /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'file(type|mime)'; + protected function getKeywords() { + return ['filetype','filemime']; } /** @@ -47,4 +47,4 @@ return [ $query, false ]; } -} \ No newline at end of file +} diff --git a/includes/Query/GeoFeature.php b/includes/Query/GeoFeature.php index ce1bc24..f55fd10 100644 --- a/includes/Query/GeoFeature.php +++ b/includes/Query/GeoFeature.php @@ -37,10 +37,10 @@ const DEFAULT_GLOBE = 'earth'; /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return '(boost-)?near(coord|title)'; + protected function getKeywords() { + return ['boost-nearcoord', 'boost-neartitle', 'nearcoord', 'neartitle']; } /** diff --git a/includes/Query/HasTemplateFeature.php b/includes/Query/HasTemplateFeature.php index 138e207..f0896da 100644 --- a/includes/Query/HasTemplateFeature.php +++ b/includes/Query/HasTemplateFeature.php @@ -12,10 +12,10 @@ */ class HasTemplateFeature extends SimpleKeywordFeature { /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'hastemplate'; + protected function getKeywords() { + return ['hastemplate']; } /** diff --git a/includes/Query/InCategoryFeature.php b/includes/Query/InCategoryFeature.php index 72adc71..905e608 100644 --- a/includes/Query/InCategoryFeature.php +++ b/includes/Query/InCategoryFeature.php @@ -37,10 +37,10 @@ } /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'incategory'; + protected function getKeywords() { + return ['incategory']; } /** diff --git a/includes/Query/InTitleFeature.php b/includes/Query/InTitleFeature.php index 6177016..85301f3 100644 --- a/includes/Query/InTitleFeature.php +++ b/includes/Query/InTitleFeature.php @@ -24,10 +24,10 @@ */ class InTitleFeature extends SimpleKeywordFeature { /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'intitle'; + protected function getKeywords() { + return ['intitle']; } /** diff --git a/includes/Query/LanguageFeature.php b/includes/Query/LanguageFeature.php index 8280f60..a4356cc 100644 --- a/includes/Query/LanguageFeature.php +++ b/includes/Query/LanguageFeature.php @@ -22,10 +22,10 @@ const QUERY_LIMIT = 20; /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'inlanguage'; + protected function getKeywords() { + return ['inlanguage']; } /** diff --git a/includes/Query/LinksToFeature.php b/includes/Query/LinksToFeature.php index 557685f..d5e073a 100644 --- a/includes/Query/LinksToFeature.php +++ b/includes/Query/LinksToFeature.php @@ -15,10 +15,10 @@ */ class LinksToFeature extends SimpleKeywordFeature { /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'linksto'; + protected function getKeywords() { + return ['linksto']; } /** diff --git a/includes/Query/SimpleInSourceFeature.php b/includes/Query/SimpleInSourceFeature.php index a6fa156..cc9fda1 100644 --- a/includes/Query/SimpleInSourceFeature.php +++ b/includes/Query/SimpleInSourceFeature.php @@ -23,10 +23,10 @@ */ class SimpleInSourceFeature extends SimpleKeywordFeature { /** - * @return string + * @return string[] */ - protected function getKeywordRegex() { - return 'insource'; + protected function getKeywords() { + return ['insource']; } /** diff --git a/includes/Query/SimpleKeywordFeature.php b/includes/Query/SimpleKeywordFeature.php index 1ff448f..718a4c4 100644 --- a/includes/Query/SimpleKeywordFeature.php +++ b/includes/Query/SimpleKeywordFeature.php @@ -12,11 +12,9 @@ */ abstract class SimpleKeywordFeature implements KeywordFeature { /** - * @return string A piece of a regular expression (not wrapped in //) that - * matches the key to trigger this feature. Does not include the negation - * (-) prefix. + * @return string[] The list of keywords this feature is supposed to match */ - abstract protected function getKeywordRegex(); + abstract protected function getKeywords(); /** * Captures either a quoted or unquoted string. Quoted strings may have @@ -53,7 +51,14 @@ * @return string Remaining search query */ public function apply( SearchContext $context, $term ) { - $keywordRegex = '(?<key>-?' . $this->getKeywordRegex() . ')'; + $keyListRegex = implode( + '|', + array_map( + function( $kw ) { return preg_quote( $kw ); }, + $this->getKeywords() + ) + ); + $keywordRegex = '(?<key>-?' . $keyListRegex . ')'; $valueRegex = '(?<value>' . $this->getValueRegex() . ')'; return QueryHelper::extractSpecialSyntaxFromTerm( diff --git a/tests/unit/Query/SimpleKeywordFeatureTest.php b/tests/unit/Query/SimpleKeywordFeatureTest.php index eb18926..2734586 100644 --- a/tests/unit/Query/SimpleKeywordFeatureTest.php +++ b/tests/unit/Query/SimpleKeywordFeatureTest.php @@ -181,8 +181,8 @@ class MockSimpleKeywordFeature extends SimpleKeywordFeature { private $calls = []; - protected function getKeywordRegex() { - return 'mock'; + protected function getKeywords() { + return ['mock']; } protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { -- To view, visit https://gerrit.wikimedia.org/r/323838 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I14228bbb1e6f80ac95a933599eccc7f6a548fd10 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/CirrusSearch Gerrit-Branch: master Gerrit-Owner: DCausse <dcau...@wikimedia.org> Gerrit-Reviewer: Cindy-the-browser-test-bot <bernhardsone...@gmail.com> Gerrit-Reviewer: DCausse <dcau...@wikimedia.org> Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org> Gerrit-Reviewer: Gehel <gleder...@wikimedia.org> Gerrit-Reviewer: Manybubbles <never...@wikimedia.org> Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org> Gerrit-Reviewer: Tjones <tjo...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits