Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / search / ferret / function / FerretSearchFunction.php
blob8019a741ca91b456216c0e5b4f7e31cdc97d2b0a
1 <?php
3 abstract class FerretSearchFunction
4 extends Phobject {
6 abstract public function getFerretFunctionName();
7 abstract public function getFerretFieldKey();
8 abstract public function supportsObject(PhabricatorFerretInterface $object);
10 final public static function getNormalizedFunctionName($name) {
11 return phutil_utf8_strtolower($name);
14 final public static function validateFerretFunctionName($function_name) {
15 if (!preg_match('/^[a-zA-Z-]+\z/', $function_name)) {
16 throw new Exception(
17 pht(
18 'Ferret search engine function name ("%s") is invalid. Function '.
19 'names must be nonempty and may only contain latin letters and '.
20 'hyphens.',
21 $function_name));
25 final public static function validateFerretFunctionFieldKey($field_key) {
26 if (!preg_match('/^[a-z]{4}\z/', $field_key)) {
27 throw new Exception(
28 pht(
29 'Ferret search engine field key ("%s") is invalid. Field keys '.
30 'must be exactly four characters long and contain only '.
31 'lowercase latin letters.',
32 $field_key));
36 final public static function newFerretSearchFunctions() {
37 $extensions = PhabricatorFulltextEngineExtension::getAllExtensions();
39 $function_map = array();
40 $field_map = array();
41 $results = array();
43 foreach ($extensions as $extension) {
44 $functions = $extension->newFerretSearchFunctions();
46 if (!is_array($functions)) {
47 throw new Exception(
48 pht(
49 'Expected fulltext engine extension ("%s") to return a '.
50 'list of functions from "newFerretSearchFunctions()", '.
51 'got "%s".',
52 get_class($extension),
53 phutil_describe_type($functions)));
56 foreach ($functions as $idx => $function) {
57 if (!($function instanceof FerretSearchFunction)) {
58 throw new Exception(
59 pht(
60 'Expected fulltext engine extension ("%s") to return a list '.
61 'of "FerretSearchFunction" objects from '.
62 '"newFerretSearchFunctions()", but found something else '.
63 '("%s") at index "%s".',
64 get_class($extension),
65 phutil_describe_type($function),
66 $idx));
69 $function_name = $function->getFerretFunctionName();
71 self::validateFerretFunctionName($function_name);
73 $normal_name = self::getNormalizedFunctionName(
74 $function_name);
75 if ($normal_name !== $function_name) {
76 throw new Exception(
77 pht(
78 'Ferret function "%s" is specified with a denormalized name. '.
79 'Instead, specify the function using the normalized '.
80 'function name ("%s").',
81 $function_name,
82 $normal_name));
85 if (isset($function_map[$function_name])) {
86 $other_extension = $function_map[$function_name];
87 throw new Exception(
88 pht(
89 'Two different fulltext engine extensions ("%s" and "%s") '.
90 'both define a search function with the same name ("%s"). '.
91 'Each function must have a unique name.',
92 get_class($extension),
93 get_class($other_extension),
94 $function_name));
96 $function_map[$function_name] = $extension;
98 $field_key = $function->getFerretFieldKey();
100 self::validateFerretFunctionFieldKey($field_key);
102 if (isset($field_map[$field_key])) {
103 $other_extension = $field_map[$field_key];
104 throw new Exception(
105 pht(
106 'Two different fulltext engine extensions ("%s" and "%s") '.
107 'both define a search function with the same key ("%s"). '.
108 'Each function must have a unique key.',
109 get_class($extension),
110 get_class($other_extension),
111 $field_key));
113 $field_map[$field_key] = $extension;
115 $results[$function_name] = $function;
119 ksort($results);
121 return $results;