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