e7d16257a0b2ac07a1db324934e5fe7b363e7003
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2 /**
3 *
4 */
5
6 /**
7 * Various globals
8 */
9 define( 'RC_EDIT', 0);
10 define( 'RC_NEW', 1);
11 define( 'RC_MOVE', 2);
12 define( 'RC_LOG', 3);
13 define( 'RC_MOVE_OVER_REDIRECT', 4);
14
15
16 /**
17 * Utility class for creating new RC entries
18 * mAttributes:
19 * rc_id id of the row in the recentchanges table
20 * rc_timestamp time the entry was made
21 * rc_cur_time timestamp on the cur row
22 * rc_namespace namespace #
23 * rc_title non-prefixed db key
24 * rc_type is new entry, used to determine whether updating is necessary
25 * rc_minor is minor
26 * rc_cur_id id of associated cur entry
27 * rc_user user id who made the entry
28 * rc_user_text user name who made the entry
29 * rc_comment edit summary
30 * rc_this_oldid old_id associated with this entry (or zero)
31 * rc_last_oldid old_id associated with the entry before this one (or zero)
32 * rc_bot is bot, hidden
33 * rc_ip IP address of the user in dotted quad notation
34 * rc_new obsolete, use rc_type==RC_NEW
35 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
36 *
37 * mExtra:
38 * prefixedDBkey prefixed db key, used by external app via msg queue
39 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
40 * lang the interwiki prefix, automatically set in save()
41 *
42 * @todo document functions and variables
43 */
44 class RecentChange
45 {
46 var $mAttribs = array(), $mExtra = array();
47 var $mTitle = false, $mMovedToTitle = false;
48
49 # Factory methods
50
51 /* static */ function newFromRow( $row )
52 {
53 $rc = new RecentChange;
54 $rc->loadFromRow( $row );
55 return $rc;
56 }
57
58 /* static */ function newFromCurRow( $row )
59 {
60 $rc = new RecentChange;
61 $rc->loadFromCurRow( $row );
62 return $rc;
63 }
64
65 # Accessors
66
67 function setAttribs( $attribs )
68 {
69 $this->mAttribs = $attribs;
70 }
71
72 function setExtra( $extra )
73 {
74 $this->mExtra = $extra;
75 }
76
77 function getTitle()
78 {
79 if ( $this->mTitle === false ) {
80 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
81 }
82 return $this->mTitle;
83 }
84
85 function getMovedToTitle()
86 {
87 if ( $this->mMovedToTitle === false ) {
88 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
89 $this->mAttribs['rc_moved_to_title'] );
90 }
91 return $this->mMovedToTitle;
92 }
93
94 # Writes the data in this object to the database
95 function save()
96 {
97 global $wgUseRCQueue, $wgRCQueueID, $wgLocalInterwiki, $wgPutIPinRC;
98 $fname = 'RecentChange::save';
99
100 $dbw =& wfGetDB( DB_MASTER );
101 if ( !is_array($this->mExtra) ) {
102 $this->mExtra = array();
103 }
104 $this->mExtra['lang'] = $wgLocalInterwiki;
105
106 if ( !$wgPutIPinRC ) {
107 $this->mAttribs['rc_ip'] = '';
108 }
109
110 # Fixup database timestamps
111 $this->mAttribs['rc_timestamp']=$dbw->timestamp($this->mAttribs['rc_timestamp']);
112 $this->mAttribs['rc_cur_time']=$dbw->timestamp($this->mAttribs['rc_cur_time']);
113
114 # Insert new row
115 $dbw->insertArray( 'recentchanges', $this->mAttribs, $fname );
116
117 # Update old rows, if necessary
118 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
119 $oldid = $this->mAttribs['rc_last_oldid'];
120 $ns = $this->mAttribs['rc_namespace'];
121 $title = $this->mAttribs['rc_title'];
122 $lastTime = $this->mExtra['lastTimestamp'];
123 $now = $this->mAttribs['rc_timestamp'];
124 $curId = $this->mAttribs['rc_cur_id'];
125
126 # Update rc_this_oldid for the entries which were current
127 $dbw->updateArray( 'recentchanges',
128 array( /* SET */
129 'rc_this_oldid' => $oldid
130 ), array( /* WHERE */
131 'rc_namespace' => $ns,
132 'rc_title' => $title,
133 'rc_timestamp' => $dbw->timestamp($lastTime)
134 ), $fname
135 );
136
137 # Update rc_cur_time
138 $dbw->updateArray( 'recentchanges', array( 'rc_cur_time' => $now ),
139 array( 'rc_cur_id' => $curId ), $fname );
140 }
141
142 # Notify external application
143 if ( $wgUseRCQueue ) {
144 $queue = msg_get_queue( $wgRCQueueID );
145 if (!msg_send( $queue, array_merge( $this->mAttribs, 1, $this->mExtra ),
146 true, false, $error ))
147 {
148 wfDebug( "Error sending message to RC queue, code $error\n" );
149 }
150 }
151 }
152
153 # Marks a certain row as patrolled
154 function markPatrolled( $rcid )
155 {
156 $fname = 'RecentChange::markPatrolled';
157
158 $dbw =& wfGetDB( DB_MASTER );
159
160 $dbw->updateArray( 'recentchanges',
161 array( /* SET */
162 'rc_patrolled' => 1
163 ), array( /* WHERE */
164 'rc_id' => $rcid
165 ), $fname
166 );
167 }
168
169 # Makes an entry in the database corresponding to an edit
170 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
171 $oldId, $lastTimestamp, $bot = "default", $ip = '' )
172 {
173 if ( $bot == 'default ' ) {
174 $bot = $user->isBot();
175 }
176
177 if ( !$ip ) {
178 global $wgIP;
179 $ip = empty( $wgIP ) ? '' : $wgIP;
180 }
181
182 $rc = new RecentChange;
183 $rc->mAttribs = array(
184 'rc_timestamp' => $timestamp,
185 'rc_cur_time' => $timestamp,
186 'rc_namespace' => $title->getNamespace(),
187 'rc_title' => $title->getDBkey(),
188 'rc_type' => RC_EDIT,
189 'rc_minor' => $minor ? 1 : 0,
190 'rc_cur_id' => $title->getArticleID(),
191 'rc_user' => $user->getID(),
192 'rc_user_text' => $user->getName(),
193 'rc_comment' => $comment,
194 'rc_this_oldid' => 0,
195 'rc_last_oldid' => $oldId,
196 'rc_bot' => $bot ? 1 : 0,
197 'rc_moved_to_ns' => 0,
198 'rc_moved_to_title' => '',
199 'rc_ip' => $ip,
200 'rc_new' => 0 # obsolete
201 );
202
203 $rc->mExtra = array(
204 'prefixedDBkey' => $title->getPrefixedDBkey(),
205 'lastTimestamp' => $lastTimestamp
206 );
207 $rc->save();
208 }
209
210 # Makes an entry in the database corresponding to page creation
211 # Note: the title object must be loaded with the new id using resetArticleID()
212 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default", $ip='' )
213 {
214 if ( !$ip ) {
215 global $wgIP;
216 $ip = empty( $wgIP ) ? '' : $wgIP;
217 }
218 if ( $bot == 'default' ) {
219 $bot = $user->isBot();
220 }
221
222 $rc = new RecentChange;
223 $rc->mAttribs = array(
224 'rc_timestamp' => $timestamp,
225 'rc_cur_time' => $timestamp,
226 'rc_namespace' => $title->getNamespace(),
227 'rc_title' => $title->getDBkey(),
228 'rc_type' => RC_NEW,
229 'rc_minor' => $minor ? 1 : 0,
230 'rc_cur_id' => $title->getArticleID(),
231 'rc_user' => $user->getID(),
232 'rc_user_text' => $user->getName(),
233 'rc_comment' => $comment,
234 'rc_this_oldid' => 0,
235 'rc_last_oldid' => 0,
236 'rc_bot' => $bot ? 1 : 0,
237 'rc_moved_to_ns' => 0,
238 'rc_moved_to_title' => '',
239 'rc_ip' => $ip,
240 'rc_new' => 1 # obsolete
241 );
242
243 $rc->mExtra = array(
244 'prefixedDBkey' => $title->getPrefixedDBkey(),
245 'lastTimestamp' => 0
246 );
247 $rc->save();
248 }
249
250 # Makes an entry in the database corresponding to a rename
251 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
252 {
253 if ( !$ip ) {
254 global $wgIP;
255 $ip = empty( $wgIP ) ? '' : $wgIP;
256 }
257 $rc = new RecentChange;
258 $rc->mAttribs = array(
259 'rc_timestamp' => $timestamp,
260 'rc_cur_time' => $timestamp,
261 'rc_namespace' => $oldTitle->getNamespace(),
262 'rc_title' => $oldTitle->getDBkey(),
263 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
264 'rc_minor' => 0,
265 'rc_cur_id' => $oldTitle->getArticleID(),
266 'rc_user' => $user->getID(),
267 'rc_user_text' => $user->getName(),
268 'rc_comment' => $comment,
269 'rc_this_oldid' => 0,
270 'rc_last_oldid' => 0,
271 'rc_bot' => $user->isBot() ? 1 : 0,
272 'rc_moved_to_ns' => $newTitle->getNamespace(),
273 'rc_moved_to_title' => $newTitle->getDBkey(),
274 'rc_ip' => $ip,
275 'rc_new' => 0, # obsolete
276 'rc_patrolled' => 1
277 );
278
279 $rc->mExtra = array(
280 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
281 'lastTimestamp' => 0,
282 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
283 );
284 $rc->save();
285 }
286
287 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
288 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
289 }
290
291 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
292 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip='', true );
293 }
294
295 # A log entry is different to an edit in that previous revisions are
296 # not kept
297 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='' )
298 {
299 if ( !$ip ) {
300 global $wgIP;
301 $ip = empty( $wgIP ) ? '' : $wgIP;
302 }
303 $rc = new RecentChange;
304 $rc->mAttribs = array(
305 'rc_timestamp' => $timestamp,
306 'rc_cur_time' => $timestamp,
307 'rc_namespace' => $title->getNamespace(),
308 'rc_title' => $title->getDBkey(),
309 'rc_type' => RC_LOG,
310 'rc_minor' => 0,
311 'rc_cur_id' => $title->getArticleID(),
312 'rc_user' => $user->getID(),
313 'rc_user_text' => $user->getName(),
314 'rc_comment' => $comment,
315 'rc_this_oldid' => 0,
316 'rc_last_oldid' => 0,
317 'rc_bot' => 0,
318 'rc_moved_to_ns' => 0,
319 'rc_moved_to_title' => '',
320 'rc_ip' => $ip,
321 'rc_patrolled' => 1,
322 'rc_new' => 0 # obsolete
323 );
324 $rc->mExtra = array(
325 'prefixedDBkey' => $title->getPrefixedDBkey(),
326 'lastTimestamp' => 0
327 );
328 $rc->save();
329 }
330
331 # Initialises the members of this object from a mysql row object
332 function loadFromRow( $row )
333 {
334 $this->mAttribs = get_object_vars( $row );
335 $this->mExtra = array();
336 }
337
338 # Makes a pseudo-RC entry from a cur row, for watchlists and things
339 function loadFromCurRow( $row )
340 {
341 $this->mAttribs = array(
342 'rc_timestamp' => $row->cur_timestamp,
343 'rc_cur_time' => $row->cur_timestamp,
344 'rc_user' => $row->cur_user,
345 'rc_user_text' => $row->cur_user_text,
346 'rc_namespace' => $row->cur_namespace,
347 'rc_title' => $row->cur_title,
348 'rc_comment' => $row->cur_comment,
349 'rc_minor' => !!$row->cur_minor_edit,
350 'rc_type' => $row->cur_is_new ? RC_NEW : RC_EDIT,
351 'rc_cur_id' => $row->cur_id,
352 'rc_this_oldid' => 0,
353 'rc_last_oldid' => 0,
354 'rc_bot' => 0,
355 'rc_moved_to_ns' => 0,
356 'rc_moved_to_title' => '',
357 'rc_ip' => '',
358 'rc_patrolled' => '1', # we can't support patrolling on the Watchlist
359 # currently because it uses cur, not recentchanges
360 'rc_new' => $row->cur_is_new # obsolete
361 );
362
363 $this->mExtra = array();
364 }
365
366
367 /**
368 * Gets the end part of the diff URL assoicated with this object
369 * Blank if no diff link should be displayed
370 */
371 function diffLinkTrail( $forceCur )
372 {
373 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
374 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
375 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
376 if ( $forceCur ) {
377 $trail .= '&diff=0' ;
378 } else {
379 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
380 }
381 } else {
382 $trail = '';
383 }
384 return $trail;
385 }
386 }
387 ?>