summaryrefslogtreecommitdiff
blob: 905013d45363684e4df6d1cce3f297eeebc3cade (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
<?php

namespace LocalisationUpdate;

use FormatJson;
use Language;
use LocalisationUpdate\Fetcher\FetcherFactory;
use LocalisationUpdate\Reader\ReaderFactory;
use Maintenance;

$IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
	? getenv( 'MW_INSTALL_PATH' )
	: realpath( __DIR__ . '/../../' );

require "$IP/maintenance/Maintenance.php";

class Update extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Fetches translation updates to MediaWiki core, skins and extensions.' );
		$this->addOption(
			'repoid',
			'Fetch translations from repositories identified by this',
			false, /*required*/
			true /*has arg*/
		);

		$this->requireExtension( 'LocalisationUpdate' );
	}

	public function execute() {
		// Prevent the script from timing out
		set_time_limit( 0 );
		ini_set( 'max_execution_time', '0' );
		ini_set( 'memory_limit', '-1' );

		global $IP;
		global $wgLocalisationUpdateRepositories;
		global $wgLocalisationUpdateRepository;

		$dir = LocalisationUpdate::getDirectory();
		if ( !$dir ) {
			$this->fatalError( 'No cache directory configured' );
			return;
		}

		$lc = Language::getLocalisationCache();
		$messagesDirs = $lc->getMessagesDirs();

		$finder = new Finder( $messagesDirs, $IP );
		$readerFactory = new ReaderFactory();
		$fetcherFactory = new FetcherFactory();

		$repoid = $this->getOption( 'repoid', $wgLocalisationUpdateRepository );
		if ( !isset( $wgLocalisationUpdateRepositories[$repoid] ) ) {
			$known = implode( ', ', array_keys( $wgLocalisationUpdateRepositories ) );
			$this->fatalError( "Unknown repoid $repoid; known: $known" );
			return;
		}
		$repos = $wgLocalisationUpdateRepositories[$repoid];

		// output and error methods are protected, hence we add logInfo and logError
		// public methods, that hopefully won't conflict in the future with the base class.
		$logger = $this;

		// Do it ;)
		$updater = new Updater();
		$updatedMessages = $updater->execute(
			$finder,
			$readerFactory,
			$fetcherFactory,
			$repos,
			$logger
		);

		// Store it ;)
		$count = array_sum( array_map( 'count', $updatedMessages ) );
		if ( !$count ) {
			$this->output( "Found no new translations\n" );
			return;
		}

		foreach ( $updatedMessages as $language => $messages ) {
			$filename = "$dir/" . LocalisationUpdate::getFilename( $language );
			file_put_contents( $filename, FormatJson::encode( $messages, true ) );
		}
		$this->output( "Saved $count new translations\n" );
	}

	/**
	 * @param string $msg
	 */
	public function logInfo( $msg ) {
		$this->output( $msg . "\n" );
	}

	/**
	 * @param string $msg
	 */
	public function logError( $msg ) {
		$this->error( $msg );
	}
}

$maintClass = Update::class;
require_once RUN_MAINTENANCE_IF_MAIN;