massive double to single quotes conversion. I have not noticed any bug after a lot...
[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 # Fixup database timestamps
104 $this->mAttribs['rc_timestamp']=$dbw->timestamp($this->mAttribs['rc_timestamp']);
105 $this->mAttribs['rc_cur_time']=$dbw->timestamp($this->mAttribs['rc_cur_time']);
106
107 # Insert new row
108 $dbw->insertArray( 'recentchanges', $this->mAttribs, $fname );
109
110 # Update old rows, if necessary
111 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
112 $oldid = $this->mAttribs['rc_last_oldid'];
113 $ns = $this->mAttribs['rc_namespace'];
114 $title = $this->mAttribs['rc_title'];
115 $lastTime = $this->mExtra['lastTimestamp'];
116 $now = $this->mAttribs['rc_timestamp'];
117 $curId = $this->mAttribs['rc_cur_id'];
118
119 # Update rc_this_oldid for the entries which were current
120 $dbw->updateArray( 'recentchanges',
121 array( /* SET */
122 'rc_this_oldid' => $oldid
123 ), array( /* WHERE */
124 'rc_namespace' => $ns,
125 'rc_title' => $title,
126 'rc_timestamp' => $dbw->timestamp($lastTime)
127 ), $fname
128 );
129
130 # Update rc_cur_time
131 $dbw->updateArray( 'recentchanges', array( 'rc_cur_time' => $now ),
132 array( 'rc_cur_id' => $curId ), $fname );
133 }
134
135 # Notify external application
136 if ( $wgUseRCQueue ) {
137 $queue = msg_get_queue( $wgRCQueueID );
138 if (!msg_send( $queue, array_merge( $this->mAttribs, 1, $this->mExtra ),
139 true, false, $error ))
140 {
141 wfDebug( "Error sending message to RC queue, code $error\n" );
142 }
143 }
144 }
145
146 # Marks a certain row as patrolled
147 function markPatrolled( $rcid )
148 {
149 $fname = 'RecentChange::markPatrolled';
150
151 $dbw =& wfGetDB( DB_MASTER );
152
153 $dbw->updateArray( 'recentchanges',
154 array( /* SET */
155 'rc_patrolled' => 1
156 ), array( /* WHERE */
157 'rc_id' => $rcid
158 ), $fname
159 );
160 }
161
162 # Makes an entry in the database corresponding to an edit
163 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
164 $oldId, $lastTimestamp, $bot = "default", $ip = '' )
165 {
166 if ( $bot == 'default ' ) {
167 $bot = $user->isBot();
168 }
169
170 if ( !$ip ) {
171 global $wgIP;
172 $ip = empty( $wgIP ) ? '' : $wgIP;
173 }
174
175 $rc = new RecentChange;
176 $rc->mAttribs = array(
177 'rc_timestamp' => $timestamp,
178 'rc_cur_time' => $timestamp,
179 'rc_namespace' => $title->getNamespace(),
180 'rc_title' => $title->getDBkey(),
181 'rc_type' => RC_EDIT,
182 'rc_minor' => $minor ? 1 : 0,
183 'rc_cur_id' => $title->getArticleID(),
184 'rc_user' => $user->getID(),
185 'rc_user_text' => $user->getName(),
186 'rc_comment' => $comment,
187 'rc_this_oldid' => 0,
188 'rc_last_oldid' => $oldId,
189 'rc_bot' => $bot ? 1 : 0,
190 'rc_moved_to_ns' => 0,
191 'rc_moved_to_title' => '',
192 'rc_ip' => $ip,
193 'rc_new' => 0 # obsolete
194 );
195
196 $rc->mExtra = array(
197 'prefixedDBkey' => $title->getPrefixedDBkey(),
198 'lastTimestamp' => $lastTimestamp
199 );
200 $rc->save();
201 }
202
203 # Makes an entry in the database corresponding to page creation
204 # Note: the title object must be loaded with the new id using resetArticleID()
205 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default", $ip='' )
206 {
207 if ( !$ip ) {
208 global $wgIP;
209 $ip = empty( $wgIP ) ? '' : $wgIP;
210 }
211 if ( $bot == 'default' ) {
212 $bot = $user->isBot();
213 }
214
215 $rc = new RecentChange;
216 $rc->mAttribs = array(
217 'rc_timestamp' => $timestamp,
218 'rc_cur_time' => $timestamp,
219 'rc_namespace' => $title->getNamespace(),
220 'rc_title' => $title->getDBkey(),
221 'rc_type' => RC_NEW,
222 'rc_minor' => $minor ? 1 : 0,
223 'rc_cur_id' => $title->getArticleID(),
224 'rc_user' => $user->getID(),
225 'rc_user_text' => $user->getName(),
226 'rc_comment' => $comment,
227 'rc_this_oldid' => 0,
228 'rc_last_oldid' => 0,
229 'rc_bot' => $bot ? 1 : 0,
230 'rc_moved_to_ns' => 0,
231 'rc_moved_to_title' => '',
232 'rc_ip' => $ip,
233 'rc_new' => 1 # obsolete
234 );
235
236 $rc->mExtra = array(
237 'prefixedDBkey' => $title->getPrefixedDBkey(),
238 'lastTimestamp' => 0
239 );
240 $rc->save();
241 }
242
243 # Makes an entry in the database corresponding to a rename
244 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
245 {
246 if ( !$ip ) {
247 global $wgIP;
248 $ip = empty( $wgIP ) ? '' : $wgIP;
249 }
250 $rc = new RecentChange;
251 $rc->mAttribs = array(
252 'rc_timestamp' => $timestamp,
253 'rc_cur_time' => $timestamp,
254 'rc_namespace' => $oldTitle->getNamespace(),
255 'rc_title' => $oldTitle->getDBkey(),
256 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
257 'rc_minor' => 0,
258 'rc_cur_id' => $oldTitle->getArticleID(),
259 'rc_user' => $user->getID(),
260 'rc_user_text' => $user->getName(),
261 'rc_comment' => $comment,
262 'rc_this_oldid' => 0,
263 'rc_last_oldid' => 0,
264 'rc_bot' => $user->isBot() ? 1 : 0,
265 'rc_moved_to_ns' => $newTitle->getNamespace(),
266 'rc_moved_to_title' => $newTitle->getDBkey(),
267 'rc_ip' => $ip,
268 'rc_new' => 0, # obsolete
269 'rc_patrolled' => 1
270 );
271
272 $rc->mExtra = array(
273 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
274 'lastTimestamp' => 0,
275 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
276 );
277 $rc->save();
278 }
279
280 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
281 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
282 }
283
284 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
285 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip='', true );
286 }
287
288 # A log entry is different to an edit in that previous revisions are
289 # not kept
290 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='' )
291 {
292 if ( !$ip ) {
293 global $wgIP;
294 $ip = empty( $wgIP ) ? '' : $wgIP;
295 }
296 $rc = new RecentChange;
297 $rc->mAttribs = array(
298 'rc_timestamp' => $timestamp,
299 'rc_cur_time' => $timestamp,
300 'rc_namespace' => $title->getNamespace(),
301 'rc_title' => $title->getDBkey(),
302 'rc_type' => RC_LOG,
303 'rc_minor' => 0,
304 'rc_cur_id' => $title->getArticleID(),
305 'rc_user' => $user->getID(),
306 'rc_user_text' => $user->getName(),
307 'rc_comment' => $comment,
308 'rc_this_oldid' => 0,
309 'rc_last_oldid' => 0,
310 'rc_bot' => 0,
311 'rc_moved_to_ns' => 0,
312 'rc_moved_to_title' => '',
313 'rc_ip' => $ip,
314 'rc_patrolled' => 1,
315 'rc_new' => 0 # obsolete
316 );
317 $rc->mExtra = array(
318 'prefixedDBkey' => $title->getPrefixedDBkey(),
319 'lastTimestamp' => 0
320 );
321 $rc->save();
322 }
323
324 # Initialises the members of this object from a mysql row object
325 function loadFromRow( $row )
326 {
327 $this->mAttribs = get_object_vars( $row );
328 $this->mExtra = array();
329 }
330
331 # Makes a pseudo-RC entry from a cur row, for watchlists and things
332 function loadFromCurRow( $row )
333 {
334 $this->mAttribs = array(
335 'rc_timestamp' => $row->cur_timestamp,
336 'rc_cur_time' => $row->cur_timestamp,
337 'rc_user' => $row->cur_user,
338 'rc_user_text' => $row->cur_user_text,
339 'rc_namespace' => $row->cur_namespace,
340 'rc_title' => $row->cur_title,
341 'rc_comment' => $row->cur_comment,
342 'rc_minor' => !!$row->cur_minor_edit,
343 'rc_type' => $row->cur_is_new ? RC_NEW : RC_EDIT,
344 'rc_cur_id' => $row->cur_id,
345 'rc_this_oldid' => 0,
346 'rc_last_oldid' => 0,
347 'rc_bot' => 0,
348 'rc_moved_to_ns' => 0,
349 'rc_moved_to_title' => '',
350 'rc_ip' => '',
351 'rc_patrolled' => '1', # we can't support patrolling on the Watchlist
352 # currently because it uses cur, not recentchanges
353 'rc_new' => $row->cur_is_new # obsolete
354 );
355
356 $this->mExtra = array();
357 }
358
359
360 # Gets the end part of the diff URL assoicated with this object
361 # Blank if no diff link should be displayed
362 function diffLinkTrail( $forceCur )
363 {
364 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
365 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
366 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
367 if ( $forceCur ) {
368 $trail .= '&diff=0' ;
369 } else {
370 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
371 }
372 } else {
373 $trail = '';
374 }
375 return $trail;
376 }
377 }
378 ?>