Setting dummy variable to prevent error message in SpecialRecentchangeslinked
[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 * mAttribs:
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 * oldSize text size before the change
43 * newSize text size after the change
44 *
45 * temporary: not stored in the database
46 * notificationtimestamp
47 * numberofWatchingusers
48 *
49 * @todo document functions and variables
50 * @package MediaWiki
51 */
52 class RecentChange
53 {
54 var $mAttribs = array(), $mExtra = array();
55 var $mTitle = false, $mMovedToTitle = false;
56 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
57
58 # Factory methods
59
60 /* static */ function newFromRow( $row )
61 {
62 $rc = new RecentChange;
63 $rc->loadFromRow( $row );
64 return $rc;
65 }
66
67 /* static */ function newFromCurRow( $row, $rc_this_oldid = 0 )
68 {
69 $rc = new RecentChange;
70 $rc->loadFromCurRow( $row, $rc_this_oldid );
71 $rc->notificationtimestamp = false;
72 $rc->numberofWatchingusers = false;
73 return $rc;
74 }
75
76 # Accessors
77
78 function setAttribs( $attribs )
79 {
80 $this->mAttribs = $attribs;
81 }
82
83 function setExtra( $extra )
84 {
85 $this->mExtra = $extra;
86 }
87
88 function &getTitle()
89 {
90 if ( $this->mTitle === false ) {
91 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
92 }
93 return $this->mTitle;
94 }
95
96 function getMovedToTitle()
97 {
98 if ( $this->mMovedToTitle === false ) {
99 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
100 $this->mAttribs['rc_moved_to_title'] );
101 }
102 return $this->mMovedToTitle;
103 }
104
105 # Writes the data in this object to the database
106 function save()
107 {
108 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix, $wgUseRCPatrol;
109 $fname = 'RecentChange::save';
110
111 $dbw =& wfGetDB( DB_MASTER );
112 if ( !is_array($this->mExtra) ) {
113 $this->mExtra = array();
114 }
115 $this->mExtra['lang'] = $wgLocalInterwiki;
116
117 if ( !$wgPutIPinRC ) {
118 $this->mAttribs['rc_ip'] = '';
119 }
120
121 # Fixup database timestamps
122 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
123 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
124 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
125
126 # Insert new row
127 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
128
129 if ( $wgUseRCPatrol ) {
130 # Retrieve the id assigned by the db, but only if we'll use it later
131 $this->mAttribs['rc_id'] = $dbw->insertId();
132 }
133
134 # Update old rows, if necessary
135 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
136 $lastTime = $this->mExtra['lastTimestamp'];
137 #$now = $this->mAttribs['rc_timestamp'];
138 #$curId = $this->mAttribs['rc_cur_id'];
139
140 # Don't bother looking for entries that have probably
141 # been purged, it just locks up the indexes needlessly.
142 global $wgRCMaxAge;
143 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
144 if( $age < $wgRCMaxAge ) {
145 # live hack, will commit once tested - kate
146 # Update rc_this_oldid for the entries which were current
147 #
148 #$oldid = $this->mAttribs['rc_last_oldid'];
149 #$ns = $this->mAttribs['rc_namespace'];
150 #$title = $this->mAttribs['rc_title'];
151 #
152 #$dbw->update( 'recentchanges',
153 # array( /* SET */
154 # 'rc_this_oldid' => $oldid
155 # ), array( /* WHERE */
156 # 'rc_namespace' => $ns,
157 # 'rc_title' => $title,
158 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
159 # ), $fname
160 #);
161 }
162
163 # Update rc_cur_time
164 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
165 # array( 'rc_cur_id' => $curId ), $fname );
166 }
167
168 # Notify external application via UDP
169 if ( $wgRC2UDPAddress ) {
170 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
171 if ( $conn ) {
172 $line = $wgRC2UDPPrefix . $this->getIRCLine();
173 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
174 socket_close( $conn );
175 }
176 }
177
178 // E-mail notifications
179 global $wgUseEnotif;
180 if( $wgUseEnotif ) {
181 # this would be better as an extension hook
182 include_once( "UserMailer.php" );
183 $enotif = new EmailNotification();
184 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
185 $enotif->notifyOnPageChange( $title,
186 $this->mAttribs['rc_timestamp'],
187 $this->mAttribs['rc_comment'],
188 $this->mAttribs['rc_minor'],
189 $this->mAttribs['rc_last_oldid'] );
190 }
191
192 }
193
194 # Marks a certain row as patrolled
195 function markPatrolled( $rcid )
196 {
197 $fname = 'RecentChange::markPatrolled';
198
199 $dbw =& wfGetDB( DB_MASTER );
200
201 $dbw->update( 'recentchanges',
202 array( /* SET */
203 'rc_patrolled' => 1
204 ), array( /* WHERE */
205 'rc_id' => $rcid
206 ), $fname
207 );
208 }
209
210 # Makes an entry in the database corresponding to an edit
211 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
212 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
213 $newId = 0)
214 {
215 if ( $bot == 'default ' ) {
216 $bot = $user->isBot();
217 }
218
219 if ( !$ip ) {
220 $ip = wfGetIP();
221 if ( !$ip ) {
222 $ip = '';
223 }
224 }
225
226 $rc = new RecentChange;
227 $rc->mAttribs = array(
228 'rc_timestamp' => $timestamp,
229 'rc_cur_time' => $timestamp,
230 'rc_namespace' => $title->getNamespace(),
231 'rc_title' => $title->getDBkey(),
232 'rc_type' => RC_EDIT,
233 'rc_minor' => $minor ? 1 : 0,
234 'rc_cur_id' => $title->getArticleID(),
235 'rc_user' => $user->getID(),
236 'rc_user_text' => $user->getName(),
237 'rc_comment' => $comment,
238 'rc_this_oldid' => $newId,
239 'rc_last_oldid' => $oldId,
240 'rc_bot' => $bot ? 1 : 0,
241 'rc_moved_to_ns' => 0,
242 'rc_moved_to_title' => '',
243 'rc_ip' => $ip,
244 'rc_patrolled' => 0,
245 'rc_new' => 0 # obsolete
246 );
247
248 $rc->mExtra = array(
249 'prefixedDBkey' => $title->getPrefixedDBkey(),
250 'lastTimestamp' => $lastTimestamp,
251 'oldSize' => $oldSize,
252 'newSize' => $newSize,
253 );
254 $rc->save();
255 }
256
257 # Makes an entry in the database corresponding to page creation
258 # Note: the title object must be loaded with the new id using resetArticleID()
259 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
260 $ip='', $size = 0, $newId = 0 )
261 {
262 if ( !$ip ) {
263 $ip = wfGetIP();
264 if ( !$ip ) {
265 $ip = '';
266 }
267 }
268 if ( $bot == 'default' ) {
269 $bot = $user->isBot();
270 }
271
272 $rc = new RecentChange;
273 $rc->mAttribs = array(
274 'rc_timestamp' => $timestamp,
275 'rc_cur_time' => $timestamp,
276 'rc_namespace' => $title->getNamespace(),
277 'rc_title' => $title->getDBkey(),
278 'rc_type' => RC_NEW,
279 'rc_minor' => $minor ? 1 : 0,
280 'rc_cur_id' => $title->getArticleID(),
281 'rc_user' => $user->getID(),
282 'rc_user_text' => $user->getName(),
283 'rc_comment' => $comment,
284 'rc_this_oldid' => $newId,
285 'rc_last_oldid' => 0,
286 'rc_bot' => $bot ? 1 : 0,
287 'rc_moved_to_ns' => 0,
288 'rc_moved_to_title' => '',
289 'rc_ip' => $ip,
290 'rc_patrolled' => 0,
291 'rc_new' => 1 # obsolete
292 );
293
294 $rc->mExtra = array(
295 'prefixedDBkey' => $title->getPrefixedDBkey(),
296 'lastTimestamp' => 0,
297 'oldSize' => 0,
298 'newSize' => $size
299 );
300 $rc->save();
301 }
302
303 # Makes an entry in the database corresponding to a rename
304 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
305 {
306 if ( !$ip ) {
307 $ip = wfGetIP();
308 if ( !$ip ) {
309 $ip = '';
310 }
311 }
312
313 $rc = new RecentChange;
314 $rc->mAttribs = array(
315 'rc_timestamp' => $timestamp,
316 'rc_cur_time' => $timestamp,
317 'rc_namespace' => $oldTitle->getNamespace(),
318 'rc_title' => $oldTitle->getDBkey(),
319 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
320 'rc_minor' => 0,
321 'rc_cur_id' => $oldTitle->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' => $user->isBot() ? 1 : 0,
328 'rc_moved_to_ns' => $newTitle->getNamespace(),
329 'rc_moved_to_title' => $newTitle->getDBkey(),
330 'rc_ip' => $ip,
331 'rc_new' => 0, # obsolete
332 'rc_patrolled' => 1
333 );
334
335 $rc->mExtra = array(
336 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
337 'lastTimestamp' => 0,
338 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
339 );
340 $rc->save();
341 }
342
343 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
344 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
345 }
346
347 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
348 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
349 }
350
351 # A log entry is different to an edit in that previous revisions are
352 # not kept
353 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='' )
354 {
355 if ( !$ip ) {
356 $ip = wfGetIP();
357 if ( !$ip ) {
358 $ip = '';
359 }
360 }
361
362 $rc = new RecentChange;
363 $rc->mAttribs = array(
364 'rc_timestamp' => $timestamp,
365 'rc_cur_time' => $timestamp,
366 'rc_namespace' => $title->getNamespace(),
367 'rc_title' => $title->getDBkey(),
368 'rc_type' => RC_LOG,
369 'rc_minor' => 0,
370 'rc_cur_id' => $title->getArticleID(),
371 'rc_user' => $user->getID(),
372 'rc_user_text' => $user->getName(),
373 'rc_comment' => $comment,
374 'rc_this_oldid' => 0,
375 'rc_last_oldid' => 0,
376 'rc_bot' => $user->isBot() ? 1 : 0,
377 'rc_moved_to_ns' => 0,
378 'rc_moved_to_title' => '',
379 'rc_ip' => $ip,
380 'rc_patrolled' => 1,
381 'rc_new' => 0 # obsolete
382 );
383 $rc->mExtra = array(
384 'prefixedDBkey' => $title->getPrefixedDBkey(),
385 'lastTimestamp' => 0
386 );
387 $rc->save();
388 }
389
390 # Initialises the members of this object from a mysql row object
391 function loadFromRow( $row )
392 {
393 $this->mAttribs = get_object_vars( $row );
394 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
395 $this->mExtra = array();
396 }
397
398 # Makes a pseudo-RC entry from a cur row, for watchlists and things
399 function loadFromCurRow( $row )
400 {
401 $this->mAttribs = array(
402 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
403 'rc_cur_time' => $row->rev_timestamp,
404 'rc_user' => $row->rev_user,
405 'rc_user_text' => $row->rev_user_text,
406 'rc_namespace' => $row->page_namespace,
407 'rc_title' => $row->page_title,
408 'rc_comment' => $row->rev_comment,
409 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
410 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
411 'rc_cur_id' => $row->page_id,
412 'rc_this_oldid' => $row->rev_id,
413 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
414 'rc_bot' => 0,
415 'rc_moved_to_ns' => 0,
416 'rc_moved_to_title' => '',
417 'rc_ip' => '',
418 'rc_patrolled' => '1', # we can't support patrolling on the Watchlist
419 # currently because it uses cur, not recentchanges
420 'rc_new' => $row->page_is_new # obsolete
421 );
422
423 $this->mExtra = array();
424 }
425
426
427 /**
428 * Gets the end part of the diff URL associated with this object
429 * Blank if no diff link should be displayed
430 */
431 function diffLinkTrail( $forceCur )
432 {
433 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
434 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
435 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
436 if ( $forceCur ) {
437 $trail .= '&diff=0' ;
438 } else {
439 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
440 }
441 } else {
442 $trail = '';
443 }
444 return $trail;
445 }
446
447 function getIRCLine() {
448 global $wgUseRCPatrol;
449
450 extract($this->mAttribs);
451 extract($this->mExtra);
452
453 $titleObj =& $this->getTitle();
454
455 $bad = array("\n", "\r");
456 $empty = array("", "");
457 $title = $titleObj->getPrefixedText();
458 $title = str_replace($bad, $empty, $title);
459
460 if ( $rc_new && $wgUseRCPatrol ) {
461 $url = $titleObj->getFullURL("rcid=$rc_id");
462 } else if ( $rc_new ) {
463 $url = $titleObj->getFullURL();
464 } else if ( $wgUseRCPatrol ) {
465 $url = $titleObj->getFullURL("diff=0&oldid=$rc_last_oldid&rcid=$rc_id");
466 } else {
467 $url = $titleObj->getFullURL("diff=0&oldid=$rc_last_oldid");
468 }
469
470 if ( isset( $oldSize ) && isset( $newSize ) ) {
471 $szdiff = $newSize - $oldSize;
472 if ($szdiff < -500) {
473 $szdiff = "\002$szdiff\002";
474 } elseif ($szdiff >= 0) {
475 $szdiff = '+' . $szdiff ;
476 }
477 $szdiff = '(' . $szdiff . ')' ;
478 } else {
479 $szdiff = '';
480 }
481
482 $comment = str_replace($bad, $empty, $rc_comment);
483 $user = str_replace($bad, $empty, $rc_user_text);
484 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
485 # see http://www.irssi.org/?page=docs&doc=formats for some colour codes. prefix is \003,
486 # no colour (\003) switches back to the term default
487 $comment = preg_replace("/\/\* (.*) \*\/(.*)/", "\00315\$1\003 - \00310\$2\003", $comment);
488 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
489 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
490
491 return $fullString;
492 }
493 }
494 ?>