summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/tag/TranslatablePageMoveJob.php
blob: 27f4b48d268b1552b52df4aa020ff2e1eb786b36 (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
<?php
/**
 * Contains class with job for moving translation pages.
 *
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Contains class with job for moving translation pages. Used together with
 * SpecialPageTranslationMovePage class.
 *
 * @ingroup PageTranslation JobQueue
 */
class TranslatablePageMoveJob extends Job {

	/**
	 * @param Title $source
	 * @param Title $target
	 * @param array $moves should include base-source and base-target
	 * @param string $summary
	 * @param User $performer
	 * @return TranslatablePageMoveJob
	 */
	public static function newJob(
		Title $source, Title $target, array $moves, $summary, User $performer
	) {
		$params = [
			'source' => $source->getPrefixedText(),
			'target' => $target->getPrefixedText(),
			'moves' => $moves,
			'summary' => $summary,
			'performer' => $performer->getName(),
		];

		$self = new self( $target, $params );
		$self->lock( array_keys( $moves ) );
		$self->lock( array_values( $moves ) );

		return $self;
	}

	public function __construct( $title, $params = [] ) {
		parent::__construct( __CLASS__, $title, $params );
	}

	public function run() {
		$sourceTitle = Title::newFromText( $this->params['source'] );
		$targetTitle = Title::newFromText( $this->params['target'] );
		$sourcePage = TranslatablePage::newFromTitle( $sourceTitle );
		$targetPage = TranslatablePage::newFromTitle( $targetTitle );

		$this->doMoves();

		$this->moveMetadata(
			$sourcePage->getMessageGroupId(),
			$targetPage->getMessageGroupId()
		);

		$entry = new ManualLogEntry( 'pagetranslation', 'moveok' );
		$entry->setPerformer( User::newFromName( $this->params['performer'] ) );
		$entry->setParameters( [ 'target' => $this->params['target'] ] );
		$entry->setTarget( $sourceTitle );
		$logid = $entry->insert();
		$entry->publish( $logid );

		// Re-render the pages to get everything in sync
		MessageGroups::singleton()->recache();
		// Update message index now so that, when after this job the MoveTranslationUnits hook
		// runs in deferred updates, it will not run MessageIndexRebuildJob (T175834).
		MessageIndex::singleton()->rebuild();

		$job = TranslationsUpdateJob::newFromPage( $targetPage );
		JobQueueGroup::singleton()->push( $job );

		return true;
	}

	protected function doMoves() {
		$fuzzybot = FuzzyBot::getUser();
		$performer = User::newFromName( $this->params['performer'] );

		PageTranslationHooks::$allowTargetEdit = true;

		foreach ( $this->params['moves'] as $source => $target ) {
			$sourceTitle = Title::newFromText( $source );
			$targetTitle = Title::newFromText( $target );

			if ( $source === $this->params['source'] ) {
				$user = $performer;
			} else {
				$user = $fuzzybot;
			}

			$mover = new MovePage( $sourceTitle, $targetTitle );
			$status = $mover->move( $user, $this->params['summary'], false );
			if ( !$status->isOK() ) {
				$entry = new ManualLogEntry( 'pagetranslation', 'movenok' );
				$entry->setPerformer( $performer );
				$entry->setTarget( $sourceTitle );
				$entry->setParameters( [
					'target' => $target,
					'error' => $status->getErrorsArray(),
				] );
				$logid = $entry->insert();
				$entry->publish( $logid );
			}

			$this->unlock( [ $source, $target ] );
		}

		PageTranslationHooks::$allowTargetEdit = false;
	}

	protected function moveMetadata( $oldGroupId, $newGroupId ) {
		$types = [ 'prioritylangs', 'priorityforce', 'priorityreason' ];

		TranslateMetadata::preloadGroups( [ $oldGroupId, $newGroupId ] );
		foreach ( $types as $type ) {
			$value = TranslateMetadata::get( $oldGroupId, $type );
			if ( $value !== false ) {
				TranslateMetadata::set( $oldGroupId, $type, false );
				TranslateMetadata::set( $newGroupId, $type, $value );
			}
		}

		// Make the changes in aggregate groups metadata, if present in any of them.
		$aggregateGroups = MessageGroups::getGroupsByType( AggregateMessageGroup::class );
		TranslateMetadata::preloadGroups( array_keys( $aggregateGroups ) );

		foreach ( $aggregateGroups as $id => $group ) {
			$subgroups = TranslateMetadata::get( $id, 'subgroups' );
			if ( $subgroups === false ) {
				continue;
			}

			$subgroups = explode( ',', $subgroups );
			$subgroups = array_flip( $subgroups );
			if ( isset( $subgroups[$oldGroupId] ) ) {
				$subgroups[$newGroupId] = $subgroups[$oldGroupId];
				unset( $subgroups[$oldGroupId] );
				$subgroups = array_flip( $subgroups );
				TranslateMetadata::set(
					$group->getId(),
					'subgroups',
					implode( ',', $subgroups )
				);
			}
		}
	}

	private function lock( array $titles ) {
		$cache = wfGetCache( CACHE_ANYTHING );
		$data = [];
		foreach ( $titles as $title ) {
			$data[wfMemcKey( 'pt-lock', sha1( $title ) )] = 'locked';
		}
		$cache->setMulti( $data );
	}

	private function unlock( array $titles ) {
		$cache = wfGetCache( CACHE_ANYTHING );
		foreach ( $titles as $title ) {
			$cache->delete( wfMemcKey( 'pt-lock', sha1( $title ) ) );
		}
	}
}