summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/scripts/processMessageChanges.php
blob: e0915ced71a4bb87732326eea89e6af61cb10d07 (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
<?php
/**
 * Script for processing message changes in file based message groups.
 *
 * @author Niklas Laxström
 *
 * @copyright Copyright © 2012-2013, Niklas Laxström
 * @license GPL-2.0+
 * @file
 */

// Standard boilerplate to define $IP
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$IP = getenv( 'MW_INSTALL_PATH' );
} else {
	$dir = __DIR__;
	$IP = "$dir/../../..";
}
require_once "$IP/maintenance/Maintenance.php";

/**
 * Script for processing message changes in file based message groups.
 *
 * We used to process changes during web request, but that was too slow. With
 * this command line script we can do all the work needed even if it takes
 * some time.
 *
 * @since 2012-04-23
 */
class ProcessMessageChanges extends Maintenance {
	protected $changes = array();

	/**
	 * @var int
	 */
	protected $counter;

	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Script for processing message changes in file based message groups';
		$this->addOption(
			'group',
			'(optional) Comma separated list of group IDs to process (can use * as wildcard). ' .
				'Default: "*"',
			false, /*required*/
			true /*has arg*/
		);
		$this->addOption(
			'skipgroup',
			'(optional) Comma separated list of group IDs to not process (can use * ' .
				'as wildcard). Overrides --group parameter.',
			false, /*required*/
			true /*has arg*/
		);
		$this->addOption(
			'name',
			'(optional) Unique name to avoid conflicts with multiple invocations of this script.',
			false, /*required*/
			true /*has arg*/
		);
		$this->addOption(
			'safe-import',
			'(optional) Import "safe" changes: message additions when no other kind of changes.',
			false, /*required*/
			false /*has arg*/
		);
	}

	public function execute() {
		$groups = $this->getGroups();
		$changes = array();
		$comparator = new ExternalMessageSourceStateComparator();

		$scripted = $this->hasOption( 'safe-import' );

		/** @var FileBasedMessageGroup $group */
		foreach ( $groups as $id => $group ) {
			if ( !$scripted ) {
				$this->output( "Processing $id\n" );
			}
			$changes[$id] = $comparator->processGroup( $group, $comparator::ALL_LANGUAGES );
		}

		// Remove all groups without changes
		$changes = array_filter( $changes );

		if ( $changes === array() ) {
			if ( !$scripted ) {
				$this->output( "No changes found\n" );
			}

			return;
		}

		if ( $this->hasOption( 'safe-import' ) ) {
			$importer = new ExternalMessageSourceStateImporter();
			$info = $importer->importSafe( $changes );
			$this->printChangeInfo( $info );

			return;
		}

		$name = $this->getOption( 'name', MessageChangeStorage::DEFAULT_NAME );
		if ( !MessageChangeStorage::isValidCdbName( $name ) ) {
			$this->error( 'Invalid name', 1 );
		}

		$file = MessageChangeStorage::getCdbPath( $name );

		MessageChangeStorage::writeChanges( $changes, $file );
		$url = SpecialPage::getTitleFor( 'ManageMessageGroups', $name )->getFullURL();
		$this->output( "Process changes at $url\n" );
	}

	/**
	 * Gets list of message groups filtered by user input.
	 * @return MessageGroup[]
	 */
	protected function getGroups() {
		$groups = MessageGroups::getGroupsByType( 'FileBasedMessageGroup' );

		// Include all if option not given
		$include = $this->getOption( 'group', '*' );
		$include = explode( ',', $include );
		$include = array_map( 'trim', $include );
		$include = MessageGroups::expandWildcards( $include );

		// Exclude nothing if option not given
		$exclude = $this->getOption( 'skipgroup', '' );
		$exclude = explode( ',', $exclude );
		$exclude = array_map( 'trim', $exclude );
		$exclude = MessageGroups::expandWildcards( $exclude );

		// Flip to allow isset
		$include = array_flip( $include );
		$exclude = array_flip( $exclude );

		$groups = array_filter( $groups,
			function ( MessageGroup $group ) use ( $include, $exclude ) {
				$id = $group->getId();

				return isset( $include[$id] ) && !isset( $exclude[$id] );
			}
		);

		return $groups;
	}

	protected function printChangeInfo( array $info ) {
		foreach ( $info['processed'] as $group => $count ) {
			$this->output( "Imported $count new messages or translations for $group.\n" );
		}

		if ( $info['skipped'] !== array() ) {
			$skipped = implode( ', ', array_keys( $info['skipped'] ) );
			$this->output( "There are changes to check for groups $skipped.\n" );
			$url = SpecialPage::getTitleFor( 'ManageMessageGroups', $info['name'] )->getFullURL();
			$this->output( "You can process them at $url\n" );
		}
	}
}

$maintClass = 'ProcessMessageChanges';
require_once RUN_MAINTENANCE_IF_MAIN;