Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 53 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
LocalPageMoveJob | |
0.00% |
0 / 53 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |||
movePage | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\CentralAuth\GlobalRename\LocalRenameJob; |
4 | |
5 | use Job; |
6 | use MediaWiki\Context\RequestContext; |
7 | use MediaWiki\Logger\LoggerFactory; |
8 | use MediaWiki\MediaWikiServices; |
9 | use MediaWiki\Title\Title; |
10 | use MediaWiki\User\User; |
11 | use Wikimedia\ScopedCallback; |
12 | |
13 | /** |
14 | * Job class to move a page |
15 | * |
16 | * Parameters: |
17 | * 'session' - Array of session data from RequestContext::exportSession() |
18 | * 'renamer' - Username of the user who should be attributed for the page move |
19 | * 'pages' (deprecated) - Array of old page title => new page title |
20 | * 'old' - array( namespace id, db key ) of old title |
21 | * 'new' - array( namespace id, db key ) of new title |
22 | * 'from' - Old username |
23 | * 'to' - New username |
24 | * 'suppressredirects' - Whether redirects should be suppressed |
25 | */ |
26 | class LocalPageMoveJob extends Job { |
27 | |
28 | /** |
29 | * Static flag for when we're moving a page; this is currently only read by the |
30 | * AbuseFilterShouldFilterAction hook handler to avoid filtering our page moves. |
31 | * @var bool |
32 | */ |
33 | public static $moveInProgress = false; |
34 | /** |
35 | * @var User |
36 | */ |
37 | private $user; |
38 | |
39 | /** |
40 | * @param Title $title |
41 | * @param array $params |
42 | */ |
43 | public function __construct( Title $title, $params ) { |
44 | parent::__construct( |
45 | 'LocalPageMoveJob', |
46 | $title, |
47 | $params |
48 | ); |
49 | } |
50 | |
51 | public function run() { |
52 | $this->user = User::newFromName( $this->params['renamer'] ); |
53 | $permissionManager = MediaWikiServices::getInstance() |
54 | ->getPermissionManager(); |
55 | |
56 | // Mark user page moves as bot on rename user process T97659 |
57 | if ( !$permissionManager->userHasRight( $this->user, 'bot' ) ) { |
58 | $guard = $permissionManager->addTemporaryUserRights( $this->user, 'bot' ); |
59 | |
60 | // Remove it at the end of the job process |
61 | $this->addTeardownCallback( static function () use ( &$guard ) { |
62 | ScopedCallback::consume( $guard ); |
63 | } ); |
64 | } |
65 | |
66 | if ( isset( $this->params['session'] ) ) { |
67 | $callback = RequestContext::importScopedSession( $this->params['session'] ); |
68 | $this->addTeardownCallback( static function () use ( &$callback ) { |
69 | ScopedCallback::consume( $callback ); |
70 | } ); |
71 | } |
72 | if ( isset( $this->params['pages'] ) ) { |
73 | // Old calling style for b/c |
74 | foreach ( $this->params['pages'] as $current => $target ) { |
75 | $this->movePage( |
76 | Title::newFromText( $current ), |
77 | Title::newFromText( $target ) |
78 | ); |
79 | } |
80 | } else { |
81 | $oldTitle = Title::makeTitle( $this->params['old'][0], $this->params['old'][1] ); |
82 | $newTitle = Title::makeTitle( $this->params['new'][0], $this->params['new'][1] ); |
83 | $this->movePage( $oldTitle, $newTitle ); |
84 | } |
85 | return true; |
86 | } |
87 | |
88 | /** |
89 | * @param Title $oldPage |
90 | * @param Title $newPage |
91 | */ |
92 | protected function movePage( Title $oldPage, Title $newPage ) { |
93 | $mp = MediaWikiServices::getInstance()->getMovePageFactory()->newMovePage( $oldPage, $newPage ); |
94 | $logger = LoggerFactory::getInstance( 'CentralAuth' ); |
95 | |
96 | $valid = $mp->isValidMove(); |
97 | if ( !$valid->isOK() ) { |
98 | $logger->info( "Invalid page move: {oldPage} -> {newPage} ({error})", [ |
99 | 'oldPage' => $oldPage->getPrefixedText(), |
100 | 'newPage' => $newPage->getPrefixedText(), |
101 | 'component' => 'GlobalRename', |
102 | 'error' => $valid->getWikiText( false, false, 'en' ), |
103 | ] ); |
104 | return; |
105 | } |
106 | |
107 | $msg = wfMessage( 'centralauth-rename-movelog' ) |
108 | ->params( $this->params['from'], $this->params['to'] ) |
109 | ->inContentLanguage() |
110 | ->text(); |
111 | |
112 | self::$moveInProgress = true; |
113 | try { |
114 | $status = $mp->move( $this->user, $msg, !$this->params['suppressredirects'] ); |
115 | } finally { |
116 | self::$moveInProgress = false; |
117 | } |
118 | if ( isset( $status ) && !$status->isOK() ) { |
119 | $logger->info( "Page move failed: {oldPage} -> {newPage} ({error})", [ |
120 | 'oldPage' => $oldPage->getPrefixedText(), |
121 | 'newPage' => $newPage->getPrefixedText(), |
122 | 'component' => 'GlobalRename', |
123 | 'error' => $status->getWikiText( false, false, 'en' ), |
124 | ] ); |
125 | } |
126 | } |
127 | } |