Remove product literal strings in "pht()", part 18
[phabricator.git] / src / applications / search / ferret / PhabricatorFerretEngine.php
blob252223548a7402729c917500a4760e6b125d80ed
1 <?php
3 abstract class PhabricatorFerretEngine extends Phobject {
5 private $fieldMap = array();
6 private $ferretFunctions;
7 private $templateObject;
9 abstract public function getApplicationName();
10 abstract public function getScopeName();
11 abstract public function newSearchEngine();
13 public function getDefaultFunctionKey() {
14 return 'all';
17 public function getObjectTypeRelevance() {
18 return 1000;
21 final public function getFunctionForName($raw_name) {
22 if (isset($this->fieldMap[$raw_name])) {
23 return $this->fieldMap[$raw_name];
26 $normalized_name =
27 FerretSearchFunction::getNormalizedFunctionName($raw_name);
29 if ($this->ferretFunctions === null) {
30 $functions = FerretSearchFunction::newFerretSearchFunctions();
31 $this->ferretFunctions = $functions;
34 if (!isset($this->ferretFunctions[$normalized_name])) {
35 throw new PhutilSearchQueryCompilerSyntaxException(
36 pht(
37 'Unknown search function "%s". Supported functions are: %s. '.
38 '(To search for a term containing a colon, surround the term '.
39 'in double quotes.)',
40 $raw_name,
41 implode(', ', array_keys($this->ferretFunctions))));
44 $function = $this->ferretFunctions[$normalized_name];
45 $this->fieldMap[$raw_name] = $function;
47 return $this->fieldMap[$raw_name];
50 public function newStemmer() {
51 return new PhutilSearchStemmer();
54 public function newTermsCorpus($raw_corpus) {
55 $term_corpus = strtr(
56 $raw_corpus,
57 array(
58 '!' => ' ',
59 '"' => ' ',
60 '#' => ' ',
61 '$' => ' ',
62 '%' => ' ',
63 '&' => ' ',
64 '(' => ' ',
65 ')' => ' ',
66 '*' => ' ',
67 '+' => ' ',
68 ',' => ' ',
69 '-' => ' ',
70 '/' => ' ',
71 ':' => ' ',
72 ';' => ' ',
73 '<' => ' ',
74 '=' => ' ',
75 '>' => ' ',
76 '?' => ' ',
77 '@' => ' ',
78 '[' => ' ',
79 '\\' => ' ',
80 ']' => ' ',
81 '^' => ' ',
82 '`' => ' ',
83 '{' => ' ',
84 '|' => ' ',
85 '}' => ' ',
86 '~' => ' ',
87 '.' => ' ',
88 '_' => ' ',
89 "\n" => ' ',
90 "\r" => ' ',
91 "\t" => ' ',
92 ));
94 // NOTE: Single quotes divide terms only if they're at a word boundary.
95 // In contractions, like "whom'st've", the entire word is a single term.
96 $term_corpus = preg_replace('/(^| )[\']+/', ' ', $term_corpus);
97 $term_corpus = preg_replace('/[\']+( |$)/', ' ', $term_corpus);
99 $term_corpus = preg_replace('/\s+/u', ' ', $term_corpus);
100 $term_corpus = trim($term_corpus, ' ');
102 if (strlen($term_corpus)) {
103 $term_corpus = ' '.$term_corpus.' ';
106 return $term_corpus;
109 /* -( Schema )------------------------------------------------------------- */
111 public function getDocumentTableName() {
112 $application = $this->getApplicationName();
113 $scope = $this->getScopeName();
115 return "{$application}_{$scope}_fdocument";
118 public function getDocumentSchemaColumns() {
119 return array(
120 'id' => 'auto',
121 'objectPHID' => 'phid',
122 'isClosed' => 'bool',
123 'authorPHID' => 'phid?',
124 'ownerPHID' => 'phid?',
125 'epochCreated' => 'epoch',
126 'epochModified' => 'epoch',
130 public function getDocumentSchemaKeys() {
131 return array(
132 'PRIMARY' => array(
133 'columns' => array('id'),
134 'unique' => true,
136 'key_object' => array(
137 'columns' => array('objectPHID'),
138 'unique' => true,
140 'key_author' => array(
141 'columns' => array('authorPHID'),
143 'key_owner' => array(
144 'columns' => array('ownerPHID'),
146 'key_created' => array(
147 'columns' => array('epochCreated'),
149 'key_modified' => array(
150 'columns' => array('epochModified'),
155 public function getFieldTableName() {
156 $application = $this->getApplicationName();
157 $scope = $this->getScopeName();
159 return "{$application}_{$scope}_ffield";
162 public function getFieldSchemaColumns() {
163 return array(
164 'id' => 'auto',
165 'documentID' => 'uint32',
166 'fieldKey' => 'text4',
167 'rawCorpus' => 'sort',
168 'termCorpus' => 'sort',
169 'normalCorpus' => 'sort',
173 public function getFieldSchemaKeys() {
174 return array(
175 'PRIMARY' => array(
176 'columns' => array('id'),
177 'unique' => true,
179 'key_documentfield' => array(
180 'columns' => array('documentID', 'fieldKey'),
181 'unique' => true,
186 public function getNgramsTableName() {
187 $application = $this->getApplicationName();
188 $scope = $this->getScopeName();
190 return "{$application}_{$scope}_fngrams";
193 public function getNgramsSchemaColumns() {
194 return array(
195 'id' => 'auto',
196 'documentID' => 'uint32',
197 'ngram' => 'char3',
201 public function getNgramsSchemaKeys() {
202 return array(
203 'PRIMARY' => array(
204 'columns' => array('id'),
205 'unique' => true,
207 'key_ngram' => array(
208 'columns' => array('ngram', 'documentID'),
210 'key_object' => array(
211 'columns' => array('documentID'),
216 public function getCommonNgramsTableName() {
217 $application = $this->getApplicationName();
218 $scope = $this->getScopeName();
220 return "{$application}_{$scope}_fngrams_common";
223 public function getCommonNgramsSchemaColumns() {
224 return array(
225 'id' => 'auto',
226 'ngram' => 'char3',
227 'needsCollection' => 'bool',
231 public function getCommonNgramsSchemaKeys() {
232 return array(
233 'PRIMARY' => array(
234 'columns' => array('id'),
235 'unique' => true,
237 'key_ngram' => array(
238 'columns' => array('ngram'),
239 'unique' => true,
241 'key_collect' => array(
242 'columns' => array('needsCollection'),