forked from Upstatement/jigsaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjigsaw-import.php
417 lines (368 loc) · 11.9 KB
/
jigsaw-import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/*
Plugin Name: Jigsaw Importer
Plugin URI: http://jigsaw.upstatement.com
Author: Jared Novack
Version: 0.3
*/
class JigsawPostImporter {
var $fields = array();
var $metas = array();
var $queries = array();
var $taxes = array();
var $table;
function __construct($posts_table){
$this->table = $posts_table;
$this->add_imported_col();
}
function wipe(){
global $wpdb;
$query = "UPDATE $this->table SET imported = NULL";
$wpdb->query($query);
echo 'my query: '.$query;
$query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'oldid'";
$pids = $wpdb->get_col($query);
$pid_string = implode(',', $pids);
echo 'pid: '.$pid_string;
if($pid_string){
$query = "DELETE FROM $wpdb->posts WHERE ID IN ($pid_string)";
$wpdb->query($query);
$query = "DELETE FROM $wpdb->postmeta WHERE post_id IN ($pid_string)";
$wpdb->query($query);
$query = "DELETE FROM $wpdb->term_relationships WHERE object_id IN ($pid_string)";
$wpdb->query($query);
}
$query='DELETE FROM wp_posts WHERE post_type = "revision"'; // remove extraneous revisions
$wpdb->query($query);
$max = $wpdb->get_var("SELECT ID FROM $wpdb->posts ORDER BY ID DESC LIMIT 1");
$max = intval($max) + 1;
$query = "ALTER TABLE $wpdb->posts AUTO_INCREMENT = $max";
echo $query;
$wpdb->query($query);
$max = $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy ORDER BY term_taxonomy_id DESC LIMIT 1");
$max = intval($max) + 1;
$query = "ALTER TABLE $wpdb->term_taxonomy AUTO_INCREMENT = $max";
echo $query;
$wpdb->query($query);
}
function add_imported_col(){
if(!$this->has_imported_col()){
global $wpdb;
$query = "ALTER TABLE $this->table ADD COLUMN imported tinyint(1)";
$wpdb->query($query);
}
}
function has_imported_col(){
$fields = mysql_list_fields(DB_NAME, $this->table);
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {$field_array[] = mysql_field_name($fields, $i);}
if (in_array('imported', $field_array)) {
return true;
}
return false;
}
function reset_imports(){
$query = "UPDATE $this->table SET imported = 0 WHERE imported = 1";
}
function set_posts_table($table){
$this->posts_table = 'articles';
}
function set_field($field, $db_col){
$this->fields[$field] = $db_col;
}
function set_meta($field, $db_col){
$this->metas[$field] = $db_col;
}
function set_taxonomy($tax){
$this->taxes[] = $tax;
}
function add_query($callback){
$this->queries[] = $callback;
}
function import($count = 10, $id = null){
global $wpdb;
$and = '';
if ($id){
$query = "SELECT * FROM $this->table WHERE id = '$id' LIMIT 1";
} else {
$query = "SELECT * FROM $this->table WHERE (imported IS NULL OR imported = 0) LIMIT $count";
}
$results = $wpdb->get_results($query);
if (!count($results)){
echo 'NO MORE IMPORTS LEFT!';
}
$queries = array();
foreach($results as $row){
$post = array('post_status' => 'publish');
foreach($this->fields as $field => $db_col){
if (isset($row->$db_col)){
$clear = strip_tags(html_entity_decode($row->$db_col));
if ($field == 'post_date'){
$clear = strtotime($clear);
$clear = date("Y-m-d H:i:s", $clear);
}
$post[$field] = $clear;
} else {
echo '</br>why are you calling '.$db_col.'?</br> for field '.$field.'</br>';
}
}
$post['post_name'] = sanitize_title($post['post_title']);
$pid = wp_insert_post($post);
foreach($this->metas as $meta_field => $meta_db_col){
update_post_meta($pid, $meta_field, $row->$meta_db_col);
}
update_post_meta($pid, 'imported', true);
foreach($this->taxes as $taxonomy){
$taxonomy->import($pid, $row);
}
foreach($this->queries as $query){
$query($pid, $row);
}
if ($pid){
$queries[] = "UPDATE $this->table SET imported = 1 WHERE id = $row->id LIMIT 1";
}
wp_remove_object_terms($pid, 1, 'category');
}
foreach($queries as $query){
$wpdb->query($query);
}
/* Convert ISO chars to UTF-8 after DB import */
$columns = array("post_title", "post_excerpt", "post_name", "post_content");
foreach($columns as $col) {
$queries = array();
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '“', '“')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, 'â€', '”')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '’', '’')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '‘', '‘')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '—', '–')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '–', '—')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '•', '-')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '…', '…')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, 'â??', '’')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, 'â?', '’')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, 'éjÃ', 'é')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '§', '§')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, '', '')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, 'âS', '’S')";
$queries[] = "UPDATE wp_posts SET $col = REPLACE($col, 'Sâ', 'S’')";
foreach($queries as $query){
$wpdb->query($query);
}
}
//$query='DELETE FROM wp_posts WHERE post_type = "revision"'; // remove extraneous revisions
}
}
class JigsawMetaImporter {
var $join_data;
function set_join_table($post_col, $tax_col, $tax_table){
$this->join_data = new stdClass();
$this->join_data->post_col = $post_col;
$this->join_data->tax_col = $tax_col;
$this->join_data->tax_table = $tax_table;
}
}
class JigsawTaxonomyImporter {
var $tax_name;
var $metas = array();
var $fields = array();
var $queries = array();
var $simple_col;
var $join_data;
var $pivot_data;
var $tax_data;
function __construct($tax_name){
if ($tax_name == 'tag'){
$tax_name = 'post_tag';
}
$this->tax_name = $tax_name;
if ($tax_name != 'category' && $tax_name != 'post_tag'){
add_action('init', function() use ($tax_name){
$this->register($tax_name);
});
}
}
function run_queries($tid, $data){
//echo 'run queries for '.$tid;
if (!is_array($this->queries)){
return;
}
foreach($this->queries as $query){
//echo 'query!';
$query($tid, $data);
}
}
function import_from_col($pid, $row){
$col = $this->simple_col;
if (!isset($row->$col)){
return;
}
$term = trim($row->$col);
if (!strlen($term)){
return;
}
$split = ' and ';
if (strstr($term, ',')){
$split = ', ';
}
$terms = explode($split, $term);
$words_to_erase = array("/\band\b/ ","/\b&\b/");
foreach($terms as $term){
$term = preg_replace($words_to_erase, "", $term);
$term = trim($term);
if (isset($term) && strlen($term)){
$tid = $this->insert_term($pid, $term);
$this->run_queries($tid, $row);
}
}
}
function insert_term($pid, $term_name){
$tid = term_exists($term_name, $this->tax_name);
if ($tid && isset($tid['term_id'])){
$tid = $tid['term_id'];
} else {
$tid = wp_insert_term($term_name, $this->tax_name);
if (is_array($tid) && isset($tid['term_id'])){
$tid = $tid['term_id'];
} else {
echo '</br>I AM AN ERROR';
echo 'tried to insert '.$term_name.' in '.$this->tax_name;
echo 'tid'.($tid);
}
}
$tid = intval($tid);
wp_set_object_terms($pid, $tid, $this->tax_name, true);
return $tid;
}
function import_from_pivot($pid, $row){
global $wpdb;
$pd = $this->pivot_data;
$td = $this->tax_data;
$id = $row->id;
$pivot_query = "SELECT * FROM $pd->pivot_table WHERE $pd->post_col = '$id'";
$pivots = $wpdb->get_results($pivot_query);
$pivot_tax_column = $pd->tax_col;
foreach($pivots as $pivot){
$tax_id = $pivot->$pivot_tax_column;
$term_data_query = "SELECT $td->name_col FROM $td->tax_table WHERE $td->tax_col = '$tax_id' LIMIT 1";
$name = $wpdb->get_var($term_data_query);
if (is_numeric($name)){
echo 'why you a number?'.$name;
}
$this->insert_term($pid, trim($name));
}
}
function import_from_join($pid, $row){
global $wpdb;
$id_col = $this->join_data->post_col;
if (isset($row->$id_col) && strlen($row->$id_col)){
$id = $row->$id_col;
}
$jd = $this->join_data;
$term_query = "SELECT * FROM $jd->tax_table WHERE $jd->tax_col = '$id' LIMIT 1";
$term_row = $wpdb->get_row($term_query);
if (isset($this->fields['name'])){
$name_col = $this->fields['name'];
}
if (isset($term_row->$name_col)){
$name = $term_row->$name_col;
if (!strlen($name)){
$name = 'Volume '.$term_row->issue_volume.' - Issue '.$term_row->issue_number;
}
$tid = $this->insert_term($pid, $name);
$this->run_queries($tid, $term_row);
}
}
function wipe($floor = 50){
global $wpdb;
$tids = "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id > $floor AND taxonomy = '$this->tax_name'";
$tids = $wpdb->get_col($tids);
foreach($tids as $tid){
wp_delete_term( $tid, $this->tax_name);
}
$key = $this->tax_name.'_';
try {
$meta = "DELETE FROM $wpdb->options WHERE option_name LIKE '$key%'";
$wpdb->query($meta);
} catch (Exception $e){
$meta = "DELETE FROM $wpdb->options WHERE option_name LIKE '$key%'";
$wpdb->query($meta);
}
$key = '_'.$key;
$meta = "DELETE FROM $wpdb->options WHERE option_name LIKE '$key'";
$wpdb->query($meta);
}
function add_query($query){
$this->queries[] = $query;
}
function import($pid, $row){
$tid;
if (isset($this->simple_col)){
$tid = $this->import_from_col($pid, $row);
}
if (isset($this->join_data)){
$this->import_from_join($pid, $row);
}
if (isset($this->pivot_data) && isset($this->tax_data)){
$this->import_from_pivot($pid, $row);
}
}
function set_col($post_col){
$this->simple_col = $post_col;
}
function set_meta($key, $db_col){
$this->metas[$key] = $db_col;
}
function set_field($field, $db_col){
$this->fields[$field] = $db_col;
}
function set_pivot_table($pivot_table, $post_col, $tax_col){
$this->pivot_data = new stdClass();
$this->pivot_data->post_col = $post_col;
$this->pivot_data->tax_col = $tax_col;
$this->pivot_data->pivot_table = $pivot_table;
}
function set_tax_table($tax_table, $tax_col, $name_col){
$this->tax_data = new stdClass();
$this->tax_data->tax_col = $tax_col;
$this->tax_data->tax_table = $tax_table;
$this->tax_data->name_col = $name_col;
}
function set_join_table($post_col, $tax_col, $tax_table){
$this->join_data = new stdClass();
$this->join_data->post_col = $post_col;
$this->join_data->tax_col = $tax_col;
$this->join_data->tax_table = $tax_table;
}
function register($tax_name){
if ($tax_name == 'post_tag' || $tax_name == 'category'){
return;
}
if (taxonomy_exists($tax_name)){
return;
}
$args = array('label' => ucwords($tax_name) );
register_taxonomy($tax_name, 'post', $args);
}
}
class JigsawUserTransformer extends JigsawTaxonomyImporter {
function __construct($tax_name = 'authors'){
parent::__construct($tax_name);
global $wpdb;
$this->fields['name'] = 'display_name';
$this->set_col_table('post_author', 'ID', $wpdb->users);
}
function run_transform($count = 50, $post_type = 'post'){
global $wpdb;
$query = "SELECT * FROM nmhvd_posts WHERE post_author != 1 AND post_type = '$post_type' LIMIT $count";
$rows = $wpdb->get_results($query);
$pids = array();
foreach ($rows as $row){
$this->import_from_join($row->ID, $row);
$pids[] = $row->ID;
}
$pid_string = implode(', ', $pids);
$finish = "UPDATE nmhvd_posts SET post_author = 1 WHERE ID IN ($pid_string)";
echo $finish;
$wpdb->query($finish);
}
}