| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 186 |
| Code Lines | 123 |
| Lines | 14 |
| Ratio | 7.53 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 160 | private function prepareQuery() { |
||
| 161 | // We're after the revision table, and the corresponding page |
||
| 162 | // row for anything we retrieve. We may also need the |
||
| 163 | // recentchanges row and/or tag summary row. |
||
| 164 | $user = $this->getUser(); |
||
| 165 | $tables = [ 'page', 'revision' ]; // Order may change |
||
| 166 | $this->addWhere( 'page_id=rev_page' ); |
||
| 167 | |||
| 168 | // Handle continue parameter |
||
| 169 | if ( !is_null( $this->params['continue'] ) ) { |
||
| 170 | $continue = explode( '|', $this->params['continue'] ); |
||
| 171 | $db = $this->getDB(); |
||
| 172 | if ( $this->multiUserMode ) { |
||
| 173 | $this->dieContinueUsageIf( count( $continue ) != 4 ); |
||
| 174 | $modeFlag = array_shift( $continue ); |
||
| 175 | $this->dieContinueUsageIf( !in_array( $modeFlag, [ 'id', 'name' ] ) ); |
||
| 176 | if ( $this->idMode && $modeFlag === 'name' ) { |
||
| 177 | // The users were created since this query started, but we |
||
| 178 | // can't go back and change modes now. So just keep on with |
||
| 179 | // name mode. |
||
| 180 | $this->idMode = false; |
||
| 181 | } |
||
| 182 | $this->dieContinueUsageIf( ( $modeFlag === 'id' ) !== $this->idMode ); |
||
| 183 | $userField = $this->idMode ? 'rev_user' : 'rev_user_text'; |
||
| 184 | $encUser = $db->addQuotes( array_shift( $continue ) ); |
||
| 185 | } else { |
||
| 186 | $this->dieContinueUsageIf( count( $continue ) != 2 ); |
||
| 187 | } |
||
| 188 | $encTS = $db->addQuotes( $db->timestamp( $continue[0] ) ); |
||
| 189 | $encId = (int)$continue[1]; |
||
| 190 | $this->dieContinueUsageIf( $encId != $continue[1] ); |
||
| 191 | $op = ( $this->params['dir'] == 'older' ? '<' : '>' ); |
||
| 192 | if ( $this->multiUserMode ) { |
||
| 193 | $this->addWhere( |
||
| 194 | "$userField $op $encUser OR " . |
||
| 195 | "($userField = $encUser AND " . |
||
| 196 | "(rev_timestamp $op $encTS OR " . |
||
| 197 | "(rev_timestamp = $encTS AND " . |
||
| 198 | "rev_id $op= $encId)))" |
||
| 199 | ); |
||
| 200 | } else { |
||
| 201 | $this->addWhere( |
||
| 202 | "rev_timestamp $op $encTS OR " . |
||
| 203 | "(rev_timestamp = $encTS AND " . |
||
| 204 | "rev_id $op= $encId)" |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // Don't include any revisions where we're not supposed to be able to |
||
| 210 | // see the username. |
||
| 211 | if ( !$user->isAllowed( 'deletedhistory' ) ) { |
||
| 212 | $bitmask = Revision::DELETED_USER; |
||
| 213 | } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
||
| 214 | $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED; |
||
| 215 | } else { |
||
| 216 | $bitmask = 0; |
||
| 217 | } |
||
| 218 | if ( $bitmask ) { |
||
| 219 | $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" ); |
||
| 220 | } |
||
| 221 | |||
| 222 | // We only want pages by the specified users. |
||
| 223 | if ( $this->prefixMode ) { |
||
| 224 | $this->addWhere( 'rev_user_text' . |
||
| 225 | $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) ); |
||
| 226 | } elseif ( $this->idMode ) { |
||
| 227 | $this->addWhereFld( 'rev_user', $this->userids ); |
||
| 228 | } else { |
||
| 229 | $this->addWhereFld( 'rev_user_text', $this->usernames ); |
||
| 230 | } |
||
| 231 | // ... and in the specified timeframe. |
||
| 232 | // Ensure the same sort order for rev_user/rev_user_text and rev_timestamp |
||
| 233 | // so our query is indexed |
||
| 234 | if ( $this->multiUserMode ) { |
||
| 235 | $this->addWhereRange( $this->idMode ? 'rev_user' : 'rev_user_text', |
||
| 236 | $this->params['dir'], null, null ); |
||
| 237 | } |
||
| 238 | $this->addTimestampWhereRange( 'rev_timestamp', |
||
| 239 | $this->params['dir'], $this->params['start'], $this->params['end'] ); |
||
| 240 | // Include in ORDER BY for uniqueness |
||
| 241 | $this->addWhereRange( 'rev_id', $this->params['dir'], null, null ); |
||
| 242 | |||
| 243 | $this->addWhereFld( 'page_namespace', $this->params['namespace'] ); |
||
| 244 | |||
| 245 | $show = $this->params['show']; |
||
| 246 | if ( $this->params['toponly'] ) { // deprecated/old param |
||
| 247 | $show[] = 'top'; |
||
| 248 | } |
||
| 249 | if ( !is_null( $show ) ) { |
||
| 250 | $show = array_flip( $show ); |
||
| 251 | |||
| 252 | View Code Duplication | if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) ) |
|
| 253 | || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) |
||
| 254 | || ( isset( $show['top'] ) && isset( $show['!top'] ) ) |
||
| 255 | || ( isset( $show['new'] ) && isset( $show['!new'] ) ) |
||
| 256 | ) { |
||
| 257 | $this->dieUsageMsg( 'show' ); |
||
| 258 | } |
||
| 259 | |||
| 260 | $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) ); |
||
| 261 | $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) ); |
||
| 262 | $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) ); |
||
| 263 | $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) ); |
||
| 264 | $this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) ); |
||
| 265 | $this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) ); |
||
| 266 | $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) ); |
||
| 267 | $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) ); |
||
| 268 | } |
||
| 269 | $this->addOption( 'LIMIT', $this->params['limit'] + 1 ); |
||
| 270 | |||
| 271 | // Mandatory fields: timestamp allows request continuation |
||
| 272 | // ns+title checks if the user has access rights for this page |
||
| 273 | // user_text is necessary if multiple users were specified |
||
| 274 | $this->addFields( [ |
||
| 275 | 'rev_id', |
||
| 276 | 'rev_timestamp', |
||
| 277 | 'page_namespace', |
||
| 278 | 'page_title', |
||
| 279 | 'rev_user', |
||
| 280 | 'rev_user_text', |
||
| 281 | 'rev_deleted' |
||
| 282 | ] ); |
||
| 283 | |||
| 284 | if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) || |
||
| 285 | $this->fld_patrolled |
||
| 286 | ) { |
||
| 287 | if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) { |
||
| 288 | $this->dieUsage( |
||
| 289 | 'You need the patrol right to request the patrolled flag', |
||
| 290 | 'permissiondenied' |
||
| 291 | ); |
||
| 292 | } |
||
| 293 | |||
| 294 | // Use a redundant join condition on both |
||
| 295 | // timestamp and ID so we can use the timestamp |
||
| 296 | // index |
||
| 297 | $index['recentchanges'] = 'rc_user_text'; |
||
| 298 | if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) { |
||
| 299 | // Put the tables in the right order for |
||
| 300 | // STRAIGHT_JOIN |
||
| 301 | $tables = [ 'revision', 'recentchanges', 'page' ]; |
||
| 302 | $this->addOption( 'STRAIGHT_JOIN' ); |
||
| 303 | $this->addWhere( 'rc_user_text=rev_user_text' ); |
||
| 304 | $this->addWhere( 'rc_timestamp=rev_timestamp' ); |
||
| 305 | $this->addWhere( 'rc_this_oldid=rev_id' ); |
||
| 306 | } else { |
||
| 307 | $tables[] = 'recentchanges'; |
||
| 308 | $this->addJoinConds( [ 'recentchanges' => [ |
||
| 309 | 'LEFT JOIN', [ |
||
| 310 | 'rc_user_text=rev_user_text', |
||
| 311 | 'rc_timestamp=rev_timestamp', |
||
| 312 | 'rc_this_oldid=rev_id' ] ] ] ); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | $this->addTables( $tables ); |
||
| 317 | $this->addFieldsIf( 'rev_page', $this->fld_ids ); |
||
| 318 | $this->addFieldsIf( 'page_latest', $this->fld_flags ); |
||
| 319 | // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed? |
||
| 320 | $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment ); |
||
| 321 | $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff ); |
||
| 322 | $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags ); |
||
| 323 | $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff || $this->fld_ids ); |
||
| 324 | $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled ); |
||
| 325 | |||
| 326 | View Code Duplication | if ( $this->fld_tags ) { |
|
| 327 | $this->addTables( 'tag_summary' ); |
||
| 328 | $this->addJoinConds( |
||
| 329 | [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ] |
||
| 330 | ); |
||
| 331 | $this->addFields( 'ts_tags' ); |
||
| 332 | } |
||
| 333 | |||
| 334 | if ( isset( $this->params['tag'] ) ) { |
||
| 335 | $this->addTables( 'change_tag' ); |
||
| 336 | $this->addJoinConds( |
||
| 337 | [ 'change_tag' => [ 'INNER JOIN', [ 'rev_id=ct_rev_id' ] ] ] |
||
| 338 | ); |
||
| 339 | $this->addWhereFld( 'ct_tag', $this->params['tag'] ); |
||
| 340 | } |
||
| 341 | |||
| 342 | if ( isset( $index ) ) { |
||
| 343 | $this->addOption( 'USE INDEX', $index ); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 560 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.