File: Hooks.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (583 lines) | stat: -rw-r--r-- 17,719 bytes parent folder | download
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
<?php
/**
 * Hooks for WikiEditor extension
 *
 * @file
 * @ingroup Extensions
 */

namespace MediaWiki\Extension\WikiEditor;

use Article;
use MediaWiki\Api\ApiMessage;
use MediaWiki\Cache\CacheKeyHelper;
use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook;
use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook;
use MediaWiki\Config\Config;
use MediaWiki\Content\Content;
use MediaWiki\Context\RequestContext;
use MediaWiki\EditPage\EditPage;
use MediaWiki\Extension\ConfirmEdit\Hooks as ConfirmEditHooks;
use MediaWiki\Extension\EventLogging\EventLogging;
use MediaWiki\Hook\EditPage__attemptSave_afterHook;
use MediaWiki\Hook\EditPage__attemptSaveHook;
use MediaWiki\Hook\EditPage__showEditForm_fieldsHook;
use MediaWiki\Hook\EditPage__showEditForm_initialHook;
use MediaWiki\Hook\EditPageGetPreviewContentHook;
use MediaWiki\Hook\RecentChange_saveHook;
use MediaWiki\Html\Html;
use MediaWiki\MediaWikiServices;
use MediaWiki\Output\OutputPage;
use MediaWiki\Preferences\Hook\GetPreferencesHook;
use MediaWiki\Registration\ExtensionRegistry;
use MediaWiki\Request\WebRequest;
use MediaWiki\ResourceLoader as RL;
use MediaWiki\Status\Status;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\User;
use MediaWiki\User\UserEditTracker;
use MediaWiki\WikiMap\WikiMap;
use MessageLocalizer;
use MobileContext;
use MWCryptRand;
use RecentChange;
use WikimediaEvents\WikimediaEventsHooks;

/**
 * @phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
 */
