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; |
10
|
|
|
|
11
|
|
|
use Waca\Exceptions\OptimisticLockFailedException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* DataObject is the base class for all the database access classes. Each |
15
|
|
|
* "DataObject" holds one record from the database, and provides functions to |
16
|
|
|
* allow loading from and saving to the database. |
17
|
|
|
* |
18
|
|
|
* Note: This requires the database tables to be named the same as the classes, |
19
|
|
|
* and the database tables must have an "id" column. Simple views can be used |
20
|
|
|
* as a way of aliasing to allow for a transition period. |
21
|
|
|
* |
22
|
|
|
* @author Simon Walker |
23
|
|
|
*/ |
24
|
|
|
abstract class DataObject |
25
|
|
|
{ |
26
|
|
|
/** @var int ID of the object */ |
27
|
|
|
protected $id = null; |
28
|
|
|
/** @var int update version for optimistic locking */ |
29
|
|
|
protected $updateversion = 0; |
30
|
|
|
/** |
31
|
|
|
* @var PdoDatabase |
32
|
|
|
*/ |
33
|
|
|
protected $dbObject; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Retrieves a data object by it's row ID. |
37
|
|
|
* |
38
|
|
|
* @param int $id |
39
|
|
|
* @param PdoDatabase $database |
40
|
|
|
* |
41
|
|
|
* @return DataObject|false |
42
|
|
|
*/ |
43
|
|
|
public static function getById($id, PdoDatabase $database) |
44
|
|
|
{ |
45
|
|
|
$array = explode('\\', get_called_class()); |
46
|
|
|
$realClassName = strtolower(end($array)); |
47
|
|
|
|
48
|
|
|
$statement = $database->prepare("SELECT * FROM {$realClassName} WHERE id = :id LIMIT 1;"); |
49
|
|
|
$statement->bindValue(":id", $id); |
50
|
|
|
|
51
|
|
|
$statement->execute(); |
52
|
|
|
|
53
|
|
|
$resultObject = $statement->fetchObject(get_called_class()); |
54
|
|
|
|
55
|
|
|
if ($resultObject != false) { |
56
|
|
|
$resultObject->setDatabase($database); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $resultObject; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function setDatabase(PdoDatabase $db) |
63
|
|
|
{ |
64
|
2 |
|
$this->dbObject = $db; |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the database associated with this data object. |
69
|
|
|
* @return PdoDatabase |
70
|
|
|
*/ |
71
|
1 |
|
public function getDatabase() |
72
|
|
|
{ |
73
|
1 |
|
return $this->dbObject; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Saves a data object to the database, either updating or inserting a record. |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
abstract public function save(); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieves the ID attribute |
85
|
|
|
*/ |
86
|
1 |
|
public function getId() |
87
|
|
|
{ |
88
|
1 |
|
return (int)$this->id; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Deletes the object from the database |
93
|
|
|
*/ |
94
|
|
|
public function delete() |
95
|
|
|
{ |
96
|
|
|
if ($this->id === null) { |
97
|
|
|
// wtf? |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$array = explode('\\', get_called_class()); |
102
|
|
|
$realClassName = strtolower(end($array)); |
103
|
|
|
|
104
|
|
|
$deleteQuery = "DELETE FROM {$realClassName} WHERE id = :id AND updateversion = :updateversion LIMIT 1;"; |
105
|
|
|
$statement = $this->dbObject->prepare($deleteQuery); |
106
|
|
|
|
107
|
|
|
$statement->bindValue(":id", $this->id); |
108
|
|
|
$statement->bindValue(":updateversion", $this->updateversion); |
109
|
|
|
$statement->execute(); |
110
|
|
|
|
111
|
|
|
if ($statement->rowCount() !== 1) { |
112
|
|
|
throw new OptimisticLockFailedException(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->id = null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
1 |
|
public function getUpdateVersion() |
122
|
|
|
{ |
123
|
1 |
|
return $this->updateversion; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sets the update version. |
128
|
|
|
* |
129
|
|
|
* You should never call this to change the value of the update version. You should only call it when passing user |
130
|
|
|
* input through. |
131
|
|
|
* |
132
|
|
|
* @param int $updateVersion |
133
|
|
|
*/ |
134
|
1 |
|
public function setUpdateVersion($updateVersion) |
135
|
|
|
{ |
136
|
1 |
|
$this->updateversion = $updateVersion; |
137
|
1 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
1 |
|
public function isNew() |
143
|
|
|
{ |
144
|
1 |
|
return $this->id === null; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|