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