r100186 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100185‎ | r100186 | r100187 >
Date:23:41, 18 October 2011
Author:johnduhart
Status:ok
Tags:
Comment:
Followup r100165, fixing formatting per CR
Modified paths:
  • /trunk/extensions/CheckUser/CheckUser.hooks.php (modified) (history)
  • /trunk/extensions/CheckUser/CheckUser.php (modified) (history)
  • /trunk/extensions/CheckUser/CheckUser_body.php (modified) (history)
  • /trunk/extensions/CheckUser/api/ApiQueryCheckUser.php (modified) (history)
  • /trunk/extensions/CheckUser/api/ApiQueryCheckUserLog.php (modified) (history)
  • /trunk/extensions/CheckUser/maintenance/purgeOldData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CheckUser/CheckUser_body.php
@@ -176,7 +176,7 @@
177177 $form .= '&#160;' . $this->getPeriodMenu( $period ) . '</td>';
178178 $form .= '</tr><tr>';
179179 $form .= '<td></td>';
180 - $form .= Xml::openElement('td', array( 'class' => 'checkuserradios' ) );
 180+ $form .= Xml::openElement( 'td', array( 'class' => 'checkuserradios' ) );
181181 $form .= Xml::openElement( 'table', array( 'style' => 'border:0' ) );
182182 $form .= '<tr>';
183183 $form .= '<td>' .
@@ -278,18 +278,18 @@
279279 $counter = $blockSize = 0;
280280 $safeUsers = array();
281281 $log = new LogPage( 'block' );
282 - foreach( $users as $name ) {
 282+ foreach ( $users as $name ) {
283283 # Enforce limits
284284 $counter++;
285285 $blockSize++;
286286 # Lets not go *too* fast
287 - if( $blockSize >= 20 ) {
 287+ if ( $blockSize >= 20 ) {
288288 $blockSize = 0;
289289 wfWaitForSlaves( 5 );
290290 }
291291 $u = User::newFromName( $name, false );
292292 // If user doesn't exist, it ought to be an IP then
293 - if( is_null( $u ) || ( !$u->getId() && !IP::isIPAddress( $u->getName() ) ) ) {
 293+ if ( is_null( $u ) || ( !$u->getId() && !IP::isIPAddress( $u->getName() ) ) ) {
294294 continue;
295295 }
296296 $userTitle = $u->getUserPage();
@@ -314,12 +314,12 @@
315315 $block->prevents( 'editownusertalk', false );
316316
317317 $oldblock = Block::newFromTarget( $u->getName() );
318 - if( !$oldblock ) {
 318+ if ( !$oldblock ) {
319319 $block->insert();
320320 # Prepare log parameters
321321 $logParams = array();
322322 $logParams[] = $expirestr;
323 - if( $anonOnly ) {
 323+ if ( $anonOnly ) {
324324 $logParams[] = 'anononly';
325325 }
326326 $logParams[] = 'nocreate';
@@ -327,10 +327,10 @@
328328 $log->addEntry( 'block', $userTitle, $reason, $logParams );
329329 }
330330 # Tag userpage! (check length to avoid mistakes)
331 - if( strlen( $tag ) > 2 ) {
 331+ if ( strlen( $tag ) > 2 ) {
332332 $userpage->doEdit( $tag, $reason, EDIT_MINOR );
333333 }
334 - if( strlen( $talkTag ) > 2 ) {
 334+ if ( strlen( $talkTag ) > 2 ) {
335335 $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR );
336336 }
337337 }
@@ -648,9 +648,9 @@
649649 $ret = $dbr->select(
650650 'cu_changes',
651651 array(
652 - 'cuc_namespace','cuc_title', 'cuc_user', 'cuc_user_text', 'cuc_comment', 'cuc_actiontext',
 652+ 'cuc_namespace', 'cuc_title', 'cuc_user', 'cuc_user_text', 'cuc_comment', 'cuc_actiontext',
653653 'cuc_timestamp', 'cuc_minor', 'cuc_page_id', 'cuc_type', 'cuc_this_oldid',
654 - 'cuc_last_oldid', 'cuc_ip', 'cuc_xff','cuc_agent'
 654+ 'cuc_last_oldid', 'cuc_ip', 'cuc_xff', 'cuc_agent'
655655 ),
656656 array( $ip_conds, $time_conds ),
657657 __METHOD__,
@@ -956,7 +956,7 @@
957957 $s = $this->noMatchesMessage( $ip, !$xfor ) . "\n";
958958 } else {
959959 global $wgAuth;
960 - foreach( $ret as $row ) {
 960+ foreach ( $ret as $row ) {
961961 if ( !array_key_exists( $row->cuc_user_text, $users_edits ) ) {
962962 $users_last[$row->cuc_user_text] = $row->cuc_timestamp;
963963 $users_edits[$row->cuc_user_text] = 0;
Index: trunk/extensions/CheckUser/maintenance/purgeOldData.php
@@ -2,7 +2,7 @@
33 if ( getenv( 'MW_INSTALL_PATH' ) ) {
44 $IP = getenv( 'MW_INSTALL_PATH' );
55 } else {
6 - $IP = dirname(__FILE__).'/../../..';
 6+ $IP = dirname( __FILE__ ) . '/../../..';
77 }
88 require_once( "$IP/maintenance/Maintenance.php" );
99
Index: trunk/extensions/CheckUser/CheckUser.hooks.php
@@ -6,7 +6,7 @@
77 */
88 public static function updateCheckUserData( RecentChange $rc ) {
99 global $wgRequest, $wgCUDMaxAge;
10 -
 10+
1111 // Extract params
1212 extract( $rc->mAttribs );
1313 // Get IP
@@ -28,7 +28,7 @@
2929 } else {
3030 $actionText = '';
3131 }
32 -
 32+
3333 $dbw = wfGetDB( DB_MASTER );
3434 $cuc_id = $dbw->nextSequenceValue( 'cu_changes_cu_id_seq' );
3535 $rcRow = array(
@@ -61,17 +61,17 @@
6262 $encCutoff = $dbw->addQuotes( $dbw->timestamp( time() - $wgCUDMaxAge ) );
6363 $dbw->delete( 'cu_changes', array( "cuc_timestamp < $encCutoff" ), __METHOD__ );
6464 }
65 -
 65+
6666 return true;
6767 }
68 -
 68+
6969 /**
7070 * Hook function to store password reset
7171 * Saves user data into the cu_changes table
7272 */
7373 public static function updateCUPasswordResetData( User $user, $ip, $account ) {
7474 global $wgRequest;
75 -
 75+
7676 // Get XFF header
7777 $xff = $wgRequest->getHeader( 'X-Forwarded-For' );
7878 list( $xff_ip, $isSquidOnly ) = self::getClientIPfromXFF( $xff );
@@ -99,10 +99,10 @@
100100 'cuc_agent' => $agent
101101 );
102102 $dbw->insert( 'cu_changes', $rcRow, __METHOD__ );
103 -
 103+
104104 return true;
105105 }
106 -
 106+
107107 /**
108108 * Hook function to store email data
109109 * Saves user data into the cu_changes table
@@ -144,10 +144,10 @@
145145 'cuc_agent' => $agent
146146 );
147147 $dbw->insert( 'cu_changes', $rcRow, __METHOD__ );
148 -
 148+
149149 return true;
150150 }
151 -
 151+
152152 /**
153153 * Hook function to store autocreation data from the auth plugin
154154 * Saves user data into the cu_changes table
@@ -159,7 +159,7 @@
160160 public static function updateAutoCreateData( User $user ) {
161161 return self::logUserAccountCreation( $user, 'checkuser-autocreate-action' );
162162 }
163 -
 163+
164164 /**
165165 * @param $user User
166166 * @param $actiontext string
@@ -167,7 +167,7 @@
168168 */
169169 public static function logUserAccountCreation( User $user, $actiontext ) {
170170 global $wgRequest;
171 -
 171+
172172 // Get IP
173173 $ip = wfGetIP();
174174 // Get XFF header
@@ -198,10 +198,10 @@
199199 'cuc_agent' => $agent
200200 );
201201 $dbw->insert( 'cu_changes', $rcRow, __METHOD__ );
202 -
 202+
203203 return true;
204204 }
205 -
 205+
206206 /**
207207 * Hook function to store registration data
208208 * Saves user data into the cu_changes table
@@ -213,7 +213,7 @@
214214 public static function addNewAccount( User $user, $byEmail ) {
215215 return self::logUserAccountCreation( $user, 'checkuser-create-action' );
216216 }
217 -
 217+
218218 /**
219219 * Locates the client IP within a given XFF string
220220 * @param string $xff
@@ -287,7 +287,7 @@
288288 create_cu_log( $db );
289289 }
290290 }
291 -
 291+
292292 /**
293293 * Tell the parser test engine to create a stub cu_changes table,
294294 * or temporary pages won't save correctly during the test run.
@@ -296,7 +296,7 @@
297297 $tables[] = 'cu_changes';
298298 return true;
299299 }
300 -
 300+
301301 /**
302302 * Add a link to Special:CheckUser on Special:Contributions/<username> for
303303 * privileged users.
Index: trunk/extensions/CheckUser/CheckUser.php
@@ -82,8 +82,8 @@
8383 $wgSpecialPages['CheckUser'] = 'CheckUser';
8484 $wgSpecialPageGroups['CheckUser'] = 'users';
8585
86 -$wgAutoloadClasses['CheckUser'] = dirname( __FILE__ ) . '/CheckUser_body.php';
87 -$wgAutoloadClasses['CheckUserHooks'] = dirname( __FILE__ ) . '/CheckUser.hooks.php';
 86+$wgAutoloadClasses['CheckUser'] = $dir . '/CheckUser_body.php';
 87+$wgAutoloadClasses['CheckUserHooks'] = $dir . '/CheckUser.hooks.php';
8888
8989 // API modules
9090 $wgAutoloadClasses['ApiQueryCheckUser'] = "$dir/api/ApiQueryCheckUser.php";
Index: trunk/extensions/CheckUser/api/ApiQueryCheckUser.php
@@ -1,271 +1,279 @@
22 <?php
 3+
34 /**
45 * CheckUser API Query Module
56 */
6 -
77 class ApiQueryCheckUser extends ApiQueryBase {
8 - public function __construct( $query, $moduleName ) {
9 - parent::__construct( $query, $moduleName, 'cu' );
10 - }
11 -
12 - public function execute() {
13 - global $wgUser, $wgCheckUserForceSummary;
14 -
15 - $db = $this->getDB( DB_SLAVE );
16 - $params = $this->extractRequestParams();
17 -
18 - if( !$wgUser->isAllowed( 'checkuser' ) ) {
19 - $this->dieUsage( 'You need the checkuser right', 'permissionerror' );
20 - }
21 -
22 - if( $wgCheckUserForceSummary && is_null($params['reason']) ) {
23 - $this->dieUsage( 'You need define reason for check', 'missingdata' );
24 - }
25 -
26 - $limit = $params['limit'];
27 - $target = $params['target'];
28 - $reason = wfMsgForContent( 'checkuser-reason-api' ) . ' ' . $params['reason'];
29 - $time = wfTimestamp( TS_MW, strtotime('now') - ( strtotime($params['timecond'] ? $params['timecond'] : '2 weeks') - strtotime('now')));
30 - if( !$time ) {
31 - $this->dieUsage( 'You need use correct time limit (like "2 weeks")', 'invalidtime' );
32 - }
33 -
34 - $this->addTables( 'cu_changes' );
35 - $this->addOption( 'LIMIT', $limit + 1 );
36 - $this->addOption( 'ORDER BY', 'cuc_timestamp DESC' );
37 - $this->addWhere( "cuc_timestamp > $time" );
38 -
39 - switch($params['request']) {
40 - case 'userips':
41 - $user_id = User::idFromName( $target );
42 - if( !$user_id ) {
43 - $this->dieUsage( 'Target user does not exist', 'nosuchuser' );
44 - }
45 -
46 - $this->addFields( array('cuc_timestamp', 'cuc_ip', 'cuc_xff') );
47 - $this->addWhere( "cuc_user_text = '$target'" );
48 - $res = $this->select( __METHOD__ );
49 - $result = $this->getResult();
50 -
51 - $ips = array();
52 - foreach( $res as $row ) {
53 - $timestamp = $row->cuc_timestamp;
54 - $ip = strval($row->cuc_ip);
55 - $xff = $row->cuc_xff;
56 -
57 - if( !isset( $ips[$ip] ) ) {
58 - $ips[$ip]['end'] = $timestamp;
59 - $ips[$ip]['editcount'] = 1;
60 - } else {
61 - $ips[$ip]['start'] = $timestamp;
62 - $ips[$ip]['editcount']++;
63 - }
64 - }
65 -
66 - $count = 0;
67 - foreach( array_keys($ips) as $ip ) {
68 - $ips[$count] = $ips[$ip];
69 - $ips[$count]['address'] = $ip;
70 - unset($ips[$ip]);
71 - $count++;
72 - }
73 -
74 - CheckUser::addLogEntry('userips', 'user', $target, $reason, $user_id);
75 - $result->addValue( array( 'query', $this->getModuleName() ), 'userips', $ips );
76 - $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'userips' ), 'ip' );
77 - break;
 8+ public function __construct( $query, $moduleName ) {
 9+ parent::__construct( $query, $moduleName, 'cu' );
 10+ }
7811
79 - case 'edits':
80 - if( IP::isIPAddress($target) && isset($params['xff']) ) {
81 - $cond = CheckUser::getIpConds($db, $target, true);
82 - if( !$cond ) {
83 - $this->dieUsage( 'IP or range is invalid', 'invalidip' );
84 - }
85 - $this->addWhere( "$cond" );
86 - $log_type = array('ipedits-xff', 'ip');
87 - } elseif ( IP::isIPAddress($target) ) {
88 - $cond = CheckUser::getIpConds($db, $target);
89 - if( !$cond ) {
90 - $this->dieUsage( 'IP or range is invalid', 'invalidip' );
91 - }
92 - $this->addWhere( "$cond" );
93 - $log_type = array('ipedits', 'ip');
94 - } else {
95 - $user_id = User::idFromName( $target );
96 - if( !$user_id ) {
97 - $this->dieUsage( 'Target user is not exists', 'nosuchuser' );
98 - }
99 - $this->addWhere( "cuc_user_text = '$target'" );
100 - $log_type = array('useredits', 'user');
101 - }
102 -
103 - $this->addFields( array('cuc_namespace', 'cuc_title', 'cuc_user_text', 'cuc_actiontext', 'cuc_comment', 'cuc_minor', 'cuc_timestamp', 'cuc_ip', 'cuc_xff', 'cuc_agent') );
104 -
105 - $res = $this->select( __METHOD__ );
106 - $result = $this->getResult();
107 -
108 - $edits = array();
109 - $count = 0;
110 - foreach( $res as $row ) {
111 - $edits[$count]['timestamp'] = $row->cuc_timestamp;
112 - $edits[$count]['ns'] = $row->cuc_namespace;
113 - $edits[$count]['title'] = $row->cuc_title;
114 - $edits[$count]['user'] = $row->cuc_user_text;
115 - if( $row->cuc_actiontext ) {
116 - $edits[$count]['summary'] = $row->cuc_actiontext;
117 - } elseif( $row->cuc_comment ) {
118 - $edits[$count]['summary'] = $row->cuc_comment;
119 - }
120 - if( $row->cuc_minor ) {
121 - $edits[$count]['minor'] = 'm';
122 - }
123 - $edits[$count]['ip'] = $row->cuc_ip;
124 - if( $row->cuc_xff ) {
125 - $edits[$count]['xff'] = $row->cuc_xff;
126 - }
127 - $edits[$count]['agent'] = $row->cuc_agent;
128 - $count++;
129 - }
130 -
131 - CheckUser::addLogEntry($log_type[0], $log_type[1], $target, $reason, $user_id ? $user_id : '0');
132 - $result->addValue( array( 'query', $this->getModuleName() ), 'edits', $edits );
133 - $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'edits' ), 'action' );
134 - break;
 12+ public function execute() {
 13+ global $wgUser, $wgCheckUserForceSummary;
13514
136 - case 'ipusers':
137 - if( IP::isIPAddress($target) && isset($params['xff']) ) {
138 - $cond = CheckUser::getIpConds($db, $target, true);
139 - $this->addWhere( $cond );
140 - } elseif ( IP::isIPAddress($target) ) {
141 - $cond = CheckUser::getIpConds($db, $target);
142 - $this->addWhere( $cond );
143 - $log_type = 'ipusers';
144 - } else {
145 - $this->dieUsage( 'IP or range is invalid', 'invalidip' );
146 - }
147 -
148 - $this->addFields( array('cuc_user_text', 'cuc_timestamp', 'cuc_ip', 'cuc_agent') );
149 -
150 - $res = $this->select( __METHOD__ );
151 - $result = $this->getResult();
152 -
153 - $users = array();
154 - foreach( $res as $row ) {
155 - $user = $row->cuc_user_text;
156 - $ip = $row->cuc_ip;
157 - $agent = $row->cuc_agent;
158 -
159 - if( !isset($users[$user]) ) {
160 - $users[$user]['end'] = $row->cuc_timestamp;
161 - $users[$user]['editcount'] = 1;
162 - $users[$user]['ips'][] = $ip;
163 - $users[$user]['agents'][] = $agent;
164 - } else {
165 - $users[$user]['start'] = $row->cuc_timestamp;
166 - $users[$user]['editcount']++;
167 - if( !in_array( $ip, $users[$user]['ips'] ) ) $users[$user]['ips'][] = $ip;
168 - if( !in_array( $agent, $users[$user]['agents'] ) ) $users[$user]['agents'][] = $agent;
169 - }
170 - }
171 -
172 - $count = 0;
173 - foreach( array_keys($users) as $user ) {
174 - $users[$count] = $users[$user];
175 - $users[$count]['name'] = $user;
176 - unset($users[$user]);
177 -
178 - $result->setIndexedTagName( $users[$count]['ips'], 'ip' );
179 - $result->setIndexedTagName( $users[$count]['agents'], 'agent' );
180 -
181 - $count++;
182 - }
183 -
184 - CheckUser::addLogEntry($log_type, 'ip', $target, $reason);
185 - $result->addValue( array( 'query', $this->getModuleName() ), 'ipusers', $users );
186 - $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'ipusers' ), 'user' );
187 - break;
188 -
189 - default:
190 - $this->dieUsage( 'Invalid request mode', 'invalidmode' );
191 - }
192 - }
193 -
194 - public function mustBePosted() {
195 - return true;
196 - }
197 -
198 - public function isWriteMode() {
199 - return true;
200 - }
 15+ $db = $this->getDB( DB_SLAVE );
 16+ $params = $this->extractRequestParams();
20117
202 - public function getAllowedParams() {
203 - return array(
204 - 'request' => array(
205 - ApiBase::PARAM_REQUIRED => false,
206 - ApiBase::PARAM_TYPE => array(
207 - 'userips',
208 - 'edits',
209 - 'ipusers'
210 - )
211 - ),
212 - 'target' => array(
213 - ApiBase::PARAM_REQUIRED => false
214 - ),
215 - 'reason' => null,
216 - 'limit' => array(
217 - ApiBase::PARAM_DFLT => 1000,
218 - ApiBase::PARAM_TYPE => 'limit',
219 - ApiBase::PARAM_MIN => 1,
220 - ApiBase::PARAM_MAX => 5000,
221 - ApiBase::PARAM_MAX2 => 5000
222 - ),
223 - 'timecond' => null
224 - );
225 - }
 18+ if ( !$wgUser->isAllowed( 'checkuser' ) ) {
 19+ $this->dieUsage( 'You need the checkuser right', 'permissionerror' );
 20+ }
22621
227 - public function getParamDescription() {
228 - return array(
229 - 'request' => array(
230 - 'Type of CheckUser request',
231 - ' userips - get IP of target user',
232 - ' edits - get changes from target IP or range',
233 - ' ipusers - get users from target IP or range',
234 - ),
235 - 'target' => "Username or IP-address/range to perform check",
236 - 'reason' => 'Reason to check',
237 - 'limit' => 'Limit of rows',
238 - 'timecond' => 'Time limit of user data (like "2 weeks")'
239 - );
240 - }
 22+ if ( $wgCheckUserForceSummary && is_null( $params['reason'] ) ) {
 23+ $this->dieUsage( 'You need define reason for check', 'missingdata' );
 24+ }
24125
242 - public function getDescription() {
243 - return 'Allows check which IPs are used by a given username and which usernames are used by a given IP';
244 - }
 26+ $limit = $params['limit'];
 27+ $target = $params['target'];
 28+ $reason = wfMsgForContent( 'checkuser-reason-api' ) . ' ' . $params['reason'];
 29+ $time = wfTimestamp( TS_MW,
 30+ strtotime( 'now' ) - ( strtotime( $params['timecond'] ? $params['timecond'] : '2 weeks' ) - strtotime( 'now'
 31+ ) )
 32+ );
 33+ if ( !$time ) {
 34+ $this->dieUsage( 'You need use correct time limit (like "2 weeks")', 'invalidtime' );
 35+ }
24536
246 - public function getPossibleErrors() {
247 - return array_merge( parent::getPossibleErrors(),
248 - array(
249 - array( 'nosuchuser' ),
250 - array( 'invalidip' ),
251 - array( 'permissionerror' ),
252 - array( 'invalidmode' ),
253 - array( 'missingdata' )
254 - )
255 - );
256 - }
 37+ $this->addTables( 'cu_changes' );
 38+ $this->addOption( 'LIMIT', $limit + 1 );
 39+ $this->addOption( 'ORDER BY', 'cuc_timestamp DESC' );
 40+ $this->addWhere( "cuc_timestamp > $time" );
25741
258 - public function getExamples() {
259 - return array(
260 - 'api.php?action=query&list=checkuser&curequest=userips&cutarget=Jimbo_Wales',
261 - 'api.php?action=query&list=checkuser&curequest=edits&cutarget=127.0.0.1/16/xff&cureason=Some_check'
262 - );
263 - }
 42+ switch ( $params['request'] ) {
 43+ case 'userips':
 44+ $user_id = User::idFromName( $target );
 45+ if ( !$user_id ) {
 46+ $this->dieUsage( 'Target user does not exist', 'nosuchuser' );
 47+ }
26448
265 - public function getHelpUrls() {
266 - return 'http://www.mediawiki.org/wiki/Extension:CheckUser#API';
267 - }
 49+ $this->addFields( array( 'cuc_timestamp', 'cuc_ip', 'cuc_xff' ) );
 50+ $this->addWhere( "cuc_user_text = '$target'" );
 51+ $res = $this->select( __METHOD__ );
 52+ $result = $this->getResult();
26853
269 - public function getVersion() {
270 - return __CLASS__ . ': $Id$';
271 - }
 54+ $ips = array();
 55+ foreach ( $res as $row ) {
 56+ $timestamp = $row->cuc_timestamp;
 57+ $ip = strval( $row->cuc_ip );
 58+ $xff = $row->cuc_xff;
 59+
 60+ if ( !isset( $ips[$ip] ) ) {
 61+ $ips[$ip]['end'] = $timestamp;
 62+ $ips[$ip]['editcount'] = 1;
 63+ } else {
 64+ $ips[$ip]['start'] = $timestamp;
 65+ $ips[$ip]['editcount']++;
 66+ }
 67+ }
 68+
 69+ $count = 0;
 70+ foreach ( array_keys( $ips ) as $ip ) {
 71+ $ips[$count] = $ips[$ip];
 72+ $ips[$count]['address'] = $ip;
 73+ unset( $ips[$ip] );
 74+ $count++;
 75+ }
 76+
 77+ CheckUser::addLogEntry( 'userips', 'user', $target, $reason, $user_id );
 78+ $result->addValue( array( 'query', $this->getModuleName() ), 'userips', $ips );
 79+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'userips' ), 'ip' );
 80+ break;
 81+
 82+ case 'edits':
 83+ if ( IP::isIPAddress( $target ) && isset( $params['xff'] ) ) {
 84+ $cond = CheckUser::getIpConds( $db, $target, true );
 85+ if ( !$cond ) {
 86+ $this->dieUsage( 'IP or range is invalid', 'invalidip' );
 87+ }
 88+ $this->addWhere( "$cond" );
 89+ $log_type = array( 'ipedits-xff', 'ip' );
 90+ } elseif ( IP::isIPAddress( $target ) ) {
 91+ $cond = CheckUser::getIpConds( $db, $target );
 92+ if ( !$cond ) {
 93+ $this->dieUsage( 'IP or range is invalid', 'invalidip' );
 94+ }
 95+ $this->addWhere( "$cond" );
 96+ $log_type = array( 'ipedits', 'ip' );
 97+ } else {
 98+ $user_id = User::idFromName( $target );
 99+ if ( !$user_id ) {
 100+ $this->dieUsage( 'Target user is not exists', 'nosuchuser' );
 101+ }
 102+ $this->addWhere( "cuc_user_text = '$target'" );
 103+ $log_type = array( 'useredits', 'user' );
 104+ }
 105+
 106+ $this->addFields( array( 'cuc_namespace', 'cuc_title', 'cuc_user_text', 'cuc_actiontext', 'cuc_comment', 'cuc_minor', 'cuc_timestamp', 'cuc_ip', 'cuc_xff', 'cuc_agent' )
 107+ );
 108+
 109+ $res = $this->select( __METHOD__ );
 110+ $result = $this->getResult();
 111+
 112+ $edits = array();
 113+ $count = 0;
 114+ foreach ( $res as $row ) {
 115+ $edits[$count]['timestamp'] = $row->cuc_timestamp;
 116+ $edits[$count]['ns'] = $row->cuc_namespace;
 117+ $edits[$count]['title'] = $row->cuc_title;
 118+ $edits[$count]['user'] = $row->cuc_user_text;
 119+ if ( $row->cuc_actiontext ) {
 120+ $edits[$count]['summary'] = $row->cuc_actiontext;
 121+ } elseif ( $row->cuc_comment ) {
 122+ $edits[$count]['summary'] = $row->cuc_comment;
 123+ }
 124+ if ( $row->cuc_minor ) {
 125+ $edits[$count]['minor'] = 'm';
 126+ }
 127+ $edits[$count]['ip'] = $row->cuc_ip;
 128+ if ( $row->cuc_xff ) {
 129+ $edits[$count]['xff'] = $row->cuc_xff;
 130+ }
 131+ $edits[$count]['agent'] = $row->cuc_agent;
 132+ $count++;
 133+ }
 134+
 135+ CheckUser::addLogEntry( $log_type[0], $log_type[1], $target, $reason, $user_id ? $user_id : '0' );
 136+ $result->addValue( array( 'query', $this->getModuleName() ), 'edits', $edits );
 137+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'edits' ), 'action' );
 138+ break;
 139+
 140+ case 'ipusers':
 141+ if ( IP::isIPAddress( $target ) && isset( $params['xff'] ) ) {
 142+ $cond = CheckUser::getIpConds( $db, $target, true );
 143+ $this->addWhere( $cond );
 144+ } elseif ( IP::isIPAddress( $target ) ) {
 145+ $cond = CheckUser::getIpConds( $db, $target );
 146+ $this->addWhere( $cond );
 147+ $log_type = 'ipusers';
 148+ } else {
 149+ $this->dieUsage( 'IP or range is invalid', 'invalidip' );
 150+ }
 151+
 152+ $this->addFields( array( 'cuc_user_text', 'cuc_timestamp', 'cuc_ip', 'cuc_agent' ) );
 153+
 154+ $res = $this->select( __METHOD__ );
 155+ $result = $this->getResult();
 156+
 157+ $users = array();
 158+ foreach ( $res as $row ) {
 159+ $user = $row->cuc_user_text;
 160+ $ip = $row->cuc_ip;
 161+ $agent = $row->cuc_agent;
 162+
 163+ if ( !isset( $users[$user] ) ) {
 164+ $users[$user]['end'] = $row->cuc_timestamp;
 165+ $users[$user]['editcount'] = 1;
 166+ $users[$user]['ips'][] = $ip;
 167+ $users[$user]['agents'][] = $agent;
 168+ } else {
 169+ $users[$user]['start'] = $row->cuc_timestamp;
 170+ $users[$user]['editcount']++;
 171+ if ( !in_array( $ip, $users[$user]['ips'] ) ) {
 172+ $users[$user]['ips'][] = $ip;
 173+ }
 174+ if ( !in_array( $agent, $users[$user]['agents'] ) ) {
 175+ $users[$user]['agents'][] = $agent;
 176+ }
 177+ }
 178+ }
 179+
 180+ $count = 0;
 181+ foreach ( array_keys( $users ) as $user ) {
 182+ $users[$count] = $users[$user];
 183+ $users[$count]['name'] = $user;
 184+ unset( $users[$user] );
 185+
 186+ $result->setIndexedTagName( $users[$count]['ips'], 'ip' );
 187+ $result->setIndexedTagName( $users[$count]['agents'], 'agent' );
 188+
 189+ $count++;
 190+ }
 191+
 192+ CheckUser::addLogEntry( $log_type, 'ip', $target, $reason );
 193+ $result->addValue( array( 'query', $this->getModuleName() ), 'ipusers', $users );
 194+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'ipusers' ), 'user' );
 195+ break;
 196+
 197+ default:
 198+ $this->dieUsage( 'Invalid request mode', 'invalidmode' );
 199+ }
 200+ }
 201+
 202+ public function mustBePosted() {
 203+ return true;
 204+ }
 205+
 206+ public function isWriteMode() {
 207+ return true;
 208+ }
 209+
 210+ public function getAllowedParams() {
 211+ return array(
 212+ 'request' => array(
 213+ ApiBase::PARAM_REQUIRED => false,
 214+ ApiBase::PARAM_TYPE => array(
 215+ 'userips',
 216+ 'edits',
 217+ 'ipusers'
 218+ )
 219+ ),
 220+ 'target' => array(
 221+ ApiBase::PARAM_REQUIRED => false
 222+ ),
 223+ 'reason' => null,
 224+ 'limit' => array(
 225+ ApiBase::PARAM_DFLT => 1000,
 226+ ApiBase::PARAM_TYPE => 'limit',
 227+ ApiBase::PARAM_MIN => 1,
 228+ ApiBase::PARAM_MAX => 5000,
 229+ ApiBase::PARAM_MAX2 => 5000
 230+ ),
 231+ 'timecond' => null
 232+ );
 233+ }
 234+
 235+ public function getParamDescription() {
 236+ return array(
 237+ 'request' => array(
 238+ 'Type of CheckUser request',
 239+ ' userips - get IP of target user',
 240+ ' edits - get changes from target IP or range',
 241+ ' ipusers - get users from target IP or range',
 242+ ),
 243+ 'target' => "Username or IP-address/range to perform check",
 244+ 'reason' => 'Reason to check',
 245+ 'limit' => 'Limit of rows',
 246+ 'timecond' => 'Time limit of user data (like "2 weeks")',
 247+ );
 248+ }
 249+
 250+ public function getDescription() {
 251+ return 'Allows check which IPs are used by a given username and which usernames are used by a given IP';
 252+ }
 253+
 254+ public function getPossibleErrors() {
 255+ return array_merge( parent::getPossibleErrors(),
 256+ array(
 257+ array( 'nosuchuser' ),
 258+ array( 'invalidip' ),
 259+ array( 'permissionerror' ),
 260+ array( 'invalidmode' ),
 261+ array( 'missingdata' ),
 262+ )
 263+ );
 264+ }
 265+
 266+ public function getExamples() {
 267+ return array(
 268+ 'api.php?action=query&list=checkuser&curequest=userips&cutarget=Jimbo_Wales',
 269+ 'api.php?action=query&list=checkuser&curequest=edits&cutarget=127.0.0.1/16/xff&cureason=Some_check',
 270+ );
 271+ }
 272+
 273+ public function getHelpUrls() {
 274+ return 'http://www.mediawiki.org/wiki/Extension:CheckUser#API';
 275+ }
 276+
 277+ public function getVersion() {
 278+ return __CLASS__ . ': $Id$';
 279+ }
