forked from emijrp/commons-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajaximages.php
161 lines (142 loc) · 4.92 KB
/
ajaximages.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
<?php
/* Original code: https://github.com/chillly/plaques/blob/master/ajax/ajxplaques.php
*
* Created by Chris Hill <[email protected]> and contributors.
* Adapted for Wiki Loves Monuments by Emijrp <[email protected]>
*
* This software and associated documentation files (the "Software") is
* released under the CC0 Public Domain Dedication, version 1.0, as
* published by Creative Commons. To the extent possible under law, the
* author(s) have dedicated all copyright and related and neighboring
* rights to the Software to the public domain worldwide. The Software is
* distributed WITHOUT ANY WARRANTY.
*
* If you did not receive a copy of the CC0 Public Domain Dedication
* along with the Software, see
* <http://creativecommons.org/publicdomain/zero/1.0/>
*/
ini_set('display_errors', 1);
error_reporting(E_ALL);
/*
* ajaximages.php
* returns images points as geojson
*/
if (isset($_GET['bbox'])) {
$bbox=$_GET['bbox'];
} else {
// invalid request
$ajaxres=array();
$ajaxres['resp']=4;
$ajaxres['dberror']=0;
$ajaxres['msg']='missing bounding box';
sendajax($ajaxres);
}
// split the bbox into it's parts
list($left,$bottom,$right,$top)=explode(",",$bbox);
// open the database
$dbmycnf = parse_ini_file("../replica.my.cnf");
$dbuser = $dbmycnf['user'];
$dbpass = $dbmycnf['password'];
unset($dbmycnf);
$dbhost = "commonswiki.labsdb";
$dbname = "commonswiki_p";
try {
$db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.';charset=utf8', $dbuser, $dbpass);
} catch(PDOException $e) {
// send the PDOException message
$ajaxres=array();
$ajaxres['resp']=40;
$ajaxres['dberror']=$e->getCode();
$ajaxres['msg']=$e->getMessage();
sendajax($ajaxres);
}
// get featured and quality images for this region
try {
$sql="SELECT page_title, cl_to FROM geo_tags, page, categorylinks WHERE gt_page_id=page_id AND cl_from=page_id AND page_namespace=6 AND gt_lon>=:left AND gt_lon<=:right AND gt_lat>=:bottom AND gt_lat<=:top AND (cl_to='Featured_pictures_on_Wikimedia_Commons' OR cl_to='Quality_images')";
$stmt = $db->prepare($sql);
$stmt->bindParam(':left', $left, PDO::PARAM_STR);
$stmt->bindParam(':right', $right, PDO::PARAM_STR);
$stmt->bindParam(':bottom', $bottom, PDO::PARAM_STR);
$stmt->bindParam(':top', $top, PDO::PARAM_STR);
$stmt->execute();
} catch(PDOException $e) {
// send the PDOException message
$ajaxres=array();
$ajaxres['resp']=40;
$ajaxres['dberror']=$e->getCode();
$ajaxres['msg']=$e->getMessage();
sendajax($ajaxres);
}
$featured=array();
$quality=array();
// go through the list adding each one to the array to be returned
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$page_title=$row['page_title'];
$cl_to=$row['cl_to'];
if ($cl_to == 'Featured_pictures_on_Wikimedia_Commons') {
$featured[]=$page_title;
}else if ($cl_to == 'Quality_images') {
$quality[]=$page_title;
}
}
// get all images for this region
try {
$limit = 250;
$sql="SELECT page_title, gt_lat, gt_lon, page_random FROM geo_tags, page WHERE gt_page_id=page_id AND page_namespace=6 AND gt_lon>=:left AND gt_lon<=:right AND gt_lat>=:bottom AND gt_lat<=:top ORDER BY page_random LIMIT ".$limit;
$stmt = $db->prepare($sql);
$stmt->bindParam(':left', $left, PDO::PARAM_STR);
$stmt->bindParam(':right', $right, PDO::PARAM_STR);
$stmt->bindParam(':bottom', $bottom, PDO::PARAM_STR);
$stmt->bindParam(':top', $top, PDO::PARAM_STR);
$stmt->execute();
} catch(PDOException $e) {
// send the PDOException message
$ajaxres=array();
$ajaxres['resp']=40;
$ajaxres['dberror']=$e->getCode();
$ajaxres['msg']=$e->getMessage();
sendajax($ajaxres);
}
$ajaxres=array(); // place to store the geojson result
$features=array(); // array to build up the feature collection
$ajaxres['type']='FeatureCollection';
// go through the list adding each one to the array to be returned
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$lat=$row['gt_lat'];
$lon=$row['gt_lon'];
$prop=array();
$prop['image']=str_replace(' ', '_', $row['page_title']);
$prop['md5']=substr(md5($prop['image']),0,2);
if (in_array($prop['image'], $featured)){
$prop['f']=1;
}else{
$prop['f']=0;
}
if (in_array($prop['image'], $quality)){
$prop['q']=1;
}else{
$prop['q']=0;
}
$f=array();
$geom=array();
$coords=array();
$geom['type']='Point';
$coords[0]=floatval($lon);
$coords[1]=floatval($lat);
$geom['coordinates']=$coords;
$f['type']='Feature';
$f['geometry']=$geom;
$f['properties']=$prop;
$features[]=$f;
}
// add the features array to the end of the ajxres array
$ajaxres['features']=$features;
// tidy up the DB
$db = null;
sendajax($ajaxres); // no return from there
function sendajax($ajax) {
// encode the ajx array as json and return it.
$encoded = json_encode($ajax);
exit($encoded);
}
?>