Commit 0a2ab2c9126a3ad5422258b37e6535da72eb1485

Authored by Adhidarma Hadiwinoto
1 parent 1343196a89
Exists in master

debug

Showing 2 changed files with 25 additions and 14 deletions Inline Diff

sinkronisasi.include.php
1 <?php 1 <?php
2 2
3 function sinkronisasi_get_terms($vocabulary) { 3 function sinkronisasi_get_terms($vocabulary) {
4 $entity_type = "taxonomy_term"; 4 $entity_type = "taxonomy_term";
5 5
6 $query = new EntityFieldQuery(); 6 $query = new EntityFieldQuery();
7 $query->entityCondition('entity_type', $entity_type); 7 $query->entityCondition('entity_type', $entity_type);
8 $query->propertyCondition('vid', $vocabulary->vid); 8 $query->propertyCondition('vid', $vocabulary->vid);
9 9
10 $result = $query->execute(); 10 $result = $query->execute();
11 if (!$result) { 11 if (!$result) {
12 return; 12 return;
13 } 13 }
14 14
15 $tids = array_keys($result[$entity_type]); 15 $tids = array_keys($result[$entity_type]);
16 $terms = taxonomy_term_load_multiple($tids); 16 $terms = taxonomy_term_load_multiple($tids);
17 return $terms; 17 return $terms;
18 } 18 }
19 19
20 function sinkronisasi_export_iterate($terms) { 20 function sinkronisasi_export_iterate($terms) {
21 foreach ($terms as $tid => $term) { 21 foreach ($terms as $tid => $term) {
22 unset($term->rdf_mapping); 22 unset($term->rdf_mapping);
23 $term = sinkronisasi_taxonomy_term_denormalization($term); 23 $term = sinkronisasi_taxonomy_term_denormalization($term);
24 $term = sinkronisasi_export_set_parent($term); 24 $term = sinkronisasi_export_set_parent($term);
25 25
26 $terms[$tid] = $term; 26 $terms[$tid] = $term;
27 } 27 }
28 return $terms; 28 return $terms;
29 } 29 }
30 30
31 function sinkronisasi_export_set_parent($term) { 31 function sinkronisasi_export_set_parent($term) {
32 $parent_terms = taxonomy_get_parents($term->tid); 32 $parent_terms = taxonomy_get_parents($term->tid);
33 $parent_term = array_shift($parent_terms); 33 $parent_term = array_shift($parent_terms);
34 $term->_parent = $parent_term->tid; 34 $term->_parent = $parent_term->tid;
35 35
36 return $term; 36 return $term;
37 } 37 }
38 38
39 function sinkronisasi_taxonomy_term_denormalization($term) { 39 function sinkronisasi_taxonomy_term_denormalization($term) {
40 foreach ($term as $field_name => $field_value) { 40 foreach ($term as $field_name => $field_value) {
41 41
42 $field = field_info_field($field_name); 42 $field = field_info_field($field_name);
43 43
44 44
45 if (!$field) continue; 45 if (!$field) continue;
46 46
47 if ($field['type'] == "taxonomy_term_reference") { 47 if ($field['type'] == "taxonomy_term_reference") {
48 48
49 foreach ($field_value as $language => $values_per_language) { 49 foreach ($field_value as $language => $values_per_language) {
50 foreach ($values_per_language as $idx => $value) { 50 foreach ($values_per_language as $idx => $value) {
51 51
52 if (empty($value['tid'])) { 52 if (empty($value['tid'])) {
53 continue; 53 continue;
54 } 54 }
55 55
56 $foreign_term = taxonomy_term_load($value['tid']); 56 $foreign_term = taxonomy_term_load($value['tid']);
57 if (isset($foreign_term->name)) { 57 if (isset($foreign_term->name)) {
58 $term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name; 58 $term->{"$field_name"}[$language][$idx]['_name'] = $foreign_term->name;
59 } 59 }
60 } 60 }
61 } 61 }
62 } 62 }
63 63
64 } 64 }
65 return $term; 65 return $term;
66 } 66 }
67 67
68 function sinkronisasi_delete_taxonomy_term_field_type($term) { 68 function sinkronisasi_delete_taxonomy_term_field_type($term) {
69 foreach ($term as $field_name => $field_value) { 69 foreach ($term as $field_name => $field_value) {
70 70
71 $field = field_info_field($field_name); 71 $field = field_info_field($field_name);
72 72
73 73
74 if (!$field) continue; 74 if (!$field) continue;
75 75
76 if ($field['type'] == "taxonomy_term_reference") { 76 if ($field['type'] == "taxonomy_term_reference") {
77 unset($term->{"$field_name"}); 77 unset($term->{"$field_name"});
78 } 78 }
79 } 79 }
80 return $term; 80 return $term;
81 } 81 }
82 82
83 function sinkronisasi_taxonomy_term_normalization($term) { 83 function sinkronisasi_taxonomy_term_normalization($term) {
84 foreach ($term as $field_name => $field_value) { 84 foreach ($term as $field_name => $field_value) {
85 85
86 $field = field_info_field($field_name); 86 $field = field_info_field($field_name);
87 87
88 88
89 if (!$field) continue; 89 if (!$field) continue;
90 90
91 if ($field['type'] == "taxonomy_term_reference") { 91 if ($field['type'] == "taxonomy_term_reference") {
92 92
93 foreach ($field_value as $language => $values_per_language) { 93 foreach ($field_value as $language => $values_per_language) {
94 foreach ($values_per_language as $idx => $value) { 94 foreach ($values_per_language as $idx => $value) {
95 95
96 if (empty($value['_name'])) { 96 if (empty($value['_name'])) {
97 continue; 97 continue;
98 } 98 }
99 99
100 $foreign_terms = taxonomy_get_term_by_name($value['_name']); 100 $foreign_terms = taxonomy_get_term_by_name($value['_name']);
101 $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms)); 101 $foreign_terms = taxonomy_term_load_multiple(array_keys($foreign_terms));
102 $foreign_term = array_shift($foreign_terms); 102 $foreign_term = array_shift($foreign_terms);
103 103
104 if (isset($foreign_term->name)) { 104 if (isset($foreign_term->name)) {
105 if ($foreign_term->name != $value['_name']) { 105 if ($foreign_term->name != $value['_name']) {
106 echo $field_name . "\n";
106 echo $foreign_term->name . " != " . $value['_name'] . "\n"; 107 echo $foreign_term->name . " != " . $value['_name'] . "\n";
107 drush_die("213AB2 foreign term name is not match!"); 108 drush_die("213AB2 foreign term name is not match!");
108 } 109 }
109 110
110 $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid; 111 $term->{"$field_name"}[$language][$idx]['tid'] = $foreign_term->tid;
111 } 112 }
112 } 113 }
113 } 114 }
114 } 115 }
115 116
116 } 117 }
117 return $term; 118 return $term;
118 } 119 }
119 120
120 121
121 function sinkronisasi_fix_vid($term) { 122 function sinkronisasi_fix_vid($term) {
122 $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); 123 $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name);
123 if (!$vocabulary) { 124 if (!$vocabulary) {
124 var_dump($term); 125 var_dump($term);
125 echo "\n"; 126 echo "\n";
126 drush_die("CD231A vocabulary not found!"); 127 drush_die("CD231A vocabulary not found!");
127 } 128 }
128 129
129 $term->vid = $vocabulary->vid; 130 $term->vid = $vocabulary->vid;
130 return $term; 131 return $term;
131 } 132 }
132 133
133 function sinkronisasi_import($terms) { 134 function sinkronisasi_import($terms) {
134 sinkronisasi_import_iterate_insert($terms); 135 sinkronisasi_import_iterate_insert($terms);
135 sinkronisasi_import_iterate_update($terms); 136 sinkronisasi_import_iterate_update($terms);
136 } 137 }
137 138
138 function sinkronisasi_import_iterate_insert($terms) { 139 function sinkronisasi_import_iterate_insert($terms) {
140 $i = 0;
139 foreach ($terms as $tid => $term) { 141 foreach ($terms as $tid => $term) {
142 $i++;
140 143
141 echo "$tid: " . $term->name . "\n"; 144 echo "$i# ORIGINAL $tid: " . $term->name . "\n";
142 145
143 $term = sinkronisasi_fix_vid($term); 146 $term = sinkronisasi_fix_vid($term);
144 $term = sinkronisasi_delete_taxonomy_term_field_type($term); 147 $term = sinkronisasi_delete_taxonomy_term_field_type($term);
145 $term = sinkronisasi_set_field_original_tid($term); 148 $term = sinkronisasi_set_field_original_tid($term);
146 149
147 unset($term->tid); 150 unset($term->tid);
148 taxonomy_term_save($term); 151 taxonomy_term_save($term);
149 echo "TID $tid inserted: " . $term->tid . "\n"; 152 echo "ORIGINAL: $tid LOCAL: " . $term->tid . " inserted.\n";
150 153
151 } 154 }
152 } 155 }
153 156
154 function sinkronisasi_import_iterate_update($terms) { 157 function sinkronisasi_import_iterate_update($terms) {
158 $i = 0;
155 foreach ($terms as $tid => $term) { 159 foreach ($terms as $tid => $term) {
160 $i++;
156 161
157 echo "$tid: " . $term->name . "\n"; 162 echo "$i# $tid: " . $term->name . "\n";
158 163
159 $term = sinkronisasi_fix_vid($term); 164 $term = sinkronisasi_fix_vid($term);
160 $term = sinkronisasi_taxonomy_term_normalization($term); 165 $term = sinkronisasi_taxonomy_term_normalization($term);
161 $term = sinkronisasi_set_field_original_tid($term); 166 $term = sinkronisasi_set_field_original_tid($term);
162 $term = sinkronisasi_pair_with_local_tid($term); 167 $term = sinkronisasi_pair_with_local_tid($term);
163 $term = sinkronisasi_pair_with_parent($term); 168 $term = sinkronisasi_pair_with_parent($term);
164 169
165 taxonomy_term_save($term); 170 taxonomy_term_save($term);
166 echo "TID $tid updated: " . $term->tid . "\n"; 171 echo "ORIGINAL: $tid LOCAL: " . $term->tid . " updated.\n";
167 172
168 } 173 }
169 } 174 }
170 175
171 function sinkronisasi_pair_with_local_tid($term) { 176 function sinkronisasi_pair_with_local_tid($term) {
172 $tid = $term->tid; 177 $tid = $term->tid;
173 $original_term = sinkronisasi_get_term_by_field_original_tid($tid); 178 $original_term = sinkronisasi_get_term_by_field_original_tid($tid);
174 if (!$original_term) { 179 if (!$original_term) {
175 drush_die("ED0129 Error getting term with original tid is $tid!\n"); 180 drush_die("ED0129 Error getting term with original tid is $tid!\n");
176 } 181 }
177 182
178 $term->tid = $original_term->tid; 183 $term->tid = $original_term->tid;
179 return $term; 184 return $term;
180 } 185 }
181 186
182 function sinkronisasi_pair_with_parent($term) { 187 function sinkronisasi_pair_with_parent($term) {
183 if ($term->_parent) { 188 if ($term->_parent) {
184 $term->parent = $term->_parent; 189 $term->parent = $term->_parent;
185 unset($term->_parent); 190 unset($term->_parent);
186 } 191 }
187 192
188 return $term; 193 return $term;
189 } 194 }
190 195
191 function sinkronisasi_get_term_by_field_original_tid($tid) { 196 function sinkronisasi_get_term_by_field_original_tid($tid) {
192 $entity_type = "taxonomy_term"; 197 $entity_type = "taxonomy_term";
193 198
194 $query = new EntityFieldQuery(); 199 $query = new EntityFieldQuery();
195 $query->entityCondition('entity_type', $entity_type); 200 $query->entityCondition('entity_type', $entity_type);
196 $query->fieldCondition('field_original_tid', 'value', $tid); 201 $query->fieldCondition('field_original_tid', 'value', $tid);
197 202
198 $result = $query->execute(); 203 $result = $query->execute();
199 if (!$result) { 204 if (!$result) {
200 return; 205 return;
201 } 206 }
202 207
203 $tids = array_keys($result[$entity_type]); 208 $tids = array_keys($result[$entity_type]);
204 $terms = taxonomy_term_load_multiple($tids); 209 $terms = taxonomy_term_load_multiple($tids);
205 210
206 $term = array_shift($terms); 211 $term = array_shift($terms);
207 return $term; 212 return $term;
208 } 213 }
209 214
210 function sinkronisasi_set_field_original_tid($term) { 215 function sinkronisasi_set_field_original_tid($term) {
211 $term->field_original_tid['und'][0]['value'] = $term->tid; 216 $term->field_original_tid['und'][0]['value'] = $term->tid;
212 return $term; 217 return $term;
213 } 218 }
214 219
215 function sinkronisasi_export($vocabulary_machine_name) { 220 function sinkronisasi_export($vocabulary_machine_name) {
216 $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name); 221 $vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
217 $terms = sinkronisasi_get_terms($vocabulary); 222 $terms = sinkronisasi_get_terms($vocabulary);
218 $terms = sinkronisasi_export_iterate($terms); 223 $terms = sinkronisasi_export_iterate($terms);
219 224
220 return $terms; 225 return $terms;
221 } 226 }
222 227
223 function delete_all_terms_by_vid($vid) { 228 function delete_all_terms_by_vid($vid) {
224 229
225 $query = new EntityFieldQuery(); 230 $query = new EntityFieldQuery();
226 $query->entityCondition('entity_type', 'taxonomy_term'); 231 $query->entityCondition('entity_type', 'taxonomy_term');
227 232
228 if (!$vid) { 233 if (!$vid) {
229 drush_die("Unknown vid"); 234 drush_die("Unknown vid");
230 } 235 }
231 236
232 $query->propertyCondition('vid', $vid); 237 $query->propertyCondition('vid', $vid);
233 $result = $query->execute(); 238 $result = $query->execute();
234 if (!$result) { 239 if (!$result) {
235 return; 240 return;
236 } 241 }
237 242
238 foreach ($result['taxonomy_term'] as $term) { 243 foreach ($result['taxonomy_term'] as $term) {
239 echo "Deleting " . $term->tid . " " . $term->name; 244 echo "Deleting " . $term->tid . " " . $term->name;
240 echo ", status: "; 245 echo ", status: ";
241 echo taxonomy_term_delete($term->tid); 246 echo taxonomy_term_delete($term->tid);
242 echo "\n"; 247 echo "\n";
243 } 248 }
244 } 249 }
245 250
sinkronisasi_import.php
1 <?php 1 <?php
2 2
3 require("sinkronisasi.include.php"); 3 require("sinkronisasi.include.php");
4 4
5 if (function_exists('drush_main')) { 5 if (function_exists('drush_main')) {
6 $do_insert = FALSE;
7 $do_update = TRUE;
8
6 $machine_names = array("jenis_kantor", "kantor"); 9 $machine_names = array("jenis_kantor", "kantor");
7 foreach ($machine_names as $machine_name) { 10 foreach ($machine_names as $machine_name) {
8 11
9 $vocabulary = taxonomy_vocabulary_machine_name_load($machine_name); 12 $vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
10
11 echo "Delete data\n";
12 delete_all_terms_by_vid($vocabulary->vid);
13
14 $data = file_get_contents("adhisimon/sinkronisasi-drupal/" . $machine_name . ".txt"); 13 $data = file_get_contents("adhisimon/sinkronisasi-drupal/" . $machine_name . ".txt");
15 14
16 echo "Insert new data\n"; 15 if ($do_insert) {
17 $terms = unserialize($data); 16 echo "Delete data\n";
18 sinkronisasi_import_iterate_insert($terms); 17 delete_all_terms_by_vid($vocabulary->vid);
18
19 echo "Insert new data\n";
20 $terms = unserialize($data);
21 sinkronisasi_import_iterate_insert($terms);
22 }
19 23
20 echo "Update data\n"; 24 if ($do_update) {
21 $terms = unserialize($data); 25 echo "Update data\n";
22 sinkronisasi_import_iterate_update($terms); 26 $terms = unserialize($data);
27 sinkronisasi_import_iterate_update($terms);