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