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\Logger\LoggerFactory; |
7 | use MediaWiki\MediaWikiServices; |
8 | use MediaWiki\Title\Title; |
9 | use MediaWiki\User\User; |
10 | use RequestContext; |
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 | * Static flag for when we're moving a page; this is currently only read by the |
29 | * AbuseFilterShouldFilterAction hook handler to avoid filtering our page moves. |
30 | * @var bool |
31 | */ |
32 | public static $moveInProgress = false; |
33 | /** |
34 | * @var User |
35 | */ |
36 | private $user; |
37 | |
38 | /** |
39 | * @param Title $title |
40 | * @param array $params |
41 | */ |
42 | public function __construct( Title $title, $params ) { |
43 | parent::__construct( |
44 | 'LocalPageMoveJob', |
45 | $title, |
46 | $params |
47 | ); |
48 | } |
49 | |
50 | public function run() { |
51 | $this->user = User::newFromName( $this->params['renamer'] ); |
52 | $permissionManager = MediaWikiServices::getInstance() |
53 | ->getPermissionManager(); |
54 | |
55 | // Mark user page moves as bot on rename user process T97659 |
56 | if ( !$permissionManager->userHasRight( $this->user, 'bot' ) ) { |
57 | $guard = $permissionManager->addTemporaryUserRights( $this->user, 'bot' ); |
58 | |
59 | // Remove it at the end of the job process |
60 | $this->addTeardownCallback( static function () use ( &$guard ) { |
61 | ScopedCallback::consume( $guard ); |
62 | } ); |
63 | } |
64 | |
65 | if ( isset( $this->params['session'] ) ) { |
66 | $callback = RequestContext::importScopedSession( $this->params['session'] ); |
67 | $this->addTeardownCallback( static function () use ( &$callback ) { |
68 | ScopedCallback::consume( $callback ); |
69 | } ); |
70 | } |
71 | if ( isset( $this->params['pages'] ) ) { |
72 | // Old calling style for b/c |
73 | foreach ( $this->params['pages'] as $current => $target ) { |
74 | $this->movePage( |
75 | Title::newFromText( $current ), |
76 | Title::newFromText( $target ) |
77 | ); |
78 | } |
79 | } else { |
80 | $oldTitle = Title::makeTitle( $this->params['old'][0], $this->params['old'][1] ); |
81 | $newTitle = Title::makeTitle( $this->params['new'][0], $this->params['new'][1] ); |
82 | $this->movePage( $oldTitle, $newTitle ); |
83 | } |
84 | return true; |
85 | } |
86 | |
87 | /** |
88 | * @param Title $oldPage |
89 | * @param Title $newPage |
90 | */ |
91 | protected function movePage( Title $oldPage, Title $newPage ) { |
92 | $mp = MediaWikiServices::getInstance()->getMovePageFactory()->newMovePage( $oldPage, $newPage ); |
93 | $logger = LoggerFactory::getInstance( 'CentralAuth' ); |
94 | |
95 | $valid = $mp->isValidMove(); |
96 | if ( !$valid->isOK() ) { |
97 | $logger->info( "Invalid page move: {oldPage} -> {newPage} ({error})", [ |
98 | 'oldPage' => $oldPage->getPrefixedText(), |
99 | 'newPage' => $newPage->getPrefixedText(), |
100 | 'component' => 'GlobalRename', |
101 | 'error' => $valid->getWikiText( false, false, 'en' ), |
102 | ] ); |
103 | return; |
104 | } |
105 | |
106 | $msg = wfMessage( 'centralauth-rename-movelog' ) |
107 | ->params( $this->params['from'], $this->params['to'] ) |
108 | ->inContentLanguage() |
109 | ->text(); |
110 | |
111 | self::$moveInProgress = true; |
112 | try { |
113 | $status = $mp->move( $this->user, $msg, !$this->params['suppressredirects'] ); |
114 | } finally { |
115 | self::$moveInProgress = false; |
116 | } |
117 | if ( isset( $status ) && !$status->isOK() ) { |
118 | $logger->info( "Page move failed: {oldPage} -> {newPage} ({error})", [ |
119 | 'oldPage' => $oldPage->getPrefixedText(), |
120 | 'newPage' => $newPage->getPrefixedText(), |
121 | 'component' => 'GlobalRename', |
122 | 'error' => $status->getWikiText( false, false, 'en' ), |
123 | ] ); |
124 | } |
125 | } |
126 | } |