summaryrefslogtreecommitdiff
blob: b63114f1617a9aa5f29776eeaab5e64e5442cd39 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
 * Contains logic for special page Special:AdvancedTranslate
 *
 * @file
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @copyright Copyright © 2008-2013, Niklas Laxström, Siebrand Mazeland
 * @license GPL-2.0+
 */

/**
 * This special page helps with the translations of %MediaWiki features that are
 * not in the main messages array (special page aliases, magic words, namespace names).
 *
 * @ingroup SpecialPage TranslateSpecialPage
 */
class SpecialMagic extends TranslateSpecialPage {
	const MODULE_MAGIC = 'words';
	const MODULE_SPECIAL = 'special';
	const MODULE_NAMESPACE = 'namespace';

	/**
	 * List of supported modules
	 */
	private $aModules = array(
		self::MODULE_SPECIAL,
		self::MODULE_NAMESPACE,
		self::MODULE_MAGIC
	);

	/**
	 * Page options
	 */
	private $options = array();
	private $defaults = array();
	private $nondefaults = array();

	public function __construct() {
		parent::__construct( 'Magic' );
	}

	/**
	 * @see SpecialPage::getDescription
	 *
	 * @return string
	 */
	function getDescription() {
		return $this->msg( 'translate-magic-pagename' )->text();
	}

	/**
	 * Returns HTML5 output of the form
	 * GLOBALS: $wgScript
	 * @return string
	 */
	protected function getForm() {
		global $wgScript;

		$form = Xml::tags( 'form',
			array(
				'action' => $wgScript,
				'method' => 'get'
			),

			'<table><tr><td>' .
				$this->msg( 'translate-page-language' )->escaped() .
				'</td><td>' .
				TranslateUtils::languageSelector(
					$this->getLanguage()->getCode(),
					$this->options['language']
				) .
				'</td></tr><tr><td>' .
				$this->msg( 'translate-magic-module' )->escaped() .
				'</td><td>' .
				$this->moduleSelector( $this->options['module'] ) .
				'</td></tr><tr><td colspan="2">' .
				Xml::submitButton( $this->msg( 'translate-magic-submit' )->text() ) . ' ' .
				Xml::submitButton(
					$this->msg( 'translate-magic-cm-export' )->text(),
					array( 'name' => 'export' )
				) .
				'</td></tr></table>' .
				Html::hidden( 'title', $this->getTitle()->getPrefixedText() )
		);

		return $form;
	}

	/**
	 * Helper function get module selector.
	 *
	 * @param string $selectedId Which value should be selected by default
	 * @return string HTML5-compatible select-element.
	 */
	protected function moduleSelector( $selectedId ) {
		// Give grep a chance to find the usages:
		// translate-magic-words, translate-magic-special, translate-magic-namespace
		$selector = new XmlSelect( 'module', 'module', $selectedId );
		foreach ( $this->aModules as $code ) {
			$selector->addOption( $this->msg( 'translate-magic-' . $code )->text(), $code );
		}

		return $selector->getHTML();
	}

	protected function setup( $parameters ) {
		$defaults = array(
			/* str  */'module'   => '',
			/* str  */'language' => $this->getUser()->getOption( 'language' ),
			/* bool */'export'   => false,
			/* bool */'savetodb' => false,
		);

		/**
		 * Place where all non default variables will end.
		 */
		$nondefaults = array();

		/**
		 * Temporary store possible values parsed from parameters.
		 */
		$options = $defaults;
		$request = $this->getRequest();
		foreach ( $options as $v => $t ) {
			if ( is_bool( $t ) ) {
				$r = $request->getBool( $v, $options[$v] );
			} elseif ( is_int( $t ) ) {
				$r = $request->getInt( $v, $options[$v] );
			} elseif ( is_string( $t ) ) {
				$r = $request->getText( $v, $options[$v] );
			}

			if ( !isset( $r ) ) {
				throw new MWException( '$r was not set' );
			}

			wfAppendToArrayIfNotDefault( $v, $r, $defaults, $nondefaults );
		}

		$this->defaults = $defaults;
		$this->nondefaults = $nondefaults;
		$this->options = $nondefaults + $defaults;
	}

	/**
	 * The special page running code
	 */
	public function execute( $parameters ) {
		$this->setup( $parameters );
		$this->setHeaders();

		$out = $this->getOutput();
		TranslateUtils::addSpecialHelpLink(
			$out,
			'//translatewiki.net/wiki/FAQ#Special:AdvancedTranslate', true
		);

		$out->addHTML( $this->getForm() );

		if ( !$this->options['module'] ) {
			return;
		}
		switch ( $this->options['module'] ) {
			case 'alias':
			case self::MODULE_SPECIAL:
				$o = new SpecialPageAliasesCM( $this->options['language'] );
				break;
			case self::MODULE_MAGIC:
				$o = new MagicWordsCM( $this->options['language'] );
				break;
			case self::MODULE_NAMESPACE:
				$o = new NamespaceCM( $this->options['language'] );
				break;
			default:
				throw new MWException( "Unknown module {$this->options['module']}" );
		}

		$request = $this->getRequest();
		if ( $request->wasPosted() && $this->options['savetodb'] ) {
			if ( !$this->getUser()->isAllowed( 'translate' ) ) {
				throw new PermissionsError( 'translate' );
			}

			$errors = array();
			$o->loadFromRequest( $request );
			$o->validate( $errors );
			if ( $errors ) {
				$out->wrapWikiMsg( '<div class="error">$1</div>',
					'translate-magic-notsaved' );
				$this->outputErrors( $errors );
				$out->addHTML( $o->output() );

				return;
			} else {
				$o->save( $request );
				$out->wrapWikiMsg( '<strong>$1</strong>', 'translate-magic-saved' );
				$out->addHTML( $o->output() );

				return;
			}
		}

		if ( $this->options['export'] ) {
			$output = $o->export();
			if ( $output === '' ) {
				$out->addWikiMsg( 'translate-magic-nothing-to-export' );

				return;
			}
			$result = Xml::element( 'textarea', array( 'rows' => '30' ), $output );
			$out->addHTML( $result );

			return;
		}

		$out->addWikiMsg( 'translate-magic-help' );
		$errors = array();
		$o->validate( $errors );
		if ( $errors ) {
			$this->outputErrors( $errors );
		}
		$out->addHTML( $o->output() );
	}

	protected function outputErrors( $errors ) {
		$count = $this->getLanguage()->formatNum( count( $errors ) );
		$out = $this->getOutput();
		$out->addWikiMsg( 'translate-magic-errors', $count );
		$out->addHTML( '<ol>' );
		foreach ( $errors as $error ) {
			$out->addHTML( "<li>$error</li>" );
		}
		$out->addHTML( '</ol>' );
	}
}