-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcps_simple.php
More file actions
475 lines (428 loc) · 19.6 KB
/
cps_simple.php
File metadata and controls
475 lines (428 loc) · 19.6 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<?php
/**
* CPS Simple API
* @package CPS
*/
/**
* including CPS API
*/
require_once dirname(__FILE__) . '/cps_api.php';
/**
* The CPS_Simple class contains methods suitable for most requests that don't require advanced parameters to be specified
* @package CPS
*/
class CPS_Simple {
/**
* Constructs an instance of the CPS_Simple class.
* @param CPS_Connection $connection A CPS_Connection object
*/
public function __construct(CPS_Connection $connection) {
$this->_connection = $connection;
$this->_lastResponse = NULL;
}
/**
* Performs a search command. Returns the documents found in an associative array with document IDs as keys and document contents as values
* @param array|string $query The query array/string. see {@link CPS_SearchRequest::setQuery()} for more info on best practices.
* @param int $offset Defines the number of documents to skip before including them in the results
* @param int $docs Maximum document count to retrieve
* @param array $list Listing parameter - an associative array with tag xpaths as keys and listing options (yes | no | snippet | highlight) as values
* @param string|array $ordering Defines the order in which results will be returned. Contains either a single sorting string or an array of those. Could be conveniently generated with ordering macros, e.g. $q->setOrdering(array(CPS_NumericOrdering('user_count', 'desc'), CPS_RelevanceOrdering())) will sort the documents in descending order according to the user_count, and if user_count is equal will sort them by relevance.
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function search($query, $offset = NULL, $docs = NULL, $list = NULL, $ordering = NULL, $returnType = DOC_TYPE_SIMPLEXML, $stemLang = NULL) {
$request = new CPS_SearchRequest($query, $offset, $docs, $list);
if (!is_null($ordering))
$request->setOrdering($ordering);
if (!is_null($stemLang))
$request->setStemLang($stemLang);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs a search command with an SQL query. Returns the documents found in an associative array with document IDs as keys and document contents as values
* @param string $sql_query The SQL query string.
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function sql_search($sql_query, $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_SQLSearchRequest($sql_query);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs an insert command. Returns the number of modified documents
* @param string $id The ID of the document to insert
* @param array|SimpleXMLElement|stdClass $document The document contents
* @return int
*/
public function insertSingle($id, $document) {
$request = new CPS_InsertRequest($id, $document);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs an insert command for multiple documents. Returns the number of modified documents
* @param array $docs an associative array with document IDs as keys and document contents as values
* @return int
*/
public function insertMultiple($docs) {
$request = new CPS_InsertRequest($docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs an update command. Returns the number of modified documents
* @param string $id The ID of the document to update
* @param array|SimpleXMLElement|stdClass $document The document contents
* @return int
*/
public function updateSingle($id, $document) {
$request = new CPS_UpdateRequest($id, $document);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs an update command for multiple documents. Returns the number of modified documents
* @param array $docs an associative array with document IDs as keys and document contents as values
* @return int
*/
public function updateMultiple($docs) {
$request = new CPS_UpdateRequest($docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs a replace command. Returns the number of modified documents
* @param string $id The ID of the document to replace
* @param array|SimpleXMLElement|stdClass $document The document contents
* @return int
*/
public function replaceSingle($id, $document) {
$request = new CPS_ReplaceRequest($id, $document);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs a replace command for multiple documents. Returns the number of modified documents
* @param array $docs an associative array with document IDs as keys and document contents as values
* @return int
*/
public function replaceMultiple($docs) {
$request = new CPS_ReplaceRequest($docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs a partial-replace command. Returns the number of modified documents
* @param string $id The ID of the document to replace
* @param array|SimpleXMLElement|stdClass $document The document contents that are to be replaced
* @return int
*/
public function partialReplaceSingle($id, $document) {
$request = new CPS_PartialReplaceRequest($id, $document);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs a partial-replace command for multiple documents. Returns the number of modified documents
* @param array $docs an associative array with document IDs as keys and replaceable document contents as values
* @return int
*/
public function partialReplaceMultiple($docs) {
$request = new CPS_PartialReplaceRequest($docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs a partial-xreplace command. Returns the number of modified documents
* @param string|array $ids The ID of the document to replace or an array of such documents
* @param CPX_PRX_Operation|array $operations The operation to perform or an array of such operations
* @see CPS_PRX_Operation
* @return int
*/
public function partialXReplace($ids, $operations) {
$request = new CPS_PartialXRequest($ids, $operations);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs a delete command. Returns the number of deleted documents
* @param string|array $id The ID of the document to delete as a string or an array of IDs
* @return int
*/
public function delete($id) {
$request = new CPS_DeleteRequest($id);
$this->_lastResponse = $this->_connection->sendRequest($request);
return count($this->_lastResponse->getModifiedIds());
}
/**
* Performs a list-last command. Returns an associative array with document IDs as keys and document contents as values
* @param array $list a listing parameter - an associative array with xpaths as keys and listing options (yes | no | snippet | highlight) as values
* @param int $offset offset
* @param int $docs the maximum number of documents to return
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function listLast($list = NULL, $offset = '', $docs = '', $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_ListLastRequest($list, $offset, $docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs a list-first command. Returns an associative array with document IDs as keys and document contents as values
* @param array $list a listing parameter - an associative array with xpaths as keys and listing options (yes | no | snippet | highlight) as values
* @param int $offset offset
* @param int $docs the maximum number of documents to return
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function listFirst($list = NULL, $offset = '', $docs = '', $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_ListFirstRequest($list, $offset, $docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs a retrieve-last command. Returns an associative array with document IDs as keys and document contents as values
* @param int $offset offset
* @param int $docs the maximum number of documents to return
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function retrieveLast($offset = '', $docs = '', $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_RetrieveLastRequest($offset, $docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs a retrieve-first command. Returns an associative array with document IDs as keys and document contents as values
* @param int $offset offset
* @param int $docs the maximum number of documents to return
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function retrieveFirst($offset = '', $docs = '', $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_RetrieveFirstRequest($offset, $docs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs a retrieve command for a single document. Returns the document contents
* @param string $id document ID
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array|SimpleXMLElement|stdClass
*/
public function retrieveSingle($id, $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_RetrieveRequest($id);
$this->_lastResponse = $this->_connection->sendRequest($request);
foreach ($this->_lastResponse->getDocuments($returnType) as $document) {
return $document;
}
return NULL;
}
/**
* Performs a retrieve command for multiple documents. Returns an associative array with document IDs as keys and document contents as values
* @param array $ids an array of document IDs
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function retrieveMultiple($ids, $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_RetrieveRequest($ids);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs a lookup command for a single document. Returns the document contents
* @param string $id document ID
* @param array $list listing parameter associative array - xpaths as keys, listing options as values
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array|SimpleXMLElement|stdClass
*/
public function lookupSingle($id, $list=NULL, $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_LookupRequest($id, $list);
$this->_lastResponse = $this->_connection->sendRequest($request);
foreach ($this->_lastResponse->getDocuments($returnType) as $document) {
return $document;
}
return NULL;
}
/**
* Performs a lookup command for multiple documents. Returns an associative array with document IDs as keys and document contents as values
* @param array $ids an array of document IDs
* @param array $list listing parameter associative array - xpaths as keys, listing options as values
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_SIMPLEXML, other possible values are DOC_TYPE_ARRAY and DOC_TYPE_STDCLASS
* @return array
*/
public function lookupMultiple($ids, $list = NULL, $returnType = DOC_TYPE_SIMPLEXML) {
$request = new CPS_LookupRequest($ids, $list);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments($returnType);
}
/**
* Performs a list-words command for one or multiple patterns with wildcards
* @param string|array $wildcard a pattern to match against, or an array of patterns
* @return array
*/
public function listWords($wildcard) {
$request = new CPS_ListWordsRequest($wildcard);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getWords();
}
/**
* Performs an alternatives command for the specified query. Returns a string with the most probable query (could be identical to the given one)
*
* @param string $query see {@link CPS_AlternativesRequest::setQuery()}
* @param float $cr see {@link CPS_AlternativesRequest::setCr()}
* @param float $idif see {@link CPS_AlternativesRequest::setIdif()}
* @param float $h see {@link CPS_AlternativesRequest::setH()}
* @return array
*/
public function alternatives($query, $cr = NULL, $idif = NULL, $h = NULL) {
$request = new CPS_AlternativesRequest($query, $cr, $idif, $h);
$this->_lastResponse = $this->_connection->sendRequest($request);
$words = $this->_lastResponse->getWords();
$res = '';
$xp_accum = '';
$prevxpath = NULL;
foreach ($words as $original => $spellings) {
$xpath = '';
if (($pos = strpos($original, '/')) !== FALSE) {
$xpath = substr($original, 0, $pos);
$original = substr($original, $pos + 1);
}
if (!is_null($prevxpath) && ($xpath != $prevxpath)) {
$res .= CPS_Term($xp_accum, $prevxpath);
$xp_accum = '';
}
$prevxpath = $xpath;
if (count($spellings) > 0) {
foreach ($spellings as $key => $n) {
$xp_accum .= ($xp_accum == '' ? '' : ' ') . $key;
break;
}
} else {
$xp_accum .= ($xp_accum == '' ? '' : ' ') . $original;
}
}
if (strlen($xp_accum) > 0) {
$res .= ($res == '' ? '' : ' ') . CPS_Term($xp_accum, is_null($prevxpath) ? '' : $prevxpath);
}
return $res;
}
/**
* Performs the status command. Returns an array with status data
* @return array
*/
public function status() {
$request = new CPS_StatusRequest();
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getStatus();
}
/**
* Performs a search-delete command. Returns the number of the documents erased
* @param string $query The query string. see {@link CPS_SearchRequest::setQuery()} for more info on best practices.
* @return int
*/
public function searchDelete($query) {
$request = new CPS_SearchDeleteRequest($query);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getHits();
}
/**
* Performs a list-paths command. Returns all available paths in an array
* @return array
*/
public function listPaths() {
$request = new CPS_ListPathsRequest();
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getPaths();
}
/**
* Performs a list-facets command. Returns an associative array where keys are facet paths and values are arrays of terms
* @param $paths a single facet path as string or an array of paths
* @return array
*/
public function listFacets($paths) {
$request = new CPS_ListFacetsRequest($paths);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getFacets();
}
/**
* Finds documents similar to the document with a given ID. Returns an associative array with similar document IDs as keys and document contents as values
* @param string $docid ID of the source document - the one that You want to search similar documents to
* @param int $len number of keywords to extract from the source
* @param int $quota minimum number of keywords matching in the destination
* @param int $offset number of results to skip before returning the following ones
* @param int $docs number of documents to retrieve
* @param string $query an optional query that all found documents have to match against
* @return array
*/
public function similarDocument($docid, $len, $quota, $offset=NULL, $docs=NULL, $query=NULL) {
$request = new CPS_SimilarDocumentRequest($docid, $len, $quota, $offset, $docs, $query);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments();
}
/**
* Finds documents similar to the given text. Returns an associative array with similar document IDs as keys and document contents as values
* @param string $text A chunk of text that the found documents have to be similar to
* @param int $len number of keywords to extract from the source
* @param int $quota minimum number of keywords matching in the destination
* @param int $offset number of results to skip before returning the following ones
* @param int $docs number of documents to retrieve
* @param string $query an optional query that all found documents have to match against
* @return array
*/
public function similarText($text, $len, $quota, $offset=NULL, $docs=NULL, $query=NULL) {
$request = new CPS_SimilarTextRequest($text, $len, $quota, $offset, $docs, $query);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments();
}
/**
* Retrieves modification history of the document. Returns an associative array with revision IDs as keys and document contents/versioning metadata as values
* @param string $id Document ID
* @param bool $returnDocs set to true to return document content (subject to license conditions)
* @return array
*/
public function showHistory($id, $returnDocs = false) {
$request = new CPS_ShowHistoryRequest($id, $returnDocs);
$this->_lastResponse = $this->_connection->sendRequest($request);
return $this->_lastResponse->getDocuments();
}
/**
* Begins new transaction
*/
public function beginTransaction() {
$request = new CPS_BeginTransactionRequest();
$this->_lastResponse = $this->_connection->sendRequest($request);
return true;
}
/**
* Commits last transaction
*/
public function commitTransaction() {
$request = new CPS_CommitTransactionRequest();
$this->_lastResponse = $this->_connection->sendRequest($request);
return true;
}
/**
* Rollbacks last transaction
*/
public function rollbackTransaction() {
$request = new CPS_RollbackTransactionRequest();
$this->_lastResponse = $this->_connection->sendRequest($request);
return true;
}
/**
* Returns last received response. This could be useful if you need extra information from the response object
* @return CPS_Response
*/
public function getLastResponse() {
return $this->_lastResponse;
}
/**#@+
* @access private
*/
private $_connection;
private $_lastResponse;
/**#@-*/
}