class Hooks implements
	EditPage__showEditForm_initialHook,
	EditPage__showEditForm_fieldsHook,
	GetPreferencesHook,
	EditPage__attemptSaveHook,
	EditPage__attemptSave_afterHook,
	EditPageGetPreviewContentHook,
	ListDefinedTagsHook,
	ChangeTagsListActiveHook,
	RecentChange_saveHook
{

	/** @var string|bool ID used for grouping entries all of a session's entries together in EventLogging. */
	private static $statsId = false;

	/** @var string[] */
	private static $tags = [ 'wikieditor' ];

	private Config $config;
	private UserEditTracker $userEditTracker;
	private UserOptionsLookup $userOptionsLookup;
	private ?MobileContext $mobileContext;

	public function __construct(
		Config $config,
		UserEditTracker $userEditTracker,
		UserOptionsLookup $userOptionsLookup,
		?MobileContext $mobileContext
	) {
		$this->config = $config;
		$this->userEditTracker = $userEditTracker;
		$this->userOptionsLookup = $userOptionsLookup;
		$this->mobileContext = $mobileContext;
	}

	/**
	 * Should the current session be sampled for EventLogging?
	 *
	 * @param string $sessionId
	 * @return bool Whether to sample the session
	 */
	protected function inEventSample( string $sessionId ): bool {
		// Sample 6.25%
		$samplingRate = $this->config->has( 'WMESchemaEditAttemptStepSamplingRate' ) ?
			$this->config->get( 'WMESchemaEditAttemptStepSamplingRate' ) : 0.0625;

		// (T314896) Convert whatever we've been given to a string of hex, as that's what EL needs
		$hexValue = hash( 'md5', $sessionId, false );

		$inSample = EventLogging::sessionInSample(
			(int)( 1 / $samplingRate ), $hexValue
		);
		return $inSample;
	}

	/**
	 * Log stuff to the eventlogging_EditAttemptStep stream in a shape that conforms to the
	 * analytics/legacy/editattemptstep schema.
	 *
	 * If the EventLogging extension is not loaded, then this is a NOP.
	 *
	 * @see https://meta.wikimedia.org/wiki/Schema:EditAttemptStep
	 *
	 * @param string $action
	 * @param Article $article Which article (with full context, page, title, etc.)
	 * @param array $data Data to log for this action
	 * @return void
	 */
	public function doEventLogging(
		string $action,
		Article $article,
		array $data = []
	): void {
		if ( defined( 'MW_PHPUNIT_TEST' ) ) {
			return;
		}

		$extensionRegistry = ExtensionRegistry::getInstance();
		if ( !$extensionRegistry->isLoaded( 'EventLogging' ) || !$extensionRegistry->isLoaded( 'WikimediaEvents' ) ) {
			return;
		}
		if ( $extensionRegistry->isLoaded( 'MobileFrontend' ) && $this->mobileContext ) {
			if ( $this->mobileContext->shouldDisplayMobileView() ) {
				// on a MobileFrontend page the logging should be handled by it
				return;
			}
		}
		$inSample = $this->inEventSample( $data['editing_session_id'] );
		$shouldOversample = WikimediaEventsHooks::shouldSchemaEditAttemptStepOversample( $article->getContext() );

		$user = $article->getContext()->getUser();
		$page = $article->getPage();
		$title = $article->getTitle();
		$revisionRecord = $page->getRevisionRecord();
		$skin = $article->getContext()->getSkin();

		$data = [
			'action' => $action,
			'version' => 1,
			'is_oversample' => !$inSample,
			'editor_interface' => 'wikitext',
			// @todo FIXME for other than 'desktop'. T249944
			'platform' => 'desktop',
			'integration' => 'page',
			'page_id' => $page->getId(),
			'page_title' => $title->getPrefixedText(),
			'page_ns' => $title->getNamespace(),
			'revision_id' => $revisionRecord ? $revisionRecord->getId() : 0,
			'user_id' => $user->getId(),
			'user_is_temp' => $user->isTemp(),
			'user_editcount' => $this->userEditTracker->getUserEditCount( $user ) ?: 0,
			'mw_version' => MW_VERSION,
			'skin' => $skin ? $skin->getSkinName() : null,
			'is_bot' => $user->isRegistered() && $user->isBot(),
			'is_anon' => $user->isAnon(),
			'wiki' => WikiMap::getCurrentWikiId(),
		] + $data;

		if ( $user->isAnon() ) {
			$data['user_class'] = 'IP';
		}

		if ( !$inSample && !$shouldOversample ) {
			return;
		}

		EventLogging::submit(
			'eventlogging_EditAttemptStep',
			[
				'$schema' => '/analytics/legacy/editattemptstep/2.0.2',
				'event' => $data,
			]
		);
	}

	/**
	 * Log stuff to EventLogging's Schema:VisualEditorFeatureUse -
	 * see https://meta.wikimedia.org/wiki/Schema:VisualEditorFeatureUse
	 * If you don't have EventLogging and WikimediaEvents installed, does nothing.
	 *
	 * @param string $feature
	 * @param string $action
	 * @param Article $article Which article (with full context, page, title, etc.)
	 * @param string $sessionId Session identifier
	 * @return bool Whether the event was logged or not.
	 */
	public function doVisualEditorFeatureUseLogging(
		string $feature,
		string $action,
		Article $article,
		string $sessionId
	): bool {
		$extensionRegistry = ExtensionRegistry::getInstance();
		if ( !$extensionRegistry->isLoaded( 'EventLogging' ) || !$extensionRegistry->isLoaded( 'WikimediaEvents' ) ) {
			return false;
		}
		$inSample = $this->inEventSample( $sessionId );
		$shouldOversample = WikimediaEventsHooks::shouldSchemaEditAttemptStepOversample( $article->getContext() );
		if ( !$inSample && !$shouldOversample ) {
			return false;
		}

		$user = $article->getContext()->getUser();
		$editCount = $this->userEditTracker->getUserEditCount( $user );
		$data = [
			'feature' => $feature,
			'action' => $action,
			'editingSessionId' => $sessionId,
			// @todo FIXME for other than 'desktop'. T249944
			'platform' => 'desktop',
			'integration' => 'page',
			'editor_interface' => 'wikitext',
			'user_id' => $user->getId(),
			'user_is_temp' => $user->isTemp(),
			'user_editcount' => $editCount ?: 0,
		];

		// NOTE: The 'VisualEditorFeatureUse' event was migrated to the Event Platform and is no
		//  longer using the legacy EventLogging schema from metawiki. $revId is actually
		//  overridden by the EventLoggingSchemas extension attribute in
		//  WikimediaEvents/extension.json.
		return EventLogging::logEvent( 'VisualEditorFeatureUse', -1, $data );
	}

	/**
	 * EditPage::showEditForm:initial hook
	 *
	 * Adds the modules to the edit form
	 *
	 * @param EditPage $editPage the current EditPage object.
	 * @param OutputPage $outputPage object.
	 */
	public function onEditPage__showEditForm_initial( $editPage, $outputPage ) {
		if ( $editPage->contentModel !== CONTENT_MODEL_WIKITEXT ) {
			return;
		}

		$article = $editPage->getArticle();
		$request = $article->getContext()->getRequest();

		// Add modules if enabled
		$user = $article->getContext()->getUser();
		if ( $this->userOptionsLookup->getBoolOption( $user, 'usebetatoolbar' ) ) {
			$outputPage->addModuleStyles( 'ext.wikiEditor.styles' );
			$outputPage->addModules( 'ext.wikiEditor' );
			if ( $this->config->get( 'WikiEditorRealtimePreview' ) ) {
				$outputPage->addModules( 'ext.wikiEditor.realtimepreview' );
			}
		}

		// Don't run this if the request was posted - we don't want to log 'init' when the
		// user just pressed 'Show preview' or 'Show changes', or switched from VE keeping
		// changes.
		if ( ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) && !$request->wasPosted() ) {
			$data = [];
			$data['editing_session_id'] = self::getEditingStatsId( $request );
			$section = $request->getRawVal( 'section' );
			if ( $section !== null ) {
				$data['init_type'] = 'section';
			} else {
				$data['init_type'] = 'page';
			}
			if ( $request->getHeader( 'Referer' ) ) {
				if (
					$section === 'new'
					|| !$article->getPage()->exists()
				) {
					$data['init_mechanism'] = 'new';
				} else {
					$data['init_mechanism'] = 'click';
				}
			} else {
				if (
					$section === 'new'
					|| !$article->getPage()->exists()
				) {
					$data['init_mechanism'] = 'url-new';
				} else {
					$data['init_mechanism'] = 'url';
				}
			}
			if ( $request->getRawVal( 'wvprov' ) === 'sticky-header' ) {
				$data['init_mechanism'] .= '-sticky-header';
			}

			$this->doEventLogging( 'init', $article, $data );
		}
	}

	/**
	 * EditPage::showEditForm:fields hook
	 *
	 * Adds the event fields to the edit form
	 *
	 * @param EditPage $editPage the current EditPage object.
	 * @param OutputPage $outputPage object.
	 */
	public function onEditPage__showEditForm_fields( $editPage, $outputPage ) {
		$outputPage->addHTML(
			Html::hidden(
				'wikieditorUsed',
				'',
				[ 'id' => 'wikieditorUsed' ]
			)
		);

		if ( $editPage->contentModel !== CONTENT_MODEL_WIKITEXT
			|| !ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) {
			return;
		}

		$req = $outputPage->getRequest();
		$editingStatsId = self::getEditingStatsId( $req );

		$shouldOversample = ExtensionRegistry::getInstance()->isLoaded( 'WikimediaEvents' ) &&
			WikimediaEventsHooks::shouldSchemaEditAttemptStepOversample( $outputPage->getContext() );

		$outputPage->addHTML(
			Html::hidden(
				'editingStatsId',
				$editingStatsId,
				[ 'id' => 'editingStatsId' ]
			)
		);

		if ( $shouldOversample ) {
			$outputPage->addHTML(
				Html::hidden(
					'editingStatsOversample',
					1,
					[ 'id' => 'editingStatsOversample' ]
				)
			);
		}
	}

	/**
	 * GetPreferences hook
	 *
	 * Adds WikiEditor-related items to the preferences
	 *
	 * @param User $user current user
	 * @param array &$defaultPreferences list of default user preference controls
	 */
	public function onGetPreferences( $user, &$defaultPreferences ) {
		// Ideally this key would be 'wikieditor-toolbar'
		$defaultPreferences['usebetatoolbar'] = [
			'type' => 'toggle',
			'label-message' => 'wikieditor-toolbar-preference',
			'help-message' => 'wikieditor-toolbar-preference-help',
			'section' => 'editing/editor',
		];
		$defaultPreferences['wikieditor-realtimepreview'] = [
			'type' => 'api',
		];
	}

	/**
	 * @param RL\Context $context
	 * @param Config $config
	 * @return array
	 */
	public static function getModuleData( RL\Context $context, Config $config ): array {
		return [
			// expose magic words for use by the wikieditor toolbar
			'magicWords' => self::getMagicWords(),
			'signature' => self::getSignatureMessage( $context ),
			'realtimeDebounce' => $config->get( 'WikiEditorRealtimePreviewDebounce' ),
			'realtimeDisableDuration' => $config->get( 'WikiEditorRealtimeDisableDuration' ),
		];
	}

	/**
	 * @param RL\Context $context
	 * @param Config $config
	 * @return array
	 */
	public static function getModuleDataSummary( RL\Context $context, Config $config ): array {
		return [
			'magicWords' => self::getMagicWords(),
			'signature' => self::getSignatureMessage( $context, true ),
			'realtimeDebounce' => $config->get( 'WikiEditorRealtimePreviewDebounce' ),
			'realtimeDisableDuration' => $config->get( 'WikiEditorRealtimeDisableDuration' ),
		];
	}

	/**
	 * @param MessageLocalizer $ml
	 * @param bool $raw
	 * @return string
	 */
	private static function getSignatureMessage( MessageLocalizer $ml, bool $raw = false ): string {
		$msg = $ml->msg( 'sig-text' )->params( '~~~~' )->inContentLanguage();
		return $raw ? $msg->plain() : $msg->text();
	}

	/**
	 * Expose useful magic words which are used by the wikieditor toolbar
	 * @return string[][]
	 */
	private static function getMagicWords(): array {
		$requiredMagicWords = [
			'redirect',
			'img_alt',
			'img_right',
			'img_left',
			'img_none',
			'img_center',
			'img_thumbnail',
			'img_framed',
			'img_frameless',
		];
		$magicWords = [];
		$factory = MediaWikiServices::getInstance()->getMagicWordFactory();
		foreach ( $requiredMagicWords as $name ) {
			$magicWords[$name] = $factory->get( $name )->getSynonyms();
		}
		return $magicWords;
	}

	/**
	 * Gets a 32 character alphanumeric random string to be used for stats.
	 * @param WebRequest $request
	 * @return string
	 */
	private static function getEditingStatsId( WebRequest $request ): string {
		$fromRequest = $request->getRawVal( 'editingStatsId' );
		if ( $fromRequest !== null ) {
			return $fromRequest;
		}
		if ( !self::$statsId ) {
			self::$statsId = MWCryptRand::generateHex( 32 );
		}
		return self::$statsId;
	}

	/**
	 * This is attached to the MediaWiki 'EditPage::attemptSave' hook.
	 *
	 * @param EditPage $editPage
	 */
	public function onEditPage__attemptSave( $editPage ) {
		$article = $editPage->getArticle();
		$request = $article->getContext()->getRequest();
		$statsId = $request->getRawVal( 'editingStatsId' );
		if ( $statsId !== null ) {
			$this->doEventLogging(
				'saveAttempt',
				$article,
				[ 'editing_session_id' => $statsId ]
			);
		}
	}

	/**
	 * This is attached to the MediaWiki 'EditPage::attemptSave:after' hook.
	 *
	 * @param EditPage $editPage
	 * @param Status $status
	 * @param array $resultDetails
	 */
	public function onEditPage__attemptSave_after( $editPage, $status, $resultDetails ) {
		$article = $editPage->getArticle();
		$request = $article->getContext()->getRequest();
		$statsId = $request->getRawVal( 'editingStatsId' );
		if ( $statsId !== null ) {
			$data = [];
			$data['editing_session_id'] = $statsId;

			if ( $status->isOK() ) {
				$action = 'saveSuccess';

				if ( $request->getRawVal( 'wikieditorUsed' ) === 'yes' ) {
					$this->doVisualEditorFeatureUseLogging(
						'mwSave', 'source-has-js', $article, $statsId
					);
				}
			} else {
				$action = 'saveFailure';

				// Compare to ve.init.mw.ArticleTargetEvents.js in VisualEditor.
				$typeMap = [
					'badtoken' => 'userBadToken',
					'assertanonfailed' => 'userNewUser',
					'assertuserfailed' => 'userNewUser',
					'assertnameduserfailed' => 'userNewUser',
					'abusefilter-disallowed' => 'extensionAbuseFilter',
					'abusefilter-warning' => 'extensionAbuseFilter',
					'captcha' => 'extensionCaptcha',
					'spamblacklist' => 'extensionSpamBlacklist',
					'titleblacklist-forbidden' => 'extensionTitleBlacklist',
					'pagedeleted' => 'editPageDeleted',
					'editconflict' => 'editConflict'
				];

				$errors = $status->getErrorsArray();
				// Replicate how the API generates error codes, in order to log data that is consistent with
				// all other tools (which save changes via the API)
				if ( isset( $errors[0] ) ) {
					$code = ApiMessage::create( $errors[0] )->getApiCode();
				} else {
					$code = 'unknown';
				}

				$wikiPage = $editPage->getArticle()->getPage();

				if ( ExtensionRegistry::getInstance()->isLoaded( 'ConfirmEdit' ) ) {
					$key = CacheKeyHelper::getKeyForPage( $wikiPage );
					/** @var SimpleCaptcha $captcha */
					$captcha = ConfirmEditHooks::getInstance();
					$activatedCaptchas = $captcha->getActivatedCaptchas();
					if ( isset( $activatedCaptchas[$key] ) ) {
						// TODO: :(
						$code = 'captcha';
					}
				}

				$data['save_failure_message'] = $code;
				$data['save_failure_type'] = $typeMap[ $code ] ?? 'responseUnknown';
			}

			$this->doEventLogging( $action, $article, $data );
		}
	}

	/**
	 * Log a 'preview-nonlive' action when a page is previewed via the non-ajax full-page preview.
	 *
	 * @param EditPage $editPage
	 * @param Content &$content Content object to be previewed (may be replaced by hook function)
	 * @return bool|void True or no return value to continue or false to abort
	 */
	public function onEditPageGetPreviewContent( $editPage, &$content ) {
		// This hook is only called for non-live previews, so we don't need to check the uselivepreview user option.
		$editingStatsId = $editPage->getContext()->getRequest()->getRawVal( 'editingStatsId' );
		if ( $editingStatsId !== null ) {
			$article = $editPage->getArticle();
			$this->doVisualEditorFeatureUseLogging( 'preview', 'preview-nonlive', $article, $editingStatsId );
		}
	}

	/**
	 * @param string[] &$tags
	 * @return bool|void
	 */
	public function onChangeTagsListActive( &$tags ) {
		$this->registerTags( $tags );
	}

	/**
	 * @param string[] &$tags
	 * @return bool|void
	 */
	public function onListDefinedTags( &$tags ) {
		$this->registerTags( $tags );
	}

	/**
	 * @param string[] &$tags
	 */
	protected function registerTags( array &$tags ): void {
		$tags = array_merge( $tags, static::$tags );
	}

	/**
	 * @param RecentChange $recentChange
	 * @return bool|void
	 */
	public function onRecentChange_save( $recentChange ) {
		$request = RequestContext::getMain()->getRequest();
		if ( $request->getRawVal( 'wikieditorUsed' ) === 'yes' ) {
			$recentChange->addTags( 'wikieditor' );
		}
	}
}