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