SWLEdit::writeToDB()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * 
5
 * 
6
 * @since 0.1
7
 * 
8
 * @file SWL_Edit.php
9
 * @ingroup SemanticWatchlist
10
 * 
11
 * @licence GNU GPL v3 or later
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class SWLEdit {
15
	
16
	/**
17
	 * The ID of the page the edit was made to.
18
	 * 
19
	 * @var integer
20
	 */
21
	private $pageId;
22
	
23
	/**
24
	 * The name of the user that made the edit.
25
	 * 
26
	 * @var string
27
	 */
28
	private $userName;
29
	
30
	/**
31
	 * The user that made the changes.
32
	 * 
33
	 * @var User or false
34
	 */
35
	private $user = false;
36
	
37
	/**
38
	 * The time on which the edit was made.
39
	 * 
40
	 * @var integer
41
	 */
42
	private $time;
43
	
44
	/**
45
	 * DB ID of the edit (swl_edits.edit_id).
46
	 * 
47
	 * @var integer
48
	 */
49
	private $id;
50
	
51
	/**
52
	 * Creates and returns a new instance of SWLEdit by getting it's info from the database.
53
	 * 
54
	 * @since 0.1 
55
	 * 
56
	 * @param integer $id
57
	 * 
58
	 * @return SWLEdit
59
	 */
60
	public static function newFromId( $id ) {
61
		$dbr = wfGetDB( DB_REPLICA );
62
		
63
		return self::newFromDBResult( $dbr->select(
64
			'swl_edits',
65
			array(
66
				'edit_id',
67
				'edit_user_name',
68
				'edit_page_id',
69
				'edit_time'
70
			),
71
			array( 'edit_id' => $id )
72
		) );
73
	}
74
	
75
	/**
76
	 * Creates and returns a new instance of SWLEdit from a database result.
77
	 * 
78
	 * @since 0.1 
79
	 * 
80
	 * @param ResultWrapper $edit
81
	 * 
82
	 * @return SWLEdit
83
	 */
84
	public static function newFromDBResult( $edit ) {
85
		return new self(
86
			$edit->edit_page_id,
87
			$edit->edit_user_name,
88
			$edit->edit_time,
89
			$edit->edit_id
90
		);
91
	}
92
	
93
	/**
94
	 * Constructor.
95
	 * 
96
	 * @since 0.1
97
	 */
98
	public function __construct( $pageId, $userName, $time, $id = null ) {
99
		$this->pageId = $pageId;
100
		$this->userName = $userName;
101
		$this->time = $time;
102
		$this->id = $id;
103
	}
104
	
105
	/**
106
	 * Writes the edit to the database, either updating it
107
	 * when it already exists, or inserting it when it doesn't.
108
	 * 
109
	 * @since 0.1
110
	 * 
111
	 * @return boolean Success indicator
112
	 */
113
	public function writeToDB() {
114
		if ( is_null( $this->id ) ) {
115
			return $this->insertIntoDB();
116
		}
117
		else {
118
			return  $this->updateInDB();
119
		}
120
	}
121
	
122
	/**
123
	 * Updates the group in the database.
124
	 * 
125
	 * @since 0.1
126
	 * 
127
	 * @return boolean Success indicator
128
	 */
129
	private function updateInDB() {
130
		$dbr = wfGetDB( DB_MASTER );
131
		
132
		return  $dbr->update(
133
			'swl_edits',
134
			array(
135
				'edit_user_name' => $this->userName,
136
				'edit_page_id' => $this->pageId,
137
				'edit_time' => $this->time
138
			),
139
			array( 'edit_id' => $this->id )
140
		);
141
	}
142
	
143
	/**
144
	 * Inserts the group into the database.
145
	 * 
146
	 * @since 0.1
147
	 * 
148
	 * @return boolean Success indicator
149
	 */
150
	private function insertIntoDB() {
151
		wfRunHooks( 'SWLBeforeEditInsert', array( &$this ) );
152
		
153
		$dbr = wfGetDB( DB_MASTER );
154
		
155
		$result = $dbr->insert(
156
			'swl_edits',
157
			array(
158
				'edit_user_name' => $this->userName,
159
				'edit_page_id' => $this->pageId,
160
				'edit_time' => $this->time
161
			)
162
		);
163
		
164
		$this->id = $dbr->insertId();
165
		
166
		wfRunHooks( 'SWLAfterEditInsert', array( &$this ) );
167
		
168
		return $result;
169
	}
170
	
171
	/**
172
	 * Returns the edit database id (swl_edits.edit_id).
173
	 * 
174
	 * @since 0.1
175
	 * 
176
	 * @return integer
177
	 */
178
	public function getId() {
179
		return $this->id;
180
	}
181
	
182
	/**
183
	 * Returns the ID of the page the edit was made to.
184
	 * 
185
	 * @since 0.1
186
	 * 
187
	 * @return integer
188
	 */
189
	public function getPageId() {
190
		return $this->pageId;
191
	}
192
	
193
	/**
194
	 * Gets the title of the page these changes belong to.
195
	 * 
196
	 * @since 0.1
197
	 * 
198
	 * @return Title
199
	 */
200
	public function getTitle() {
201
		return Title::newFromID( $this->pageId );
202
	}
203
	
204
	/**
205
	 * Gets the name of the user that made the changes.
206
	 * 
207
	 * @since 0.1
208
	 * 
209
	 * @return string
210
	 */
211
	public function getUserName() {
212
		return $this->userName;
213
	}
214
	
215
	/**
216
	 * Gets the user that made the changes.
217
	 * 
218
	 * @since 0.1
219
	 * 
220
	 * @return User
221
	 */
222
	public function getUser() {
223
		if ( $this->user === false ) {
224
			$this->user = User::newFromName( $this->userName, false );
225
		}
226
		
227
		return $this->user;
228
	}
229
	
230
	/**
231
	 * Gets the time on which the changes where made.
232
	 * 
233
	 * @since 0.1
234
	 * 
235
	 * @return integer
236
	 */
237
	public function getTime() {
238
		return $this->time;
239
	}
240
	
241
}