| Conditions | 47 |
| Paths | > 20000 |
| Total Lines | 271 |
| Code Lines | 158 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 508 | function doSubmit() { |
||
| 509 | $user = $this->getUser(); |
||
| 510 | |||
| 511 | if ( $user->pingLimiter( 'move' ) ) { |
||
| 512 | throw new ThrottledError; |
||
| 513 | } |
||
| 514 | |||
| 515 | $ot = $this->oldTitle; |
||
| 516 | $nt = $this->newTitle; |
||
| 517 | |||
| 518 | # don't allow moving to pages with # in |
||
| 519 | if ( !$nt || $nt->hasFragment() ) { |
||
| 520 | $this->showForm( [ [ 'badtitletext' ] ] ); |
||
| 521 | |||
| 522 | return; |
||
| 523 | } |
||
| 524 | |||
| 525 | # Show a warning if the target file exists on a shared repo |
||
| 526 | if ( $nt->getNamespace() == NS_FILE |
||
| 527 | && !( $this->moveOverShared && $user->isAllowed( 'reupload-shared' ) ) |
||
| 528 | && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt ) |
||
| 529 | && wfFindFile( $nt ) |
||
| 530 | ) { |
||
| 531 | $this->showForm( [ [ 'file-exists-sharedrepo' ] ] ); |
||
| 532 | |||
| 533 | return; |
||
| 534 | } |
||
| 535 | |||
| 536 | # Delete to make way if requested |
||
| 537 | if ( $this->deleteAndMove ) { |
||
| 538 | $permErrors = $nt->getUserPermissionsErrors( 'delete', $user ); |
||
| 539 | if ( count( $permErrors ) ) { |
||
| 540 | # Only show the first error |
||
| 541 | $this->showForm( $permErrors ); |
||
| 542 | |||
| 543 | return; |
||
| 544 | } |
||
| 545 | |||
| 546 | $reason = $this->msg( 'delete_and_move_reason', $ot )->inContentLanguage()->text(); |
||
| 547 | |||
| 548 | // Delete an associated image if there is |
||
| 549 | if ( $nt->getNamespace() == NS_FILE ) { |
||
| 550 | $file = wfLocalFile( $nt ); |
||
| 551 | $file->load( File::READ_LATEST ); |
||
| 552 | if ( $file->exists() ) { |
||
| 553 | $file->delete( $reason, false, $user ); |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | $error = ''; // passed by ref |
||
| 558 | $page = WikiPage::factory( $nt ); |
||
| 559 | $deleteStatus = $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user ); |
||
| 560 | if ( !$deleteStatus->isGood() ) { |
||
| 561 | $this->showForm( $deleteStatus->getErrorsArray() ); |
||
| 562 | |||
| 563 | return; |
||
| 564 | } |
||
| 565 | } |
||
| 566 | |||
| 567 | $handler = ContentHandler::getForTitle( $ot ); |
||
| 568 | |||
| 569 | if ( !$handler->supportsRedirects() ) { |
||
| 570 | $createRedirect = false; |
||
| 571 | } elseif ( $user->isAllowed( 'suppressredirect' ) ) { |
||
| 572 | $createRedirect = $this->leaveRedirect; |
||
| 573 | } else { |
||
| 574 | $createRedirect = true; |
||
| 575 | } |
||
| 576 | |||
| 577 | # Do the actual move. |
||
| 578 | $mp = new MovePage( $ot, $nt ); |
||
| 579 | $valid = $mp->isValidMove(); |
||
| 580 | if ( !$valid->isOK() ) { |
||
| 581 | $this->showForm( $valid->getErrorsArray() ); |
||
| 582 | return; |
||
| 583 | } |
||
| 584 | |||
| 585 | $permStatus = $mp->checkPermissions( $user, $this->reason ); |
||
| 586 | if ( !$permStatus->isOK() ) { |
||
| 587 | $this->showForm( $permStatus->getErrorsArray() ); |
||
| 588 | return; |
||
| 589 | } |
||
| 590 | |||
| 591 | $status = $mp->move( $user, $this->reason, $createRedirect ); |
||
| 592 | if ( !$status->isOK() ) { |
||
| 593 | $this->showForm( $status->getErrorsArray() ); |
||
| 594 | return; |
||
| 595 | } |
||
| 596 | |||
| 597 | if ( $this->getConfig()->get( 'FixDoubleRedirects' ) && $this->fixRedirects ) { |
||
| 598 | DoubleRedirectJob::fixRedirects( 'move', $ot, $nt ); |
||
| 599 | } |
||
| 600 | |||
| 601 | $out = $this->getOutput(); |
||
| 602 | $out->setPageTitle( $this->msg( 'pagemovedsub' ) ); |
||
| 603 | |||
| 604 | $linkRenderer = $this->getLinkRenderer(); |
||
| 605 | $oldLink = $linkRenderer->makeLink( |
||
| 606 | $ot, |
||
| 607 | null, |
||
| 608 | [ 'id' => 'movepage-oldlink' ], |
||
| 609 | [ 'redirect' => 'no' ] |
||
| 610 | ); |
||
| 611 | $newLink = $linkRenderer->makeKnownLink( |
||
| 612 | $nt, |
||
| 613 | null, |
||
| 614 | [ 'id' => 'movepage-newlink' ] |
||
| 615 | ); |
||
| 616 | $oldText = $ot->getPrefixedText(); |
||
| 617 | $newText = $nt->getPrefixedText(); |
||
| 618 | |||
| 619 | if ( $ot->exists() ) { |
||
| 620 | // NOTE: we assume that if the old title exists, it's because it was re-created as |
||
| 621 | // a redirect to the new title. This is not safe, but what we did before was |
||
| 622 | // even worse: we just determined whether a redirect should have been created, |
||
| 623 | // and reported that it was created if it should have, without any checks. |
||
| 624 | // Also note that isRedirect() is unreliable because of bug 37209. |
||
| 625 | $msgName = 'movepage-moved-redirect'; |
||
| 626 | } else { |
||
| 627 | $msgName = 'movepage-moved-noredirect'; |
||
| 628 | } |
||
| 629 | |||
| 630 | $out->addHTML( $this->msg( 'movepage-moved' )->rawParams( $oldLink, |
||
| 631 | $newLink )->params( $oldText, $newText )->parseAsBlock() ); |
||
| 632 | $out->addWikiMsg( $msgName ); |
||
| 633 | |||
| 634 | Hooks::run( 'SpecialMovepageAfterMove', [ &$this, &$ot, &$nt ] ); |
||
| 635 | |||
| 636 | # Now we move extra pages we've been asked to move: subpages and talk |
||
| 637 | # pages. First, if the old page or the new page is a talk page, we |
||
| 638 | # can't move any talk pages: cancel that. |
||
| 639 | if ( $ot->isTalkPage() || $nt->isTalkPage() ) { |
||
| 640 | $this->moveTalk = false; |
||
| 641 | } |
||
| 642 | |||
| 643 | if ( count( $ot->getUserPermissionsErrors( 'move-subpages', $user ) ) ) { |
||
| 644 | $this->moveSubpages = false; |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Next make a list of id's. This might be marginally less efficient |
||
| 649 | * than a more direct method, but this is not a highly performance-cri- |
||
| 650 | * tical code path and readable code is more important here. |
||
| 651 | * |
||
| 652 | * If the target namespace doesn't allow subpages, moving with subpages |
||
| 653 | * would mean that you couldn't move them back in one operation, which |
||
| 654 | * is bad. |
||
| 655 | * @todo FIXME: A specific error message should be given in this case. |
||
| 656 | */ |
||
| 657 | |||
| 658 | // @todo FIXME: Use Title::moveSubpages() here |
||
| 659 | $dbr = wfGetDB( DB_MASTER ); |
||
| 660 | if ( $this->moveSubpages && ( |
||
| 661 | MWNamespace::hasSubpages( $nt->getNamespace() ) || ( |
||
| 662 | $this->moveTalk |
||
| 663 | && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) |
||
| 664 | ) |
||
| 665 | ) ) { |
||
| 666 | $conds = [ |
||
| 667 | 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() ) |
||
| 668 | . ' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() ) |
||
| 669 | ]; |
||
| 670 | $conds['page_namespace'] = []; |
||
| 671 | if ( MWNamespace::hasSubpages( $nt->getNamespace() ) ) { |
||
| 672 | $conds['page_namespace'][] = $ot->getNamespace(); |
||
| 673 | } |
||
| 674 | if ( $this->moveTalk && |
||
| 675 | MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) |
||
| 676 | ) { |
||
| 677 | $conds['page_namespace'][] = $ot->getTalkPage()->getNamespace(); |
||
| 678 | } |
||
| 679 | } elseif ( $this->moveTalk ) { |
||
| 680 | $conds = [ |
||
| 681 | 'page_namespace' => $ot->getTalkPage()->getNamespace(), |
||
| 682 | 'page_title' => $ot->getDBkey() |
||
| 683 | ]; |
||
| 684 | } else { |
||
| 685 | # Skip the query |
||
| 686 | $conds = null; |
||
| 687 | } |
||
| 688 | |||
| 689 | $extraPages = []; |
||
| 690 | if ( !is_null( $conds ) ) { |
||
| 691 | $extraPages = TitleArray::newFromResult( |
||
| 692 | $dbr->select( 'page', |
||
| 693 | [ 'page_id', 'page_namespace', 'page_title' ], |
||
| 694 | $conds, |
||
| 695 | __METHOD__ |
||
| 696 | ) |
||
| 697 | ); |
||
| 698 | } |
||
| 699 | |||
| 700 | $extraOutput = []; |
||
| 701 | $count = 1; |
||
| 702 | foreach ( $extraPages as $oldSubpage ) { |
||
| 703 | if ( $ot->equals( $oldSubpage ) || $nt->equals( $oldSubpage ) ) { |
||
| 704 | # Already did this one. |
||
| 705 | continue; |
||
| 706 | } |
||
| 707 | |||
| 708 | $newPageName = preg_replace( |
||
| 709 | '#^' . preg_quote( $ot->getDBkey(), '#' ) . '#', |
||
| 710 | StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234 |
||
| 711 | $oldSubpage->getDBkey() |
||
| 712 | ); |
||
| 713 | |||
| 714 | if ( $oldSubpage->isSubpage() && ( $ot->isTalkPage() xor $nt->isTalkPage() ) ) { |
||
| 715 | // Moving a subpage from a subject namespace to a talk namespace or vice-versa |
||
| 716 | $newNs = $nt->getNamespace(); |
||
| 717 | } elseif ( $oldSubpage->isTalkPage() ) { |
||
| 718 | $newNs = $nt->getTalkPage()->getNamespace(); |
||
| 719 | } else { |
||
| 720 | $newNs = $nt->getSubjectPage()->getNamespace(); |
||
| 721 | } |
||
| 722 | |||
| 723 | # Bug 14385: we need makeTitleSafe because the new page names may |
||
| 724 | # be longer than 255 characters. |
||
| 725 | $newSubpage = Title::makeTitleSafe( $newNs, $newPageName ); |
||
| 726 | if ( !$newSubpage ) { |
||
| 727 | $oldLink = $linkRenderer->makeKnownLink( $oldSubpage ); |
||
| 728 | $extraOutput[] = $this->msg( 'movepage-page-unmoved' )->rawParams( $oldLink ) |
||
| 729 | ->params( Title::makeName( $newNs, $newPageName ) )->escaped(); |
||
| 730 | continue; |
||
| 731 | } |
||
| 732 | |||
| 733 | # This was copy-pasted from Renameuser, bleh. |
||
| 734 | if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) { |
||
| 735 | $link = $linkRenderer->makeKnownLink( $newSubpage ); |
||
| 736 | $extraOutput[] = $this->msg( 'movepage-page-exists' )->rawParams( $link )->escaped(); |
||
| 737 | } else { |
||
| 738 | $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect ); |
||
| 739 | |||
| 740 | if ( $success === true ) { |
||
| 741 | if ( $this->fixRedirects ) { |
||
| 742 | DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage ); |
||
| 743 | } |
||
| 744 | $oldLink = $linkRenderer->makeLink( |
||
| 745 | $oldSubpage, |
||
| 746 | null, |
||
| 747 | [], |
||
| 748 | [ 'redirect' => 'no' ] |
||
| 749 | ); |
||
| 750 | |||
| 751 | $newLink = $linkRenderer->makeKnownLink( $newSubpage ); |
||
| 752 | $extraOutput[] = $this->msg( 'movepage-page-moved' ) |
||
| 753 | ->rawParams( $oldLink, $newLink )->escaped(); |
||
| 754 | ++$count; |
||
| 755 | |||
| 756 | $maximumMovedPages = $this->getConfig()->get( 'MaximumMovedPages' ); |
||
| 757 | if ( $count >= $maximumMovedPages ) { |
||
| 758 | $extraOutput[] = $this->msg( 'movepage-max-pages' ) |
||
| 759 | ->numParams( $maximumMovedPages )->escaped(); |
||
| 760 | break; |
||
| 761 | } |
||
| 762 | } else { |
||
| 763 | $oldLink = $linkRenderer->makeKnownLink( $oldSubpage ); |
||
| 764 | $newLink = $linkRenderer->makeLink( $newSubpage ); |
||
| 765 | $extraOutput[] = $this->msg( 'movepage-page-unmoved' ) |
||
| 766 | ->rawParams( $oldLink, $newLink )->escaped(); |
||
| 767 | } |
||
| 768 | } |
||
| 769 | } |
||
| 770 | |||
| 771 | if ( $extraOutput !== [] ) { |
||
| 772 | $out->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" ); |
||
| 773 | } |
||
| 774 | |||
| 775 | # Deal with watches (we don't watch subpages) |
||
| 776 | WatchAction::doWatchOrUnwatch( $this->watch, $ot, $user ); |
||
| 777 | WatchAction::doWatchOrUnwatch( $this->watch, $nt, $user ); |
||
| 778 | } |
||
| 779 | |||
| 832 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.