This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
/
LiveTranslate.hooks.php
351 lines (300 loc) · 8.74 KB
/
LiveTranslate.hooks.php
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* Static class for hooks handled by the Live Translate extension.
*
* @since 0.1
*
* @file LiveTranslate.hooks.php
* @ingroup LiveTranslate
*
* @author Jeroen De Dauw
*/
final class LiveTranslateHooks {
/**
* Hook to insert things into article headers.
*
* @since 0.1
*
* @param Article &$article
* @param boolean $outputDone
* @param boolean $useParserCache
*
* @return true
*/
public static function onArticleViewHeader( Article &$article, &$outputDone, &$useParserCache ) {
global $egLiveTranslateLanguages;
$title = $article->getTitle();
$currentLang = LiveTranslateFunctions::getCurrentLang( $title );
if ( in_array( $title->getFullText(), LiveTranslateFunctions::getLocalMemoryNames() ) ) {
self::displayDictionaryPage( $article, $title );
$outputDone = true; // The translations themselves should not be shown.
}
elseif (
LiveTranslateFunctions::hasTranslationService()
&& $article->exists()
&& ( count( $egLiveTranslateLanguages ) > 1 || ( count( $egLiveTranslateLanguages ) == 1 && $egLiveTranslateLanguages[0] != $currentLang ) ) ) {
global $wgParser;
$po = $wgParser->getOutput();
$magicWords = isset( $po->mLTMagicWords ) ? $po->mLTMagicWords : array();
if ( !in_array( 'LT_NOTRANSLATIONCONTROL', $magicWords ) ) {
global $egLTNSWithTranslationControl, $egLTUnknownNSShowControl;
$ns = $title->getNamespace();
if ( in_array( 'LT_SHOWTRANSLATIONCONTROL', $magicWords )
|| ( array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTNSWithTranslationControl[$ns] )
|| ( !array_key_exists( $ns, $egLTNSWithTranslationControl ) && $egLTUnknownNSShowControl )
) {
self::displayTranslationControl( $currentLang );
}
}
}
return true;
}
/**
* Displays some shorts statistics about the dictionary page.
*
* @since 0.4
*
* @param Article $article
* @param Title $title
*/
protected static function displayDictionaryPage( Article &$article, Title $title ) {
global $wgOut, $wgLang, $wgUser, $egLiveTranslateLanguages;
$dbr = wfGetDb( DB_SLAVE );
$res = $dbr->select(
'live_translate_memories',
array(
'memory_lang_count',
'memory_tu_count'
),
array( 'memory_location' => $title->getFullText() ),
array( 'LIMIT' => 1 )
);
foreach ( $res as $tm ) {
break;
}
if ( $tm->memory_tu_count == 0 ) {
$wgOut->addWikiMsg( 'livetranslate-dictionary-empty' );
}
else {
$wgOut->addWikiMsg(
'livetranslate-dictionary-count',
$wgLang->formatNum( $tm->memory_tu_count ) ,
$wgLang->formatNum( $tm->memory_lang_count )
);
/*
$notAllowedLanguages = array();
foreach ( $tus[0]->getVariants() as $languageCode => $translations ) {
$languageCode = strtolower( $languageCode );
$mappings = LiveTranslateFunctions::getInputLangMapping();
if ( array_key_exists( $languageCode, $mappings ) ) {
$languageCode = $mappings[$languageCode];
}
if ( !in_array( $languageCode, $egLiveTranslateLanguages ) ) {
$notAllowedLanguages[] = $languageCode;
}
}
if ( count( $notAllowedLanguages ) > 0 ) {
$languages = Language::getLanguageNames( false );
foreach ( $notAllowedLanguages as &$notAllowedLang ) {
if ( array_key_exists( $notAllowedLang, $languages ) ) {
$notAllowedLang = $languages[$notAllowedLang];
}
}
$wgOut->addHTML(
Html::element(
'span',
array( 'style' => 'color:darkred' ),
wfMsgExt( 'livetranslate-dictionary-unallowed-langs', 'parsemag', $wgLang->listToText( $notAllowedLanguages ), count( $notAllowedLanguages ) )
)
);
}
*/
}
if ( $wgUser->isAllowed( 'managetms' ) ) {
$wgOut->addHTML(
Html::element(
'a',
array( 'href' => Title::newFromText( 'Special:LiveTranslate' )->getInternalURL() ),
wfMsg( 'livetranslate-dictionary-goto-edit' )
)
);
}
}
/**
* Outputs the Live Translate translation control.
*
* @since 0.4
*
* @param string $currentLang
*/
protected static function displayTranslationControl( $currentLang ) {
global $wgOut;
$langs = array();
foreach ( LiveTranslateFunctions::getLanguages( $currentLang ) as $label => $code ) {
$langs[] = "$code|$label";
}
$wgOut->addHTML(
Html::rawElement(
'div',
array(
'id' => 'livetranslatediv',
'sourcelang' => $currentLang,
'languages' => implode( '||', $langs )
)
)
);
LiveTranslateFunctions::loadJs();
}
/**
* Schema update to set up the needed database tables.
*
* @since 0.1
*
* @param DatabaseUpdater $updater
*
* @return true
*/
public static function onSchemaUpdate( /* DatabaseUpdater */ $updater = null ) {
global $wgDBtype, $egLiveTranslateIP;
if ( $wgDBtype == 'mysql' ) {
// Set up the current schema.
if ( $updater === null ) {
global $wgExtNewTables, $wgExtNewIndexes, $wgExtNewFields;
$wgExtNewTables[] = array(
'live_translate',
$egLiveTranslateIP . '/LiveTranslate.sql',
true
);
$wgExtNewTables[] = array(
'live_translate_memories',
$egLiveTranslateIP . '/LiveTranslate.sql',
true
);
$wgExtNewIndexes[] = array(
'live_translate',
'word_translation',
$egLiveTranslateIP . '/sql/LT_IndexWordTranslation.sql',
true
);
$wgExtNewFields[] = array(
'live_translate',
'memory_id',
$egLiveTranslateIP . '/sql/LT_addTMField.sql'
);
$wgExtNewFields[] = array(
'live_translate_memories',
'memory_version_hash',
$egLiveTranslateIP . '/sql/LT_addTMHashField.sql'
);
}
else {
$updater->addExtensionUpdate( array(
'addTable',
'live_translate',
$egLiveTranslateIP . '/LiveTranslate.sql',
true
) );
$updater->addExtensionUpdate( array(
'addTable',
'live_translate_memories',
$egLiveTranslateIP . '/LiveTranslate.sql',
true
) );
$updater->addExtensionUpdate( array(
'addIndex',
'live_translate',
'word_translation',
$egLiveTranslateIP . '/sql/LT_IndexWordTranslation.sql',
true
) );
$updater->addExtensionUpdate( array(
'addField',
'live_translate',
'memory_id',
$egLiveTranslateIP . '/sql/LT_addTMField.sql',
true
) );
$updater->addExtensionUpdate( array(
'addField',
'live_translate_memories',
'memory_version_hash',
$egLiveTranslateIP . '/sql/LT_addTMHashField.sql',
true
) );
}
}
return true;
}
/**
* Handles edits to the dictionary page to save the translations into the db.
*
* @since 0.1
*
* @return true
*/
public static function onArticleSaveComplete( &$article, &$user, $text, $summary,
$minoredit, $watchthis, $sectionanchor, &$flags, $revision, &$status, $baseRevId, &$redirect = null ) {
$title = $article->getTitle();
// FIXME: Hitting the db on every page save should be avoided
if ( in_array( $title->getFullText(), LiveTranslateFunctions::getLocalMemoryNames() ) ) {
$requestData = array(
'action' => 'importtms',
'format' => 'json',
'source' => $title->getFullText(),
'type' => LiveTranslateFunctions::getMemoryType( $title->getFullText() ),
'local' => 1
);
$api = new ApiMain( new FauxRequest( $requestData, true ), true );
$api->execute();
$response = $api->getResultData();
}
return true;
}
/**
* Registers the magic words to show and hide the translation control.
*
* TODO: apparently there is some new way of doing this for quite a while,
* which is not linked in the docs. If someone cares to explain the new stuff,
* I'll be happy to update this.
*
* @since 0.6
*
* @param array &$magicWords
* @param string $langCode
*
* @return true
*/
public static function addMagicWords( array &$magicWords, $langCode ) {
$magicWords['LT_NOTRANSLATIONCONTROL'] = array( 0, '__NOTRANSLATIONCONTROL__' );
$magicWords['LT_SHOWTRANSLATIONCONTROL'] =array( 0, '__SHOWTRANSLATIONCONTROL__' );
return true;
}
/**
* Strips the magic words added by Live Translate from the page text.
*
* @since 0.6
*
* @param Parser &$parser
* @param string &$text
*
* @return true
*/
public static function stripMagicWords( Parser &$parser, &$text ) {
global $egLiveTranslateMagicWords;
$mw = MagicWord::get( 'LT_NOTRANSLATIONCONTROL' );
if ( $mw->matchAndRemove( $text ) ) {
$egLiveTranslateMagicWords[] = 'LT_NOTRANSLATIONCONTROL';
}
$mw = MagicWord::get( 'LT_SHOWTRANSLATIONCONTROL' );
if ( $mw->matchAndRemove( $text ) ) {
$egLiveTranslateMagicWords[] = 'LT_SHOWTRANSLATIONCONTROL';
}
$po = $parser->getOutput();
$po->mLTMagicWords = $egLiveTranslateMagicWords;
return true;
}
public static function onOutputPageParserOutput( $outputpage, $parseroutput ) {
$magicWords = isset( $parseroutput->mLTMagicWords ) ? $parseroutput->mLTMagicWords : array();
return true;
}
}