-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
1989 lines (1664 loc) · 68.2 KB
/
index.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
require 'vendor/autoload.php';
session_start();
use \Slim\Middleware\HttpBasicAuthentication\AuthenticatorInterface;
class DBAuthenticator implements AuthenticatorInterface {
public function __invoke(array $arguments) {
$db = getConnection();
$passHash = hash('sha256', $arguments['password']);
$res = $db->query("SELECT id, user, password FROM users WHERE user = '".$arguments['user']."' AND password = '".$passHash."'");
if ($res->num_rows > 0) {
$row = $res->fetch_assoc();
if ($row['password'] == $passHash) {
$_SESSION['user'] = $row['user'];
return true;
}
else {
return false;
}
}
else {
return false;
}
}
}
$app = new \Slim\Slim();
$app->add(new \Slim\Middleware\HttpBasicAuthentication([
"path" => '/admin',
"secure" => false,
"authenticator" => new DBAuthenticator()
]));
$app->get('/', 'frontPage');
// Get single persons metadata by ID
$app->get('/person/:id', 'getPerson');
// Get list of persons that appear to be duplicate entires
$app->get('/persons/duplicates/:num1/:num2', 'getDuplicatePersons');
/*
------------------
DEPRECATED v1 methods, look at API v2 instead
*/
$app->get('/persons/place/:place', 'getPersonsByPlace');
$app->get('/persons/birthplace/:place', 'getPersonsByBirthPlace');
$app->get('/persons/deathplace/:place', 'getPersonsByDeathPlace');
$app->get('/persons/year_range/:num1/:num2', 'getPersonsByYears');
$app->get('/persons/age/:age/:buffer', 'getPersonsByAge');
$app->get('/persons/search/:query', 'searchPersons');
$app->get('/persons/:num1/:num2(/:order(/:dir))', 'getPersons');
$app->get('/locations/year_range/:num1/:num2', 'getLocationsByYears');
$app->get('/locations/age/:age/:buffer', 'getLocationsByAge');
$app->get('/locations/population', 'getLocationsPopulations');
$app->get('/locations/movements(/:num1/:num2)', 'getMovementLocations');
$app->get('/locations', 'getLocations');
/*
------------------
*/
// Get single document metadata by ID
$app->get('/document/:id', 'getDocument');
// Get single place metadata by ID
$app->get('/place/:id', 'getPlace');
// Get list of places which appear to be duplicate entires
$app->get('/places/duplicates/:num1/:num2', 'getDuplicatePlaces');
// Search for places by name
$app->get('/places/search/:query', 'searchPlaces');
// Get list of places
$app->get('/places/:num1/:num2(/:order(/:dir))', 'getPlaces');
// Get list of areas (countries, disctricts, counties, ...)
$app->get('/areas/:num1/:num2', 'getAreas');
/*
------------------
API v2
------------------
Get data about movements (from birth place to death place)
- year_range: birth year and death year
- range_type (birth/death/null): defines if searching for persons born within range (birth),
persons deceased within the range (death) or persons living within the range (null/empty)
- gender (male/female/null): search by gender
- place: search for a place name related to the persons
- placerelation (birth/death/null): defines relation of place parameter to persons, birth place, death place or both (null/empty)
- name: search for firstname or surname
- firstname: search for firstname
- surname: search for surname
- archive: search by archive (Herrnhut = 1, Bethlehem = 2, London = 3, 1...)
*/
$app->get('/v2/locations/movements(/)(year_range/:num1/:num2/?)(/)(range_type/:rangetype/?)(/)(gender/:gender/?)(/)(place/:place/?)(/)(placerelation/:placerelation/?)(/)(name/:name/?)(/)(firstname/:firstname/?)(/)(surname/:surname/?)(/)(archive/:archive/?)', 'getMovementLocationsV2');
/*
Get locations (places with known coordinates)
- year_range: birth year and death year of persons related to places
- range_type (birth/death/null): defines if searching for places related to persons born within range (birth),
persons deceased within the range (death) or persons living within the range (null/empty)
- gender (male/female/null): search by gender
- place: search for a place name
- placerelation (birth/death/null): defines relation of places to persons, birth place, death place or both (null/empty)
- name: search for firstname or surname
- firstname: search for firstname
- surname: search for surname
- archive: search by archive (Herrnhut = 1, Bethlehem = 2, London = 3, 1...)
- doc_id: search for places related to persons related to a specific document ID
*/
$app->get('/v2/locations(/)(year_range/:num1/:num2/?)(/)(range_type/:rangetype/?)(/)(relation/:relation/?)(/)(gender/:gender/?)(/)(place/:place/?)(/)(placerelation/:placerelation/?)(/)(name/:name/?)(/)(firstname/:firstname/?)(/)(surname/:surname/?)(/)(archive/:archive/?)(doc_id/:doc_id/?)(/)', 'getLocationsV2');
/*
Get graph data with count of persons by birth years and death years
- year_range: birth year and death year of persons
- range_type (birth/death/null): defines if searching for persons born within range (birth),
persons deceased within the range (death) or persons living within the range (null/empty)
- gender (male/female/null): search by gender
- place: search for a place name related to the persons
- placerelation (birth/death/null): defines relation of place parameter persons, birth place, death place or both (null/empty)
- name: search for firstname or surname
- firstname: search for firstname
- surname: search for surname
- archive: search by archive (Herrnhut = 1, Bethlehem = 2, London = 3, 1...)
- doc_id: search for persons related to a specific document ID
*/
$app->get('/v2/persons/per_year(/)(year_range/:num1/:num2/?)(/)(range_type/:rangetype/?)(/)(gender/:gender/?)(/)(place/:place/?)(/)(placerelation/:placerelation/?)(/)(name/:name/?)(/)(firstname/:firstname/?)(/)(surname/:surname/?)(/)(archive/:archive/?)(doc_id/:doc_id/?)(/)', 'getPersonsPerYearV2');
/*
Get list of persons
- year_range: birth year and death year of persons
- range_type (birth/death/null): defines if searching for persons born within range (birth),
persons deceased within the range (death) or persons living within the range (null/empty)
- gender (male/female/null): search by gender
- place: search for a place name related to the persons
- placerelation (birth/death/null): defines relation of place parameter persons, birth place, death place or both (null/empty)
- name: search for firstname or surname
- firstname: search for firstname
- surname: search for surname
- archive: search by archive (Herrnhut = 1, Bethlehem = 2, London = 3, 1...)
- page: defines which page we are getting (40 persons at a time)
- doc_id: search for places related to a specific document ID
*/
$app->get('/v2/persons(/)(year_range/:num1/:num2/?)(/)(range_type/:rangetype/?)(/)(gender/:gender/?)(/)(place/:place/?)(/)(placerelation/:placerelation/?)(/)(name/:name/?)(/)(firstname/:firstname/?)(/)(surname/:surname/?)(/)(archive/:archive/?)(page/:page/?)(/)(doc_id/:doc_id/?)(/)', 'getPersonsV2');
// Get transcription count
$app->get('/transcriptions/count', 'getTranscriptionsCount');
// Get transcriptions by location (not finished)
$app->get('/transcriptions/locations', 'getTranscriptionsLocations');
// Search for transcriptions through WordPress API
$app->get('/transcriptions/wp_search(/)(search/:query/?)(/)(language/:language/?)(/)(country/:country/?)(/)(archive/:archive/?)', 'wpSearchTranscriptions');
// Get transcription (metadata + text from MediaWiki + images from WordPress) by ID
$app->get('/transcription/:id', 'getTranscriptionsById');
// TEI export
$app->get('/tei/document/:id', 'getTEIDocument');
// Admin
$app->post('/admin/person/:id', 'postPerson');
$app->post('/admin/place/:id', 'postPlace');
$app->post('/admin/places/combine/:id', 'combinePlaces');
$app->post('/admin/persons/combine/:id', 'combinePersons');
$app->contentType('application/json;charset=utf-8');
$app->response()->header('Access-Control-Allow-Origin', '*');
$app->run();
function getConnection() {
include 'config.php';
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$db->set_charset('utf8');
return $db;
}
function getConnectionWP() {
include 'config.php';
$db = new mysqli($dbhost, $dbuser, $dbpass, $wpDbname);
$db->set_charset('utf8');
return $db;
}
function processItem(&$item, $key) {
if (is_null($item)) {
unset($item);
}
else if (is_string($item)) {
$item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
}
}
function array_unset_recursive(&$array, $remove) {
if (!is_array($remove)) $remove = array($remove);
foreach ($array as $key => &$value) {
if (in_array($value, $remove)) unset($array[$key]);
else if (is_array($value)) {
array_unset_recursive($value, $remove);
}
}
}
function json_encode_is($arr, $metadata = null) {
$output = array();
if (!is_null($metadata)) {
$output['metadata'] = $metadata;
}
$output['data']= $arr;
$output['login_info'] = array(
'user' => isset($_SESSION['user']) ? $_SESSION['user'] : 'null'
);
array_walk_recursive($output, 'processItem');
array_unset_recursive($output, null);
return mb_decode_numericentity(json_encode($output), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
}
function frontPage() {
global $app;
$app->contentType('text/html;charset=utf-8');
readfile('front.html');
}
function getPersons($num1 = null, $num2 = null, $order = null, $orderdir = null) {
$sql = "SELECT SQL_CALC_FOUND_ROWS persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons INNER JOIN places b_p ON persons.birthplace = b_p.id INNER JOIN places d_p ON persons.deathplace = d_p.id".
(
!is_null($order) ? " ORDER BY ".$order.(!is_null($orderdir) ? ' '.$orderdir : ' ASC') : ''
).
(
!is_null($num1) && is_null($num2) ? " LIMIT 0, ".$num1 :
!is_null($num1) && !is_null($num2) ? " LIMIT ".$num1.", ".$num2 :
''
);
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, personObj($row));
}
$res = $db->query('SELECT FOUND_ROWS() total');
$row = $res->fetch_assoc();
echo json_encode_is($data, array(
'page' => $num1,
'total' => $row['total']
));
}
function searchPersons($query, $order = null, $orderdir = null) {
$sql = "SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons LEFT JOIN places b_p ON persons.birthplace = b_p.id LEFT JOIN places d_p ON persons.deathplace = d_p.id ".
" WHERE LOWER(persons.surname) LIKE '%".strtolower($query)."%' OR LOWER(persons.surname_literal) LIKE '%".strtolower($query)."%' OR LOWER(persons.firstname) LIKE '%".strtolower($query)."%' OR LOWER(persons.ll_id) LIKE '%".strtolower($query)."%'".
(
!is_null($order) ? " ORDER BY persons.".$order.(!is_null($orderdir) ? ' '.$orderdir : ' ASC') : ''
);
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, personObj($row));
}
echo json_encode_is($data);
}
function getPersonsByPlace($place) {
$sql = "SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons INNER JOIN places b_p ON persons.birthplace = b_p.id INNER JOIN places d_p ON persons.deathplace = d_p.id WHERE persons.birthplace = ".$place." OR persons.deathplace = ".$place;
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, personObj($row));
}
echo json_encode_is($data);
}
function getPersonsByBirthPlace($place) {
$sql = "SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons INNER JOIN places b_p ON persons.birthplace = b_p.id INNER JOIN places d_p ON persons.deathplace = d_p.id WHERE persons.birthplace = ".$place;
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, personObj($row));
}
echo json_encode_is($data);
}
function getPersonsByDeathPlace($place) {
$sql = "SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons INNER JOIN places b_p ON persons.birthplace = b_p.id INNER JOIN places d_p ON persons.deathplace = d_p.id WHERE persons.deathplace = ".$place;
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, personObj($row));
}
echo json_encode_is($data);
}
function getPersonsByYears($num1, $num2) {
$sql = "SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons INNER JOIN places b_p ON persons.birthplace = b_p.id INNER JOIN places d_p ON persons.deathplace = d_p.id WHERE persons.death_year >= ".$num1." AND persons.death_year < ".$num2." AND persons.birth_year < persons.death_year";
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, personObj($row));
}
echo json_encode_is($data);
}
function getPersonsByAge($age, $buffer) {
$sql = "SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons INNER JOIN places b_p ON persons.birthplace = b_p.id INNER JOIN places d_p ON persons.deathplace = d_p.id WHERE (persons.death_year-persons.birth_year)-".$buffer." < ".$age." AND (persons.death_year-persons.birth_year)+".$buffer." > ".$age." AND persons.birth_year < persons.death_year";
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, personObj($row));
}
echo json_encode_is($data);
}
function getPerson($id) {
$sql = "SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons LEFT JOIN places b_p ON persons.birthplace = b_p.id LEFT JOIN places d_p ON persons.deathplace = d_p.id WHERE persons.id = ".$id;
$db = getConnection();
$res = $db->query($sql);
$row = $res->fetch_assoc();
if ($row['gender'] == 0) {
$gender = 'male';
}
else if ($row['gender'] == 1) {
$gender = 'female';
}
else {
$gender = '';
}
$data = array(
'id' => $row['id'],
'surname' => $row['surname'],
'surname_literal' => $row['surname_literal'],
'firstname' => $row['firstname'],
'gender' => $gender,
'familystatus' => $row['familystatus'],
'birth' => array(
'day' => $row['birth_day'],
'month' => $row['birth_month'],
'year' => $row['birth_year'],
'place' => array(
'id' => $row['birthplace'],
'name' => $row['birthplacename'],
'lat' => $row['birthplacelat'],
'lng' => $row['birthplacelng']
)
),
'death' => array(
'day' => $row['death_day'],
'month' => $row['death_month'],
'year' => $row['death_year'],
'place' => array(
'id' => $row['deathplace'],
'name' => $row['deathplacename'],
'lat' => $row['deathplacelat'],
'lng' => $row['deathplacelng']
)
),
'documents' => array()
);
$docSql = 'SELECT documents.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM documents INNER JOIN places b_p ON documents.birthplace = b_p.id INNER JOIN places d_p ON documents.deathplace = d_p.id INNER JOIN persondocuments ON persondocuments.document = documents.id WHERE persondocuments.person = '.$row['id'];
$docRes = $db->query($docSql);
while ($docRow = $docRes->fetch_assoc()) {
array_push($data['documents'], array(
'id' => $docRow['id'],
'll_id' => $docRow['ll_id'],
'll_idnum' => $docRow['ll_idnum'],
'surname' => $docRow['surname'],
'surname_literal' => $docRow['surname_literal'],
'firstname' => $docRow['firstname'],
'gender' => $gender,
'familystatus' => $docRow['familystatus'],
'reference' => $docRow['reference'],
'birth' => array(
'day' => $docRow['birth_day'],
'month' => $docRow['birth_month'],
'year' => $docRow['birth_year'],
'll_place' => $docRow['ll_birthplace'],
'place' => array(
'id' => $docRow['birthplace'],
'name' => $docRow['birthplacename'],
'lat' => $docRow['birthplacelat'],
'lng' => $docRow['birthplacelng']
)
),
'death' => array(
'day' => $docRow['death_day'],
'month' => $docRow['death_month'],
'year' => $docRow['death_year'],
'll_place' => $docRow['ll_deathplace'],
'place' => array(
'id' => $docRow['deathplace'],
'name' => $docRow['deathplacename'],
'lat' => $docRow['deathplacelat'],
'lng' => $docRow['deathplacelng']
)
)
));
}
echo json_encode_is($data);
}
function personObj($row) {
return array(
'id' => $row['id'],
'll_id' => $row['ll_id'],
'll_idnum' => $row['ll_idnum'],
'surname' => $row['surname'],
'surname_literal' => $row['surname_literal'],
'firstname' => $row['firstname'],
'birth' => array(
'day' => $row['birth_day'],
'month' => $row['birth_month'],
'year' => $row['birth_year'],
'place' => array(
'id' => $row['birthplace'],
'name' => $row['birthplacename'],
'lat' => $row['birthplacelat'],
'lng' => $row['birthplacelng']
)
),
'death' => array(
'day' => $row['death_day'],
'month' => $row['death_month'],
'year' => $row['death_year'],
'place' => array(
'id' => $row['deathplace'],
'name' => $row['deathplacename'],
'lat' => $row['deathplacelat'],
'lng' => $row['deathplacelng']
)
)
);
}
function placeObj($row) {
return array(
'id' => $row['id'],
'name' => $row['name'],
'name_en' => $row['name_en'],
'area' => $row['area'],
'area_en' => $row['area_en'],
'google_id' => $row['google_id'],
'google_name' => $row['google_name'],
'google_location_type' => $row['google_location_type'],
'lat' => $row['lat'],
'lng' => $row['lng']
);
}
function locationObj($row) {
$ret = array(
'id' => $row['id'],
'name' => $row['name'],
'area' => $row['area'],
'lat' => $row['lat'],
'lng' => $row['lng']
);
if (isset($row['c'])) {
$ret['c'] = $row['c'];
}
return $ret;
}
function getLocations() {
$sql = "SELECT DISTINCT * FROM places WHERE lat IS NOT NULL AND lng IS NOT NULL";
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, locationObj($row));
}
echo json_encode_is($data);
}
function getLocationsPopulations() {
$sql = "SELECT places.*, Count(persons.id) AS c FROM persons INNER JOIN places ON persons.birthplace = places.id OR persons.deathplace = places.id WHERE lat IS NOT NULL AND lng IS NOT NULL GROUP BY places.id ORDER BY c DESC";
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, array(
'id' => $row['id'],
'name' => $row['name'],
'area' => $row['area'],
'lat' => $row['lat'],
'lng' => $row['lng'],
'persons' => $row['c']
));
}
echo json_encode_is($data);
}
function getMovementLocations($num1 = null, $num2 = null) {
$condition = "";
if (!is_null($num1) && !is_null($num2)) {
$condition = " AND persons.death_year >= ".$num1." AND persons.death_year <= ".$num2." AND persons.birth_year < persons.death_year";
}
$sql = "SELECT DISTINCT p1.id birthplace_id, p1.name birthplace_name, p1.area birthplace_area, p1.lat birthplace_lat, p1.lng birthplace_lng, p2.id deathplace_id, p2.name deathplace_name, p2.area deathplace_area, p2.lat deathplace_lat, p2.lng deathplace_lng FROM persons INNER JOIN places p1 ON persons.birthplace = p1.id INNER JOIN places p2 ON persons.deathplace = p2.id WHERE p1.id <> p2.id AND p1.lat IS NOT NULL AND p1.lng IS NOT NULL and p2.lat IS NOT NULL and p2.lng IS NOT NULL".$condition." ORDER BY p1.id";
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, array(
'birthplace' => array(
'id' => $row['birthplace_id'],
'name' => $row['birthplace_name'],
'area' => $row['birthplace_area'],
'lat' => $row['birthplace_lat'],
'lng' => $row['birthplace_lng']
),
'deathplace' => array(
'id' => $row['deathplace_id'],
'name' => $row['deathplace_name'],
'area' => $row['deathplace_area'],
'lat' => $row['deathplace_lat'],
'lng' => $row['deathplace_lng']
)
));
}
echo json_encode_is($data);
}
function getLocationsByYears($num1, $num2) {
// $sql = "SELECT places.*, Count(persons.id) AS c FROM persons INNER JOIN places ON persons.birthplace = places.id OR persons.deathplace = places.id WHERE lat IS NOT NULL AND lng IS NOT NULL GROUP BY places.id ORDER BY c DESC";
$sql = "SELECT DISTINCT places.id, places.name, places.area, places.lat, places.lng, Count(persons.id) AS c FROM places INNER JOIN places ON persons.birthplace = places.id OR persons.deathplace = places.id WHERE places.lat IS NOT NULL AND places.lng IS NOT NULL AND persons.death_year >= ".$num1." AND persons.death_year <= ".$num2." AND persons.birth_year < persons.death_year GROUP BY places.id ORDER BY c DESC";
echo $sql;
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, locationObj($row));
}
echo json_encode_is($data);
}
function getLocationsByAge($age, $buffer) {
$sql = "SELECT places.id, places.name, places.area, places.lat, places.lng FROM places INNER JOIN persons ON places.id = persons.birthplace OR places.id = persons.deathplace WHERE places.lat IS NOT NULL AND places.lng IS NOT NULL AND (persons.death_year-persons.birth_year)-".$buffer." < ".$age." AND (persons.death_year-persons.birth_year)+".$buffer." > ".$age." AND persons.birth_year < persons.death_year";
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, locationObj($row));
}
echo json_encode_is($data);
}
function getPlaces($num1 = null, $num2 = null, $order = null, $orderdir = null) {
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM places".
(
!is_null($order) ? " ORDER BY ".$order.(!is_null($orderdir) ? ' '.$orderdir : ' ASC') : ''
).
(
!is_null($num1) && is_null($num2) ? " LIMIT 0, ".$num1 :
!is_null($num1) && !is_null($num2) ? " LIMIT ".$num1.", ".$num2 :
''
);
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, placeObj($row));
}
$res = $db->query('SELECT FOUND_ROWS() total');
$row = $res->fetch_assoc();
echo json_encode_is($data, array(
'page' => $num1,
'total' => $row['total']
));
}
function getAreas($num1 = null, $num2 = null) {
$sql = "SELECT DISTINCT area, area_en FROM places WHERE area != '' AND area_en != '' ORDER BY area".
(
!is_null($num1) && is_null($num2) ? " LIMIT 0, ".$num1 :
!is_null($num1) && !is_null($num2) ? " LIMIT ".$num1.", ".$num2 :
''
);
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, array(
'area' => $row['area'],
'area_en' => $row['area_en']
));
}
echo json_encode_is($data);
}
function getDuplicatePlaces($num1 = null, $num2 = null) {
$sql = "SELECT SQL_CALC_FOUND_ROWS google_id, google_name, lat, lng, COUNT(*) c FROM places WHERE google_id != '' GROUP BY google_id HAVING c > 1 ORDER BY c DESC".
(
!is_null($num1) && is_null($num2) ? " LIMIT 0, ".$num1 :
!is_null($num1) && !is_null($num2) ? " LIMIT ".$num1.", ".$num2 :
''
);
$db = getConnection();
$sqlModesRes = $db->query('SELECT @@sql_mode');
$sqlModes = $sqlModesRes->fetch_assoc();
$sqlModesArray = explode(',', $sqlModes['@@sql_mode']);
$sqlModesArray = array_diff($sqlModesArray, array('ONLY_FULL_GROUP_BY'));
$db->query("SET sql_mode = '".implode(',', $sqlModesArray)."'");
$res = $db->query($sql);
$rowCountRes = $db->query('SELECT FOUND_ROWS() total');
$rowCount = $rowCountRes->fetch_assoc();
$data = array();
while ($row = $res->fetch_assoc()) {
$places = array();
$placesRes = $db->query('SELECT * FROM places WHERE google_id = "'.$row['google_id'].'"');
while ($placesRow = $placesRes->fetch_assoc()) {
array_push($places, placeObj($placesRow));
}
array_push($data, array(
'google_id' => $row['google_id'],
'google_name' => $row['google_name'],
'lat' => $row['lat'],
'lng' => $row['lng'],
'count' => $row['c'],
'places' => $places
));
}
echo json_encode_is($data, array(
'page' => $num1,
'total' => $rowCount['total']
));
}
function getDuplicatePersons($num1 = null, $num2 = null) {
$sql = "SELECT SQL_CALC_FOUND_ROWS surname_literal, firstname, birth_year, death_year, birthplace, deathplace, count(*) c FROM persons GROUP BY surname_literal, firstname, birth_year, death_year, birthplace, deathplace HAVING c > 1 ORDER BY c DESC".
(
!is_null($num1) && is_null($num2) ? " LIMIT 0, ".$num1 :
!is_null($num1) && !is_null($num2) ? " LIMIT ".$num1.", ".$num2 :
''
);
$db = getConnection();
$res = $db->query($sql);
$rowCountRes = $db->query('SELECT FOUND_ROWS() total');
$rowCount = $rowCountRes->fetch_assoc();
$data = array();
while ($row = $res->fetch_assoc()) {
$persons = array();
$personsSql = 'SELECT persons.*, b_p.name birthplacename, b_p.area birthplacearea, b_p.lat birthplacelat, b_p.lng birthplacelng, d_p.name deathplacename, d_p.area deathplacearea, d_p.lat deathplacelat, d_p.lng deathplacelng FROM persons '.
'INNER JOIN places b_p ON persons.birthplace = b_p.id '.
'INNER JOIN places d_p ON persons.deathplace = d_p.id '.
'WHERE persons.surname_literal = "'.$row['surname_literal'].'" '.
'AND persons.firstname = "'.(is_null($row['firstname']) ? 'null' : $row['firstname']).'" '.
'AND persons.birth_year = '.(is_null($row['birth_year']) ? 'null' : $row['birth_year']).' '.
'AND persons.death_year = '.(is_null($row['death_year']) ? 'null' : $row['death_year']).' '.
'AND persons.birthplace = '.(is_null($row['birthplace']) ? 'null' : $row['birthplace']).' '.
'AND persons.deathplace = '.(is_null($row['deathplace']) ? 'null' : $row['deathplace']);
$personsRes = $db->query($personsSql);
while ($personsRow = $personsRes->fetch_assoc()) {
array_push($persons, personObj($personsRow));
}
array_push($data, array(
'surname_literal' => $row['surname_literal'],
'firstname' => $row['firstname'],
'birth_year' => $row['birth_year'],
'death_year' => $row['death_year'],
'birthplace' => $row['birthplace'],
'deathplace' => $row['deathplace'],
'count' => $row['c'],
'persons' => $persons
));
}
echo json_encode_is($data, array(
'page' => $num1,
'total' => $rowCount['total']
));
}
function searchPlaces($query, $order = null, $orderdir = null) {
$sql = "SELECT * FROM places WHERE LOWER(name) LIKE '%".strtolower($query)."%' OR LOWER(name_en) LIKE '%".strtolower($query)."%' OR LOWER(google_id) LIKE '%".strtolower($query)."%' OR LOWER(area) LIKE '%".strtolower($query)."%' OR LOWER(area_en) LIKE '%".strtolower($query)."%' OR LOWER(name_ll) LIKE '%".strtolower($query)."%' OR LOWER(area_ll) LIKE '%".strtolower($query)."%'".
(
!is_null($order) ? " ORDER BY ".$order.(!is_null($orderdir) ? ' '.$orderdir : ' ASC') : ' ORDER BY name'
);
$db = getConnection();
$res = $db->query($sql);
$data = array();
while ($row = $res->fetch_assoc()) {
array_push($data, placeObj($row));
}
echo json_encode_is($data);
}
function getPlace($id) {
$sql = "SELECT * FROM places WHERE id = ".$id;
$db = getConnection();
$res = $db->query($sql);
$row = $res->fetch_assoc();
if (@unserialize($row['google_address_data'])) {
$googleAddressData = unserialize($row['google_address_data']);
}
else if (@json_decode($row['google_address_data'])) {
$googleAddressData = json_decode($row['google_address_data']);
}
else {
$googleAddressData = null;
}
$data = array(
'id' => $row['id'],
'name' => $row['name'],
'name_en' => $row['name_en'],
'name_ll' => $row['name_ll'],
'area' => $row['area'],
'area_en' => $row['area_en'],
'area_ll' => $row['area_ll'],
'google_id' => $row['google_id'],
'google_name' => $row['google_name'],
'google_location_type' => $row['google_location_type'],
'google_address' => $googleAddressData,
'commentary' => $row['commentary'],
'lat' => $row['lat'],
'lng' => $row['lng']
);
echo json_encode_is($data);
}
function formatDate($day, $month, $year) {
return $year > 0 ?
$year.($month > 0 ? '-'.$month.($day > 0 ? '-'.$day : ''
)
: '')
: null;
}
function getGender($gender) {
if (is_null($gender)) {
return null;
}
else if ($gender == 0) {
return 'M';
}
else if ($gender == 1) {
return 'F';
}
}
function postPerson($id) {
$app = \Slim\Slim::getInstance();
$request = $app->request();
$requestBody = $request->getBody();
$requestData = json_decode($requestBody);
$db = getConnection();
$sql = "UPDATE persons SET ".
"surname = '".(isset($requestData->surname) ? $requestData->surname : '')."', ".
"surname_literal = '".(isset($requestData->surname_literal) ? $requestData->surname_literal : '')."', ".
"firstname = '".(isset($requestData->firstname) ? $requestData->firstname : '')."', ".
"gender = ".($requestData->gender == 'male' ? 0 : $requestData->gender == 'female' ? 1 : 'null').", ".
"birth_day = ".(isset($requestData->birth) && isset($requestData->birth->day) ? $requestData->birth->day : 'null').", ".
"birth_month = ".(isset($requestData->birth) && isset($requestData->birth->month) ? $requestData->birth->month : 'null').", ".
"birth_year = ".(isset($requestData->birth) && isset($requestData->birth->year) ? $requestData->birth->year : 'null').", ".
"death_day = ".(isset($requestData->death) && isset($requestData->death->day) ? $requestData->death->day : 'null').", ".
"death_month = ".(isset($requestData->death) && isset($requestData->death->month) ? $requestData->death->month : 'null').", ".
"death_year = ".(isset($requestData->death) && isset($requestData->death->year) ? $requestData->death->year : 'null')." ".
"WHERE id = ".$id;
$res = $db->query($sql);
echo json_encode_is(array(
'action' => 'putPerson',
'status' => 'success'
));
}
function postPlace($id) {
$app = \Slim\Slim::getInstance();
$request = $app->request();
$requestBody = $request->getBody();
$requestData = json_decode($requestBody);
$db = getConnection();
$updateQueries = array(
"name = '".$requestData->name."'",
"name_en = '".$requestData->name_en."'"
);
if (isset($requestData->area)) {
array_push($updateQueries, "area = '".$requestData->area."'");
}
if (isset($requestData->area_en)) {
array_push($updateQueries, "area_en = '".$requestData->area_en."'");
}
if (isset($requestData->lat)) {
array_push($updateQueries, "lat = ".$requestData->lat);
}
if (isset($requestData->lng)) {
array_push($updateQueries, "lng = ".$requestData->lng);
}
if (isset($requestData->google_id)) {
array_push($updateQueries, "google_id = '".$requestData->google_id."'");
}
if (isset($requestData->google_location_type)) {
array_push($updateQueries, "google_location_type = '".$requestData->google_location_type."'");
}
if (isset($requestData->google_name)) {
array_push($updateQueries, "google_name = '".$requestData->google_name."'");
}
if (isset($requestData->google_address)) {
array_push($updateQueries, "google_address_data = '".serialize($requestData->google_address)."'");
}
if (isset($requestData->commentary)) {
array_push($updateQueries, "commentary = '".$requestData->commentary."'");
}
$sql = "UPDATE places SET ".implode(', ', $updateQueries)." WHERE id = ".$id;
$res = $db->query($sql);
echo json_encode_is(array(
'action' => 'putPlace',
'status' => 'success'
));
}
function combinePlaces($finalId) {
$ids = $_POST['ids'];
$db = getConnection();
foreach ($ids as $id) {
$db->query('UPDATE persons SET birthplace = '.$finalId.' WHERE birthplace = '.$id);
$db->query('UPDATE persons SET deathplace = '.$finalId.' WHERE deathplace = '.$id);
$db->query('UPDATE documents SET birthplace = '.$finalId.' WHERE birthplace = '.$id);
$db->query('UPDATE documents SET deathplace = '.$finalId.' WHERE deathplace = '.$id);
$db->query('UPDATE personplaces SET place = '.$finalId.' WHERE place = '.$id);
$db->query('UPDATE documentplaces SET place = '.$finalId.' WHERE place = '.$id);
if ($id != $finalId) {
$db->query('DELETE FROM places WHERE id = '.$id);
$db->query('DELETE FROM personplaces WHERE place = '.$id);
$db->query('DELETE FROM documentplaces WHERE place = '.$id);
}
}
echo json_encode_is(array(
'action' => 'combinePlaces',
'status' => 'success'
));
}
function combinePersons($finalId) {
$ids = $_POST['ids'];
$db = getConnection();
foreach ($ids as $id) {
$db->query('UPDATE persondocuments SET person = '.$finalId.' WHERE person = '.$id);
if ($id != $finalId) {
$db->query('DELETE FROM persons WHERE id = '.$id);
$db->query('DELETE FROM personplaces WHERE person = '.$id);
}
}
$finalPersonRes = $db->query('SELECT * FROM persons WHERE id = '.$finalId);
$finalPersonRow = $finalPersonRes->fetch_assoc();
$db->query("UPDATE personplaces SET place = ".$finalPersonRow['birthplace']." WHERE person = ".$finalId." AND relation = 'b'");
$db->query("UPDATE personplaces SET place = ".$finalPersonRow['deathplace']." WHERE person = ".$finalId." AND relation = 'd'");
echo json_encode_is(array(
'action' => 'combinePersons',
'status' => 'success'
));
}
// v2
function getMovementLocationsV2($yearFrom = null, $yearTo = null, $rangeType = null, $gender = null, $place = null, $placerelation = null, $name = null, $firstname = null, $surname = null, $archive = null) {
$db = getConnection();
$criteras = array();
array_push($criteras, "p1.lat IS NOT NULL");
array_push($criteras, "p1.lng IS NOT NULL");
array_push($criteras, "p2.lat IS NOT NULL");
array_push($criteras, "p2.lng IS NOT NULL");
if (!is_null($gender) && $gender != '') {
if ($gender == 'male') {
array_push($criteras, "persons.gender = 0");
}
else if ($gender == 'female') {
array_push($criteras, "persons.gender = 1");