-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsolrlib.php
executable file
·167 lines (164 loc) · 5.51 KB
/
solrlib.php
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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
*
*
* @package tool
* @subpackage coursesearch
* @copyright 2013 Shashikant Vaishnav {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
class tool_coursesearch_solrlib
{
private $solr = null;
private $solrhost = null;
private $solrport = null;
private $solrpath = null;
private $errorcode = "";
private $errormessage = "";
public function get_errorcode() {
return $this->errorcode;
}
public function get_errormessage() {
return $this->errormessage;
}
public function connect($options, $ping = false, $path = '') {
// Get the connection options.
$this->solrhost = $options['solr_host'];
$this->solrport = $options['solr_port'];
$this->solrpath = $options['solr_path'];
// Double check everything has been set.
if (!($this->solrhost and $this->solrport and $this->solrpath)) {
$this->errorcode = -1;
$this->errormessage = "Invalid Solr Params";
return false;
}
// Create the solr service object.
try {
require_once($path . "SolrPhpClient/Apache/Solr/HttpTransport/Curl.php");
$httptransport = new Apache_Solr_HttpTransport_Curl();
$this->solr = new Apache_Solr_Service($this->solrhost, $this->solrport, $this->solrpath, $httptransport);
$this->solr->setAuthenticationCredentials(
get_config('tool_coursesearch', 'solrusername'), get_config('tool_coursesearch', 'solrpassword'));
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
// If we want to check if the server is alive, ping it.
if ($ping) {
try {
if (!$this->solr->ping(2000)) {
$this->errorcode = -1;
$this->errormessage = "Ping failed !";
return false;
}
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
}
return true;
}
public function commit() {
try {
$this->solr->commit();
return true;
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
}
public function optimize() {
try {
$this->solr->optimize();
return true;
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
}
public function deletebyquery($courseid) {
try {
$this->solr->deleteByQuery('courseid:'.$courseid);
$this->solr->commit();
return true;
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
}
public function adddocuments($documents) {
try {
$this->solr->addDocuments($documents);
return true;
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
}
public function adddocument($document) {
try {
$this->solr->addDocument($document);
$this->solr->commit();
return true;
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
}
public function deleteall($optimize = false) {
try {
$this->solr->deleteByQuery('*:*');
if (!$this->commit()) {
return false;
}
if ($optimize) {
return $this->optimize();
}
return true;
} catch (Exception $e) {
$this->errorcode = $e->getCode();
$this->errormessage = $e->getMessage();
return false;
}
}
public function deletebyid($docid) {
try {
$this->solr->deleteById($docid);
$this->solr->commit();
} catch (Exception $e) {
echo $e->getMessage();
}
}
public function extract($url, $array, $docs = null) {
try {
$this->solr->extract($url, $array, $docs);
} catch (Exception $e) {
echo $e->getMessage();
}
}
public function search($qry, $offset, $count, $params) {
return $this->solr->search($qry, $offset, $count, $params);
}
}