summaryrefslogtreecommitdiff
blob: 1197aa3106980dabb74b1b3d6d48576bc6724d6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\PageTranslation;

use CommentStore;
use ErrorPageError;
use Html;
use HTMLForm;
use MediaWiki\Permissions\PermissionManager;
use OutputPage;
use PermissionsError;
use ReadOnlyError;
use SplObjectStorage;
use ThrottledError;
use Title;
use TranslatablePage;
use UnlistedSpecialPage;
use Wikimedia\ObjectFactory;

/**
 * Replacement for Special:Movepage to allow renaming a translatable page and
 * all pages associated with it.
 *
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 * @ingroup SpecialPage PageTranslation
 */
class MoveTranslatablePageSpecialPage extends UnlistedSpecialPage {
	// Form parameters both as text and as titles
	/** @var string */
	private $oldText;
	/** @var string */
	private $reason;
	/** @var bool */
	private $moveTalkpages = true;
	/** @var bool */
	private $moveSubpages = true;
	// Dependencies
	/** @var ObjectFactory */
	private $objectFactory;
	/** @var TranslatablePageMover */
	private $pageMover;
	/** @var PermissionManager */
	private $permissionManager;
	private $movePageSpec;
	// Other
	/** @var Title */
	private $oldTitle;

	public function __construct(
		ObjectFactory $objectFactory,
		PermissionManager $permissionManager,
		TranslatablePageMover $pageMover,
		$movePageSpec
	) {
		parent::__construct( 'Movepage' );
		$this->objectFactory = $objectFactory;
		$this->permissionManager = $permissionManager;
		$this->pageMover = $pageMover;
		// SpecialMovepage started using service injection in
		// I6d4fe09891a126d803fee90bc3bb4959e8b29eb9
		// Needed for MW < 1.36
		if ( is_string( $movePageSpec ) ) {
			$this->movePageSpec = [ 'class' => $movePageSpec ];
		} else {
			$this->movePageSpec = $movePageSpec;
		}
	}

	public function doesWrites(): bool {
		return true;
	}

	protected function getGroupName(): string {
		return 'pagetools';
	}

	/** @inheritDoc */
	public function execute( $par ) {
		$request = $this->getRequest();
		$user = $this->getUser();
		$this->addHelpLink( 'Help:Extension:Translate/Move_translatable_page' );

		$this->oldText = $request->getText( 'wpOldTitle', $request->getText( 'target', $par ) );
		$newText = $request->getText( 'wpNewTitle' );

		$this->oldTitle = Title::newFromText( $this->oldText );
		$newTitle = Title::newFromText( $newText );
		// Normalize input
		if ( $this->oldTitle ) {
			$this->oldText = $this->oldTitle->getPrefixedText();
		}

		$this->reason = $request->getText( 'reason' );

		// This will throw exceptions if there is an error.
		$this->doBasicChecks();

		// Real stuff starts here
		$page = TranslatablePage::newFromTitle( $this->oldTitle );
		if ( $page->getMarkedTag() !== false ) {
				$this->getOutput()->setPageTitle( $this->msg( 'pt-movepage-title', $this->oldText ) );

			if ( !$user->isAllowed( 'pagetranslation' ) ) {
				throw new PermissionsError( 'pagetranslation' );
			}

			// Is there really no better way to do this?
			$subactionText = $request->getText( 'subaction' );
			switch ( $subactionText ) {
				case $this->msg( 'pt-movepage-action-check' )->text():
					$subaction = 'check';
					break;
				case $this->msg( 'pt-movepage-action-perform' )->text():
					$subaction = 'perform';
					break;
				case $this->msg( 'pt-movepage-action-other' )->text():
					$subaction = '';
					break;
				default:
					$subaction = '';
			}

			if ( $subaction === 'check' && $this->checkToken() && $request->wasPosted() ) {
				try {
					$pageCollection = $this->pageMover->getPageMoveCollection(
						$this->oldTitle,
						$newTitle,
						$user,
						$this->reason,
						$this->moveSubpages,
						$this->moveTalkpages
					);
				} catch ( ImpossiblePageMove $e ) {
					$this->showErrors( $e->getBlockers() );
					$this->showForm( [] );
					return;
				}

				$this->showConfirmation( $pageCollection );
			} elseif ( $subaction === 'perform' && $this->checkToken() && $request->wasPosted() ) {
				$this->moveSubpages = $request->getBool( 'subpages' );
				$this->moveTalkpages = $request->getBool( 'talkpages' );

				$this->pageMover->moveAsynchronously(
					$this->oldTitle,
					$newTitle,
					$this->moveSubpages,
					$this->getUser(),
					$this->msg( 'pt-movepage-logreason', $this->oldTitle )->inContentLanguage()->text(),
					$this->moveTalkpages
				);
				$this->getOutput()->addWikiMsg( 'pt-movepage-started' );
			} else {
				$this->showForm( [] );
			}
		} else {
			// Delegate... don't want to reimplement this
			$sp = $this->objectFactory->createObject( $this->movePageSpec );
			$sp->execute( $par );
		}
	}

