summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/utils/MessageUpdateJob.php
blob: fe6b1cdd24a0a372869571995add95b2fd7e9233 (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
<?php
/**
 * Job for updating translation pages.
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2008-2013, Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Job for updating translation pages when translation or message definition changes.
 *
 * @ingroup JobQueue
 */
class MessageUpdateJob extends Job {
	public static function newJob( Title $target, $content, $fuzzy = false ) {
		$params = [
			'content' => $content,
			'fuzzy' => $fuzzy,
		];
		$job = new self( $target, $params );

		return $job;
	}

	/**
	 * @param Title $title
	 * @param array $params
	 */
	public function __construct( $title, $params = [] ) {
		parent::__construct( __CLASS__, $title, $params );
	}

	public function run() {
		global $wgTranslateDocumentationLanguageCode;

		$title = $this->title;
		$params = $this->params;
		$user = FuzzyBot::getUser();
		$flags = EDIT_FORCE_BOT;

		$wikiPage = WikiPage::factory( $title );
		$summary = wfMessage( 'translate-manage-import-summary' )
			->inContentLanguage()->plain();
		$content = ContentHandler::makeContent( $params['content'], $title );
		$wikiPage->doEditContent( $content, $summary, $flags, false, $user );

		// NOTE: message documentation is excluded from fuzzying!
		if ( $params['fuzzy'] ) {
			$handle = new MessageHandle( $title );
			$key = $handle->getKey();

			$languages = TranslateUtils::getLanguageNames( 'en' );
			unset( $languages[$wgTranslateDocumentationLanguageCode] );
			$languages = array_keys( $languages );

			$dbw = wfGetDB( DB_MASTER );
			$fields = [ 'page_id', 'page_latest' ];
			$conds = [ 'page_namespace' => $title->getNamespace() ];

			$pages = [];
			foreach ( $languages as $code ) {
				$otherTitle = Title::makeTitleSafe( $title->getNamespace(), "$key/$code" );
				$pages[$otherTitle->getDBkey()] = true;
			}
			unset( $pages[$title->getDBkey()] );
			if ( $pages === [] ) {
				return true;
			}

			$conds['page_title'] = array_keys( $pages );

			$res = $dbw->select( 'page', $fields, $conds, __METHOD__ );
			$inserts = [];
			foreach ( $res as $row ) {
				$inserts[] = [
					'rt_type' => RevTag::getType( 'fuzzy' ),
					'rt_page' => $row->page_id,
					'rt_revision' => $row->page_latest,
				];
			}

			if ( $inserts === [] ) {
				return true;
			}

			$dbw->replace(
				'revtag',
				[ [ 'rt_type', 'rt_page', 'rt_revision' ] ],
				$inserts,
				__METHOD__
			);
		}

		return true;
	}
}