-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_classes.php
More file actions
1795 lines (1669 loc) · 57.9 KB
/
command_classes.php
File metadata and controls
1795 lines (1669 loc) · 57.9 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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Request and response classes for all commands in the CPS API
* @package CPS
*/
/**
* request/response includes
*/
require_once dirname(__FILE__) . '/request.class.php';
require_once dirname(__FILE__) . '/response.class.php';
/**
* Escapes <, > and & characters in the given term for inclusion into XML (like the search query). Also wraps the term in XML tags if xpath is specified.
* Note that this function doesn't escape the @, $, " and other symbols that are meaningful in a search query. If You want to escape input that comes directly
* from the user and that isn't supposed to contain any search operators at all, it's probably better to use {@link CPS_QueryTerm}
* @param string $term the term to be escaped (e.g. a search query term)
* @param string $xpath an optional xpath, to be specified if the search term is to be searched under a specific xpath
* @param bool $escape an optional parameter - whether to escape the term's XML
* @see CPS_QueryTerm
*/
function CPS_Term($term, $xpath = '', $escape = TRUE)
{
$prefix = ' ';
$postfix = ' ';
if (strlen($xpath) > 0) {
$tags = explode('/', $xpath);
foreach ($tags as $tag) {
if (strlen($tag) > 0) {
$prefix .= '<' . $tag . '>';
$postfix = '</' . $tag . '>' . $postfix;
}
}
}
// ENT_NOQUOTES Will leave both double and single quotes unconverted.
if (defined('ENT_SUBSTITUTE')) { // php >5.4
// ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string.
return $prefix . ($escape ? htmlspecialchars($term, ENT_NOQUOTES | ENT_SUBSTITUTE) : $term) . $postfix;
}
return $prefix . ($escape ? htmlspecialchars($term, ENT_NOQUOTES) : $term) . $postfix;
}
/**
* Escapes <, > and & characters, as well as @"{}()=$~+ (search query operators) in the given term for inclusion into the search query.
* Also wraps the term in XML tags if xpath is specified.
* @param string $term the term to be escaped (e.g. a search query term)
* @param string $xpath an optional xpath, to be specified if the search term is to be searched under a specific xpath
* @param string $allowed_symbols a string containing operator symbols that the user is allowed to use (e.g. ")
* @see CPS_Term
*/
function CPS_QueryTerm($term, $xpath = '', $allowed_symbols = '')
{
$newTerm = '';
$len = strlen($term);
for ($x = 0; $x < $len; ++$x) {
switch ($term[$x]) {
case '@':
case '$':
case '"':
case '=':
case '>':
case '<':
case ')':
case '(':
case '{':
case '}':
case '~':
case '+':
if (strstr($allowed_symbols, $term[$x]) === FALSE)
$newTerm .= '\\';
default:
$newTerm .= $term[$x];
}
}
return CPS_Term($newTerm, $xpath);
}
/**
* Converts a given query array to a query string
* @param array $array the query array
* @return string
*/
function CPS_QueryArray($array)
{
$r = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$r .= CPS_Term(CPS_QueryArray($value), $key, false);
} else {
$r .= CPS_Term($value, $key);
}
}
return $r;
}
/**
* Returns an circle definition string with provided center and radius
* @function
* @param String $name name of a shape, should be a valid xml name
* @param array $center array with two elements identifying center of circle
* @param double|String $radius radius of circle with optional distance type (km/mi), default is km
* @param String $tagName1 tag name of first coordinate (e.g. latitude), if not passed, then default configuration values will be used
* @param String $tagName2 tag name of second coordinate (e.g. longitude), if not passed, then default configuration values will be used
* @param String $coord_type coordinate type, either latlong or plane
*/
function CPS_CircleDefinition($name, $center, $radius, $tagName1 = null, $tagName2 = null, $coord_type = null)
{
$res = '<' . $name . '>';
$res .= '<center>' . $center[0] . ' ' . $center[1] . '</center>';
$res .= '<radius>' . $radius . '</radius>';
if ($tagName1)
$res .= '<coord1_tag_name>' . $tagName1 . '</coord1_tag_name>';
if ($tagName2)
$res .= '<coord2_tag_name>' . $tagName2 . '</coord2_tag_name>';
if ($coord_type)
$res .= '<coord_type>' . $coord_type . '</coord_type>';
$res .= '</' . $name . '>';
return $res;
}
/**
* Returns an polygon definition string from provided vertice points
* @function
* @param String $name name of a shape, should be a valid xml name
* @param array $vertices array of vertice coordinates identifying polygon each element should contain array of two elements which correspond to vertice coordinates
* @param String $tagName1 tag name of first coordinate (e.g. latitude), if not passed, then default configuration values will be used
* @param String $tagName2 tag name of second coordinate (e.g. longitude), if not passed, then default configuration values will be used
* @param String $coord_type coordinate type, either latlong or plane
*/
function CPS_PolygonDefinition($name, $vertices, $tagName1 = null, $tagName2 = null, $coord_type = null)
{
$res = '<' . $name . '>';
foreach ($vertices as $vertice) {
$res .= $vertice[0] . ' ' . $vertice[1] . '; ';
}
if ($tagName1)
$res .= '<coord1_tag_name>' . $tagName1 . '</coord1_tag_name>';
if ($tagName2)
$res .= '<coord2_tag_name>' . $tagName2 . '</coord2_tag_name>';
if ($coord_type)
$res .= '<coord_type>' . $coord_type . '</coord_type>';
$res .= '</' . $name . '>';
return $res;
}
/**
* Returns an ordering string for sorting by relevance
* @see CPS_SearchRequest::setOrdering()
* @param string $ascdesc optional parameter to specify ascending/descending order. By default most relevant documents are returned first
*/
function CPS_RelevanceOrdering($ascdesc = '')
{
return '<relevance>' . htmlspecialchars($ascdesc, ENT_NOQUOTES) . '</relevance>';
}
/**
* Returns an ordering string for sorting by a numeric field
* @see CPS_SearchRequest::setOrdering()
* @param string $tag the xpath of the tag by which You wish to perform sorting
* @param string $ascdesc optional parameter to specify ascending/descending order. By default ascending order is used.
*/
function CPS_NumericOrdering($tag, $ascdesc = 'ascending')
{
return '<numeric>' . CPS_Term($ascdesc, $tag) . '</numeric>';
}
/**
* Returns an ordering string for sorting by a date field
* @see CPS_SearchRequest::setOrdering()
* @param string $tag the xpath of the tag by which You wish to perform sorting
* @param string $ascdesc optional parameter to specify ascending/descending order. By default ascending order is used.
*/
function CPS_DateOrdering($tag, $ascdesc = 'ascending')
{
return '<date>' . CPS_Term($ascdesc, $tag) . '</date>';
}
/**
* Returns an ordering string for sorting by a string field
* @see CPS_SearchRequest::setOrdering()
* @param string $tag the xpath of the tag by which You wish to perform sorting
* @param string $lang specifies the language (collation) to be used for ordering. E.g. "en"
* @param string $ascdesc optional parameter to specify ascending/descending order. By default ascending order is used.
*/
function CPS_StringOrdering($tag, $lang, $ascdesc = 'ascending')
{
return '<string>' . CPS_Term($ascdesc . ',' . $lang, $tag) . '</string>';
}
/**#@+
* @access private
*/
function CPS_GenericDistanceOrdering($type, $array, $ascdesc)
{
$res = '<distance type="' . htmlspecialchars($type) . '" order="' . htmlspecialchars($ascdesc) . '">';
foreach ($array as $path => $value) {
$res .= CPS_Term($value, $path);
}
$res .= '</distance>';
return $res;
}
/**#@-*/
/**
* Returns an ordering string for sorting by distance from a latitude/longitude coordinate pair
* @see CPS_SearchRequest::setOrdering()
* @param array $array an associative array with tag xpaths as keys and centerpoint coordinates as values. Should contain exactly two elements - latitude first and longitude second.
* @param string $ascdesc optional parameter to specify ascending/descending order. By default ascending order is used.
*/
function CPS_LatLonDistanceOrdering($array, $ascdesc = 'ascending')
{
return CPS_GenericDistanceOrdering('latlong', $array, $ascdesc);
}
/**
* Returns an ordering string for sorting by distance from specified coordinates on a geometric plane
* @see CPS_SearchRequest::setOrdering()
* @param array $array an associative array with tag xpaths as keys and centerpoint coordinates as values.
* @param string $ascdesc optional parameter to specify ascending/descending order. By default ascending order is used.
*/
function CPS_PlaneDistanceOrdering($array, $ascdesc = 'ascending')
{
return CPS_GenericDistanceOrdering('plane', $array, $ascdesc);
}
// Search
/**
* The CPS_SearchRequest class is a wrapper for the Request class
* @package CPS
* @see CPS_SearchResponse
*/
class CPS_SearchRequest extends CPS_Request
{
/**
* Constructs an instance of the CPS_SearchRequest class.
* @param array|string $query The query array/string. see {@link CPS_SearchRequest::setQuery()} for more info.
* @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
*/
public function __construct($query, $offset = NULL, $docs = NULL, $list = NULL)
{
parent::__construct('search');
$this->setQuery($query);
if (!is_null($offset))
$this->setOffset($offset);
if (!is_null($docs))
$this->setDocs($docs);
if (!is_null($list))
$this->setList($list);
}
/**
* Sets the search query.
*
* Example usage:
* <code>$r->setQuery('(' . CPS_Term('predefined_term', '/generated_fields/type/') . CPS_QueryTerm($user_supplied_terms, '/searchable_fields/text') . ')');</code>
* or
* <code>$r->setQuery(array('tags' => array('title' => 'Title', 'text' => 'Text')));</code>
* or
* <code>$r->setQuery(array('tags/title' => 'Title', 'tags/text' => 'Text'));</code>
* @param array|string $value The query array/string.
* If the string form is used, all <, > and & characters that aren't supposed to be XML tags, should be escaped (e.g. with {@link CPS_Term} or {@link CPS_QueryTerm});
* @see CPS_QueryTerm, CPS_Term
*/
public function setQuery($value)
{
if (is_array($value)) {
$this->setParam('query', CPS_QueryArray($value));
} else {
$this->setParam('query', $value);
}
}
/**
* Sets the maximum number of documents to be returned
* @param int $value maximum number of documents
*/
public function setDocs($value)
{
$this->setParam('docs', $value);
}
/**
* Sets the number of documents to skip in the results
* @param int $value number of results to skip
*/
public function setOffset($value)
{
$this->setParam('offset', $value);
}
/**
* Sets the paths for facets
* @param string|array $value a single path as a string or an array of paths
*/
public function setFacet($value)
{
$this->setParam('facet', $value);
}
/**
* Sets the stemming language
* @param string $value 2-letter language ID
*/
public function setStemLang($value)
{
$this->setParam('stem-lang', $value);
}
/**
* Sets the exact match option
* @param string $value Exact match option : text, binary or all
*/
public function setExactMatch($value)
{
$this->setParam('exact-match', $value);
}
/**
* Sets grouping options
* @param string $tagName name of the grouping tag
* @param int $count maximum number of documents to return from each group
*/
public function setGroup($tagName, $count)
{
$this->setParam('group', $tagName);
$this->setParam('group_size', $count);
}
/**
* Defines which tags of the search results should be listed in the response
* @param array $array an associative array with tag xpaths as keys and listing options (yes, no, snippet or highlight) as values
*/
public function setList($array)
{
$listString = '';
foreach ($array as $key => $value) {
$listString .= CPS_Term($value, $key);
}
$this->setParam('list', $listString);
}
/**
* Defines the order in which results should be returned.
* @param string|array $order 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.
* @see CPS_RelevanceOrdering, CPS_NumericOrdering, CPS_LatLonDistanceOrdering, CPS_PlainDistanceOrdering
*/
public function setOrdering($order)
{
if (is_array($order)) {
$order = implode('', $order);
}
$this->setParam('ordering', $order);
}
/**
* Defines aggregation queries for the search request
* @param string|array $aggregate either a single aggregation query, or an array of queries
* @see CPS_SearchResponse::getAggregate()
*/
public function setAggregate($aggregate)
{
$this->setParam('aggregate', $aggregate);
}
}
/**
* The CPS_SQLSearchRequest class is virtually identical to the {@link CPS_SearchRequest} class, but
* is used for querying the database through the Basic SQL interface
* @package CPS
* @see CPS_SearchResponse
*/
class CPS_SQLSearchRequest extends CPS_SearchRequest
{
/**
* Constructs an instance of the CPS_SQLSearchRequest class.
* @param string $sql The SQL query
*/
public function __construct($sql_query)
{
CPS_Request::__construct('search');
$this->setParam('sql', $sql_query);
}
}
/**
* The CPS_SearchResponse class is a wrapper for the Response class
* @package CPS
* @see CPS_SearchRequest
*/
class CPS_SearchResponse extends CPS_Response
{
/**
* Returns the documents from the response as an associative array, where keys are document IDs and values area document contents
* @param int $type 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 getDocuments($type = DOC_TYPE_SIMPLEXML)
{
if (isset($this->_simpleXml->cursor_id)) return $this->getCursor($type);
return parent::getRawDocuments($type);
}
/**
* Returns the raw SimpleXMLElement results from the response
* @return SimpleXMLElement
*/
public function getRawResults()
{
return parent::getRawResults();
}
/**
* Returns the facets from the response in a form of a multi-dimensional associative array, e.g. array('category' => array('Sports' => 15, 'News' => 20));
* @return array
*/
public function getFacets()
{
return parent::getRawFacets();
}
/**
* Returns aggregated data from the response as an associative array with queries as keys and results as values
* @param int $returnType defines which datatype the returned documents will be in. Default is DOC_TYPE_ARRAY, other possible values are DOC_TYPE_SIMPLEXML and DOC_TYPE_STDCLASS
* @return array
*/
public function getAggregate($returnType = DOC_TYPE_SIMPLEXML)
{
return parent::getRawAggregate($returnType);
}
/**
* Returns the number of documents returned
* @return int
*/
public function getFound()
{
return $this->getParam('found');
}
/**
* Returns the total number of hits - i.e. the number of documents in a storage that match the request
* @return int
*/
public function getHits()
{
return $this->getParam('hits');
}
/**
* Returns the position of the first document that was returned
* @return int
* @see CPS_SearchRequest::setOffset(), CPS_SearchRequest::setDocs()
*/
public function getFrom()
{
return $this->getParam('from');
}
/**
* Returns the position of the last document that was returned
* @see CPS_SearchRequest::setOffset(), CPS_SearchRequest::setDocs()
* @return int
*/
public function getTo()
{
return $this->getParam('to');
}
public function getCursor($type = DOC_TYPE_SIMPLEXML)
{
return new CPS_Cursor($this->_connection, $this->getParam('cursor_id'), $this->getParam('cursor_data'), $this->_documents, $type);
}
}
class CPS_Cursor implements Iterator
{
public function __construct($connection, $cursor_id, $cursor_data, $data, $type = DOC_TYPE_SIMPLEXML, $batch_size = 10)
{
$this->_connection = $connection;
$this->_cursor_id = $cursor_id;
$this->_cursor_data = $cursor_data;
$this->_data = $data;
$this->_type = $type;
$this->_batch_size = $batch_size;
}
public function current()
{
if ($this->type == DOC_TYPE_ARRAY) {
return CPS_Response::simpleXmlToArray(current($this->_data));
} else if ($this->type == DOC_TYPE_STDCLASS) {
return CPS_Response::simpleXmlToStdClass(current($this->_data));
} else {
return current($this->_data);
}
}
public function key()
{
return key($this->_data);
}
public function next()
{
if (next($this->_data) === FALSE) {
// Try to request new data
$req = new CPS_Request('cursor-next-batch');
$req->setParam('cursor_id', $this->_cursor_id);
$req->setParam('cursor_data', $this->_cursor_data);
$req->setParam('docs', $this->_batch_size);
$resp = $this->_connection->sendRequest($req);
$this->_cursor_id = $resp->getParam('cursor_id');
$this->_cursor_data = $resp->getParam('cursor_data');
$this->_data = $resp->getRawDocuments(DOC_TYPE_SIMPLEXML);
}
}
public function rewind()
{
//TODO: Rewind iterator completely??
reset($this->_data);
}
public function valid()
{
return current($this->_data) !== FALSE;
}
private $_connection;
private $_cursor_id;
private $_cursor_data;
private $_data;
}
/**#@+
* @access private
*/
/**
* The CPS_ModifyRequest class is a wrapper for the Request class
* @package CPS
* @see CPS_ModifyResponse
*/
class CPS_ModifyRequest extends CPS_Request
{
/**
* Constructs an instance of the CPS_ModifyRequest class.
* Possible parameter sets:<br />
* 3 parameters - ($command, $id, $document), where $id is the document ID and $document is its contents<br />
* 2 parameters - ($command, $array), where $array is an associative array with document IDs as keys and document contents as values<br />
* @param string $command name of the command
* @param string|array $arg1 Either the document id or the associative array of ids => docs
* @param mixed|null $arg2 Either the document contents or NULL
*/
public function __construct($command, $arg1, $arg2)
{
parent::__construct($command);
if (is_array($arg1)) {
if (!is_null($arg2)) {
throw new CPS_Exception(array(array('long_message' => 'Invalid request parameter', 'code' => ERROR_CODE_INVALID_PARAMETER, 'level' => 'REJECTED', 'source' => 'CPS_API')));
}
// single argument - an associative array
$this->_documents = $arg1;
} elseif (!is_null($arg2)) {
// two arguments - first is the id, second is the document content
$this->_documents = array($arg1 => $arg2);
} else {
throw new CPS_Exception(array(array('long_message' => 'Invalid request parameter', 'code' => ERROR_CODE_INVALID_PARAMETER, 'level' => 'REJECTED', 'source' => 'CPS_API')));
}
}
}
/**
* The CPS_ListLastRetrieveFirstRequest class is a wrapper for the Request class for list-last, list-first, retrieve-last, and retrieve-first requests
* @package CPS
* @see CPS_LookupResponse
*/
class CPS_ListLastRetrieveFirstRequest extends CPS_Request
{
/**
* Constructs an instance of the CPS_ListLastRetrieveFirstRequest class.
* @param string $command command name
* @param int $offset offset
* @param int $docs max number of docs to return
*/
public function __construct($command, $offset, $docs, $list = NULL)
{
parent::__construct($command);
if (strlen($offset) > 0) {
$this->setParam('offset', $offset);
}
if (strlen($docs) > 0) {
$this->setParam('docs', $docs);
}
if (!is_null($list))
$this->setList($list);
}
/**
* Defines which tags of the search results should be listed in the response
* @param array $array an associative array with tag xpaths as keys and listing options (yes, no, snippet or highlight) as values
*/
public function setList($array)
{
$listString = '';
foreach ($array as $key => $value) {
$listString .= CPS_Term($value, $key);
}
$this->setParam('list', $listString);
}
}
/**#@-*/
/**
* The CPS_InsertRequest class is a wrapper for the Response class for the insert command
* @package CPS
* @see CPS_ModifyResponse
*/
class CPS_InsertRequest extends CPS_ModifyRequest
{
/**
* Constructs an instance of the CPS_InsertRequest class.
* Possible parameter sets:<br />
* 2 parameters - ($id, $document), where $id is the document ID and $document are its contents<br />
* 1 parameter - ($array), where $array is an associative array with document IDs as keys and document contents as values<br />
* @param string|array $arg1 Either the document id or the associative array of ids => docs
* @param mixed|null $arg2 Either the document contents or NULL (can also be omitted)
*/
public function __construct($arg1, $arg2 = NULL)
{
parent::__construct('insert', $arg1, $arg2);
}
}
/**
* The CPS_UpdateRequest class is a wrapper for the Response class for the update command
* @package CPS
* @see CPS_ModifyResponse
*/
class CPS_UpdateRequest extends CPS_ModifyRequest
{
/**
* Constructs an instance of the CPS_UpdateRequest class.
* Possible parameter sets:<br />
* 2 parameters - ($id, $document), where $id is the document ID and $document are its contents<br />
* 1 parameter - ($array), where $array is an associative array with document IDs as keys and document contents as values<br />
* @param string|array $arg1 Either the document id or the associative array of ids => docs
* @param mixed|null $arg2 Either the document contents or NULL (can also be omitted)
*/
public function __construct($arg1, $arg2 = NULL)
{
parent::__construct('update', $arg1, $arg2);
}
}
/**
* The CPS_ReplaceRequest class is a wrapper for the Response class for the replace command
* @package CPS
* @see CPS_ModifyResponse
*/
class CPS_ReplaceRequest extends CPS_ModifyRequest
{
/**
* Constructs an instance of the CPS_ReplaceRequest class.
* Possible parameter sets:<br />
* 2 parameters - ($id, $document), where $id is the document ID and $document are its contents<br />
* 1 parameter - ($array), where $array is an associative array with document IDs as keys and document contents as values<br />
* @param string|array $arg1 Either the document id or the associative array of ids => docs
* @param mixed|null $arg2 Either the document contents or NULL (can also be omitted)
*/
public function __construct($arg1, $arg2 = NULL)
{
parent::__construct('replace', $arg1, $arg2);
}
}
/**
* The CPS_PartialReplaceRequest class is a wrapper for the Response class for the partial-replace command
* @package CPS
* @see CPS_ModifyResponse
*/
class CPS_PartialReplaceRequest extends CPS_ModifyRequest
{
/**
* Constructs an instance of the CPS_PartialReplaceRequest class.
* Possible parameter sets:<br />
* 2 parameters - ($id, $document), where $id is the document ID and $document are its partial contents with only those fields set that You want changed.<br />
* 1 parameter - ($array), where $array is an associative array with document IDs as keys and replaceable document contents as values<br />
* @param string|array $arg1 Either the document id or the associative array of ids => docs
* @param mixed|null $arg2 Either the replaceable document contents or NULL (can also be omitted)
*/
public function __construct($arg1, $arg2 = NULL)
{
parent::__construct('partial-replace', $arg1, $arg2);
}
}
/**
* The CPS_PRX_Operation class represents a particular operation for the partial-xreplace command.
* An operation consists of the xpath that identifies which nodes the operation should be performed on,
* the type of operation and new content that should be used in the operation.
* @package CPS
* @see CPS_PartialXRequest
*/
class CPS_PRX_Operation
{
/**
* Constructs an instance of the CPS_PRX_Operation class.
* @param string $xpath an XPath 1.0 expression denoting which nodes the operation should be performed on
* @param string $operation the type of the operation. E.g. "append_children" or "remove"
* @param mixed $document content of the operation - required for anything except the remove operation. Can be specified in any format that general document content can be specified in (array, SimpleXML, XML string, etc.)
*/
public function __construct($xpath, $operation, $document = NULL)
{
$this->_xpath = $xpath;
$this->_operation = $operation;
$this->_document = $document;
}
/**
* Renders the resulting XML
* @ignore
*/
public function renderXML(&$req)
{
$res = '<cps:xpath_match>';
$res .= htmlspecialchars($this->_xpath, ENT_NOQUOTES);
$res .= '</cps:xpath_match>';
$res .= '<cps:' . htmlspecialchars($this->_operation) . '>';
if (is_string($this->_document)) {
$res .= $this->_document;
} else if (!is_null($this->_document)) {
$doc = new DOMDocument('1.0', 'utf-8');
$root = $doc->createElement('root');
$doc->appendChild($root);
$req->_loadIntoDom($doc, $root, $this->_document);
foreach ($root->childNodes as $node) {
$res .= $doc->saveXML($node);
}
}
$res .= '</cps:' . htmlspecialchars($this->_operation) . '>';
return $res;
}
private $_xpath;
private $_operation;
private $_document;
}
/**
* The CPS_PRX_Changeset class represents a changeset for the partial-xreplace command.
* A changeset is one or more document IDs and one or more operations to be performed on documents with these IDs
* @package CPS
* @see CPS_PartialXRequest
*/
class CPS_PRX_Changeset
{
/**
* Constructs an instance of the CPS_PRX_Changeset class.
* @param string|array $ids Either a document id or an array of ids
* @param CPS_PRX_Operation|array $operations Either a CPS_PRX_Operation instance or an array of them
* @see CPS_PRX_Operation
*/
public function __construct($ids, $operations)
{
if (!is_array($ids)) {
$this->_ids = array($ids);
} else {
$this->_ids = $ids;
}
if (!is_array($operations)) {
$this->_operations = array($operations);
} else {
$this->_operations = $operations;
}
}
/**
* Renders the resulting XML
* @ignore
*/
public function renderXML($docRootXpath, $docIdXpath, &$req)
{
$res = '';
$prefix = '';
$postfix = '';
foreach ($docIdXpath as $part) {
$prefix .= '<' . htmlspecialchars($part) . '>';
$postfix = '</' . htmlspecialchars($part) . '>' . $postfix;
}
foreach ($this->_ids as $id) {
$res .= $prefix;
$res .= htmlspecialchars($id);
$res .= $postfix;
}
$isolate_changes = (count($this->_operations) > 1);
foreach ($this->_operations as $op) {
if ($isolate_changes) {
$res .= '<cps:change>';
}
$res .= $op->renderXML($req);
if ($isolate_changes) {
$res .= '</cps:change>';
}
}
return $res;
}
private $_ids;
private $_operations;
}
/**
* The CPS_PartialXRequest class is a wrapper for the Response class for the partial-xreplace command
* @package CPS
* @see CPS_ModifyResponse
*/
class CPS_PartialXRequest extends CPS_ModifyRequest
{
/**
* Constructs an instance of the CPS_PartialXRequest class.
* Possible parameter sets:<br />
* 2 parameters - ($id, $operations), where $id is the document ID (or an array of ids) and $operations is an instance of the CPS_PRX_Operation class or an array of such instances.<br />
* 1 parameter - ($array), where $array is an array of CPS_PRX_Changeset objects<br />
* @param mixed|array $arg1 Either doc ID(s) or an array of changesets
* @param mixed|null $arg2 a CPS_PRX_Operation instance or an array of them for the 2-parameter syntax
* @see CPS_PRX_Operation
* @see CPS_PRX_Changeset
*/
public function __construct($arg1, $arg2 = NULL)
{
parent::__construct('partial-xreplace', array(), NULL);
if (is_null($arg2)) {
// 1 argument syntax - changesets
$valid_arg1 = false;
if ($arg1 instanceof CPS_PRX_Changeset) {
$valid_arg1 = true;
$arg1 = array($arg1);
} else if (is_array($arg1)) {
$valid_arg1 = true;
foreach ($arg1 as $val) {
if (!($val instanceof CPS_PRX_Changeset)) {
$valid_arg1 = false;
break;
}
}
}
if (!$valid_arg1) {
throw new CPS_Exception(array(array('long_message' => 'Invalid request parameter - expecting CPS_PRX_Changeset or an array of them', 'code' => ERROR_CODE_INVALID_PARAMETER, 'level' => 'REJECTED', 'source' => 'CPS_API')));
}
$this->_changesets = $arg1;
} else {
$this->_changesets = array(new CPS_PRX_Changeset($arg1, $arg2));
}
}
}
/**
* The CPS_DeleteRequest class is a wrapper for the Response class for the delete command
* @package CPS
* @see CPS_ModifyResponse
*/
class CPS_DeleteRequest extends CPS_ModifyRequest
{
/**
* Constructs an instance of the CPS_DeleteRequest class.
* @param string|array $arg1 Either the document id as string or an array of document IDs to be deleted
*/
public function __construct($arg1)
{
$nextArg = array();
if (!is_array($arg1)) {
$nextArg = array((string)$arg1 => NULL);
} else {
foreach ($arg1 as $value) {
$nextArg[$value] = NULL;
}
}
parent::__construct('delete', $nextArg, NULL);
}
}
// Modify response
/**
* The CPS_ModifyResponse class is a wrapper for the Response class for insert, update, delete, replace and partial-replace commands
* @package CPS
* @see CPS_InsertRequest, CPS_UpdateRequest, CPS_DeleteRequest, CPS_ReplaceRequest, CPS_PartialReplaceRequest
*/
class CPS_ModifyResponse extends CPS_Response
{
/**
* Returns an array of IDs of documents that have been successfully modified
* @return array
*/
public function getModifiedIds()
{
return array_keys(parent::getRawDocuments(NULL));
}
}
/**
* The CPS_AlternativesRequest class is a wrapper for the Response class for the alternatives command
* @package CPS
* @see CPS_AlternativesResponse
*/
class CPS_AlternativesRequest extends CPS_Request
{
/**
* Constructs an instance of CPS_AlternativesRequest
*
* @param string $query see {@link setQuery}
* @param float $cr see {@link setCr}
* @param float $idif see {@link setIdif}
* @param float $h see {@link setH}
*/
public function __construct($query, $cr = NULL, $idif = NULL, $h = NULL)
{
parent::__construct('alternatives');
$this->setQuery($query);
if (!is_null($cr))
$this->setCr($cr);
if (!is_null($idif))
$this->setIdif($idif);
if (!is_null($h))
$this->setH($h);
}
/**
* Sets the search query.
*
* Example usage:
* <code>$r->setQuery('(' . CPS_Term('predefined_term', '/generated_fields/type/') . CPS_QueryTerm($user_supplied_terms, '/searchable_fields/text') . ')');</code>
* @param string $value The query string.
* All <, > and & characters that aren't supposed to be XML tags, should be escaped (e.g. with {@link CPS_Term} or {@link CPS_QueryTerm});
* @see CPS_QueryTerm, CPS_Term
*/
public function setQuery($value)
{
$this->setParam('query', $value);
}
/**
* Minimum ratio between the occurrence of the alternative and the occurrence of the search term.
* If this parameter is increased, less results are returned while performance is improved.
* @param float $value
*/
public function setCr($value)
{
$this->setParam('cr', $value);
}
/**
* A number that limits how much the alternative may differ from the search term,
* the greater the idif value, the greater the allowed difference.
* If this parameter is increased, more results are returned while performance is decreased.
* @param float $value
*/
public function setIdif($value)
{
$this->setParam('idif', $value);
}
/**
* A number that limits the overall estimate of the quality of the alternative,
* the greater the cr value and the smaller the idif value, the greater the h value.
* If this parameter is increased, less results are returned while performance is improved.
* @param float $value
*/
public function setH($value)
{
$this->setParam('h', $value);
}
}
/**
* The CPS_AlternativesResponse class is a wrapper for the Response class for the alternatives command
* @package CPS
* @see CPS_AlternativesRequest
*/
class CPS_AlternativesResponse extends CPS_Response
{
/**
* Gets the spelling alternatives to the specified query terms
*
* Returns an associative array, where keys are query terms and values are associative arrays