	/**
	 * Do the basic checks whether moving is possible and whether
	 * the input looks anywhere near sane.
	 * @throws PermissionsError|ErrorPageError|ReadOnlyError|ThrottledError
	 */
	protected function doBasicChecks(): void {
		$this->checkReadOnly();

		if ( $this->oldTitle === null ) {
			throw new ErrorPageError( 'notargettitle', 'notargettext' );
		}

		if ( !$this->oldTitle->exists() ) {
			throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
		}

		if ( $this->getUser()->pingLimiter( 'move' ) ) {
			throw new ThrottledError;
		}

		// Check rights
		$permErrors = $this->permissionManager
			->getPermissionErrors( 'move', $this->getUser(), $this->oldTitle );
		if ( count( $permErrors ) ) {
			throw new PermissionsError( 'move', $permErrors );
		}
	}

	/**
	 * Checks token to protect against CSRF.
	 *
	 * FIXME: make this a form special page instead of manually checking stuff?
	 * @return bool
	 */
	protected function checkToken(): bool {
		return $this->getUser()->matchEditToken( $this->getRequest()->getVal( 'wpEditToken' ) );
	}

	/**
	 * Pretty-print the list of errors.
	 * @param SplObjectStorage $errors Array with message key and parameters
	 */
	protected function showErrors( SplObjectStorage $errors ): void {
		$out = $this->getOutput();

		$out->addHTML( Html::openElement( 'div', [ 'class' => 'errorbox' ] ) );
		$out->addWikiMsg(
			'pt-movepage-blockers',
			$this->getLanguage()->formatNum( count( $errors ) )
		);

		// If there are many errors, for performance reasons we must parse them all at once
		$s = '';
		$context = 'pt-movepage-error-placeholder';
		foreach ( $errors as $title ) {
			$titleText = $title->getPrefixedText();
			$s .= "'''$titleText'''\n\n";
			$s .= $errors[ $title ]->getWikiText( false, $context );
		}

		$out->addWikiTextAsInterface( $s );
		$out->addHTML( Html::closeElement( 'div' ) );
	}

	/**
	 * The query form.
	 * @param array $err Unused.
	 * @param bool $isPermError Unused.
	 */
	public function showForm( $err, $isPermError = false ): void {
		$this->getOutput()->addWikiMsg( 'pt-movepage-intro' );

		HTMLForm::factory( 'ooui', $this->getCommonFormFields(), $this->getContext() )
			->setMethod( 'post' )
			->setAction( $this->getPageTitle( $this->oldText )->getLocalURL() )
			->setSubmitName( 'subaction' )
			->setSubmitTextMsg( 'pt-movepage-action-check' )
			->setWrapperLegendMsg( 'pt-movepage-legend' )
			->prepareForm()
			->displayForm( false );
	}

