This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexport-database.php
1276 lines (1142 loc) · 44.2 KB
/
export-database.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if (php_sapi_name() != 'cli') {
die("Only cli execution is allowed.");
}
$debug = false;
require_once("wp-includes/formatting.php");
$configuration = parse_ini_file("config.ini", true);
// ** MySQL settings - You can get this info from your web host ** //
$db_configuration = $configuration['database'];
/** The name of the database for WordPress */
define('DB_NAME', $db_configuration['name']);
/** MySQL database username */
define('DB_USER', $db_configuration['user']);
/** MySQL database password */
define('DB_PASSWORD', $db_configuration['password']);
/** MySQL hostname */
define('DB_HOST', $db_configuration['host']);
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', $db_configuration['charset']);
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', $db_configuration['collate']);
$table_prefix = 'wp_';
define('REPL_DOMAIN_OLD', "cms.integreat-app.de");
define('REPL_DOMAIN_NEW', "cms-dev.integreat-app.de");
define('REPL_PATH_OLD', "/");
define('REPL_PATH_NEW', "/");
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$slug_map_file = tempnam('tmp', 'integreat-export-');
fwrite(STDERR, "Slug Mapping File: ". $slug_map_file . "\n");
$slug_map_file_resource = fopen($slug_map_file, 'w');
fputs($slug_map_file_resource, "Region Slug;Page ID;Translation ID;Language ID;Version;WP Slug\n");
function now() {
$objDateTime = new DateTime('NOW');
return $objDateTime->format('c');
}
function slugify( $string ) {
return str_replace( ["!", "#", "&", "'", "(", ")", "*", "*", "+", ",", "/", ":", ";", "=", "?", "@", "[", "]"], "", urldecode($string) );
}
function map_status ( $status ) {
if ( $status == "auto-draft" || $status == "draft" )
return "DRAFT";
elseif ( $status == "private" || $status == "trash" )
return "REVIEW";
elseif ( $status == "publish" )
return "PUBLIC";
else
return null;
}
class MPTT {
function __construct( $pk_offset = null, $id = null ) {
$this->id = $id;
$this->tree = array();
if ( $pk_offset ) {
$this->pk_counter = $pk_offset;
} else {
$this->pk_counter = 0;
}
}
function add_node( $node_id, $parent_node_id = null ) {
if ( sizeof( $this->tree ) == 0 && $parent_node_id == null ) {
$this->tree[] = array( "id" => $node_id, "parent" => null, "parent_pk" => null, "left" => 1, "right" => 2, "level" => 1, "pk" => $this->pk_counter );
} elseif ( sizeof( $this->tree) > 0 && $parent_node_id ) {
$parent = $this->get_parent( $parent_node_id );
$this->increase_counts( $parent["right"] );
$this->tree[] = array( "id" => $node_id, "parent" => $parent_node_id, "parent_pk" => $parent["pk"], "left" => $parent["right"], "right" => ( $parent["right"] + 1 ), "level" => $parent["level"] + 1, "pk" => $this->pk_counter );
} else {
return false;
}
$this->pk_counter++;
return true;
}
function get_parent( $parent_node_id ) {
for ( $i = 0; $i < sizeof( $this->tree ); $i++ ) {
if ( $this->tree[$i]["id"] == $parent_node_id ) {
return $this->tree[$i];
}
}
}
function increase_counts( $num ) {
for ( $i = 0; $i < sizeof( $this->tree ); $i++ ) {
if ( $this->tree[$i]["left"] >= $num ) {
$this->tree[$i]["left"] = $this->tree[$i]["left"] + 2;
}
if ( $this->tree[$i]["right"] >= $num ) {
$this->tree[$i]["right"] = $this->tree[$i]["right"] + 2;
}
}
}
function get_node( $node_id ) {
foreach ( $this->tree as $key => $node ) {
if ( $node["id"] == $node_id ) {
return $node;
}
}
}
}
abstract class DjangoModel {
public $pk;
public $fields = array();
public static $pk_counter = array();
function __construct( $pk = null ) {
if ( !array_key_exists( $this->model, self::$pk_counter ) )
self::$pk_counter[$this->model] = 1;
if ( $pk == null )
$this->pk = self::$pk_counter[$this->model];
else
$this->pk = $pk;
self::$pk_counter[$this->model] ++;
}
function is_valid() {
foreach ( $fields as $item ) {
if ( $item == null )
return false;
}
return true;
}
}
class User extends DjangoModel {
public $model = "cms.user";
function __construct( $user, $blog_roles ) {
parent::__construct( (int)$user->ID );
$this->init_fields( $user, $blog_roles );
}
function init_fields( $user, $blog_roles ) {
$group = $this->select_role( $this->combine_roles( $blog_roles ));
$regions = $this->filter_regions( array_keys( $blog_roles ) );
$this->fields["username"] = $user->user_login;
$this->fields["password"] = "bcrypt_php$" . $user->user_pass;
$this->fields["email"] = $user->user_email;
$this->fields["first_name"] = "";
$this->fields["last_name"] = "";
$this->fields["is_superuser"] = False;
$this->fields["is_staff"] = False;
$this->fields["is_active"] = (sizeof($regions) == 0 ? False : True);
$this->fields["groups"] = ( is_null( $group ) ? array(2) : array( $group ) );
$this->fields["regions"] = $regions;
$this->fields["expert_mode"] = ( $group === 1 ? true : false ); // turn on if Verwalter
}
static function filter_regions( $regions ) {
global $existing_regions;
$result = [];
foreach ( $regions as $region ) {
if ( $region === 0 || !in_array( $region, $existing_regions ) ) {
// pass
} else {
$result[] = $region;
}
}
return $result;
}
static function combine_roles( $blog_roles ) {
$all_roles = array();
foreach ( $blog_roles as $blog => $roles ) {
$all_roles = array_merge( $all_roles, $roles );
}
return $all_roles;
}
static function select_role( $roles ) {
if ( array_key_exists( "manager", $roles )) {
return 1; // -> Role MANAGEMENT
} elseif ( array_key_exists( "trustworthy_organization", $roles )) {
return 2; // -> Role EDITOR
} elseif ( array_key_exists( "event_planer", $roles )) {
return 3; // -> Role EVENT_MANAGER
} elseif ( array_key_exists( "organizer", $roles )) {
return 8; // -> Role AUTHOR
}
return null;
}
}
class Region extends DjangoModel {
public $model = "cms.region";
function __construct( $blog ) {
parent::__construct( (int)$blog->blog_id );
$this->init_fields( $blog );
}
function init_fields( $blog ) {
$this->fields["name"] = ( $blog->get_integreat_setting( "name_without_prefix" ) ? $blog->get_integreat_setting( "name_without_prefix" ) : $blog->get_blog_option( "blogname" ) );
$this->fields["slug"] = ( $blog->slug );
$this->fields["aliases"] = ( $blog->get_integreat_setting( "aliases" ) != null ? $blog->get_integreat_setting( "aliases" ) : array() );
$this->fields["status"] = ( $blog->get_integreat_setting( "disabled" ) == 1 ? "ARCHIVED" : ( $blog->get_integreat_setting( "hidden" ) == 0 ? "ACTIVE" : "HIDDEN" ) );
$this->fields["latitude"] = $blog->get_integreat_setting( "latitude" );
$this->fields["longitude"] = $blog->get_integreat_setting( "longitude" );
$this->fields["postal_code"] = ( $blog->get_integreat_setting( "plz" ) != null ? $blog->get_integreat_setting( "plz" ) : 1);
$this->fields["administrative_division"] = $this->get_administrative_division( $blog->get_blog_option( "blogname" ), $blog->get_integreat_setting( "prefix" ) );
$this->fields["administrative_division_included"] = ( $blog->get_integreat_setting( "prefix" ) !== "" );
$this->fields["events_enabled"] = true;
$this->fields["chat_enabled"] = true;
$this->fields["push_notifications_enabled"] = ( $blog->get_integreat_setting( "push_notifications" ) == 1 ? true : false );
$this->fields["admin_mail"] = "info@integreat-app.de";
$this->fields["created_date"] = now();
$this->fields["last_updated"] = now();
$this->fields["statistics_enabled"] = ( strpos( $blog->get_blog_option( "active_plugins" ), "wp-piwik" ) ? true : false );
$this->fields["matomo_token"] = ( $blog->get_blog_option( "wp-piwik_global-piwik_token" ) != null ? $blog->get_blog_option( "wp-piwik_global-piwik_token" ) : "");
$this->fields["matomo_id"] = ( in_array( $blog->get_blog_option( "wp-piwik-site_id" ) , ["n/a", null, "", "0"] ) ? null : $blog->get_blog_option( "wp-piwik-site_id" ) );
$this->fields["offers"] = $blog->get_blog_extras();
}
function get_administrative_division( $name, $prefix ) {
switch ($prefix) {
case "Landkreis":
$administrative_division = "RURAL_DISTRICT";
break;
case "Kreis":
$administrative_division = "DISTRICT";
break;
case "Stadt":
$administrative_division = "CITY";
break;
case "Stadt und Landkreis":
$administrative_division = "CITY_AND_DISTRICT";
break;
case "Region":
$administrative_division = "REGION";
break;
default:
if (stripos($name, "kreis") !== false) {
$administrative_division = "RURAL_DISTRICT";
} else {
$administrative_division = "MUNICIPALITY";
}
}
return $administrative_division;
}
}
class OfferTemplate extends DjangoModel {
public $model = "cms.offertemplate";
function __construct( $offer ) {
$this->pk = $offer->id;
$this->init_fields( $offer );
}
function init_fields( $offer ) {
$this->fields = array(
"name"=> $offer->name,
"slug"=> slugify($offer->alias),
"thumbnail"=> $offer->thumbnail,
"url"=> $offer->url,
"post_data"=> (is_null($offer->post) ? "" : $offer->post ),
"use_postal_code"=> "GET",
"created_date"=> now(),
"last_updated"=> now()
);
}
}
class Language extends DjangoModel {
public $model = "cms.language";
function __construct( $language ) {
parent::__construct();
$this->init_fields( $language );
}
function init_fields( $language ) {
if ( empty($language["tag"]) ) {
$language["tag"] = $language["code"];
}
list($primary_cc, $secondary_cc) = $this->get_country_codes( mb_substr( $language["code"], 0, 2 ) );
$this->fields = array(
"slug"=>$language["code"],
"bcp47_tag"=>( $language["tag"] == "uz-uz" && $language["code"] == "ur" ? "ur-ur" : $language["tag"] ),
"primary_country_code"=>$primary_cc,
"secondary_country_code"=>$secondary_cc,
"english_name"=>$language["english_name"],
"native_name"=>$language["native_name"],
"text_direction"=>(in_array($language["code"], array('ar','fa','ckb')) ? "RIGHT_TO_LEFT" : "LEFT_TO_RIGHT"),
"table_of_contents"=>"Inhaltsverzeichnis",
"created_date"=>now(),
"last_updated"=>now(),
);
}
function get_country_codes( $language_code ) {
$country_code_mapping = [
"am" => ["et", "er"],
"ar" => ["dz", "sa"],
"bs" => ["ba", ""],
"ca" => ["es", ""],
"ck" => ["ir", "iq"],
"cs" => ["cz", ""],
"cy" => ["gb", ""],
"da" => ["dk", ""],
"el" => ["gr", ""],
"en" => ["gb", "us"],
"et" => ["ee", ""],
"eu" => ["es", "fr"],
"fa" => ["ir", "af"],
"ga" => ["ie", "gb"],
"hb" => ["rs", ""],
"he" => ["il", ""],
"hi" => ["in", ""],
"hy" => ["am", ""],
"ja" => ["jp", ""],
"ka" => ["ge", ""],
"km" => ["sy", "tr"],
"ko" => ["kr", "kp"],
"ku" => ["sy", "tr"],
"la" => ["va", ""],
"mo" => ["md", "ro"],
"ms" => ["my", ""],
"nb" => ["no", ""],
"ne" => ["np", ""],
"pa" => ["pk", "in"],
"pe" => ["ir", "af"],
"pu" => ["af", "ir"],
"qu" => ["bo", "ec"],
"sl" => ["si", ""],
"sq" => ["al", ""],
"sr" => ["rs", ""],
"sv" => ["se", ""],
"ta" => ["in", "lk"],
"ti" => ["er", "et"],
"uk" => ["ua", ""],
"ur" => ["pk", "in"],
"vi" => ["vn", ""],
"yi" => ["ba", "ro"],
"zh" => ["cn", ""],
"zu" => ["za", "bw"],
];
if (array_key_exists( $language_code, $country_code_mapping )) {
return $country_code_mapping[$language_code];
} else {
return [$language_code, ""];
}
}
}
class LanguageTreeNode extends DjangoModel {
public $model = "cms.languagetreenode";
function __construct( $blog, $language, $active, $visible, $mptt_node ) {
$this->pk = $mptt_node["pk"];
$this->init_fields( $blog, $language, $active, $visible, $mptt_node );
}
function init_fields( $blog, $language, $active, $visible, $mptt_node ) {
$this->fields = array(
"language"=>$language->pk,
"parent"=>$mptt_node["parent_pk"],
"region"=>$blog->blog_id,
"active"=>$active,
"visible"=>$visible,
"created_date"=>now(),
"last_updated"=>now(),
"lft"=>$mptt_node["left"],
"rgt"=>$mptt_node["right"],
"tree_id"=>$blog->blog_id,
"depth"=>$mptt_node["level"],
);
}
}
class Page extends DjangoModel {
public $model = "cms.page";
function __construct( $blog, $mptt_node, $page_tree_counter ) {
$this->pk = $mptt_node["pk"];
$this->init_fields( $blog, $mptt_node, $page_tree_counter );
}
function init_fields( $blog, $mptt_node, $page_tree_counter ) {
global $media_pk_map;
$attachment_guid = $blog->get_post_thumbnail_guid( $mptt_node["id"] );
$this->fields = array(
"parent"=>$mptt_node["parent_pk"],
"icon"=>( $attachment_guid ? $media_pk_map[$blog->blog_id][$attachment_guid] : null),
"region"=>(int)$blog->blog_id,
"explicitly_archived"=>$blog->get_trashed_status( $mptt_node["id"] ),
"mirrored_page"=>null,
"mirrored_page_first"=>null,
"created_date"=>now(),
"lft"=>$mptt_node["left"],
"rgt"=>$mptt_node["right"],
"tree_id"=>$page_tree_counter,
"depth"=>$mptt_node["level"],
"editors"=>[],
"publishers"=>[],
"api_token"=>$blog->get_api_token( $mptt_node["id"] ),
);
}
}
class PageTranslation extends DjangoModel {
public $model = "cms.pagetranslation";
function __construct( $translation ) {
parent::__construct();
$this->init_fields( $translation );
}
function init_fields( $translation ) {
$this->fields = array(
"page"=>$translation["page"],
"slug"=>$translation["slug"],
"title"=>( empty($translation["title"]) ? "No title" : mb_substr($translation["title"], 0, 250) ),
"status"=>$translation["status"],
"content"=>$translation["content"],
"language"=>$translation["language"],
"currently_in_translation"=>$translation["currently_in_translation"],
"version"=>$translation["version"],
"minor_edit"=>$translation["minor_edit"],
"creator"=>$translation["creator"],
//"created_date"=>str_replace("0000-00-00","1970-01-01",$translation["created_date"]),
"last_updated"=>str_replace("0000-00-00","1970-01-01",$translation["last_updated"]),
);
}
}
class Imprint extends DjangoModel {
public $model = "cms.imprintpage";
function __construct( $blog ) {
parent::__construct();
$this->init_fields( $blog );
}
function init_fields( $blog ) {
$this->fields = array(
"region"=>(int)$blog->blog_id,
"created_date"=>now(),
);
}
}
class ImprintTranslation extends DjangoModel {
public $model = "cms.imprintpagetranslation";
function __construct( $translation ) {
parent::__construct();
$this->init_fields( $translation );
}
function init_fields( $translation ) {
$this->fields = array(
"page_id"=>$translation["page"],
"title"=>( empty($translation["title"]) ? "Impressum" : mb_substr($translation["title"], 0, 250) ),
"status"=>"PUBLIC",
"content"=>$translation["content"],
"language_id"=>$translation["language"],
"currently_in_translation"=>False,
"version"=>1,
"minor_edit"=>False,
"creator_id"=>$translation["creator"],
"last_updated"=>str_replace("0000-00-00","1970-01-01",$translation["last_updated"]),
);
}
}
class MediaFile extends DjangoModel {
public $model = "cms.mediafile";
function __construct( $blog, $item, $file_path ) {
parent::__construct();
$this->init_fields( $blog, $item, $file_path );
}
function init_fields( $blog, $item, $file_path) {
$this->fields = array(
"file" => "regions/" . $blog->blog_id . "/" . $item->meta_value,
"thumbnail" => (in_array(strtolower(substr($item->meta_value, -4)), [".svg", ".pdf", "docx", ".doc", ".xls", "xlsx"]) ? null : ("regions/" . $blog->blog_id . "/" . $file_path[0] . "/" . $file_path[1] . "/thumbnail/" . $file_path[2]) ),
"type" =>$item->post_mime_type,
"name" =>$file_path[2],
"parent_directory" => null,
"region" => $blog->blog_id,
"alt_text" => ( is_null($item->alt_text) ? $item->post_title : $item->alt_text ),
"uploaded_date" => $item->post_date_gmt,
"file_size" => 1,
"last_modified" => now()
);
}
}
class DjangoDirectory extends DjangoModel {
public $model = "cms.directory";
function __construct( ) {
parent::__construct();
$this->init_fields( );
}
function init_fields( $translation ) {
$this->fields = array(
"name" => "",
"region" => null,
"parent" => null,
"created_date" => ""
);
}
}
class Poi extends DjangoModel {
public $model = "cms.poi";
function __construct( $item ) {
parent::__construct();
$this->init_fields( $item );
}
function init_fields( $item ) {
$this->fields = array(
"region" => $item["region"],
"created_date" => str_replace("0000-00-00","1970-01-01",$item["created_date"]),
"address" => $item["address"],
"postcode" => $item["postcode"],
"city" => $item["city"],
"country" => $item["country"],
"latitude" => $item["latitude"],
"longitude" => $item["longitude"],
"location_on_map" => $item["location_on_map"],
"icon" => $item["icon"],
"archived" => $item["archived"],
"website" => $item["website"],
"email" => $item["email"],
"phone_number" => $item["phone_number"]
);
}
}
class PoiTranslation extends DjangoModel {
public $model = "cms.poitranslation";
function __construct( $translation ) {
parent::__construct();
$this->init_fields( $translation );
}
function init_fields( $translation ) {
$this->fields = array(
"poi" => $translation["poi"],
"short_description" => $translation["short_description"],
"title" => $translation["title"],
"slug" => $translation["slug"],
"status" => $translation["status"],
"content" => $translation["content"],
"language" => $translation["language"],
"currently_in_translation" => $translation["currently_in_translation"],
"version" => $translation["version"],
"minor_edit" => $translation["minor_edit"],
"last_updated" => str_replace("0000-00-00","1970-01-01",$translation["last_updated"]),
"creator" => $translation["creator"]
);
}
}
class Event extends DjangoModel {
public $model = "cms.event";
function __construct( $item ) {
parent::__construct();
$this->init_fields( $item );
}
function init_fields( $item ) {
$this->fields = array(
"location" => $item["location"],
"start_date" => $item["start_date"],
"start_time" => $item["start_time"],
"end_date" => $item["end_date"],
"end_time" => $item["end_time"],
"recurrence_rule" => $item["recurrence_rule"],
"icon" => $item["icon"],
"archived" => $item["archived"],
"region" => $item["region"],
"created_date" => str_replace("0000-00-00","1970-01-01",$item["created_date"])
);
}
}
class EventTranslation extends DjangoModel {
public $model = "cms.eventtranslation";
function __construct( $translation ) {
parent::__construct();
$this->init_fields( $translation );
}
function init_fields( $translation ) {
$this->fields = array(
"event" => $translation["event"],
"title" => $translation["title"],
"slug" => $translation["slug"],
"status" => $translation["status"],
"content" => $translation["content"],
"language" => $translation["language"],
"currently_in_translation" => $translation["currently_in_translation"],
"version" => $translation["version"],
"minor_edit" => $translation["minor_edit"],
"last_updated" => str_replace("0000-00-00","1970-01-01",$translation["last_updated"]),
"creator" => $translation["creator"]
);
}
}
class DjangoFixtures {
function __construct() {
$this->object_list = array();
}
function append( $object ) {
array_push( $this->object_list, $object );
}
function is_valid() {
foreach ( $this->object_list as $object ) {
if ( $object->is_valid == false ) {
return false;
}
return true;
}
}
function get_language_by_slug( $code ) {
foreach ( $this->object_list as $object ) {
if ( $object->model == "cms.language" && $object->fields["slug"] == $code ) {
return $object;
}
}
return null;
}
function get_languagetreenode_by_language_pk( $blog_id, $pk ) {
foreach ( $this->object_list as $object ) {
if ( $object->model == "cms.languagetreenode" && $object->pk == $pk && $object->fields["tree_id"] == $blog_id ) {
return $object;
}
}
return null;
}
function dump() {
return json_encode( $this->object_list, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
fwrite(STDERR, "JSON ERROR: ". json_last_error());
}
}
class WPBlog {
function __construct( $db, $blog_id, $path ) {
$this->db = $db;
$this->blog_id = $blog_id;
$this->path = $path;
if ( $blog_id == 1 ) {
$this->dbprefix = "wp_";
} else {
$this->dbprefix = "wp_".$blog_id."_";
}
$this->slug = ( $this->get_blog_option( "blogname") == "Integreat" ? "integreat" : str_replace( "/", "", $path ) );
}
function get_blog_option( $option_name ) {
$query = "SELECT option_value FROM " . $this->dbprefix . "options WHERE option_name='" . $option_name . "'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
return $row->option_value;
}
}
function get_blog_extras() {
$query = "SELECT extra_id FROM " . $this->dbprefix . "ig_extras_config WHERE enabled=1";
$result = $this->db->query( $query );
$extras = [];
while ( $row = $result->fetch_object() ) {
$extras[] = $row->extra_id;
}
return $extras;
}
function get_integreat_setting( $alias ) {
$query = "SELECT value FROM " . $this->dbprefix . "ig_settings_config c LEFT JOIN wp_ig_settings s ON c.setting_id=s.id WHERE alias='$alias'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
return $row->value;
}
}
function get_postmeta( $post_id, $meta_key ) {
$query = "SELECT meta_value FROM " . $this->dbprefix . "postmeta WHERE post_id=$post_id AND meta_key='" . $meta_key . "'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
return $row->option_value;
}
}
function get_languages() {
$query = "SELECT code, english_name, name AS native_name, tag FROM " . $this->dbprefix ."icl_languages l LEFT JOIN " . $this->dbprefix . "icl_languages_translations t ON l.code=t.language_code WHERE t.display_language_code=l.code";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
$languages[$row->code] = array( "code"=>$row->code, "english_name"=>$row->english_name, "native_name"=>$row->native_name, "tag"=>$row->tag );
}
return $languages;
}
function get_used_languages() {
$query = "SELECT code, active FROM " . $this->dbprefix ."icl_languages WHERE active=1";
$result = $this->db->query( $query );
$languages_active = array();
$languages_active[$this->get_default_language()] = 1;
while ( $row = $result->fetch_object() ) {
$languages_active[$row->code] = $row->active;
}
return $languages_active;
}
function get_default_language() {
$query = "SELECT option_value FROM " . $this->dbprefix ."options WHERE option_name='icl_sitepress_settings'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
$data = preg_replace_callback( '!s:(\d+):"(.*?)";!', function($m) {
return 's:'.strlen($m[2]).':"'.$m[2].'";';
}, $row->option_value );
$var = unserialize( $data );
if ( array_key_exists( "default_language", $var ) ) {
return $var["default_language"];
} else {
return "de";
}
}
return null;
}
function get_hidden_languages() {
$query = "SELECT option_value FROM " . $this->dbprefix ."options WHERE option_name='icl_sitepress_settings'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
$data = preg_replace_callback( '!s:(\d+):"(.*?)";!', function($m) {
return 's:'.strlen($m[2]).':"'.$m[2].'";';
}, $row->option_value );
$var = unserialize( $data );
if ( array_key_exists( "hidden_languages", $var ) ) {
return $var["hidden_languages"];
}
}
return [];
}
function generate_language_tree( $treenode_pk_counter ) {
$language_tree = new MPTT( $treenode_pk_counter );
$used_languages = $this->get_used_languages();
$default_language = $this->get_default_language();
$language_tree->add_node( $default_language );
unset( $used_languages[$default_language] );
foreach ( $used_languages as $lang_code => $active ) {
$language_tree->add_node( $lang_code, $default_language );
}
return $language_tree;
}
function get_pages_for_language( $language_code, $parents ) {
$query = "SELECT p.ID, p.post_parent FROM " . $this->dbprefix . "posts p LEFT JOIN (SELECT * FROM " . $this->dbprefix . "icl_translations WHERE element_type='post_page') t ON t.element_id=p.ID WHERE t.language_code='$language_code' AND p.post_parent IN (" . implode( ",", $parents ) . ") AND p.post_status!='auto-draft' ORDER BY menu_order ASC" ;
$result = $this->db->query( $query );
$posts = [];
while ( $row = $result->fetch_object() ) {
$posts[] = ["id" => $row->ID, "parent" => $row->post_parent ];
}
return $posts;
}
function generate_page_tree( $page_tree_node_pk_counter, $root_page_id, $page_tree_counter ) {
global $page_pk_map;
global $page_wp_map;
global $page_mirror_map;
/* Get pages in hierarchical order, starting with a root page */
$new_posts = $this->get_pages_for_language( $this->get_default_language(), [ $root_page_id ] );
$posts = $new_posts;
while ( ! empty( $new_posts ) ) {
$new_posts = $this->get_pages_for_language( $this->get_default_language(), array_column( $new_posts, "id" ));
$posts = array_merge( $posts, $new_posts );
}
$page_tree = new MPTT( $pk_offset = $page_tree_node_pk_counter, $id = $page_tree_counter );
if ( $page_tree->add_node( $root_page_id, null ) ) {
$page_pk_map[$this->blog_id][$root_page_id] = $page_tree->pk_counter - 1;
$page_wp_map[$page_tree->pk_counter - 1] = array( "blog_id" => $this->blog_id, "post_id" => $root_page_id );
$page_mirror_map[$this->blog_id][$root_page_id] = $this->get_mirrored_page( $root_page_id );
} else {
return false;
}
foreach ( $posts as $post ) {
if ( $page_tree->add_node( $post["id"], $post["parent"] ) ) {
$page_pk_map[$this->blog_id][$post["id"]] = $page_tree->pk_counter - 1;
$page_wp_map[$page_tree->pk_counter - 1] = array( "blog_id" => $this->blog_id, "post_id" => $post["id"] );
$page_mirror_map[$this->blog_id][$post["id"]] = $this->get_mirrored_page( $post["id"] );
}
}
return $page_tree;
}
/* Get the post ID that is the first version (post parent) of the page in the given language */
function get_page_language_root_id( $post_id, $language ) {
$query = "SELECT element_id FROM " . $this->dbprefix . "icl_translations WHERE trid IN (SELECT trid FROM " . $this->dbprefix . "icl_translations WHERE element_type='post_page' AND element_id='" . $post_id . "') AND language_code = '" . $language . "'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
return $row->element_id;
}
return false;
}
function get_mirrored_page( $post_id ) {
$query = "SELECT * FROM " . $this->dbprefix . "postmeta WHERE post_id=" . $post_id . " AND (meta_key='ig-attach-content-position' OR meta_key='ig-attach-content-blog' OR meta_key='ig-attach-content-page') AND meta_value IS NOT Null" ;
$result = $this->db->query( $query );
$source_page = array();
while ( $row = $result->fetch_object() ) {
if ( $row->meta_key == "ig-attach-content-position" ) {
$source_page["pos"] = $row->meta_value;
}
elseif ( $row->meta_key == "ig-attach-content-blog" ) {
$source_page["blog_id"] = $row->meta_value;
}
elseif ( $row->meta_key == "ig-attach-content-page" ) {
$source_page["post_id"] = $row->meta_value;
}
}
return $source_page;
}
function duplicate_content( $post_id ) {
$query = "SELECT meta_value FROM " . $this->dbprefix. "postmeta WHERE post_id=$post_id AND meta_key='_icl_lang_duplicate_of'";
$result = $this->db->query( $query );
if ( $result->num_rows == 0 ) {
return false;
}
return true;
}
/* Create array of all revisions in all translations */
function get_page_translations( $mptt_node, $language, $language_pk ) {
$parent = $this->get_page_language_root_id( $mptt_node["id"], $language );
if ( !$parent ) {
return [];
}
$query = "SELECT * FROM " . $this->dbprefix . "posts WHERE (post_parent = " . $parent . " AND post_type='revision') OR ID = " . $parent . " ORDER BY ID ASC" ;
$result = $this->db->query( $query );
$translations = [];
$version = 1;
$status = "DRAFT";
$slug = null;
$duplicate_content = $this->duplicate_content( $parent );
while ( $row = $result->fetch_object() ) {
if ( is_null($slug) ) $slug = $row->post_name;
$cur_status = map_status( $row->post_status );
// if $cur_status is null, the status does not change (inherit)
if ( !is_null($cur_status) ) {
$status = $cur_status;
}
$page_translation = new PageTranslation([
"page"=>$mptt_node["pk"],
"slug"=>slugify($slug),
"title"=>$row->post_title,
"status"=>$status,
"content"=> ( $duplicate_content ? "" : wpautop($row->post_content) ),
"language"=>$language_pk,
"currently_in_translation"=>false,
"version"=>$version,
"minor_edit"=>false,
"creator"=>$row->post_author,
"created_date"=>$row->post_date_gmt,
"last_updated"=>$row->post_modified_gmt,
]);
global $slug_map_file_resource;
fputs($slug_map_file_resource, "$this->slug;$mptt_node[pk];$page_translation->pk;$language_pk;$version;$slug\n");
$page_translations[] = $page_translation;
$version++;
}
return $page_translations;
}
function export_imprint( $used_languages, $language_pk_map ) {
global $fixtures;
$imprintpage = new Imprint( $this );
$fixtures->append( $imprintpage );
foreach ( $used_languages as $used_language => $active) {
if ( ! array_key_exists( $used_language, $language_pk_map ) ) continue;
$version = 1;
$query = "SELECT * FROM " . $this->dbprefix . "posts p LEFT JOIN (SELECT * FROM " . $this->dbprefix . "icl_translations WHERE element_type='post_disclaimer') t ON t.element_id=p.ID WHERE t.language_code='".$used_language."' AND p.post_type='disclaimer' ORDER BY ID ASC";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
$fixtures->append( new ImprintTranslation([
"page"=>$imprintpage->pk,
"title"=>$row->post_title,
"status"=>"PUBLIC",
"content"=>wpautop($row->post_content),
"language"=>$language_pk_map[$row->language_code],
"currently_in_translation"=>false,
"version"=>$version,
"minor_edit"=>false,
"creator"=>$row->post_author,
"last_updated"=>$row->post_modified_gmt,
]) );
$version++;
}
}
}
function export_attached_files( ) {
global $media_pk_map;
global $fixtures;
$query = "SELECT ID,guid,meta_value,post_mime_type,post_date_gmt,post_title,alt_text FROM (" . $this->dbprefix . "posts p LEFT JOIN (SELECT * FROM " . $this->dbprefix . "postmeta WHERE meta_key='_wp_attached_file') AS pm ON p.ID=pm.post_id) LEFT JOIN (SELECT post_id,meta_value AS alt_text FROM " . $this->dbprefix. "postmeta WHERE meta_key='_wp_attachment_image_alt') at ON at.post_id=p.ID WHERE post_type='attachment' GROUP BY guid";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
if ( $row->meta_value === null ) { continue; }
$file_path = explode("/", $row->meta_value);
if ( count( $file_path ) < 2 ) {
$file_path[0] = "1970";
$file_path[1] = "01";
$file_path[2] = $row->meta_value;
}
$full_file_path = '/var/www/cms/wp-content/uploads/sites/' . $this->blog_id . "/" . $row->meta_value;
if ( ! file_exists($full_file_path) ) {
continue;
}
$media_file = new MediaFile( $this, $row, $file_path );
$fixtures->append( $media_file );
$media_pk_map[$this->blog_id][$row->guid] = $media_file->pk;
}
}
function get_trashed_status( $post_id ) {
$query = "SELECT post_status FROM " . $this->dbprefix . "posts WHERE ((post_parent=$post_id AND post_type='revision') OR ID=$post_id) AND post_status!='inherit' ORDER BY ID DESC LIMIT 1";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
if ( $row->post_status == "trash" ) {
return true;
}
}
return false;
}
function get_api_token( $post_id ) {
$query = "SELECT meta_value FROM " . $this->dbprefix . "postmeta WHERE post_id=$post_id AND meta_key='ig_push_content_token'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
return $row->meta_value;
}
return false;
}
function get_post_thumbnail_guid( $post_id ) {
// original post ID -> post meta -> meta_key -> post guid
$thumbnail_post = null;
$query = "SELECT meta_value FROM " . $this->dbprefix . "postmeta WHERE post_id=$post_id AND meta_key='_thumbnail_id'";
$result = $this->db->query( $query );
while ( $row = $result->fetch_object() ) {
$thumbnail_post = (int)$row->meta_value;
}
if ( ! $thumbnail_post ) return null;
$query = "SELECT guid FROM " . $this->dbprefix . "posts WHERE ID=$thumbnail_post";
while ( $row = $this->db->query( $query )->fetch_object() ) {
return $row->guid;
}
return null;