stwalkerster /
waca
| 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 | * @param bool $forUpdate |
||
| 32 | * @return GeoLocation |
||
| 33 | */ |
||
| 34 | public static function getByAddress($address, PdoDatabase $database, $forUpdate = false) |
||
| 35 | { |
||
| 36 | $lockMode = $forUpdate ? ' FOR UPDATE' : ''; |
||
| 37 | $sql = "SELECT * FROM geolocation WHERE address = :id LIMIT 1" . $lockMode; |
||
| 38 | |||
| 39 | $statement = $database->prepare($sql); |
||
| 40 | $statement->bindValue(":id", $address); |
||
| 41 | |||
| 42 | $statement->execute(); |
||
| 43 | |||
| 44 | $resultObject = $statement->fetchObject(get_called_class()); |
||
| 45 | |||
| 46 | if ($resultObject != false) { |
||
| 47 | $resultObject->setDatabase($database); |
||
| 48 | } |
||
| 49 | |||
| 50 | return $resultObject; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function save() |
||
| 54 | { |
||
| 55 | if ($this->isNew()) { |
||
| 56 | // insert |
||
| 57 | $statement = $this->dbObject->prepare("INSERT INTO `geolocation` (address, data) VALUES (:address, :data) ON DUPLICATE KEY UPDATE address = address;"); |
||
| 58 | |||
| 59 | $statement->bindValue(":address", $this->address); |
||
| 60 | $statement->bindValue(":data", $this->data); |
||
| 61 | |||
| 62 | if ($statement->execute()) { |
||
| 63 | $this->id = (int)$this->dbObject->lastInsertId(); |
||
| 64 | } |
||
| 65 | else { |
||
| 66 | throw new Exception($statement->errorInfo()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 67 | } |
||
| 68 | } |
||
| 69 | else { |
||
| 70 | // update |
||
| 71 | $statement = $this->dbObject->prepare(<<<SQL |
||
| 72 | UPDATE `geolocation` |
||
| 73 | SET address = :address, data = :data, updateversion = updateversion + 1 |
||
| 74 | WHERE id = :id AND updateversion = :updateversion; |
||
| 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 |