forked from yaronkoren/TinyMCE-old
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTinyMCE.hooks.php
590 lines (505 loc) · 15.2 KB
/
TinyMCE.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<?php
use MediaWiki\MediaWikiServices;
#use MediaWiki\User\UserOptionsManager;
/**
* Static functions called by various outside hooks, as well as by
* extension.json.
*
* @author Yaron Koren
* @file
* @ingroup TinyMCE
*/
class TinyMCEHooks {
public static function registerExtension() {
if ( defined( 'TINYMCE_VERSION' ) ) {
// Do not load this extension more than once.
return 1;
}
define( 'TINYMCE_VERSION', '1.0' );
$GLOBALS['wgTinyMCEIP'] = dirname( __DIR__ ) . '/../';
$GLOBALS['wgTinyMCEUse'] = null;
// We have to have this hook called here, instead of in
// extension.json, because it's conditional.
if ( class_exists( 'MediaWiki\Linker\LinkRenderer' ) ) {
// MW 1.28+
$GLOBALS['wgHooks']['HtmlPageLinkRendererEnd'][] = 'TinyMCEHooks::changeRedLink';
} else {
$GLOBALS['wgHooks']['LinkEnd'][] = 'TinyMCEHooks::changeRedLinkOld';
}
}
/**
* Try to translate between MediaWiki's language codes and TinyMCE's -
* they're similar, but not identical. MW's are always lowercase, and
* they tend to use fewer country codes.
*/
static function mwLangToTinyMCELang( $mwLang ) {
// 'en' gets special handling, because it's TinyMCE's
// default language - it requires no language add-on.
if ( $mwLang == null || $mwLang === 'en' ) {
return 'en';
}
$tinyMCELanguages = array(
'af_ZA',
'ar', 'ar_SA',
'az',
'eu',
'be',
'bn_BD',
'bs',
'bg_BG',
'ca',
'cs', 'cs_CZ',
'cy',
'da',
'de',
'de_AT',
'dv',
'el',
'en_CA', 'en_GB',
'eo',
'es', 'es_AR', 'es_MX',
'et',
'eu',
'fa',
'fa_IR',
'fi',
'fo',
'fr_FR', 'fr_CH',
'ga',
'gd',
'gl',
'he_IL',
'hi_IN',
'hr',
'hu_HU',
'hy',
'id',
'is_IS',
'it',
'ja',
'ka_GE',
'kab',
'kk',
'km_KH',
'ko',
'ko_KR',
'ku',
'ku_IQ',
'lb',
'lt',
'lv',
'mk_MK',
'ml', 'ml_IN',
'mn_MN',
'nb_NO',
'nl',
'pl',
'pt_BR', 'pt_PT',
'ro',
'ru', 'ru_RU', 'ru@petr1708',
'si_LK',
'sk',
'sl_SI',
'sr',
'sv_SE',
'ta', 'ta_IN',
'tg',
'th_TH',
'tr', 'tr_TR',
'tt',
'ug',
'uk', 'uk_UA',
'uz',
'vi', 'vi_VN',
'zh_CN', 'zh_CN.GB2312', 'zh_TW',
);
foreach ( $tinyMCELanguages as $tinyMCELang ) {
if ( $mwLang === strtolower( $tinyMCELang ) ||
$mwLang === substr( $tinyMCELang, 0, 2 ) ) {
return $tinyMCELang;
}
}
// If there was no match found, see if there's a matching
// "fallback language" for the current language - like
// 'fr' for 'frc'.
$fallbackLangs = Language::getFallbacksFor( $mwLang );
foreach ( $fallbackLangs as $fallbackLang ) {
if ( $fallbackLang === 'en' ) {
continue;
}
foreach ( $tinyMCELanguages as $tinyMCELang ) {
if ( $fallbackLang === strtolower( $tinyMCELang ) ||
$fallbackLang === substr( $tinyMCELang, 0, 2 ) ) {
return $tinyMCELang;
}
}
}
return 'en';
}
static function setGlobalJSVariables( &$vars, $out ) {
global $wgTinyMCEEnabled, $wgTinyMCETemplates, $wgTinyMCEPreservedTags;
global $wgCheckFileExtensions, $wgStrictFileExtensions;
global $wgFileExtensions, $wgFileBlacklist;
global $wgEnableUploads;
global $wgTinyMCESettings;
global $wgWsTinyMCEModals;
global $wgTinyMCEUse;
if ( !$wgTinyMCEEnabled ) {
return true;
}
$context = $out->getContext();
$extensionTags = MediaWikiServices::getInstance()->getParser()->getTags();
$specialTags = '';
$preservedTags = '';
foreach ( $extensionTags as $tagName ) {
if ( ( $tagName == 'pre' ) || ($tagName == 'nowiki') ) {
continue;
}
$specialTags .= $tagName . '|';
}
$defaultTags = array(
"includeonly", "onlyinclude", "noinclude", "nowiki" //Definitively MediaWiki core
);
$tinyMCETagList = $specialTags . implode( '|', $defaultTags );
$vars['wgTinyMCETagList'] = $tinyMCETagList;
$mwLanguage = $context->getLanguage()->getCode();
$tinyMCELanguage = self::mwLangToTinyMCELang( $mwLanguage );
$vars['wgTinyMCELanguage'] = $tinyMCELanguage;
$directionality = $context->getLanguage()->getDir();
$vars['wgTinyMCEDirectionality'] = $directionality;
$vars['wgCheckFileExtensions'] = $wgCheckFileExtensions;
$vars['wgStrictFileExtensions'] = $wgStrictFileExtensions;
$vars['wgFileExtensions'] = $wgFileExtensions;
$vars['wgFileBlacklist'] = $wgFileBlacklist;
$vars['wgEnableUploads'] = $wgEnableUploads;
$vars['wgTinyMCEVersion'] = ExtensionRegistry::getInstance()->getAllThings()['TinyMCE']['version'];
$user = $context->getUser();
if ($user->isAllowed('upload')) {
$userMayUpload = true;
} else {
$userMayUpload = false;
}
$vars['wgTinyMCEUserMayUpload'] = $userMayUpload;
if ($user->isAllowed('upload_by_url')) {
$userMayUploadFromURL = true;
} else {
$userMayUploadFromURL= false;
}
$vars['wgTinyMCEUserMayUploadFromURL'] = $userMayUploadFromURL;
if ( $user->getBlock() ) {
$userIsBlocked = true;
} else {
$userIsBlocked = false;
}
$vars['wgTinyMCEUserIsBlocked'] = $userIsBlocked ;
$vars['wgTinyMCESettings'] = $wgTinyMCESettings;
$vars['wgWsTinyMCEModals'] = $wgWsTinyMCEModals;
return true;
}
/**
* Register magic-word variable IDs
*/
static function addMagicWordVariableIDs( &$magicWordVariableIDs ) {
$magicWordVariableIDs[] = 'MAG_NOTINYMCE';
return true;
}
/**
* Set a value in the page_props table based on the presence of the
* 'NOTINYMCE' magic word in a page
*/
static function handleMagicWords( &$parser, &$text ) {
if ( class_exists( MagicWordFactory::class ) ) {
// MW 1.32+
$factory = MediaWikiServices::getInstance()->getMagicWordFactory();
$magicWord = $factory->get( 'MAG_NOTINYMCE' );
} else {
$magicWord = MagicWord::get( 'MAG_NOTINYMCE' );
}
if ( $magicWord->matchAndRemove( $text ) ) {
$parser->mOutput->setProperty( 'notinymce', 'y' );
}
return true;
}
/**
* Adds an "edit" link for TinyMCE, and renames the current "edit"
* link to "edit source", for all sections on the page, if it's
* editable with TinyMCE in the first place.
*/
public static function addEditSectionLink( $skin, $title, $section, $tooltip, &$links, $lang ) {
global $wgTinyMCEUse;
$context = $skin->getContext();
$user = $context->getUser();
$wgTinyMCEUse = TinyMCEHooks::enableTinyMCE( $title, $context );
/* if ( method_exists( 'MediaWiki\Permissions\PermissionManager', 'userCan' ) ) {
// MW 1.33+
$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
$userCanEdit = $permissionManager->userCan( 'edit', $user, $title );
} else {
$userCanEdit = $title->userCan( 'edit', $user ) && $user->isAllowed( 'edit' );
}
if ( !isset( $title ) || !$userCanEdit ) {
return true;
}*/
if ( !$wgTinyMCEUse ) {
return true;
}
foreach ( $links as &$link ) {
if ( $link['query']['action'] == 'edit' ) {
$newLink = $link;
$link['text'] = ' | ' . $skin->msg( 'tinymce-editsectionsource' )->text() ;
}
}
$newLink['query']['action'] = 'tinymceedit';
$links = array_merge( array( 'tinymceeditsection' => $newLink ), $links );
return true;
}
/**
* Sets broken/red links to point to TinyMCE edit page, if they
* haven't been customized already - for MW < 1.28.
*
* @param Linker $linker
* @param Title $target
* @param array $options
* @param string $text
* @param array &$attribs
* @param bool &$ret
* @return true
*/
static function changeRedLinkOld( $linker, $target, $options, $text, &$attribs, &$ret ) {
global $wgTinyMCEUse;
global $wgOut;
// If it's not a broken (red) link, exit.
if ( !in_array( 'broken', $options, true ) ) {
return true;
}
// If the link is to a special page, exit.
if ( $target->getNamespace() == NS_SPECIAL ) {
return true;
}
// This link may have been modified already by Page Forms or
// some other extension - if so, leave it as it is.
if ( strpos( $attribs['href'], 'action=edit&' ) === false ) {
return true;
}
$exWgTinyMCEUse = $wgTinyMCEUse;
$context = $wgOut->getContext();
if ( !TinyMCEHooks::enableTinyMCE( $title, $context ) ) {
return true;
}
$wgTinyMCEUse = $exWgTinyMCEUse;
$attribs['href'] = $target->getLinkURL( array( 'action' => 'tinymceedit', 'redlink' => '1' ) );
return true;
}
/**
* Called by the HtmlPageLinkRendererEnd hook.
*
* The $target argument is listed in the documentation as being of type
* LinkTarget, but in practice it seems to sometimes be of type Title
* and sometimes of type TitleValue. So we just leave out a type
* declaration for that argument in the declaration.
*
* @param LinkRenderer $linkRenderer
* @param Title $target
* @param bool $isKnown
* @param string &$text
* @param array &$attribs
* @param bool &$ret
* @return true
*/
static function changeRedLink( MediaWiki\Linker\LinkRenderer $linkRenderer, $target, $isKnown, &$text, &$attribs, &$ret ) {
global $wgTinyMCEUse;
global $wgTinyMCEDisabledNamespaces;
// If it's not a broken (red) link, exit.
if ( $isKnown ) {
return true;
}
// If the link is to a special page, exit.
if ( $target->getNamespace() == NS_SPECIAL ) {
return true;
}
// This link may have been modified already by Page Forms or
// some other extension - if so, leave it as it is.
if ( strpos( $attribs['href'], 'action=edit&' ) === false ) {
return true;
}
if ( $target instanceof Title ) {
$title = $target;
} else {
// TitleValue object in MW >= 1.34?
$title = Title::newFromLinkTarget( $target );
}
// check TinyMCE allowed for namespace
if ( in_array( $title->getNamespace(), $wgTinyMCEDisabledNamespaces ) ) {
return true;
}
$attribs['href'] = $title->getLinkURL( array( 'action' => 'tinymceedit', 'redlink' => '1' ) );
return true;
}
/**
* Is there a less hacky way to do this, like stopping the toolbar
* creation before it starts?
*/
public static function removeDefaultToolbar( &$toolbar ) {
global $wgTinyMCEEnabled;
if ( $wgTinyMCEEnabled ) {
$toolbar = null;
}
return true;
}
public static function enableTinyMCE( $title, $context ) {
global $wgTinyMCEDisabledNamespaces, $wgTinyMCEUnhandledStrings;
global $wgTinyMCEUse;
$user = $context->getUser();
// already checked so save work
if ( $wgTinyMCEUse !== null) return $wgTinyMCEUse;
// if the user isn't allowed to edit this page then won't need TinyMCE!
if ( method_exists( 'MediaWiki\Permissions\PermissionManager', 'userCan' ) ) {
// MW 1.33+
$permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
$userCanEdit = $permissionManager->userCan( 'edit', $user, $title );
} else {
$userCanEdit = $title->userCan( 'edit', $user ) && $user->isAllowed( 'edit' );
}
if ( !isset( $title ) || !$userCanEdit ) {
return $wgTinyMCEUse = false;
}
if ( in_array( $title->getNamespace(), $wgTinyMCEDisabledNamespaces ) ) {
return $wgTinyMCEUse = false;
}
if ( $context->getRequest()->getCheck( 'undo' ) ) {
return $wgTinyMCEUse = false;
}
if ( !$context->getUser()->getOption( 'tinymce-use' ) ) {
return $wgTinyMCEUse = false;
}
if ( !empty( $wgTinyMCEUnhandledStrings ) ) {
$wikiPage = new WikiPage( $title );
$content = $wikiPage->getContent();
if ( $content != null ) {
$pageText = $content->getText();
foreach ( $wgTinyMCEUnhandledStrings as $str ) {
if ( strpos( $pageText, $str ) !== false ) {
return $wgTinyMCEUse = false;
}
}
}
}
// If there's a 'notinymce' property for this page in the
// page_props table, regardless of the value, disable TinyMCE.
$dbr = wfGetDB( DB_REPLICA );
$row = $dbr->selectRow( 'page_props',
array(
'pp_value'
),
array(
'pp_page' => $title->getArticleID(),
'pp_propname' => 'notinymce'
)
);
if ( $row ) {
return $wgTinyMCEUse = false;
}
global $wgTinyMCELoadOnView;
if ( Action::getActionName( $context ) === 'view') {
# return (bool)$wgTinyMCELoadOnView;
}
return $wgTinyMCEUse = true;
}
/**
* This code is called separately from addToEditPage() because it needs
* to be called earlier - addToEditPage() is called via the same hook
* (EditPage::showEditForm:initial) as WikiEditor's code for
* determining whether to install WikiEditor - which means that, if
* this code were part of addToEditPage(), whether or not it ran
* correctly would depend on the order in which the extensions were
* called. Instead, for this code we use a hook called earlier -
* AlternateEdit.
*/
public static function determineIfTinyMCEIsEnabled( EditPage $editPage ) {
global $wgTinyMCEEnabled;
global $wgTinyMCEUse;
$wgTinyMCEEnabled = false;
$context = $editPage->getArticle()->getContext();
$action = Action::getActionName( $context );
if ( $action != 'tinymceedit' ) {
return true;
}
$title = $editPage->getTitle();
$wgTinyMCEEnabled = self::enableTinyMCE( $title, $context );
return true;
}
public static function addToEditPage( EditPage &$editPage, OutputPage &$output ) {
global $wgTinyMCEEnabled;
if ( !$wgTinyMCEEnabled ) {
return true;
}
$output->addModules( 'ext.tinymce' );
$output->addHTML( Html::rawElement( 'p', null, wfMessage( 'tinymce-notice' )->parse() ) );
return true;
}
public static function disableWikiEditor( $editPage ) {
global $wgTinyMCEEnabled;
if ( $wgTinyMCEEnabled ) {
return false;
}
return true;
}
/**
* Load the extension on every view, if allowed.
*
* @param OutputPage $output
* @return void
*/
public static function addToViewPage( OutputPage $output, ParserOutput $parserOutput ) {
$context = $output->getContext();
$action = Action::getActionName( $context );
if ( $action != 'view' ) {
return;
}
self::enableTinyMCE( $output->getTitle(), $context );
$GLOBALS['wgTinyMCEEnabled'] = true;
$output->addModules( 'ext.tinymce' );
/*0422 if (( self::enableTinyMCE( $output->getTitle(), $context ) ) ||
( substr( $output->getTitle()->getBaseText(), 0, 8 ) == 'FormEdit' )) {
$GLOBALS['wgTinyMCEEnabled'] = true;
$output->addModules( 'ext.tinymce' );
} else {
$GLOBALS['wgTinyMCEEnabled'] = false;
}*/
}
/**
* Add a tag for bracketing in-line editable wiki text.
*
* @param OutputPage $output
* @return void
*/
public static function onParserFirstCallInit( Parser $parser ) {
$parser->setHook( 'editinline', [ self::class, 'renderEditInline' ] );
}
public static function renderEditInline( $input, array $args, Parser $parser, PPFrame $frame ) {
$attr = [];
// This time, make a list of attributes and their values, and dump them, along with the user input
foreach( $args as $name => $value ) {
$attr[] = $name . '="' . $value . '"';
}
return '<form action="get"><div ' . implode( ' ', $attr ) . '>' . htmlspecialchars( $input ) . '</div></form>';
# return implode( '<br />', $attr ) . "\n\n" . htmlspecialchars( $input );
/**
* The following lines can be used to get the variable values directly:
* $to = $args['to'] ;
* $email = $args['email'] ;
*/
}
public static function addPreference( $user, &$preferences ) {
$preferences['tinymce-use'] = array(
'type' => 'toggle',
'label-message' => 'tinymce-preference', // a system message
'section' => 'editing/advancedediting',
);
return true;
}
public static function addRLModules( &$otherModules ) {
$otherModules[] = 'ext.tinymce';
return true;
}
}