* Recent Changes improvements: object oriented back end, move page annotation and...
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?
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
9 /*
10 mAttributes:
11 rc_timestamp time the entry was made
12 rc_cur_time timestamp on the cur row
13 rc_namespace namespace #
14 rc_title non-prefixed db key
15 rc_type is new entry, used to determine whether updating is necessary
16 rc_minor is minor
17 rc_cur_id id of associated cur entry
18 rc_user user id who made the entry
19 rc_user_text user name who made the entry
20 rc_comment edit summary
21 rc_this_oldid old_id associated with this entry (or zero)
22 rc_last_oldid old_id associated with the entry before this one (or zero)
23 rc_bot is bot, hidden
24
25 mExtra:
26 prefixedDBkey prefixed db key, used by external app via msg queue
27 lastTimestamp timestamp of previous entry, used in WHERE clause during update
28 lang the interwiki prefix, automatically set in save()
29 */
30
31 class RecentChange
32 {
33 var $mAttribs = array(), $mExtra = array();
34 var $mTitle = false, $mMovedToTitle = false;
35
36 # Factory methods
37
38 /* static */ function newFromRow( $row )
39 {
40 $rc = new RecentChange;
41 $rc->loadFromRow( $row );
42 return $rc;
43 }
44
45 # Accessors
46
47 function setAttribs( $attribs )
48 {
49 $this->mAttribs = $attribs;
50 }
51
52 function setExtra( $extra )
53 {
54 $this->mExtra = $extra;
55 }
56
57 function getTitle()
58 {
59 if ( $this->mTitle === false ) {
60 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
61 }
62 return $this->mTitle;
63 }
64
65 function getMovedToTitle()
66 {
67 if ( $this->mMovedToTitle === false ) {
68 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
69 $this->mAttribs['rc_moved_to_title'] );
70 }
71 return $this->mMovedToTitle;
72 }
73
74 # Writes the data in this object to the database
75 function save()
76 {
77 global $wgUseRCQueue, $wgRCQueueID, $wgLocalInterwiki;
78 $fname = "RecentChange::save";
79
80 if ( !is_array($this->mExtra) ) {
81 $this->mExtra = array();
82 }
83 $this->mExtra['lang'] = $wgLocalInterwiki;
84
85 # Insert new row
86 wfInsertArray( "recentchanges", $this->mAttribs, $fname );
87
88 # Update old rows, if necessary
89 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
90 $oldid = $this->mAttribs['rc_last_oldid'];
91 $ns = $this->mAttribs['rc_namespace'];
92 $title = wfStrencode($this->mAttribs['rc_title']);
93 $lastTime = $this->mExtra['lastTimestamp'];
94 $now = $this->mAttribs['rc_timestamp'];
95 $curId = $this->mAttribs['rc_cur_id'];
96
97 # Update rc_this_oldid for the entries which were current
98 $sql = "UPDATE recentchanges SET rc_this_oldid={$oldid} " .
99 "WHERE rc_namespace=$ns AND rc_title='$title' AND rc_timestamp='$lastTime'";
100 wfQuery( $sql, DB_WRITE, $fname );
101
102 # Update rc_cur_time
103 $sql = "UPDATE recentchanges SET rc_cur_time='{$now}' " .
104 "WHERE rc_cur_id=" . $curId;
105 wfQuery( $sql, DB_WRITE, $fname );
106 }
107
108 # Notify external application
109 if ( $wgUseRCQueue ) {
110 $queue = msg_get_queue( $wgRCQueueID );
111 if (!msg_send( $queue, array_merge( $this->mAttribs, 1, $this->mExtra ),
112 true, false, $error ))
113 {
114 wfDebug( "Error sending message to RC queue, code $error\n" );
115 }
116 }
117 }
118
119 # Makes an entry in the database corresponding to an edit
120 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
121 $oldId, $lastTimestamp )
122 {
123 $rc = new RecentChange;
124 $rc->mAttribs = array(
125 'rc_timestamp' => $timestamp,
126 'rc_cur_time' => $timestamp,
127 'rc_namespace' => $title->getNamespace(),
128 'rc_title' => $title->getDBkey(),
129 'rc_type' => RC_EDIT,
130 'rc_minor' => $minor ? 1 : 0,
131 'rc_cur_id' => $title->getArticleID(),
132 'rc_user' => $user->getID(),
133 'rc_user_text' => $user->getName(),
134 'rc_comment' => $comment,
135 'rc_this_oldid' => 0,
136 'rc_last_oldid' => $oldId,
137 'rc_bot' => $user->isBot() ? 1 : 0,
138 'rc_moved_to_ns' => 0,
139 'rc_moved_to_title' => ''
140 );
141
142 $rc->mExtra = array(
143 'prefixedDBkey' => $title->getPrefixedDBkey(),
144 'lastTimestamp' => $lastTimestamp
145 );
146 $rc->save();
147 }
148
149 # Makes an entry in the database corresponding to page creation
150 # Note: the title object must be loaded with the new id using resetArticleID()
151 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment )
152 {
153 $rc = new RecentChange;
154 $rc->mAttribs = array(
155 'rc_timestamp' => $timestamp,
156 'rc_cur_time' => $timestamp,
157 'rc_namespace' => $title->getNamespace(),
158 'rc_title' => $title->getDBkey(),
159 'rc_type' => RC_NEW,
160 'rc_minor' => $minor ? 1 : 0,
161 'rc_cur_id' => $title->getArticleID(),
162 'rc_user' => $user->getID(),
163 'rc_user_text' => $user->getName(),
164 'rc_comment' => $comment,
165 'rc_this_oldid' => 0,
166 'rc_last_oldid' => 0,
167 'rc_bot' => $user->isBot() ? 1 : 0,
168 'rc_moved_to_ns' => 0,
169 'rc_moved_to_title' => ''
170 );
171
172 $rc->mExtra = array(
173 'prefixedDBkey' => $title->getPrefixedDBkey(),
174 'lastTimestamp' => 0
175 );
176 $rc->save();
177 }
178
179 # Makes an entry in the database corresponding to a rename
180 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment )
181 {
182 $rc = new RecentChange;
183 $rc->mAttribs = array(
184 'rc_timestamp' => $timestamp,
185 'rc_cur_time' => $timestamp,
186 'rc_namespace' => $oldTitle->getNamespace(),
187 'rc_title' => $oldTitle->getDBkey(),
188 'rc_type' => RC_MOVE,
189 'rc_minor' => 0,
190 'rc_cur_id' => $oldTitle->getArticleID(),
191 'rc_user' => $user->getID(),
192 'rc_user_text' => $user->getName(),
193 'rc_comment' => $comment,
194 'rc_this_oldid' => 0,
195 'rc_last_oldid' => 0,
196 'rc_bot' => $user->isBot() ? 1 : 0,
197 'rc_moved_to_ns' => $newTitle->getNamespace(),
198 'rc_moved_to_title' => $newTitle->getDBkey()
199 );
200
201 $rc->mExtra = array(
202 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
203 'lastTimestamp' => 0,
204 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
205 );
206 $rc->save();
207 }
208
209 # A log entry is different to an edit in that previous revisions are
210 # not kept
211 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment )
212 {
213 $rc = new RecentChange;
214 $rc->mAttribs = array(
215 'rc_timestamp' => $timestamp,
216 'rc_cur_time' => $timestamp,
217 'rc_namespace' => $title->getNamespace(),
218 'rc_title' => $title->getDBkey(),
219 'rc_type' => RC_LOG,
220 'rc_minor' => 0,
221 'rc_cur_id' => $title->getArticleID(),
222 'rc_user' => $user->getID(),
223 'rc_user_text' => $user->getName(),
224 'rc_comment' => $comment,
225 'rc_this_oldid' => 0,
226 'rc_last_oldid' => 0,
227 'rc_bot' => 0,
228 'rc_moved_to_ns' => 0,
229 'rc_moved_to_title' => ''
230 );
231 $rc->mExtra = array(
232 'prefixedDBkey' => $title->getPrefixedDBkey(),
233 'lastTimestamp' => 0
234 );
235 $rc->save();
236 }
237
238 # Initialises the members of this object from a mysql row object
239 function loadFromRow( $row )
240 {
241 $this->mAttribs = get_object_vars( $row );
242 $this->mExtra = array();
243 }
244
245 # Gets the end part of the diff URL assoicated with this object
246 # Blank if no diff link should be displayed
247 function diffLinkTrail( $forceCur )
248 {
249 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
250 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
251 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
252 if ( $forceCur ) {
253 $trail .= "&diff=0" ;
254 } else {
255 $trail .= "&diff=" . (int)($this->mAttribs['rc_this_oldid']);
256 }
257 } else {
258 $trail = "";
259 }
260 return $trail;
261 }
262 }
263 ?>