|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\DataObjects; |
|
10
|
|
|
|
|
11
|
|
|
use DateTimeImmutable; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
use Waca\DataObject; |
|
14
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
|
15
|
|
|
use Waca\PdoDatabase; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Geolocation data object |
|
19
|
|
|
* |
|
20
|
|
|
* This is a cache. |
|
21
|
|
|
*/ |
|
22
|
|
|
class GeoLocation extends DataObject |
|
23
|
|
|
{ |
|
24
|
|
|
private $address; |
|
25
|
|
|
private $data; |
|
26
|
|
|
private $creation; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $address |
|
30
|
|
|
* @param PdoDatabase $database |
|
31
|
|
|
* |
|
32
|
|
|
* @return GeoLocation |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function getByAddress($address, PdoDatabase $database) |
|
35
|
|
|
{ |
|
36
|
|
|
$statement = $database->prepare("SELECT * FROM geolocation WHERE address = :id LIMIT 1;"); |
|
|
|
|
|
|
37
|
|
|
$statement->bindValue(":id", $address); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$statement->execute(); |
|
40
|
|
|
|
|
41
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
|
42
|
|
|
|
|
43
|
|
|
if ($resultObject != false) { |
|
44
|
|
|
$resultObject->setDatabase($database); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $resultObject; |
|
48
|
|
|
} |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function save() |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->isNew()) { |
|
53
|
|
|
// insert |
|
54
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
55
|
|
|
INSERT INTO `geolocation` (address, data) VALUES (:address, :data); |
|
56
|
|
|
SQL |
|
57
|
|
|
); |
|
58
|
|
|
$statement->bindValue(":address", $this->address); |
|
|
|
|
|
|
59
|
|
|
$statement->bindValue(":data", $this->data); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if ($statement->execute()) { |
|
62
|
|
|
$this->id = (int)$this->dbObject->lastInsertId(); |
|
63
|
|
|
} |
|
64
|
|
|
else { |
|
65
|
|
|
throw new Exception($statement->errorInfo()); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
else { |
|
69
|
|
|
// update |
|
70
|
|
|
$statement = $this->dbObject->prepare(<<<SQL |
|
71
|
|
|
UPDATE `geolocation` |
|
72
|
|
|
SET address = :address, data = :data, updateversion = updateversion + 1 |
|
73
|
|
|
WHERE id = :id AND updateversion = :updateversion |
|
74
|
|
|
LIMIT 1; |
|
75
|
|
|
SQL |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$statement->bindValue(":id", $this->id); |
|
|
|
|
|
|
79
|
|
|
$statement->bindValue(":updateversion", $this->updateversion); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$statement->bindValue(":address", $this->address); |
|
|
|
|
|
|
82
|
|
|
$statement->bindValue(":data", $this->data); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (!$statement->execute()) { |
|
85
|
|
|
throw new Exception($statement->errorInfo()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($statement->rowCount() !== 1) { |
|
89
|
|
|
throw new OptimisticLockFailedException(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->updateversion++; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
public function getAddress() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->address; |
|
99
|
|
|
} |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $address |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setAddress($address) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->address = $address; |
|
107
|
|
|
} |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getData() |
|
113
|
|
|
{ |
|
114
|
|
|
return unserialize($this->data); |
|
115
|
|
|
} |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param array $data |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setData($data) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->data = serialize($data); |
|
123
|
|
|
} |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/** @return DateTimeImmutable */ |
|
126
|
|
|
public function getCreation() |
|
127
|
|
|
{ |
|
128
|
|
|
return new DateTimeImmutable($this->creation); |
|
129
|
|
|
} |
|
|
|
|
|
|
130
|
|
|
} |
|
|
|
|
|
|
131
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'or with double quotes"literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\') and the backslash (\\). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is ValueIf your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.