Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.1 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2011 | | |
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 | function getTableInfo(&$contactIDs, &$values, $tableName, &$fields, $whereField, $additionalWhereCond = NULL) { | |
112 | $selectString = implode(',', array_keys($fields)); | |
113 | $idString = implode(',', $contactIDs); | |
114 | ||
115 | $sql = " | |
116 | SELECT $selectString, $whereField as contact_id | |
117 | FROM $tableName | |
118 | WHERE $whereField IN ( $idString ) | |
119 | "; | |
120 | ||
121 | if ($additionalWhereCond) { | |
122 | $sql .= " AND $additionalWhereCond"; | |
123 | } | |
124 | ||
125 | $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); | |
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 | } | |
134 | $values[$dao->contact_id][] = array($name, $dao->$fld); | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | function getContactInfo(&$contactIDs, &$values) { | |
140 | $fields = array('sort_name' => NULL, | |
141 | 'display_name' => NULL, | |
142 | 'contact_type' => NULL, | |
143 | 'legal_identifier' => NULL, | |
144 | 'external_identifier' => NULL, | |
145 | 'source' => 'contact_source', | |
146 | ); | |
147 | getTableInfo($contactIDs, $values, 'civicrm_contact', $fields, 'id'); | |
148 | ||
149 | $fields = array('first_name' => NULL, | |
150 | 'last_name' => NULL, | |
151 | 'middle_name' => NULL, | |
152 | 'job_title' => NULL, | |
153 | ); | |
154 | getTableInfo($contactIDs, $values, 'civicrm_individual', $fields, 'contact_id'); | |
155 | ||
156 | $fields = array('household_name' => NULL); | |
157 | getTableInfo($contactIDs, $values, 'civicrm_household', $fields, 'contact_id'); | |
158 | ||
159 | $fields = array('organization_name' => NULL, | |
160 | 'legal_name' => NULL, | |
161 | 'sic_code' => NULL, | |
162 | ); | |
163 | getTableInfo($contactIDs, $values, 'civicrm_organization', $fields, 'contact_id'); | |
164 | ||
165 | $fields = array('note' => 'note_body', | |
166 | 'subject' => 'note_subject', | |
167 | ); | |
168 | getTableInfo($contactIDs, $values, 'civicrm_note', $fields, 'entity_id', "entity_table = 'civicrm_contact'"); | |
169 | } | |
170 | ||
171 | function getLocationInfo(&$contactIDs, &$values) { | |
172 | $ids = implode(',', $contactIDs); | |
173 | ||
174 | $sql = " | |
175 | SELECT | |
176 | l.entity_id as contact_id, l.name as location_name, | |
177 | a.street_address, a.supplemental_address_1, a.supplemental_address_2, | |
178 | a.city, a.postal_code, | |
179 | co.name as county, s.name as state, c.name as country, | |
180 | e.email, p.phone, i.name as im | |
181 | FROM | |
182 | civicrm_location l | |
183 | LEFT JOIN civicrm_address a ON a.location_id = l.id | |
184 | LEFT JOIN civicrm_email e ON e.location_id = l.id | |
185 | LEFT JOIN civicrm_phone p ON p.location_id = l.id | |
186 | LEFT JOIN civicrm_im i ON i.location_id = l.id | |
187 | LEFT JOIN civicrm_state_province s ON a.state_province_id = s.id | |
188 | LEFT JOIN civicrm_country c ON a.country_id = c.id | |
189 | LEFT JOIN civicrm_county co ON a.county_id = co.id | |
190 | WHERE l.entity_table = 'civicrm_contact' | |
191 | AND l.entity_id IN ( $ids ) | |
192 | "; | |
193 | ||
194 | $fields = array('location_name', 'street_address', 'supplemental_address_1', | |
195 | 'supplemental_address_2', 'city', 'postal_code', 'county', 'state', | |
196 | 'country', 'email', 'phone', 'im', | |
197 | ); | |
198 | $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); | |
199 | while ($dao->fetch()) { | |
200 | foreach ($fields as $fld) { | |
201 | if (empty($dao->$fld)) { | |
202 | continue; | |
203 | } | |
204 | $values[$dao->contact_id][] = array($fld, $dao->$fld); | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
209 | function run(&$contactIDs) { | |
210 | $chunks = &splitContactIDs($contactIDs); | |
211 | ||
212 | foreach ($chunks as $chunk) { | |
213 | $values = array(); | |
214 | getValues($chunk, $values); | |
215 | $xml = &generateSolrXML($values); | |
216 | echo $xml; | |
217 | } | |
218 | } | |
219 | ||
220 | $config = &CRM_Core_Config::singleton(); | |
221 | $config->userFramework = 'Soap'; | |
222 | $config->userFrameworkClass = 'CRM_Utils_System_Soap'; | |
223 | $config->userHookClass = 'CRM_Utils_Hook_Soap'; | |
224 | ||
225 | $sql = <<<EOT | |
226 | SELECT id | |
227 | FROM civicrm_contact | |
228 | EOT; | |
229 | $dao = &CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); | |
230 | ||
231 | $contactIDs = array(); | |
232 | while ($dao->fetch()) { | |
233 | $contactIDs[] = $dao->id; | |
234 | } | |
235 | ||
236 | run($contactIDs); | |
237 |