	/**
	 * The second form, which still allows changing some things.
	 * Lists all the action which would take place.
	 * @param PageMoveCollection $pageCollection
	 */
	protected function showConfirmation( PageMoveCollection $pageCollection ): void {
		$out = $this->getOutput();

		$out->addWikiMsg( 'pt-movepage-intro' );

		$count = 0;
		$subpagesCount = 0;
		$talkpagesCount = 0;

		/** @var PageMoveOperation[][] */
		$pagesToMove = [
			'pt-movepage-list-pages' => [ $pageCollection->getTranslatablePage() ],
			'pt-movepage-list-translation' => $pageCollection->getTranslationPagesPair(),
			'pt-movepage-list-section' => $pageCollection->getUnitPagesPair()
		];

		$subpages = $pageCollection->getSubpagesPair();
		if ( $subpages ) {
			$pagesToMove[ 'pt-movepage-list-other'] = $subpages;
		}

		foreach ( $pagesToMove as $type => $pages ) {
			$this->addSectionHeader( $out, $type, $pages );

			if ( !$pages ) {
				$out->addWikiMsg( 'pt-movepage-list-no-pages' );
				continue;
			}

			$lines = [];

			foreach ( $pages as $pagePairs ) {
				$count++;

				if ( $type === 'pt-movepage-list-other' ) {
					$subpagesCount++;
				}

				$old = $pagePairs->getOldTitle();
				$new = $pagePairs->getNewTitle();
				$line = '* ' . $old->getPrefixedText() . ' → ' . $new->getPrefixedText();
				if ( $pagePairs->hasTalkpage() ) {
					$count++;
					$talkpagesCount++;
					$line .= ' ' . $this->msg( 'pt-movepage-talkpage-exists' )->text();
				}

				$lines[] = $line;
			}

			$out->addWikiTextAsInterface( implode( "\n", $lines ) );
		}

		$translatableSubpages = $pageCollection->getTranslatableSubpages();
		$sectionType = 'pt-movepage-list-translatable';
		$this->addSectionHeader( $out, $sectionType, $translatableSubpages );
		if ( $translatableSubpages ) {
			$lines = [];
			$out->wrapWikiMsg( "'''$1'''", $this->msg( 'pt-movepage-list-translatable-note' ) );
			foreach ( $translatableSubpages as $page ) {
				$lines[] = '* ' . $page->getPrefixedText();
			}
			$out->addWikiTextAsInterface( implode( "\n", $lines ) );
		}

		$out->addWikiTextAsInterface( "----\n" );
		$out->addWikiMsg(
			'pt-movepage-list-count',
			$this->getLanguage()->formatNum( $count ),
			$this->getLanguage()->formatNum( $subpagesCount ),
			$this->getLanguage()->formatNum( $talkpagesCount )
		);

		$formDescriptor = array_merge(
			$this->getCommonFormFields(),
			[
				'subpages' => [
					'type' => 'check',
					'name' => 'subpages',
					'id' => 'mw-subpages',
					'label-message' => 'pt-movepage-subpages',
					'default' => $this->moveSubpages,
				],
				'talkpages' => [
					'type' => 'check',
					'name' => 'talkpages',
					'id' => 'mw-talkpages',
					'label-message' => 'pt-movepage-talkpages',
					'default' => $this->moveTalkpages
				]
			]
		);

		$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );
		$htmlForm
			->addButton( [
				'name' => 'subaction',
				'value' => $this->msg( 'pt-movepage-action-other' )->text(),
			] )
			->setMethod( 'post' )
			->setAction( $this->getPageTitle( $this->oldText )->getLocalURL() )
			->setSubmitName( 'subaction' )
			->setSubmitTextMsg( 'pt-movepage-action-perform' )
			->setWrapperLegendMsg( 'pt-movepage-legend' )
			->prepareForm()
			->displayForm( false );
	}

	private function addSectionHeader( OutputPage $out, string $type, array $pages ): void {
		$pageCount = count( $pages );
		$out->wrapWikiMsg( '=== $1 ===', [ $type, $pageCount ] );

		if ( !$pageCount ) {
			$out->addWikiMsg( 'pt-movepage-list-no-pages' );
		}
	}

	private function getCommonFormFields(): array {
		return [
			'wpOldTitle' => [
				'type' => 'text',
				'name' => 'wpOldTitle',
				'label-message' => 'pt-movepage-current',
				'default' => $this->oldText,
				'readonly' => true,
			],
			'wpNewTitle' => [
				'type' => 'text',
				'name' => 'wpNewTitle',
				'label-message' => 'pt-movepage-new',
			],
			'reason' => [
				'type' => 'text',
				'name' => 'reason',
				'label-message' => 'pt-movepage-reason',
				'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
				'default' => $this->reason,
			],
			'subpages' => [
				'type' => 'hidden',
				'name' => 'subpages',
				'default' => $this->moveSubpages,
			],
			'talkpages' => [
				'type' => 'hidden',
				'name' => 'talkpages',
				'default' => $this->moveTalkpages
			]
		];
	}
}