summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/scripts/mwcore-export.php
blob: c87921314f6fa5dac2454991a887c96e04e456ac (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
<?php
/**
 * Script to export special core features of %MediaWiki.
 *
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @copyright Copyright © 2009-2013, Niklas Laxström, Siebrand Mazeland
 * @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";

class MwCoreExport extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = 'Core special features exporter.';
		$this->addOption(
			'target',
			'Target directory for exported files',
			true, /*required*/
			true /*has arg*/
		);
		$this->addOption(
			'lang',
			'(optional) Comma separated list of language codes. Default: *',
			false, /*required*/
			true /*has arg*/
		);
		$this->addOption(
			'type',
			'Export type: "namespace", "special" or "magic"',
			true, /*required*/
			true /*has arg*/
		);
	}

	public function execute() {
		if ( !is_writable( $this->getOption( 'target' ) ) ) {
			$this->error( 'Target directory is not writable.', 1 );
		}

		$langs = TranslateUtils::parseLanguageCodes( $this->getOption( 'lang', '*' ) );
		$group = MessageGroups::getGroup( 'core' );
		$type = $this->getOption( 'type' );

		foreach ( $langs as $l ) {
			$o = null;

			switch ( $type ) {
				case 'special':
					$o = new SpecialPageAliasesCM( $l );
					break;
				case 'magic':
					$o = new MagicWordsCM( $l );
					break;
				case 'namespace':
					$o = new NamespaceCM( $l );
					break;
				default:
					$this->error( 'Invalid type: Must be one of special, magic, namespace.', 1 );
			}

			$export = $o->export( 'core' );
			if ( $export === '' ) {
				continue;
			}

			$matches = array();
			preg_match( '~^(\$[a-zA-Z]+)\s*=~m', $export, $matches );

			if ( !isset( $matches[1] ) ) {
				continue;
			}

			# remove useles comment
			$export = preg_replace( "~^# .*$\n~m", '', $export );

			if ( strpos( $export, '#!!' ) !== false ) {
				$this->error( "There are warnings with $l." );
			}

			$variable = preg_quote( $matches[1], '~' );

			/** @var FileBasedMessageGroup $group */
			$file = $group->getSourceFilePath( $l );
			// bandage
			$real = Language::getFileName( '/messages/Messages', $l );
			$file = preg_replace( '~/i18n/(.+)\.json$~', $real, $file );

			if ( !file_exists( $file ) ) {
				$this->error( "File $file does not exist!" );
				continue;
			}

			$data = file_get_contents( $file );

			$export = trim( $export ) . "\n";
			$escExport = addcslashes( $export, '\\$' ); # Darn backreferences

			$outFile = $this->getOption( 'target' ) . '/' . $group->getTargetFilename( $l );
			$outFile = preg_replace( '~/i18n/(.+)\.json$~', $real, $outFile );

			$count = 0;
			$data = preg_replace( "~$variable\s*=.*?\n\);\n~s", $escExport, $data, 1, $count );
			if ( $count ) {
				file_put_contents( $outFile, $data );
			} else {
				$this->error( "Adding new entry to $outFile, please double check location." );
				$pos = strpos( $data, '*/' );
				if ( $pos === false ) {
					$this->error( '. FAILED! Totally new file? No header?' );
				} else {
					$pos += 3;
				}

				$data = substr( $data, 0, $pos ) . "\n" . $export . substr( $data, $pos );

				file_put_contents( $outFile, $data );
			}
		}
	}
}

$maintClass = 'MwCoreExport';
require_once RUN_MAINTENANCE_IF_MAIN;