Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
5b71fd5f | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | |
d7c8cf03 EM |
39 | * @param $contactIDs |
40 | * @return array | |
6a488035 TO |
41 | */ |
42 | function &splitContactIDs(&$contactIDs) { | |
43 | // contactIDs could be a real large array, so we split it up into | |
44 | // smaller chunks and then general xml for each chunk | |
45 | $chunks = array(); | |
46 | $current = 0; | |
47 | $chunks[$current] = array(); | |
48 | $count = 0; | |
49 | ||
50 | foreach ($contactIDs as $cid) { | |
51 | $chunks[$current][] = $cid; | |
52 | $count++; | |
53 | ||
54 | if ($count == CHUNK_SIZE) { | |
55 | $current++; | |
56 | $chunks[$current] = array(); | |
57 | $count = 0; | |
58 | } | |
59 | } | |
60 | ||
61 | if (empty($chunks[$current])) { | |
62 | unset($chunks[$current]); | |
63 | } | |
64 | ||
65 | return $chunks; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Given an array of values, generate the XML in the Solr format | |
d7c8cf03 EM |
70 | * @param $values |
71 | * @return string | |
6a488035 TO |
72 | */ |
73 | function &generateSolrXML($values) { | |
74 | $result = "<add>\n"; | |
75 | foreach ($values as $cid => $tokens) { | |
76 | if (empty($tokens)) { | |
77 | continue; | |
78 | } | |
79 | ||
80 | $result .= <<<EOT | |
81 | <doc> | |
82 | <field name="id">$cid</field>\n | |
83 | EOT; | |
84 | ||
85 | foreach ($tokens as $t) { | |
86 | $result .= <<<EOT | |
87 | <field name="$t[0]">$t[1]</field>\n | |
88 | EOT; | |
89 | } | |
90 | ||
91 | $result .= " </doc>\n"; | |
92 | } | |
93 | $result .= "</add>\n"; | |
94 | ||
95 | ||
96 | return $result; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Given a set of contact IDs get the values | |
d7c8cf03 EM |
101 | * @param $contactIDs |
102 | * @param $values | |
103 | * @return array | |
6a488035 TO |
104 | */ |
105 | function getValues(&$contactIDs, &$values) { | |
106 | $values = array(); | |
107 | ||
108 | foreach ($contactIDs as $cid) { | |
109 | $values[$cid] = array(); | |
110 | } | |
111 | ||
112 | getContactInfo($contactIDs, $values); | |
113 | getLocationInfo($contactIDs, $values); | |
114 | ||
115 | return $values; | |
116 | } | |
117 | ||
a1a55b61 EM |
118 | /** |
119 | * @param $contactIDs | |
120 | * @param $values | |
121 | * @param $tableName | |
122 | * @param $fields | |
123 | * @param $whereField | |
124 | * @param null $additionalWhereCond | |
125 | */ | |
6a488035 TO |
126 | function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) { |
127 | $selectString = implode(',', array_keys($fields)); | |
128 | $idString = implode(',', $contactIDs); | |
129 | ||
130 | $sql = " | |
131 | SELECT $selectString, $whereField as contact_id | |
132 | FROM $tableName | |
133 | WHERE $whereField IN ( $idString ) | |
134 | "; | |
135 | ||
136 | if ($additionalWhereCond) { | |
137 | $sql .= " AND $additionalWhereCond"; | |
138 | } | |
139 | ||
9d2678f4 | 140 | $dao = CRM_Core_DAO::executeQuery($sql); |
6a488035 TO |
141 | while ($dao->fetch()) { |
142 | foreach ($fields as $fld => $name) { | |
143 | if (empty($dao->$fld)) { | |
144 | continue; | |
145 | } | |
146 | if (!$name) { | |
147 | $name = $fld; | |
148 | } | |
149 | $values[$dao->contact_id][] = array($name, $dao->$fld); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
a1a55b61 EM |
154 | /** |
155 | * @param $contactIDs | |
156 | * @param $values | |
157 | */ | |
6a488035 TO |
158 | function getContactInfo(&$contactIDs, &$values) { |
159 | $fields = array('sort_name' => NULL, | |
160 | 'display_name' => NULL, | |
161 | 'contact_type' => NULL, | |
162 | 'legal_identifier' => NULL, | |
163 | 'external_identifier' => NULL, | |
164 | 'source' => 'contact_source', | |
165 | ); | |
166 | getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id'); | |
167 | ||
168 | $fields = array('first_name' => NULL, | |
169 | 'last_name' => NULL, | |
170 | 'middle_name' => NULL, | |
171 | 'job_title' => NULL, | |
172 | ); | |
173 | getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id'); | |
174 | ||
175 | $fields = array('household_name' => NULL); | |
176 | getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id'); | |
177 | ||
178 | $fields = array('organization_name' => NULL, | |
179 | 'legal_name' => NULL, | |
180 | 'sic_code' => NULL, | |
181 | ); | |
182 | getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id'); | |
183 | ||
184 | $fields = array('note' => 'note_body', | |
185 | 'subject' => 'note_subject', | |
186 | ); | |
187 | getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'"); | |
188 | } | |
189 | ||
a1a55b61 EM |
190 | /** |
191 | * @param $contactIDs | |
192 | * @param $values | |
193 | */ | |
6a488035 TO |
194 | function getLocationInfo(&$contactIDs, &$values) { |
195 | $ids = implode(',', $contactIDs); | |
196 | ||
197 | $sql = " | |
198 | SELECT | |
199 | l.entity_id as contact_id, l.name as location_name, | |
200 | a.street_address, a.supplemental_address_1, a.supplemental_address_2, | |
207f62c6 | 201 | a.supplemental_address_3, |
a1a55b61 | 202 | a.city, a.postal_code, |
6a488035 TO |
203 | co.name as county, s.name as state, c.name as country, |
204 | e.email, p.phone, i.name as im | |
205 | FROM | |
206 | civicrm_location l | |
207 | LEFT JOIN civicrm_address a ON a.location_id = l.id | |
208 | LEFT JOIN civicrm_email e ON e.location_id = l.id | |
209 | LEFT JOIN civicrm_phone p ON p.location_id = l.id | |
210 | LEFT JOIN civicrm_im i ON i.location_id = l.id | |
211 | LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id | |
212 | LEFT JOIN civicrm_country c ON a.country_id = c.id | |
213 | LEFT JOIN civicrm_county co ON a.county_id = co.id | |
214 | WHERE l.entity_table = 'civicrm_contact' | |
215 | AND l.entity_id IN ( $ids ) | |
216 | "; | |
217 | ||
218 | $fields = array('location_name', 'street_address', 'supplemental_address_1', | |
207f62c6 | 219 | 'supplemental_address_2', 'supplemental_address_3', 'city', 'postal_code', 'county', 'state', |
6a488035 TO |
220 | 'country', 'email', 'phone', '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 | } | |
228 | $values[$dao->contact_id][] = array($fld, $dao->$fld); | |
229 | } | |
230 | } | |
231 | } | |
232 | ||
a1a55b61 EM |
233 | /** |
234 | * @param $contactIDs | |
235 | */ | |
6a488035 TO |
236 | function run(&$contactIDs) { |
237 | $chunks = &splitContactIDs($contactIDs); | |
238 | ||
239 | foreach ($chunks as $chunk) { | |
240 | $values = array(); | |
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 | 253 | SELECT id |
6a488035 TO |
254 | FROM civicrm_contact |
255 | EOT; | |
9d2678f4 | 256 | $dao = CRM_Core_DAO::executeQuery($sql); |
6a488035 TO |
257 | |
258 | $contactIDs = array(); | |
259 | while ($dao->fetch()) { | |
260 | $contactIDs[] = $dao->id; | |
261 | } | |
262 | ||
263 | run($contactIDs); | |
264 |