forked from jaeksoft/opensearchserver-php-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoss_api.class.php
More file actions
316 lines (281 loc) · 10.4 KB
/
oss_api.class.php
File metadata and controls
316 lines (281 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
* This file is part of OpenSearchServer PHP Client.
*
* Copyright (C) 2008-2013 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* OpenSearchServer PHP Client is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer PHP Client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenSearchServer PHP Client. If not, see <http://www.gnu.org/licenses/>.
*/
if (!extension_loaded('curl')) {
trigger_error("OssApi won't work whitout curl extension", E_USER_ERROR); die();
}
if (!extension_loaded('SimpleXML')) {
trigger_error("OssApi won't work whitout SimpleXML extension", E_USER_ERROR); die();
}
if (!class_exists('RuntimeException')) {
class RuntimeException extends Exception {}
}
if (!class_exists('LogicException')) {
class LogicException extends Exception {}
}
if (!class_exists('InvalidArgumentException')) {
class InvalidArgumentException extends LogicException {}
}
if (!class_exists('OutOfRangeException')) {
class OutOfRangeException extends LogicException {}
}
/**
* @file
* Class to access OpenSearchServer API
* @author pmercier <pmercier@open-search-server.com>
* @package OpenSearchServer
*/
require_once(dirname(__FILE__).'/oss_abstract.class.php');
require_once(dirname(__FILE__).'/oss_schema.class.php');
require_once(dirname(__FILE__) . '/oss_search.class.php');
class OssApi extends OssAbstract {
const API_UPDATE = 'update';
const API_DELETE = 'delete';
const API_OPTIMIZE = 'optimize';
const API_RELOAD = 'reload';
const API_INDEX = 'index';
const API_ENGINE = 'engine';
const API_PATTERN = 'pattern';
const API_MONITOR = 'monitor';
const INDEX_TEMPLATE_EMPTY = 'empty_index';
const API_AUTOCOMPLETION = 'autocompletion';
/* The new REST API for autocompletion*/
const REST_API_AUTOCOMPLETION = 'services/rest/index/{index_name}/autocompletion/{autocompletion_name}?field={field_name}&rows={rows}';
const REST_API_AUTOCOMPLETION_BUILD = 'services/rest/index/{index_name}/autocompletion/{autocompletion_name}';
/** @var int Default timeout (specified in seconds) for CURLOPT_TIMEOUT option. See curl documentation */
const DEFAULT_QUERY_TIMEOUT = 0;
/** @var int Timeout (specified in seconds) for CURLOPT_CONNECTTIMEOUT option. See curl documentation */
const DEFAULT_CONNEXION_TIMEOUT = 5;
/**
* List of supported languages
* @todo Provide an API to request the supported languages from the engine (if possible)
* @var array
*/
protected static $supportedLanguages = array(
'' => 'Undefined',
'ar' => 'Arabic',
'zh' => 'Chinese',
'da' => 'Danish',
'nl' => 'Dutch',
'en' => 'English',
'fi' => 'Finnish',
'fr' => 'French',
'de' => 'German',
'hu' => 'Hungarian',
'it' => 'Italian',
'no' => 'Norwegian',
'pt' => 'Portuguese',
'ro' => 'Romanian',
'ru' => 'Russian',
'es' => 'Spanish',
'sv' => 'Swedish',
'tr' => 'Turkish'
);
/**
* @param $enginePath The URL to access the OSS Engine
* @param $index The index name
* @return OssApi
*/
public function __construct($enginePath, $index = NULL, $login = NULL, $apiKey = NULL) {
$this->init($enginePath, $index, $login, $apiKey);
}
/**
* Return an OssSearch using the current engine path and index
* @param string $index If provided, this index name is used in place of the one defined in the API instance
* @return OssSearch
* This method require the file OssSearch.class.php. It'll be included if the OssSearch class don't exist.
* It's expected to be in the same directory as OssApi.class.php.
*/
public function select() {
return new OssSearch($this->enginePath, $this->index, NULL, NULL, $this->login, $this->apiKey);
}
/**
* Return an OssSearch using the current engine path and index
* @return OssSearch
* @deprecated Use OssApi::select
*/
public function search() {
return $this->select();
}
/**
* Optimize the index
* return boolean True on success
* see OSS Wiki [Web API optimize] documentation before using this method
* FIXME Provide a link to the OSS WiKi
*/
public function optimize() {
$return = $this->queryServerTXT(OssApi::API_OPTIMIZE);
return ($return !== FALSE);
}
/**
* Reload the index
* param string $index If provided, this index name is used in place of the one defined in the API instance
* return boolean True on success
* see OSS Wiki [Web API reload] documentation before using this method
* FIXME See why API have been removed
*/
public function reload() {
$return = $this->queryServerTXT(OssApi::API_RELOAD);
return ($return !== FALSE);
}
/**
* Add one or many pattern to crawl
* param string|string[] $patterns
* param boolean $deleteAll The provided patterns will replace the patterns already in the search engine
* return boolean True on success
*/
public function pattern($patterns, $deleteAll = FALSE) {
if (is_array($patterns)) {
$patterns = implode("\n", $patterns);
}
$return = $this->queryServer($this->getQueryURL(OssApi::API_PATTERN) . ($deleteAll?'&deleteAll=yes':'&deleteAll=no'), $patterns);
return ($return !== FALSE);
}
/**
* Send an xml list of documents to be indexed by the search engine
* @param mixed $xml Can be an xml string, a OssIndexDocument, a SimpleXMLElement,
* a DOMDocument or any object that implement the __toString
* magic method
* @return boolean True on success
*/
public function update($xml) {
// Cast $xml to a string
if (!is_string($xml)) {
if ($xml instanceof DOMDocument) {
$xml = $xml->saveXML();
}
elseif ($xml instanceof SimpleXMLElement) {
$xml = $xml->asXML();
}
elseif (is_object($xml)) {
if (method_exists($xml, '__toString') || $xml instanceof SimpleXMLElement) {
$xml = $xml->__toString();
}
}
}
if (!is_string($xml)) {
if (class_exists('OssException')) {
throw new UnexpectedValueException('String, SimpleXMLElement or DOMDocument was expected for $xml.');
}
trigger_error(__CLASS__ . '::' . __METHOD__ . '($xml): String, SimpleXMLElement or DOMDocument was expected for $xml.', E_USER_ERROR);
return FALSE;
}
$return = $this->queryServer($this->getQueryURL(OssApi::API_UPDATE), $xml);
return ($return !== FALSE);
}
/**
* Return the list of indexes usable by the current credential
* @return string[]
*/
public function indexList() {
$ossSchema = new OssSchema($this->enginePath, $this->index, $this->login, $this->apiKey);
return $ossSchema->indexList();
}
/**
* Create a new index using a template
* @param string $index The name of the new index
* @param string $template Optional. The name of the template to use
* @return boolean
*/
public function createIndex($index, $template = FALSE) {
$ossSchema = new OssSchema($this->enginePath, $this->index, $this->login, $this->apiKey);
return $ossSchema->createIndex($index, $template);
}
/**
* Delete an index
* @param string $index The name of the index to delete
*/
public function deleteIndex($index) {
$ossSchema = new OssSchema($this->enginePath, $this->index, $this->login, $this->apiKey);
return $ossSchema->deleteIndex($index);
}
/**
* Retreive the complete schema of the index
* @return SimpleXMLElement|OSS_Schema
* The schema is provided by the OSS engine as an xml. This xml is actualy the complete configuration of the schema.
* If you want to manipulate the schema, pass it to OSS_Schema::factoryFromXML(...) for easier access.
*/
public function getSchema() {
$ossSchema = new OssSchema($this->enginePath, $this->index, $this->login, $this->apiKey);
return $ossSchema->getSchema();
}
/**
* Create or alter a field
* @param string $name
* @param string $analyzer
* @param string $stored
* @param string $indexed
* @param string $termVector
* @param string $default
* @param string $unique
* @return boolean
*/
public function setField($name, $analyzer = NULL, $stored = NULL, $indexed = NULL, $termVector = NULL, $default = NULL, $unique = NULL) {
$ossSchema = new OssSchema($this->enginePath, $this->index, $this->login, $this->apiKey);
return $ossSchema->setField($name, $analyzer, $stored, $indexed, $termVector, $default, $unique);
}
/**
* Return a list of supported language. Array is indexed by ISO 639-1 format (en, de, fr, ...)
* return Array<String>
* see OssApi::$supportedLanguages
*/
public static function supportedLanguages() {
return OssApi::$supportedLanguages;
}
/**
* Return the language constant from a 2 characters language code
* @param string $twoCharsLang Two characters language
*/
public static function getLanguage($twoCharsLang) {
$lang = OssApi::$supportedLanguages[mb_strtolower($twoCharsLang)];
if ($lang == NULL) {
return OssApi::$supportedLanguages[''];
}
return $lang;
}
/**
* Escape Lucene's special chars
* param string $string
* return string
*/
public static function escape($string) {
static $escaping = array(
array("+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", '\\'),
array('\+', '\-', '\&\&', '\|\|', '\!', '\(', '\)', '\{', '\}', '\[', '\]', '\^', '\"', '\~', '\*', '\?', '\:', '\\\\')
);
return str_replace($escaping[0], $escaping[1], $string);
}
/**
* Clean an UTF-8 string to prevent simpleXMLElement to fail on some characters (remove them)
* @param string $string
* @return string
*/
public static function cleanUTF8($string, $replacement = '') {
static $remove = array(
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07",
"\x08", "\x0B", "\x0C", "0x0E", "\x0F",
"\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17",
"\x18", "\x19", "\x1A", "\x1B", "\x1C", "\x1D", "\x1E", "\x1F"
);
return str_replace($remove, $replacement, $string);
}
}
?>