-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdomapper.class.php
439 lines (377 loc) · 16.7 KB
/
pdomapper.class.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
<?php
/**
* Parser that uses data from a semantic wiki and outputs an
* XML file for import into plurio.net
*
* @author David Raison <david@hackerspace.lu>
* @file pdomapper.class.php
* @ingroup plurioparser
*/
class PDOMapper implements Interface_DataSource {
private $_dbh; // db handle
private $_tEvents;
private $_tLocations;
protected static $_dbData;
public function __construct(){
global $config;
try {
$this->_dbh = new PDO( $config['data.source'], $config['data.user'], $config['data.pass'] );
$this->_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
printf( "FATAL ERROR: %s. ABORTING\n" , $e->getMessage());
exit(0);
}
// set event and location tables
$this->_tEvents = $config['tables.events'];
$this->_tLocations = $config['tables.locations'];
// build the data cache
if( !isset( self::$_dbData ) )
self::$_dbData = array();
}
/**
* Retrieve initial activity data from the database
* Build the appropriate query
*/
public function getInitialData() {
// restrictions to be applied to the data queried
$filter = array(
'Internet' => 1,
'DateDebut' => array('>', date( 'Y-m-d H:i:s' ) )
);
// fields to be retrieved (missing: has URL, has ticket url, has subtitle)
$keys = array(
'IDAct',
'CAST(nom as varchar(255)) AS nom', // label
'DateDebut', // Has StartDate
'DateFin', // Has EndDate
'Heure',
'Heure2',
'CAST(Description AS text) AS Description', // Has description
'CAST(DescriptionFR AS text) AS DescriptionFR',
'CAST(Categorie AS varchar(50)) AS Categorie', // Has_category
'CAST(cat1 AS varchar(28)) AS cat1',
'CAST(cat2 AS varchar(28)) AS cat2',
'CAST(cat3 AS varchar(4)) AS cat3', // language, not currently used
'CAST(TrancheAge AS varchar(12)) AS TrancheAge', // NO CORRESPONDING ITEM in smw
'IDlieu', // Has location (yes, we use an ID here)
'CAST(Organisateur AS varchar(50)) AS Organisateur', // Has organizer
'CAST(Organisateur2 AS varchar(50)) AS Organisateur2', // Has organizer
'CAST(Responsables1 AS varchar(90)) AS Responsables1', // no equiv
'CAST(Responsables2 AS varchar(90)) AS Responsables2', // no equiv
'CAST(Image AS varchar(54)) AS Image', // Has picture
'Prix' // Has cost
);
$options = "ORDER BY DateDebut ASC";
$initial = $this->_doQuery( $keys, $filter, $this->_tEvents, 'PDOEventItem', $options );
$wrapper = new stdClass;
$wrapper->items = $initial;
return $wrapper;
}
/**
* FIXME: this is ugly, very ugly
*/
public function getIdFor( $entity, $caller ) {
global $config;
if ( $config['debug'] ) printf( "Method %s called for entity %s of type %s\n", __METHOD__, $entity, get_class( $caller ) );
if ( get_class( $caller ) == 'Organisation' ) {
// returning plurio IDs, see addToGuide() in organisation.class and the change in event.class
switch( $entity ) {
case "'natur musée'":
return 29699; // FIXME, Building ID??
break;
case 'Panda-Club':
return 46093;
break;
case 'Science-Club':
return 46095;
break;
}
} elseif ( get_class( $caller ) == 'Building' ) {
return $entity;
} else {
$pdoitem = 'PDOEventID';
$table = $this->_tEvents;
$keys = array( 'IDAct' );
$filter = array( 'nom' => $entity );
$data = $this->_doQuery( $keys, $filter, $table, $pdoitem );
return $data[0]->IDAct;
}
}
/**
* Retrieve information on a location using the database connection
* hmm... would be better to use the ID... but that would require changes in the building class...
* FIXME: missing: picture, url, email, phone)
*/
public function fetchLocationInfo( $idlieu ) {
$keys = array(
'ID_plurio', // plurio ID (optional)
'lieu', // label
'numero', // has address
'rue', // has address
'cp', // has city
'ville', // has city
'pays', // has country
'affichageSN' // has_localDescription
);
$filter = array( 'IDlieu' => $idlieu );
$resultset = $this->_doQuery( $keys, $filter, $this->_tLocations, 'PDOLocationItem' );
return $resultset[0];
}
// we're just adding the url from the config
public function fetchPictureInfo( $file, $category ){
global $config;
$path = $config['media.path'];
$path = (substr( $path, -1) == '/' ) ? $path : $path . '/';
return $path . $category . '/' . $file;
}
public function fetchOrganisationInfo( $org ) {
global $config;
// FIXME (one config per organisation?)
if ($org == "'natur musée'" || "Panda-Club" || "Science-Club" ) {
// get organisation info from config file instead FIXME FIXME FIXME
$info = new StdClass;
$info->has_contact = array( $config['org.contact'] );
$info->has_description = array( $config['org.description'] );
$info->has_location = array( 234 ); // natur musée // FIXME
$info->has_picture = array( $config['org.logo'] );
$info->has_subtitle = array('Musée national d\'histoire naturelle'); //FIXME
$info->url = array( $config['org.url'] );
return $info;
}
}
/**
* Assemble and execute a query against the cache or againt the pdo database
*/
private function _doQuery( $keys, $filter, $table, $pdoitem = 'PDOEventItem', $options = null ) {
global $config;
$config['debug'] && $time_start = microtime(true);
// we will probably need to store information seperately for every combination of filters and keys
$idxhash = md5( implode('|', array_merge( $keys, $filter ) ) );
if( array_key_exists( $idxhash, self::$_dbData ) ) {
if ( $config['debug'] ) printf("Retrieved data of type %s from cache. idxhash: %s \o/\n", $pdoitem, $idxhash );
return self::$_dbData[ $idxhash ];
} else {
if ( $config['debug'] ) printf( "Retrieving fresh data of type %s from database. idxhash: %s\n", $pdoitem, $idxhash );
$where = array();
foreach ( $filter as $key => $value ) {
if ( is_string( $value ) || is_int( $value ) ) {
// update 12.12.12 $where[] = sprintf("%s = '%s'", $key, iconv( 'UTF-8','ISO-8859-1', $value ) ); // FIXME
$where_keys[] = sprintf("%s = :%s", $key, $key);
$where_values[ ":$key" ] = iconv( 'UTF-8','ISO-8859-1', $value );
//$where_values[] = array( ":$key" => iconv( 'UTF-8','ISO-8859-1', $value ) );
} elseif( is_array( $value ) ) {
// id $where[] = sprintf("%s %s '%s'", $key, $value[0], $value[1]);
$where_keys[] = sprintf("%s %s :%s", $key, $value[0], $key);
$where_values[ ":$key" ] = $value[1];
} else throw new Exception( sprintf("Got an invalid filter object: %s\n", print_r( $value ) ) );
}
$template = 'SELECT %1$s FROM %2$s WHERE %3$s %4$s;';
$query = sprintf( $template,
implode( ', ',$keys ),
$table,
implode( ' AND ', $where_keys ),
$options
);
$config['debug'] && print($query . "\n");
$qq = $this->_dbh->prepare( $query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY) );
$qq->execute( $where_values );
$data = $qq->fetchAll( PDO::FETCH_CLASS, $pdoitem );
if ( $data ) {
self::$_dbData[ $idxhash ] = $data;
if ( $config['debug'] ) {
$exectime = microtime(true) - $time_start;
printf("Query took %s seconds\n", round( $exectime, 2 ) );
}
return $data;
} else throw new Exception( sprintf( "Could not retrieve data from database. Query: %s\n", $query ) );
}
}
}
class PDOEventID {
}
class PDOEventItem {
/**
* This is where we're doing the actual mapping to match the smw object
*/
private $_pandaWeb = 'www.panda-club.lu';
private $_scienceWeb = 'www.science-club.lu';
private $_mnhnWeb = 'www.mnhn.lu';
private $_pandaSignUp = 'http://www.panda-club.lu/umeldung/login/';
private $_pandaMail = 'panda-club@mnhn.lu';
private $_scienceSignUp = 'http://www.science-club.lu/umeldung/login/';
private $_scienceMail = 'science-club@mnhn.lu';
private $_mnhnMail = 'musee-info@mnhn.lu';
//FIXME: this mapping would be much better off in the config file!!
public function __construct() {
// setting data as first element of an array is necessary since the mediawiki
// json export has these things exported as arrays as well. Both need to have the same structure
// and since we're already mapping these to smw, we're changing everything here and nothing there
!empty( $this->nom ) && $this->label = $this->_ic( $this->nom );
!empty( $this->DateDebut ) && $this->startdate[0] = $this->_createDateArray( $this->_ic( $this->DateDebut ), $this->_ic( $this->Heure ) );
if ( !empty( $this->DateFin ) ) {
$this->enddate[0] = $this->_createDateArray( $this->_ic( $this->DateFin ), $this->_ic( $this->Heure2 ) );
} else {
$this->enddate[0] = $this->_createDateArray( $this->_ic( $this->DateDebut), $this->_ic( $this->Heure2) );
}
// MNHN would like their lb description to be also considered for the german version of the website
if( !empty( $this->Description ) ) {
$this->has_description['lb'] = $this->_ic( $this->Description );
$this->has_description['de'] = $this->_ic( $this->Description );
}
!empty( $this->DescriptionFR ) && $this->has_description['fr'] = $this->_ic( $this->DescriptionFR );
// cat3 is the language ("L"), though it's not currently being used. It could also be renamed in the query
for( $i = 1; $i < 4; $i++ ) {
$val = 'cat' . $i;
!empty( $this->$val ) && $this->category[] = $this->_ic( $this->$val );
}
// data relative to the category
if( !empty( $this->Categorie ) && $this->Categorie != "MNHN" ) {
$this->has_organizer[0] = $this->_ic( $this->Categorie );
$this->has_ticket_url[0] = ( $this->Categorie == 'Panda-Club' ) ? $this->_pandaSignUp : $this->_scienceSignUp;
$this->has_website[0] = ( $this->Categorie == 'Panda-Club' ) ? $this->_pandaWeb : $this->_scienceWeb;
$this->has_contact[0] = ( $this->Categorie == 'Panda-Club' ) ? $this->_pandaMail : $this->_scienceMail;
} else {
$this->has_organizer[0] = "'natur musée'";
$this->has_contact[0] = $this->_mnhnMail;
$this->has_website[0] = $this->_mnhnWeb;
}
// Add the organiser to the categories, since it influences them
!empty( $this->has_organizer[0] ) && $this->category[] = $this->has_organizer[0];
!empty( $this->TrancheAge ) && $this->is_event_of_type[0] = $this->_ic( $this->TrancheAge );
!empty( $this->IDlieu ) && $this->has_location_id[0] = $this->_ic( $this->IDlieu );
!empty( $this->Prix ) && $this->has_cost[0] = $this->Prix;
// Update 11.12.2012 -> Add Responsables1 and Responsables2 to description text
!empty( $this->Responsables1 ) && $this->is_in_charge[0] = $this->_ic( $this->Responsables1 );
!empty( $this->Responsables2 ) && $this->is_in_charge[1] = $this->_ic( $this->Responsables2 );
if( $this->Categorie == "MNHN" ) {
// ignore Responsables2, which is actually used as "Destinataires"
$this->has_description['lb'] .= '<p>Responsabel: ' . $this->is_in_charge[0];
( $this->DescriptionFR ) && $this->has_description['fr'] .= '<p>Responsable(s): ' . $this->is_in_charge[0];
$this->has_description['de'] .= '<p>Leitung: ' . $this->is_in_charge[0];
} else {
$this->has_description['lb'] .= '<p>Responsabel: ' . $this->is_in_charge[0];
isset( $this->is_in_charge[1] ) && $this->has_description['lb'] .= ', ' . $this->is_in_charge[1];
$this->has_description['de'] .= '<p>Leitung: ' . $this->is_in_charge[0];
isset( $this->is_in_charge[1] ) && $this->has_description['de'] .= ', ' . $this->is_in_charge[1];
if( $this->DescriptionFR ) {
isset( $this->is_in_charge[0] ) && $addon = 'Responsable: ' . $this->is_in_charge[0];
isset( $this->is_in_charge[1] ) && $addon = 'Responsables: '
. $this->is_in_charge[0]
. ', ' . $this->is_in_charge[1];
$this->has_description['fr'] .= '<p>' . $addon;
}
}
// For organizers other than "natur musée", add a snippet to the description text.
// shortening some vars for better readability
!empty( $this->Organisateur ) && $org = $this->_ic( $this->Organisateur );
!empty( $this->Organisateur2 ) && $org2 = $this->_ic( $this->Organisateur2 );
$tests = array( "'natur musée'", "Musée national d'histoire naturelle" );
$support = '';
// Initially, did not consider natur musée a co-organiser
/*
* $support .= ( $org && !in_array( $org, $tests ) ) ? $org : '';
* if ( $org2 && !in_array( $org2, $tests ) ) {
*/
// Now even natur musée is considered a co-organiser, but not if it's already the organiser
$support .= ( $org && $org != $this->has_organizer[0] ) ? $org : '';
if ( $org2 && $org2 != $this->has_organizer[0] ) {
$support .= empty( $support) ? $org2 : ' & ' . $org2;
}
/*
* Science-Club
* Workshop / 13-15 Joer (Anmeldung erforderlich / Inscription obligatoire)
*
* Additional info: cat1 contains values such as workshop, exhibition, camp, etc
*/
if( $this->has_organizer[0] != "'natur musée'" ) {
$this->has_subtitle[0] = $this->has_organizer[0]
. "<br/>" . ucfirst( $this->_ic( $this->cat1 ) )
. " / " . $this->TrancheAge . " Joer"
. " / (Anmeldung erforderlich / Inscription obligatoire)";
$this->has_description['lb'] .= '<br/>Eng Aktivitéit vum '
. $this->has_organizer[0]
.' fir Jonker vun '
. $this->TrancheAge . ' Joer.';
$this->has_description['de'] .= '<br/>Eine Veranstaltung des '
. $this->has_organizer[0]
.' für junge Leute zwischen '
. $this->TrancheAge . ' Jahren';
$this->has_description['fr'] .= '<br/>Une activité du '
. $this->has_organizer[0]
.' pour jeunes âgés de '
. $this->TrancheAge . ' ans.';
} else {
$this->has_subtitle[0] = ucfirst( $this->_ic( $this->cat1 ) )
. " / " . $this->TrancheAge;
}
// Add support text (if we have some)
if ( $support ) {
$this->has_description['lb'] .= '<br/>Mat der Ennerstëtzung vu <i>'
. $support
. '</i>';
$this->has_description['de'] .= '<br/>Mit freundlicher Unterstützung von <i>'
. $support
. '</i>';
!empty($this->DescriptionFR) && $this->has_description['fr'] .= '<br/>Avec le soutien de <i>'
. $support
. '</i>';
}
// Add website
$this->has_description['lb'] .= '.<br/>Méi Informatiounen op <a href="http://'
. $this->has_website[0]
. '" target="_blank" title="Websäit vum ' . $this->has_organizer[0] .'">'
. $this->has_website[0]
.'</a></p>';
$this->has_description['de'] .= '.<br/>Zusätzliche Informatiounen auf <a href="http://'
. $this->has_website[0]
. '" target="_blank" title="Webseite des ' . $this->has_organizer[0] .'">'
. $this->has_website[0]
.'</a></p>';
// Add website
$this->has_description['fr'] .= '.<br/>Plus d\'infos sur <a href="http://'
. $this->has_website[0]
. '" target="_blank" title="Page web du ' . $this->has_organizer[0] .'">'
. $this->has_website[0]
.'</a></p>';
// Set an illustrative picture, if available
!empty( $this->Image ) && $this->has_picture[0] = $this->Image;
}
/**
* Using convert() might be an option, but it seems that using php conversion will be easier
* http://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
* See also wikiApiClient.class.php --> _parseDate() method
*/
private function _createDateArray( $date, $time ) {
$datestring = strtotime( $date );
$date = date( "Y-m-d", $datestring );
$timestring = strtotime( $time );
$time = date( "H:i", $timestring );
$datetime = array( $date, $time );
return $datetime;
}
/**
* Map SQLServer Latin_1 collation to Unicode
*/
private function _ic( $val ){
global $config;
return ( $config['data.iconv'] ) ? iconv( 'ISO-8859-1', 'UTF-8', $val) : $val;
}
}
class PDOLocationItem {
public function __construct() {
$this->label = $this->_ic( $this->lieu );
$this->has_number = $this->_ic( $this->numero );
$this->has_address = $this->_ic( $this->rue );
$this->has_zipcode = $this->_ic( $this->cp );
$this->has_city = $this->_ic( $this->ville );
$this->has_country = $this->_ic( $this->pays );
$this->has_localDescription[0] = ( !empty($this->affichageSN) ) ? $this->_ic( $this->affichageSN ) : $this->label;
}
/**
* Map SQLServer Latin_1 collation to Unicode
*/
private function _ic( $val ){
global $config;
return ( $config['data.iconv'] ) ? trim( iconv( 'ISO-8859-1', 'UTF-8', $val) ) : $val;
}
}