| Conditions | 31 |
| Paths | 4186 |
| Total Lines | 167 |
| Code Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 248 | protected function dumpFrom( $cond = '' ) { |
||
| 249 | # For logging dumps... |
||
| 250 | if ( $this->history & self::LOGS ) { |
||
| 251 | $where = [ 'user_id = log_user' ]; |
||
| 252 | # Hide private logs |
||
| 253 | $hideLogs = LogEventsList::getExcludeClause( $this->db ); |
||
| 254 | if ( $hideLogs ) { |
||
| 255 | $where[] = $hideLogs; |
||
| 256 | } |
||
| 257 | # Add on any caller specified conditions |
||
| 258 | if ( $cond ) { |
||
| 259 | $where[] = $cond; |
||
| 260 | } |
||
| 261 | # Get logging table name for logging.* clause |
||
| 262 | $logging = $this->db->tableName( 'logging' ); |
||
| 263 | |||
| 264 | if ( $this->buffer == WikiExporter::STREAM ) { |
||
| 265 | $prev = $this->db->bufferResults( false ); |
||
| 266 | } |
||
| 267 | $result = null; // Assuring $result is not undefined, if exception occurs early |
||
| 268 | try { |
||
| 269 | $result = $this->db->select( [ 'logging', 'user' ], |
||
| 270 | [ "{$logging}.*", 'user_name' ], // grab the user name |
||
| 271 | $where, |
||
| 272 | __METHOD__, |
||
| 273 | [ 'ORDER BY' => 'log_id', 'USE INDEX' => [ 'logging' => 'PRIMARY' ] ] |
||
| 274 | ); |
||
| 275 | $this->outputLogStream( $result ); |
||
| 276 | if ( $this->buffer == WikiExporter::STREAM ) { |
||
| 277 | $this->db->bufferResults( $prev ); |
||
| 278 | } |
||
| 279 | } catch ( Exception $e ) { |
||
| 280 | // Throwing the exception does not reliably free the resultset, and |
||
| 281 | // would also leave the connection in unbuffered mode. |
||
| 282 | |||
| 283 | // Freeing result |
||
| 284 | try { |
||
| 285 | if ( $result ) { |
||
| 286 | $result->free(); |
||
| 287 | } |
||
| 288 | } catch ( Exception $e2 ) { |
||
| 289 | // Already in panic mode -> ignoring $e2 as $e has |
||
| 290 | // higher priority |
||
| 291 | } |
||
| 292 | |||
| 293 | // Putting database back in previous buffer mode |
||
| 294 | try { |
||
| 295 | if ( $this->buffer == WikiExporter::STREAM ) { |
||
| 296 | $this->db->bufferResults( $prev ); |
||
| 297 | } |
||
| 298 | } catch ( Exception $e2 ) { |
||
| 299 | // Already in panic mode -> ignoring $e2 as $e has |
||
| 300 | // higher priority |
||
| 301 | } |
||
| 302 | |||
| 303 | // Inform caller about problem |
||
| 304 | throw $e; |
||
| 305 | } |
||
| 306 | # For page dumps... |
||
| 307 | } else { |
||
| 308 | $tables = [ 'page', 'revision' ]; |
||
| 309 | $opts = [ 'ORDER BY' => 'page_id ASC' ]; |
||
| 310 | $opts['USE INDEX'] = []; |
||
| 311 | $join = []; |
||
| 312 | if ( is_array( $this->history ) ) { |
||
| 313 | # Time offset/limit for all pages/history... |
||
| 314 | $revJoin = 'page_id=rev_page'; |
||
| 315 | # Set time order |
||
| 316 | if ( $this->history['dir'] == 'asc' ) { |
||
| 317 | $op = '>'; |
||
| 318 | $opts['ORDER BY'] = 'rev_timestamp ASC'; |
||
| 319 | } else { |
||
| 320 | $op = '<'; |
||
| 321 | $opts['ORDER BY'] = 'rev_timestamp DESC'; |
||
| 322 | } |
||
| 323 | # Set offset |
||
| 324 | if ( !empty( $this->history['offset'] ) ) { |
||
| 325 | $revJoin .= " AND rev_timestamp $op " . |
||
| 326 | $this->db->addQuotes( $this->db->timestamp( $this->history['offset'] ) ); |
||
| 327 | } |
||
| 328 | $join['revision'] = [ 'INNER JOIN', $revJoin ]; |
||
| 329 | # Set query limit |
||
| 330 | if ( !empty( $this->history['limit'] ) ) { |
||
| 331 | $opts['LIMIT'] = intval( $this->history['limit'] ); |
||
| 332 | } |
||
| 333 | } elseif ( $this->history & WikiExporter::FULL ) { |
||
| 334 | # Full history dumps... |
||
| 335 | $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page' ]; |
||
| 336 | } elseif ( $this->history & WikiExporter::CURRENT ) { |
||
| 337 | # Latest revision dumps... |
||
| 338 | if ( $this->list_authors && $cond != '' ) { // List authors, if so desired |
||
| 339 | $this->do_list_authors( $cond ); |
||
| 340 | } |
||
| 341 | $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' ]; |
||
| 342 | } elseif ( $this->history & WikiExporter::STABLE ) { |
||
| 343 | # "Stable" revision dumps... |
||
| 344 | # Default JOIN, to be overridden... |
||
| 345 | $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page AND page_latest=rev_id' ]; |
||
| 346 | # One, and only one hook should set this, and return false |
||
| 347 | if ( Hooks::run( 'WikiExporter::dumpStableQuery', [ &$tables, &$opts, &$join ] ) ) { |
||
| 348 | throw new MWException( __METHOD__ . " given invalid history dump type." ); |
||
| 349 | } |
||
| 350 | } elseif ( $this->history & WikiExporter::RANGE ) { |
||
| 351 | # Dump of revisions within a specified range |
||
| 352 | $join['revision'] = [ 'INNER JOIN', 'page_id=rev_page' ]; |
||
| 353 | $opts['ORDER BY'] = [ 'rev_page ASC', 'rev_id ASC' ]; |
||
| 354 | } else { |
||
| 355 | # Unknown history specification parameter? |
||
| 356 | throw new MWException( __METHOD__ . " given invalid history dump type." ); |
||
| 357 | } |
||
| 358 | # Query optimization hacks |
||
| 359 | if ( $cond == '' ) { |
||
| 360 | $opts[] = 'STRAIGHT_JOIN'; |
||
| 361 | $opts['USE INDEX']['page'] = 'PRIMARY'; |
||
| 362 | } |
||
| 363 | # Build text join options |
||
| 364 | if ( $this->text != WikiExporter::STUB ) { // 1-pass |
||
| 365 | $tables[] = 'text'; |
||
| 366 | $join['text'] = [ 'INNER JOIN', 'rev_text_id=old_id' ]; |
||
| 367 | } |
||
| 368 | |||
| 369 | if ( $this->buffer == WikiExporter::STREAM ) { |
||
| 370 | $prev = $this->db->bufferResults( false ); |
||
| 371 | } |
||
| 372 | |||
| 373 | $result = null; // Assuring $result is not undefined, if exception occurs early |
||
| 374 | try { |
||
| 375 | Hooks::run( 'ModifyExportQuery', |
||
| 376 | [ $this->db, &$tables, &$cond, &$opts, &$join ] ); |
||
| 377 | |||
| 378 | # Do the query! |
||
| 379 | $result = $this->db->select( $tables, '*', $cond, __METHOD__, $opts, $join ); |
||
| 380 | # Output dump results |
||
| 381 | $this->outputPageStream( $result ); |
||
| 382 | |||
| 383 | if ( $this->buffer == WikiExporter::STREAM ) { |
||
| 384 | $this->db->bufferResults( $prev ); |
||
| 385 | } |
||
| 386 | } catch ( Exception $e ) { |
||
| 387 | // Throwing the exception does not reliably free the resultset, and |
||
| 388 | // would also leave the connection in unbuffered mode. |
||
| 389 | |||
| 390 | // Freeing result |
||
| 391 | try { |
||
| 392 | if ( $result ) { |
||
| 393 | $result->free(); |
||
| 394 | } |
||
| 395 | } catch ( Exception $e2 ) { |
||
| 396 | // Already in panic mode -> ignoring $e2 as $e has |
||
| 397 | // higher priority |
||
| 398 | } |
||
| 399 | |||
| 400 | // Putting database back in previous buffer mode |
||
| 401 | try { |
||
| 402 | if ( $this->buffer == WikiExporter::STREAM ) { |
||
| 403 | $this->db->bufferResults( $prev ); |
||
| 404 | } |
||
| 405 | } catch ( Exception $e2 ) { |
||
| 406 | // Already in panic mode -> ignoring $e2 as $e has |
||
| 407 | // higher priority |
||
| 408 | } |
||
| 409 | |||
| 410 | // Inform caller about problem |
||
| 411 | throw $e; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | } |
||
| 415 | |||
| 470 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: