| Conditions | 51 |
| Paths | > 20000 |
| Total Lines | 240 |
| Code Lines | 161 |
| Lines | 52 |
| Ratio | 21.67 % |
| 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 |
||
| 57 | private function run( $resultPageSet = null ) { |
||
| 58 | $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' ); |
||
| 59 | |||
| 60 | $params = $this->extractRequestParams(); |
||
| 61 | |||
| 62 | $user = $this->getUser(); |
||
| 63 | $wlowner = $this->getWatchlistUser( $params ); |
||
| 64 | |||
| 65 | if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) { |
||
| 66 | $prop = array_flip( $params['prop'] ); |
||
| 67 | |||
| 68 | $this->fld_ids = isset( $prop['ids'] ); |
||
| 69 | $this->fld_title = isset( $prop['title'] ); |
||
| 70 | $this->fld_flags = isset( $prop['flags'] ); |
||
| 71 | $this->fld_user = isset( $prop['user'] ); |
||
| 72 | $this->fld_userid = isset( $prop['userid'] ); |
||
| 73 | $this->fld_comment = isset( $prop['comment'] ); |
||
| 74 | $this->fld_parsedcomment = isset( $prop['parsedcomment'] ); |
||
| 75 | $this->fld_timestamp = isset( $prop['timestamp'] ); |
||
| 76 | $this->fld_sizes = isset( $prop['sizes'] ); |
||
| 77 | $this->fld_patrol = isset( $prop['patrol'] ); |
||
| 78 | $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] ); |
||
| 79 | $this->fld_loginfo = isset( $prop['loginfo'] ); |
||
| 80 | |||
| 81 | if ( $this->fld_patrol ) { |
||
| 82 | if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) { |
||
| 83 | $this->dieUsage( 'patrol property is not available', 'patrol' ); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | $this->addFields( [ |
||
| 89 | 'rc_id', |
||
| 90 | 'rc_namespace', |
||
| 91 | 'rc_title', |
||
| 92 | 'rc_timestamp', |
||
| 93 | 'rc_type', |
||
| 94 | 'rc_deleted', |
||
| 95 | ] ); |
||
| 96 | |||
| 97 | if ( is_null( $resultPageSet ) ) { |
||
| 98 | $this->addFields( [ |
||
| 99 | 'rc_cur_id', |
||
| 100 | 'rc_this_oldid', |
||
| 101 | 'rc_last_oldid', |
||
| 102 | ] ); |
||
| 103 | |||
| 104 | $this->addFieldsIf( [ 'rc_type', 'rc_minor', 'rc_bot' ], $this->fld_flags ); |
||
| 105 | $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid ); |
||
| 106 | $this->addFieldsIf( 'rc_user_text', $this->fld_user ); |
||
| 107 | $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment ); |
||
| 108 | $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrol ); |
||
| 109 | $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes ); |
||
| 110 | $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp ); |
||
| 111 | $this->addFieldsIf( |
||
| 112 | [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ], |
||
| 113 | $this->fld_loginfo |
||
| 114 | ); |
||
| 115 | } elseif ( $params['allrev'] ) { |
||
| 116 | $this->addFields( 'rc_this_oldid' ); |
||
| 117 | } else { |
||
| 118 | $this->addFields( 'rc_cur_id' ); |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->addTables( [ |
||
| 122 | 'recentchanges', |
||
| 123 | 'watchlist', |
||
| 124 | ] ); |
||
| 125 | |||
| 126 | $userId = $wlowner->getId(); |
||
| 127 | $this->addJoinConds( [ 'watchlist' => [ 'INNER JOIN', |
||
| 128 | [ |
||
| 129 | 'wl_user' => $userId, |
||
| 130 | 'wl_namespace=rc_namespace', |
||
| 131 | 'wl_title=rc_title' |
||
| 132 | ] |
||
| 133 | ] ] ); |
||
| 134 | |||
| 135 | $db = $this->getDB(); |
||
| 136 | |||
| 137 | $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], |
||
| 138 | $params['start'], $params['end'] ); |
||
| 139 | // Include in ORDER BY for uniqueness |
||
| 140 | $this->addWhereRange( 'rc_id', $params['dir'], null, null ); |
||
| 141 | |||
| 142 | if ( !is_null( $params['continue'] ) ) { |
||
| 143 | $cont = explode( '|', $params['continue'] ); |
||
| 144 | $this->dieContinueUsageIf( count( $cont ) != 2 ); |
||
| 145 | $op = ( $params['dir'] === 'newer' ? '>' : '<' ); |
||
| 146 | $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) ); |
||
| 147 | $continueId = (int)$cont[1]; |
||
| 148 | $this->dieContinueUsageIf( $continueId != $cont[1] ); |
||
| 149 | $this->addWhere( "rc_timestamp $op $continueTimestamp OR " . |
||
| 150 | "(rc_timestamp = $continueTimestamp AND " . |
||
| 151 | "rc_id $op= $continueId)" |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | $this->addWhereFld( 'wl_namespace', $params['namespace'] ); |
||
| 156 | |||
| 157 | if ( !$params['allrev'] ) { |
||
| 158 | $this->addTables( 'page' ); |
||
| 159 | $this->addJoinConds( [ 'page' => [ 'LEFT JOIN', 'rc_cur_id=page_id' ] ] ); |
||
| 160 | $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG ); |
||
| 161 | } |
||
| 162 | |||
| 163 | if ( !is_null( $params['show'] ) ) { |
||
| 164 | $show = array_flip( $params['show'] ); |
||
| 165 | |||
| 166 | /* Check for conflicting parameters. */ |
||
| 167 | if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) ) |
||
| 168 | || ( isset( $show['bot'] ) && isset( $show['!bot'] ) ) |
||
| 169 | || ( isset( $show['anon'] ) && isset( $show['!anon'] ) ) |
||
| 170 | || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) |
||
| 171 | || ( isset( $show['unread'] ) && isset( $show['!unread'] ) ) |
||
| 172 | ) { |
||
| 173 | $this->dieUsageMsg( 'show' ); |
||
| 174 | } |
||
| 175 | |||
| 176 | // Check permissions. |
||
| 177 | View Code Duplication | if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) { |
|
| 178 | if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) { |
||
| 179 | $this->dieUsage( |
||
| 180 | 'You need the patrol right to request the patrolled flag', |
||
| 181 | 'permissiondenied' |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /* Add additional conditions to query depending upon parameters. */ |
||
| 187 | $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) ); |
||
| 188 | $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) ); |
||
| 189 | $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) ); |
||
| 190 | $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) ); |
||
| 191 | $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) ); |
||
| 192 | $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) ); |
||
| 193 | $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) ); |
||
| 194 | $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) ); |
||
| 195 | $this->addWhereIf( 'rc_timestamp >= wl_notificationtimestamp', isset( $show['unread'] ) ); |
||
| 196 | $this->addWhereIf( |
||
| 197 | 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp', |
||
| 198 | isset( $show['!unread'] ) |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | |||
| 202 | View Code Duplication | if ( !is_null( $params['type'] ) ) { |
|
| 203 | try { |
||
| 204 | $this->addWhereFld( 'rc_type', RecentChange::parseToRCType( $params['type'] ) ); |
||
| 205 | } catch ( Exception $e ) { |
||
| 206 | ApiBase::dieDebug( __METHOD__, $e->getMessage() ); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | View Code Duplication | if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) { |
|
| 211 | $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' ); |
||
| 212 | } |
||
| 213 | if ( !is_null( $params['user'] ) ) { |
||
| 214 | $this->addWhereFld( 'rc_user_text', $params['user'] ); |
||
| 215 | } |
||
| 216 | if ( !is_null( $params['excludeuser'] ) ) { |
||
| 217 | $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) ); |
||
| 218 | } |
||
| 219 | |||
| 220 | // This is an index optimization for mysql, as done in the Special:Watchlist page |
||
| 221 | $this->addWhereIf( |
||
| 222 | "rc_timestamp > ''", |
||
| 223 | !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' |
||
| 224 | ); |
||
| 225 | |||
| 226 | // Paranoia: avoid brute force searches (bug 17342) |
||
| 227 | View Code Duplication | if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) { |
|
| 228 | if ( !$user->isAllowed( 'deletedhistory' ) ) { |
||
| 229 | $bitmask = Revision::DELETED_USER; |
||
| 230 | } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
||
| 231 | $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED; |
||
| 232 | } else { |
||
| 233 | $bitmask = 0; |
||
| 234 | } |
||
| 235 | if ( $bitmask ) { |
||
| 236 | $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" ); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | // LogPage::DELETED_ACTION hides the affected page, too. So hide those |
||
| 241 | // entirely from the watchlist, or someone could guess the title. |
||
| 242 | if ( !$user->isAllowed( 'deletedhistory' ) ) { |
||
| 243 | $bitmask = LogPage::DELETED_ACTION; |
||
| 244 | } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
||
| 245 | $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED; |
||
| 246 | } else { |
||
| 247 | $bitmask = 0; |
||
| 248 | } |
||
| 249 | View Code Duplication | if ( $bitmask ) { |
|
| 250 | $this->addWhere( $this->getDB()->makeList( [ |
||
| 251 | 'rc_type != ' . RC_LOG, |
||
| 252 | $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask", |
||
| 253 | ], LIST_OR ) ); |
||
| 254 | } |
||
| 255 | |||
| 256 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
||
| 257 | |||
| 258 | $ids = []; |
||
| 259 | $count = 0; |
||
| 260 | $res = $this->select( __METHOD__ ); |
||
| 261 | |||
| 262 | foreach ( $res as $row ) { |
||
| 263 | View Code Duplication | if ( ++$count > $params['limit'] ) { |
|
| 264 | // We've reached the one extra which shows that there are |
||
| 265 | // additional pages to be had. Stop here... |
||
| 266 | $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" ); |
||
| 267 | break; |
||
| 268 | } |
||
| 269 | |||
| 270 | if ( is_null( $resultPageSet ) ) { |
||
| 271 | $vals = $this->extractRowInfo( $row ); |
||
| 272 | $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals ); |
||
| 273 | if ( !$fit ) { |
||
| 274 | $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" ); |
||
| 275 | break; |
||
| 276 | } |
||
| 277 | } else { |
||
| 278 | if ( $params['allrev'] ) { |
||
| 279 | $ids[] = intval( $row->rc_this_oldid ); |
||
| 280 | } else { |
||
| 281 | $ids[] = intval( $row->rc_cur_id ); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | View Code Duplication | if ( is_null( $resultPageSet ) ) { |
|
| 287 | $this->getResult()->addIndexedTagName( |
||
| 288 | [ 'query', $this->getModuleName() ], |
||
| 289 | 'item' |
||
| 290 | ); |
||
| 291 | } elseif ( $params['allrev'] ) { |
||
| 292 | $resultPageSet->populateFromRevisionIDs( $ids ); |
||
| 293 | } else { |
||
| 294 | $resultPageSet->populateFromPageIDs( $ids ); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 527 |
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.