272280 }
\ No newline at end of file
Index: trunk/extensions/CheckUser/api/ApiQueryCheckUserLog.php
@@ -1,117 +1,121 @@
22 <?php
 3+
34 /**
45 * CheckUser API Query Module
56 */
 7+class ApiQueryCheckUserLog extends ApiQueryBase {
 8+ public function __construct( $query, $moduleName ) {
 9+ parent::__construct( $query, $moduleName, 'cul' );
 10+ }
611
7 -class ApiQueryCheckUserLog extends ApiQueryBase {
8 - public function __construct( $query, $moduleName ) {
9 - parent::__construct( $query, $moduleName, 'cul' );
10 - }
11 -
12 - public function execute() {
13 - global $wgUser;
14 -
15 - $params = $this->extractRequestParams();
16 -
17 - if( !$wgUser->isAllowed( 'checkuser-log' ) ) {
18 - $this->dieUsage( 'You need the checkuser-log right', 'permissionerror' );
19 - }
20 -
21 - $user = $params['user'];
22 - $limit = $params['limit'];
23 - $target = $params['target'];
24 - $from = $params['from'];
25 - $to = $params['to'];
26 -
27 - $this->addTables( 'cu_log' );
28 - $this->addOption( 'LIMIT', $limit + 1 );
29 - $this->addOption( 'ORDER BY', 'cul_timestamp DESC' );
30 -
31 - $this->addFields( array('cul_timestamp', 'cul_user_text', 'cul_reason', 'cul_type', 'cul_target_text') );
32 -
33 - if( isset($user) ) $this->addWhere( "cul_user_text = '$user'" );
34 - if( isset($target) ) $this->addWhere( "cul_target_text = '$target'" );
35 - if( isset($from) && isset($to) ) {
36 - $this->addWhere( "cul_timestamp BETWEEN '$from' AND '$to'" );
37 - unset($from, $to);
38 - } elseif ( isset($from) ) {
39 - $this->addWhere( "cul_timestamp < $from" );
40 - } elseif ( isset($to) ) {
41 - $this->addWhere( "cul_timestamp > $to" );
42 - }
43 -
44 - $res = $this->select( __METHOD__ );
45 - $result = $this->getResult();
46 -
47 - $count = 0;
48 - $log = array();
49 - foreach( $res as $row ) {
50 - $log[$count]['timestamp'] = $row->cul_timestamp;
51 - $log[$count]['checkuser'] = $row->cul_user_text;
52 - $log[$count]['type'] = $row->cul_type;
53 - $log[$count]['reason'] = $row->cul_reason;
54 - $log[$count]['target'] = $row->cul_target_text;
55 - $count++;
56 - }
57 -
58 - $result->addValue( array( 'query', $this->getModuleName() ), 'entries', $log );
59 - $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'entries' ), 'entry' );
60 - }
61 -
62 - public function getAllowedParams() {
63 - return array(
64 - 'user' => null,
65 - 'target' => null,
66 - 'limit' => array(
 12+ public function execute() {
 13+ global $wgUser;
 14+
 15+ $params = $this->extractRequestParams();
 16+
 17+ if ( !$wgUser->isAllowed( 'checkuser-log' ) ) {
 18+ $this->dieUsage( 'You need the checkuser-log right', 'permissionerror' );
 19+ }
 20+
 21+ $user = $params['user'];
 22+ $limit = $params['limit'];
 23+ $target = $params['target'];
 24+ $from = $params['from'];
 25+ $to = $params['to'];
 26+
 27+ $this->addTables( 'cu_log' );
 28+ $this->addOption( 'LIMIT', $limit + 1 );
 29+ $this->addOption( 'ORDER BY', 'cul_timestamp DESC' );
 30+
 31+ $this->addFields( array( 'cul_timestamp', 'cul_user_text', 'cul_reason', 'cul_type', 'cul_target_text' ) );
 32+
 33+ if ( isset( $user ) ) {
 34+ $this->addWhere( "cul_user_text = '$user'" );
 35+ }
 36+ if ( isset( $target ) ) {
 37+ $this->addWhere( "cul_target_text = '$target'" );
 38+ }
 39+ if ( isset( $from ) && isset( $to ) ) {
 40+ $this->addWhere( "cul_timestamp BETWEEN '$from' AND '$to'" );
 41+ unset( $from, $to );
 42+ } elseif ( isset( $from ) ) {
 43+ $this->addWhere( "cul_timestamp < $from" );
 44+ } elseif ( isset( $to ) ) {
 45+ $this->addWhere( "cul_timestamp > $to" );
 46+ }
 47+
 48+ $res = $this->select( __METHOD__ );
 49+ $result = $this->getResult();
 50+
 51+ $count = 0;
 52+ $log = array();
 53+ foreach ( $res as $row ) {
 54+ $log[$count]['timestamp'] = $row->cul_timestamp;
 55+ $log[$count]['checkuser'] = $row->cul_user_text;
 56+ $log[$count]['type'] = $row->cul_type;
 57+ $log[$count]['reason'] = $row->cul_reason;
 58+ $log[$count]['target'] = $row->cul_target_text;
 59+ $count++;
 60+ }
 61+
 62+ $result->addValue( array( 'query', $this->getModuleName() ), 'entries', $log );
 63+ $result->setIndexedTagName_internal( array( 'query', $this->getModuleName(), 'entries' ), 'entry' );
 64+ }
 65+
 66+ public function getAllowedParams() {
 67+ return array(
 68+ 'user' => null,
 69+ 'target' => null,
 70+ 'limit' => array(
6771 ApiBase::PARAM_DFLT => 10,
6872 ApiBase::PARAM_TYPE => 'limit',
6973 ApiBase::PARAM_MIN => 1,
7074 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
71 - ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
 75+ ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
7276 ),
73 - 'from' => array(
74 - ApiBase::PARAM_TYPE => 'timestamp'
 77+ 'from' => array(
 78+ ApiBase::PARAM_TYPE => 'timestamp',
7579 ),
76 - 'to' => array(
77 - ApiBase::PARAM_TYPE => 'timestamp'
 80+ 'to' => array(
 81+ ApiBase::PARAM_TYPE => 'timestamp',
7882 ),
79 - );
80 - }
 83+ );
 84+ }
8185
82 - public function getParamDescription() {
83 - return array(
84 - 'user' => 'Username of CheckUser',
85 - 'target' => "Checked user or IP-address/range",
86 - 'limit' => 'Limit of rows',
87 - 'from' => 'The timestamp to start enumerating from',
88 - 'to' => 'The timestamp to end enumerating'
89 - );
90 - }
 86+ public function getParamDescription() {
 87+ return array(
 88+ 'user' => 'Username of CheckUser',
 89+ 'target' => "Checked user or IP-address/range",
 90+ 'limit' => 'Limit of rows',
 91+ 'from' => 'The timestamp to start enumerating from',
 92+ 'to' => 'The timestamp to end enumerating',
 93+ );
 94+ }
9195
92 - public function getDescription() {
93 - return 'Allows get entries of CheckUser log';
94 - }
 96+ public function getDescription() {
 97+ return 'Allows get entries of CheckUser log';
 98+ }
9599
96 - public function getPossibleErrors() {
97 - return array_merge( parent::getPossibleErrors(),
98 - array(
99 - array( 'permissionerror' )
100 - )
101 - );
102 - }
 100+ public function getPossibleErrors() {
 101+ return array_merge( parent::getPossibleErrors(),
 102+ array(
 103+ array( 'permissionerror' ),
 104+ ),
 105+ );
 106+ }
103107
104 - public function getExamples() {
105 - return array(
106 - 'api.php?action=query&list=checkuserlog&culuser=WikiSysop&limit=25',
107 - 'api.php?action=query&list=checkuserlog&cultarget=127.0.0.1&culfrom=20111015230000'
108 - );
109 - }
 108+ public function getExamples() {
 109+ return array(
 110+ 'api.php?action=query&list=checkuserlog&culuser=WikiSysop&limit=25',
 111+ 'api.php?action=query&list=checkuserlog&cultarget=127.0.0.1&culfrom=20111015230000',
 112+ );
 113+ }
110114
111 - public function getHelpUrls() {
112 - return 'http://www.mediawiki.org/wiki/Extension:CheckUser#API';
113 - }
 115+ public function getHelpUrls() {
 116+ return 'http://www.mediawiki.org/wiki/Extension:CheckUser#API';
 117+ }
114118
115 - public function getVersion() {
116 - return __CLASS__ . ': $Id$';
117 - }
 119+ public function getVersion() {
 120+ return __CLASS__ . ': $Id$';
 121+ }
118122 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r100187Followup r100165 and r100186: Formatting, requirement change, and removing mi...johnduhart23:46, 18 October 2011
r100188Follow up r100186: Removing mistakenly placed commajohnduhart23:48, 18 October 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r100165Adds API Module by cryptocoryne...johnduhart21:37, 18 October 2011

Status & tagging log