tools - add missing comment blocks
[civicrm-core.git] / tools / scripts / solr / createSolrXML.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27 /**
28 * Create a xml file for a set of contact ID's in a format digestible
29 * by Solr
30 */
31
32 require_once '../../civicrm.config.php';
33 require_once 'CRM/Core/Config.php';
34
35 define('CHUNK_SIZE', 128);
36
37 /**
38 * Split a large array of contactIDs into more manageable smaller chunks
39 */
40 function &splitContactIDs(&$contactIDs) {
41 // contactIDs could be a real large array, so we split it up into
42 // smaller chunks and then general xml for each chunk
43 $chunks = array();
44 $current = 0;
45 $chunks[$current] = array();
46 $count = 0;
47
48 foreach ($contactIDs as $cid) {
49 $chunks[$current][] = $cid;
50 $count++;
51
52 if ($count == CHUNK_SIZE) {
53 $current++;
54 $chunks[$current] = array();
55 $count = 0;
56 }
57 }
58
59 if (empty($chunks[$current])) {
60 unset($chunks[$current]);
61 }
62
63 return $chunks;
64 }
65
66 /**
67 * Given an array of values, generate the XML in the Solr format
68 */
69 function &generateSolrXML($values) {
70 $result = "<add>\n";
71 foreach ($values as $cid => $tokens) {
72 if (empty($tokens)) {
73 continue;
74 }
75
76 $result .= <<<EOT
77 <doc>
78 <field name="id">$cid</field>\n
79 EOT;
80
81 foreach ($tokens as $t) {
82 $result .= <<<EOT
83 <field name="$t[0]">$t[1]</field>\n
84 EOT;
85 }
86
87 $result .= " </doc>\n";
88 }
89 $result .= "</add>\n";
90
91
92 return $result;
93 }
94
95 /**
96 * Given a set of contact IDs get the values
97 */
98 function getValues(&$contactIDs, &$values) {
99 $values = array();
100
101 foreach ($contactIDs as $cid) {
102 $values[$cid] = array();
103 }
104
105 getContactInfo($contactIDs, $values);
106 getLocationInfo($contactIDs, $values);
107
108 return $values;
109 }
110
111 /**
112 * @param $contactIDs
113 * @param $values
114 * @param $tableName
115 * @param $fields
116 * @param $whereField
117 * @param null $additionalWhereCond
118 */
119 function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) {
120 $selectString = implode(',', array_keys($fields));
121 $idString = implode(',', $contactIDs);
122
123 $sql = "
124 SELECT $selectString, $whereField as contact_id
125 FROM $tableName
126 WHERE $whereField IN ( $idString )
127 ";
128
129 if ($additionalWhereCond) {
130 $sql .= " AND $additionalWhereCond";
131 }
132
133 $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
134 while ($dao->fetch()) {
135 foreach ($fields as $fld => $name) {
136 if (empty($dao->$fld)) {
137 continue;
138 }
139 if (!$name) {
140 $name = $fld;
141 }
142 $values[$dao->contact_id][] = array($name, $dao->$fld);
143 }
144 }
145 }
146
147 /**
148 * @param $contactIDs
149 * @param $values
150 */
151 function getContactInfo(&$contactIDs, &$values) {
152 $fields = array('sort_name' => NULL,
153 'display_name' => NULL,
154 'contact_type' => NULL,
155 'legal_identifier' => NULL,
156 'external_identifier' => NULL,
157 'source' => 'contact_source',
158 );
159 getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id');
160
161 $fields = array('first_name' => NULL,
162 'last_name' => NULL,
163 'middle_name' => NULL,
164 'job_title' => NULL,
165 );
166 getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id');
167
168 $fields = array('household_name' => NULL);
169 getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id');
170
171 $fields = array('organization_name' => NULL,
172 'legal_name' => NULL,
173 'sic_code' => NULL,
174 );
175 getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id');
176
177 $fields = array('note' => 'note_body',
178 'subject' => 'note_subject',
179 );
180 getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'");
181 }
182
183 /**
184 * @param $contactIDs
185 * @param $values
186 */
187 function getLocationInfo(&$contactIDs, &$values) {
188 $ids = implode(',', $contactIDs);
189
190 $sql = "
191 SELECT
192 l.entity_id as contact_id, l.name as location_name,
193 a.street_address, a.supplemental_address_1, a.supplemental_address_2,
194 a.city, a.postal_code,
195 co.name as county, s.name as state, c.name as country,
196 e.email, p.phone, i.name as im
197 FROM
198 civicrm_location l
199 LEFT JOIN civicrm_address a ON a.location_id = l.id
200 LEFT JOIN civicrm_email e ON e.location_id = l.id
201 LEFT JOIN civicrm_phone p ON p.location_id = l.id
202 LEFT JOIN civicrm_im i ON i.location_id = l.id
203 LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id
204 LEFT JOIN civicrm_country c ON a.country_id = c.id
205 LEFT JOIN civicrm_county co ON a.county_id = co.id
206 WHERE l.entity_table = 'civicrm_contact'
207 AND l.entity_id IN ( $ids )
208 ";
209
210 $fields = array('location_name', 'street_address', 'supplemental_address_1',
211 'supplemental_address_2', 'city', 'postal_code', 'county', 'state',
212 'country', 'email', 'phone', 'im',
213 );
214 $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
215 while ($dao->fetch()) {
216 foreach ($fields as $fld) {
217 if (empty($dao->$fld)) {
218 continue;
219 }
220 $values[$dao->contact_id][] = array($fld, $dao->$fld);
221 }
222 }
223 }
224
225 /**
226 * @param $contactIDs
227 */
228 function run(&$contactIDs) {
229 $chunks = &splitContactIDs($contactIDs);
230
231 foreach ($chunks as $chunk) {
232 $values = array();
233 getValues($chunk, $values);
234 $xml = &generateSolrXML($values);
235 echo $xml;
236 }
237 }
238
239 $config = &CRM_Core_Config::singleton();
240 $config->userFramework = 'Soap';
241 $config->userFrameworkClass = 'CRM_Utils_System_Soap';
242 $config->userHookClass = 'CRM_Utils_Hook_Soap';
243
244 $sql = <<<EOT
245 SELECT id
246 FROM civicrm_contact
247 EOT;
248 $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
249
250 $contactIDs = array();
251 while ($dao->fetch()) {
252 $contactIDs[] = $dao->id;
253 }
254
255 run($contactIDs);
256