*gasp*
[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 page_id of associated page 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 rev_id associated with this entry (or zero)
32 * rc_last_oldid rev_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 # Don't bother looking for entries that have probably
135 # been purged, it just locks up the indexes needlessly.
136 global $wgRCMaxAge;
137 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
138 if( $age < $wgRCMaxAge ) {
139 # Update rc_this_oldid for the entries which were current
140 $dbw->update( 'recentchanges',
141 array( /* SET */
142 'rc_this_oldid' => $oldid
143 ), array( /* WHERE */
144 'rc_namespace' => $ns,
145 'rc_title' => $title,
146 'rc_timestamp' => $dbw->timestamp( $lastTime )
147 ), $fname
148 );
149 }
150
151 # Update rc_cur_time
152 $dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
153 array( 'rc_cur_id' => $curId ), $fname );
154 }
155
156 # Notify external application
157 if ( $wgUseRCQueue ) {
158 $queue = msg_get_queue( $wgRCQueueID );
159 if (!msg_send( $queue, array_merge( $this->mAttribs, 1, $this->mExtra ),
160 true, false, $error ))
161 {
162 wfDebug( "Error sending message to RC queue, code $error\n" );
163 }
164 }
165 }
166
167 # Marks a certain row as patrolled
168 function markPatrolled( $rcid )
169 {
170 $fname = 'RecentChange::markPatrolled';
171
172 $dbw =& wfGetDB( DB_MASTER );
173
174 $dbw->update( 'recentchanges',
175 array( /* SET */
176 'rc_patrolled' => 1
177 ), array( /* WHERE */
178 'rc_id' => $rcid
179 ), $fname
180 );
181 }
182
183 # Makes an entry in the database corresponding to an edit
184 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
185 $oldId, $lastTimestamp, $bot = "default", $ip = '' )
186 {
187 if ( $bot == 'default ' ) {
188 $bot = $user->isBot();
189 }
190
191 if ( !$ip ) {
192 global $wgIP;
193 $ip = empty( $wgIP ) ? '' : $wgIP;
194 }
195
196 $rc = new RecentChange;
197 $rc->mAttribs = array(
198 'rc_timestamp' => $timestamp,
199 'rc_cur_time' => $timestamp,
200 'rc_namespace' => $title->getNamespace(),
201 'rc_title' => $title->getDBkey(),
202 'rc_type' => RC_EDIT,
203 'rc_minor' => $minor ? 1 : 0,
204 'rc_cur_id' => $title->getArticleID(),
205 'rc_user' => $user->getID(),
206 'rc_user_text' => $user->getName(),
207 'rc_comment' => $comment,
208 'rc_this_oldid' => 0,
209 'rc_last_oldid' => $oldId,
210 'rc_bot' => $bot ? 1 : 0,
211 'rc_moved_to_ns' => 0,
212 'rc_moved_to_title' => '',
213 'rc_ip' => $ip,
214 'rc_patrolled' => 0,
215 'rc_new' => 0 # obsolete
216 );
217
218 $rc->mExtra = array(
219 'prefixedDBkey' => $title->getPrefixedDBkey(),
220 'lastTimestamp' => $lastTimestamp
221 );
222 $rc->save();
223 }
224
225 # Makes an entry in the database corresponding to page creation
226 # Note: the title object must be loaded with the new id using resetArticleID()
227 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default", $ip='' )
228 {
229 if ( !$ip ) {
230 global $wgIP;
231 $ip = empty( $wgIP ) ? '' : $wgIP;
232 }
233 if ( $bot == 'default' ) {
234 $bot = $user->isBot();
235 }
236
237 $rc = new RecentChange;
238 $rc->mAttribs = array(
239 'rc_timestamp' => $timestamp,
240 'rc_cur_time' => $timestamp,
241 'rc_namespace' => $title->getNamespace(),
242 'rc_title' => $title->getDBkey(),
243 'rc_type' => RC_NEW,
244 'rc_minor' => $minor ? 1 : 0,
245 'rc_cur_id' => $title->getArticleID(),
246 'rc_user' => $user->getID(),
247 'rc_user_text' => $user->getName(),
248 'rc_comment' => $comment,
249 'rc_this_oldid' => 0,
250 'rc_last_oldid' => 0,
251 'rc_bot' => $bot ? 1 : 0,
252 'rc_moved_to_ns' => 0,
253 'rc_moved_to_title' => '',
254 'rc_ip' => $ip,
255 'rc_patrolled' => 0,
256 'rc_new' => 1 # obsolete
257 );
258
259 $rc->mExtra = array(
260 'prefixedDBkey' => $title->getPrefixedDBkey(),
261 'lastTimestamp' => 0
262 );
263 $rc->save();
264 }
265
266 # Makes an entry in the database corresponding to a rename
267 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
268 {
269 if ( !$ip ) {
270 global $wgIP;
271 $ip = empty( $wgIP ) ? '' : $wgIP;
272 }
273 $rc = new RecentChange;
274 $rc->mAttribs = array(
275 'rc_timestamp' => $timestamp,
276 'rc_cur_time' => $timestamp,
277 'rc_namespace' => $oldTitle->getNamespace(),
278 'rc_title' => $oldTitle->getDBkey(),
279 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
280 'rc_minor' => 0,
281 'rc_cur_id' => $oldTitle->getArticleID(),
282 'rc_user' => $user->getID(),
283 'rc_user_text' => $user->getName(),
284 'rc_comment' => $comment,
285 'rc_this_oldid' => 0,
286 'rc_last_oldid' => 0,
287 'rc_bot' => $user->isBot() ? 1 : 0,
288 'rc_moved_to_ns' => $newTitle->getNamespace(),
289 'rc_moved_to_title' => $newTitle->getDBkey(),
290 'rc_ip' => $ip,
291 'rc_new' => 0, # obsolete
292 'rc_patrolled' => 1
293 );
294
295 $rc->mExtra = array(
296 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
297 'lastTimestamp' => 0,
298 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
299 );
300 $rc->save();
301 }
302
303 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
304 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
305 }
306
307 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
308 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip='', true );
309 }
310
311 # A log entry is different to an edit in that previous revisions are
312 # not kept
313 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='' )
314 {
315 if ( !$ip ) {
316 global $wgIP;
317 $ip = empty( $wgIP ) ? '' : $wgIP;
318 }
319 $rc = new RecentChange;
320 $rc->mAttribs = array(
321 'rc_timestamp' => $timestamp,
322 'rc_cur_time' => $timestamp,
323 'rc_namespace' => $title->getNamespace(),
324 'rc_title' => $title->getDBkey(),
325 'rc_type' => RC_LOG,
326 'rc_minor' => 0,
327 'rc_cur_id' => $title->getArticleID(),
328 'rc_user' => $user->getID(),
329 'rc_user_text' => $user->getName(),
330 'rc_comment' => $comment,
331 'rc_this_oldid' => 0,
332 'rc_last_oldid' => 0,
333 'rc_bot' => 0,
334 'rc_moved_to_ns' => 0,
335 'rc_moved_to_title' => '',
336 'rc_ip' => $ip,
337 'rc_patrolled' => 1,
338 'rc_new' => 0 # obsolete
339 );
340 $rc->mExtra = array(
341 'prefixedDBkey' => $title->getPrefixedDBkey(),
342 'lastTimestamp' => 0
343 );
344 $rc->save();
345 }
346
347 # Initialises the members of this object from a mysql row object
348 function loadFromRow( $row )
349 {
350 $this->mAttribs = get_object_vars( $row );
351 $this->mExtra = array();
352 }
353
354 # Makes a pseudo-RC entry from a cur row, for watchlists and things
355 function loadFromCurRow( $row )
356 {
357 $this->mAttribs = array(
358 'rc_timestamp' => $row->rev_timestamp,
359 'rc_cur_time' => $row->rev_timestamp,
360 'rc_user' => $row->rev_user,
361 'rc_user_text' => $row->rev_user_text,
362 'rc_namespace' => $row->page_namespace,
363 'rc_title' => $row->page_title,
364 'rc_comment' => $row->rev_comment,
365 'rc_minor' => !!$row->rev_minor_edit,
366 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
367 'rc_cur_id' => $row->page_id,
368 'rc_this_oldid' => 0,
369 'rc_last_oldid' => 0,
370 'rc_bot' => 0,
371 'rc_moved_to_ns' => 0,
372 'rc_moved_to_title' => '',
373 'rc_ip' => '',
374 'rc_patrolled' => '1', # we can't support patrolling on the Watchlist
375 # currently because it uses cur, not recentchanges
376 'rc_new' => $row->page_is_new # obsolete
377 );
378
379 $this->mExtra = array();
380 }
381
382
383 /**
384 * Gets the end part of the diff URL assoicated with this object
385 * Blank if no diff link should be displayed
386 */
387 function diffLinkTrail( $forceCur )
388 {
389 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
390 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
391 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
392 if ( $forceCur ) {
393 $trail .= '&diff=0' ;
394 } else {
395 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
396 }
397 } else {
398 $trail = '';
399 }
400 return $trail;
401 }
402 }
403 ?>