/
WikimediaEventsHooks.php
598 lines (540 loc) · 18.4 KB
/
WikimediaEventsHooks.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
591
592
593
594
595
596
597
598
<?php
// phpcs:disable MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName
namespace WikimediaEvents;
use Article;
use ExtensionRegistry;
use IContextSource;
use ISearchResultSet;
use MediaWiki\Actions\ActionEntryPoint;
use MediaWiki\ChangeTags\Hook\ChangeTagsListActiveHook;
use MediaWiki\ChangeTags\Hook\ListDefinedTagsHook;
use MediaWiki\Config\Config;
use MediaWiki\Deferred\DeferredUpdates;
use MediaWiki\Hook\BeforeInitializeHook;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Hook\MakeGlobalVariablesScriptHook;
use MediaWiki\Hook\RecentChange_saveHook;
use MediaWiki\Hook\SpecialSearchGoResultHook;
use MediaWiki\Hook\SpecialSearchResultsHook;
use MediaWiki\MediaWikiServices;
use MediaWiki\Output\OutputPage;
use MediaWiki\Page\Hook\ArticleViewHeaderHook;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Request\WebRequest;
use MediaWiki\ResourceLoader as RL;
use MediaWiki\ResourceLoader\Hook\ResourceLoaderRegisterModulesHook;
use MediaWiki\ResourceLoader\ResourceLoader;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Storage\EditResult;
use MediaWiki\Storage\Hook\PageSaveCompleteHook;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use MediaWiki\User\UserIdentity;
use MediaWiki\WikiMap\WikiMap;
use MobileContext;
use RecentChange;
use RequestContext;
use Skin;
use WikimediaEvents\Hooks\HookRunner;
use WikiPage;
/**
* Hooks used for Wikimedia-related logging
*
* @author Ori Livneh <ori@wikimedia.org>
* @author Matthew Flaschen <mflaschen@wikimedia.org>
* @author Benny Situ <bsitu@wikimedia.org>
*/
class WikimediaEventsHooks implements
BeforeInitializeHook,
BeforePageDisplayHook,
PageSaveCompleteHook,
ArticleViewHeaderHook,
ListDefinedTagsHook,
ChangeTagsListActiveHook,
SpecialSearchGoResultHook,
SpecialSearchResultsHook,
RecentChange_saveHook,
ResourceLoaderRegisterModulesHook,
MakeGlobalVariablesScriptHook
{
/**
* @param OutputPage $out
* @param Skin $skin
*/
public function onBeforePageDisplay( $out, $skin ): void {
$out->addModules( 'ext.wikimediaEvents' );
if ( ExtensionRegistry::getInstance()->isLoaded( 'WikibaseRepository' ) ) {
// If we are in Wikibase Repo, load Wikibase module
$out->addModules( 'ext.wikimediaEvents.wikibase' );
}
}
/**
* On XAnalyticsSetHeader
*
* When adding new headers here please update the docs:
* https://wikitech.wikimedia.org/wiki/X-Analytics
*
* Insert a 'page_id' key with the page ID as value (if the request is for a page with a pageid)
* Insert a 'ns' key with the namespace ID as value (if the request is for a valid title)
* Insert a 'special' key with the resolved name of the special page (if the request is for a
* special page). If the name does not resolve, special is set to 'unknown' (see T304362).
*
* Add a 'loggedIn' key with the value of 1 if the user is logged in
* @param OutputPage $out
* @param array &$headerItems
*/
public static function onXAnalyticsSetHeader( OutputPage $out, array &$headerItems ): void {
$title = $out->getTitle();
if ( $title !== null && !defined( 'MW_API' ) ) {
$pageId = $title->getArticleID();
$headerItems['ns'] = $title->getNamespace();
if ( is_int( $pageId ) && $pageId > 0 ) {
$headerItems['page_id'] = $pageId;
}
if ( $title->isSpecialPage() ) {
[ $name, /* $subpage */ ] = MediaWikiServices::getInstance()->getSpecialPageFactory()
->resolveAlias( $title->getDBkey() );
$headerItems['special'] = $name ?? 'unknown';
}
$revId = $out->getRevisionId();
// The revision ID will be positive for page and diff views, as well
// as viewing old revisions.
if ( $revId > 0 ) {
$headerItems['rev_id'] = $revId;
}
}
if ( $out->getUser()->isRegistered() ) {
$headerItems['loggedIn'] = 1;
}
}
/**
* Log server-side event on successful page edit.
*
* Imported from EventLogging extension
*
* @param WikiPage $wikiPage
* @param UserIdentity $userIdentity
* @param string $summary
* @param int $flags
* @param RevisionRecord $revisionRecord
* @param EditResult $editResult
*
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageSaveComplete
*/
public function onPageSaveComplete(
$wikiPage,
$userIdentity,
$summary,
$flags,
$revisionRecord,
$editResult
): void {
if ( PHP_SAPI === 'cli' ) {
return; // ignore maintenance scripts
}
// Discard null edits from these metrics as they do not produce a
// saved an edit to a page, and thus notably differ in code execution.
// Note that null edits can still be relatively slow, as they do perform
// reparses.
if ( $editResult->isNullEdit() ) {
return;
}
$title = $wikiPage->getTitle();
$request = RequestContext::getMain()->getRequest();
$services = MediaWikiServices::getInstance();
$nsInfo = $services->getNamespaceInfo();
$permMgr = $services->getPermissionManager();
$user = User::newFromIdentity( $userIdentity );
$content = $wikiPage->getContent();
if (
$user->isBot() ||
( $request->getCheck( 'bot' ) && $permMgr->userHasRight( $user, 'bot' ) )
) {
$accType = 'bot'; // registered bot or script acting on behalf of a user
} elseif ( $request->getCheck( 'maxlag' ) ) {
$accType = 'throttled'; // probably an unregistered bot
} else {
$accType = 'normal';
}
if ( in_array( $content->getModel(), [ 'wikibase-item', 'wikibase-property' ] ) ) {
$nsType = 'entity';
} elseif ( $nsInfo->isContent( $title->getNamespace() ) ) {
$nsType = 'content';
} elseif ( $nsInfo->isTalk( $title->getNamespace() ) ) {
$nsType = 'talk';
} else {
$nsType = 'meta';
}
if ( MW_ENTRY_POINT === 'index' ) {
// non-AJAX submission from user interface
// (for non-WMF this could also mean jobrunner, since jobs run post-send
// from index.php by default)
$entry = 'index';
} elseif ( MW_ENTRY_POINT === 'api' || MW_ENTRY_POINT === 'rest' ) {
$entry = 'api';
} else {
// jobrunner, maint/cli
$entry = 'other';
}
$size = $content->getSize();
DeferredUpdates::addCallableUpdate(
static function () use ( $size, $nsType, $accType, $entry ) {
$reqCtxTiming = RequestContext::getMain()->getTiming();
$measure = $reqCtxTiming->measure(
'editResponseTime', 'requestStart', 'requestShutdown' );
if ( $measure === false ) {
return;
}
$timeMs = $measure['duration'] * 1000;
$statsFactory = MediaWikiServices::getInstance()->getStatsFactory()
->withComponent( 'WikimediaEvents' );
$statsFactory->getTiming( 'editResponseTime_seconds' )
->setLabel( 'page', $nsType )
->setLabel( 'user', $accType )
->setLabel( 'entry', $entry )
->copyToStatsdAt( [
"timing.editResponseTime",
"timing.editResponseTime.page.$nsType",
"timing.editResponseTime.user.$accType",
"timing.editResponseTime.entry.$entry",
] )->observe( $timeMs );
$msPerKb = $timeMs / ( max( $size, 1 ) / 1e3 ); // T224686
$statsFactory->getTiming( 'editResponseTimePerKB_seconds' )
->setLabel( 'page', $nsType )
->setLabel( 'user', $accType )
->setLabel( 'entry', $entry )
->copyToStatsdAt( [
"timing.editResponseTimePerKB.page.$nsType",
"timing.editResponseTimePerKB.user.$accType",
"timing.editResponseTimePerKB.entry.$entry",
] )->observe( $msPerKb );
}
);
}
/**
* Callback for ext.wikimediaEvents virtual config.json file.
*
* @param RL\Context $context
* @param Config $config
* @return array
*/
public static function getModuleConfig( RL\Context $context, Config $config ) {
$vars = [];
$vars['clientErrorIntakeURL'] = $config->get( 'WMEClientErrorIntakeURL' );
$vars['statsdBaseUri'] = $config->get( 'WMEStatsdBaseUri' );
$vars['wikidataCompletionSearchClicks'] = $config->get( 'WMEWikidataCompletionSearchClicks' );
$vars['sessionTick'] = $config->get( 'WMESessionTick' );
$vars['readingDepthSamplingRate'] = $config->get( 'WMEReadingDepthSamplingRate' );
$vars['newPHPSamplingRate'] = $config->get( 'WMENewPHPSamplingRate' );
$vars['newPHPVersion'] = $config->get( 'WMENewPHPVersion' );
$skin = $context->getSkin();
if ( $skin === 'minerva' ) {
$vars['mobileWebUIActionsTracking'] = $config->get( 'WMEMobileWebUIActionsTracking' );
} elseif ( in_array( $skin, [ 'vector', 'vector-2022' ] ) ) {
$vars['desktopWebUIActionsTracking'] = $config->get( 'WMEDesktopWebUIActionsTracking' );
$vars['desktopWebUIActionsTrackingOversampleLoggedInUsers'] =
$config->get( 'WMEDesktopWebUIActionsTrackingOversampleLoggedInUsers' );
$vars['webUIScrollTrackingSamplingRate'] = $config->get( 'WMEWebUIScrollTrackingSamplingRate' );
$vars['webUIScrollTrackingSamplingRateAnons'] = $config->get( 'WMEWebUIScrollTrackingSamplingRateAnons' );
$vars['webUIScrollTrackingTimeToWaitBeforeScrollUp'] =
$config->get( 'WMEWebUIScrollTrackingTimeToWaitBeforeScrollUp' );
}
// editAttemptStep.js
$vars['WMESchemaEditAttemptStepSamplingRate'] =
$config->get( 'WMESchemaEditAttemptStepSamplingRate' );
$vars['WMESchemaVisualEditorFeatureUseSamplingRate'] =
$config->get( 'WMESchemaVisualEditorFeatureUseSamplingRate' );
$vars['DTSchemaEditAttemptStepSamplingRate'] =
$config->get( 'DTSchemaEditAttemptStepSamplingRate' );
$vars['DTSchemaEditAttemptStepOversample'] =
$config->get( 'DTSchemaEditAttemptStepOversample' );
$vars['MFSchemaEditAttemptStepOversample'] =
$config->get( 'MFSchemaEditAttemptStepOversample' );
return $vars;
}
/**
* Callback for dynamic source files, for conditional loading based on the current skin.
*
* @param RL\Context $context
* @param Config $config
* @param string $param callback param - corresponds to the file name to conditionally load
* @return RL\FilePath|string
*/
public static function getModuleFile( RL\Context $context, Config $config, $param ) {
$skin = $context->getSkin();
switch ( $skin ) {
case 'vector':
case 'vector-2022':
switch ( $param ) {
case 'clickTracking/desktop':
case 'searchSatisfaction':
case 'searchSli':
case 'universalLanguageSelector':
case 'webUIScroll':
return new RL\FilePath( $param . '.js' );
default:
return '';
}
case 'minerva':
switch ( $param ) {
case 'clickTracking/mobile':
return new RL\FilePath( $param . '.js' );
default:
return '';
}
default:
switch ( $param ) {
case 'searchSatisfaction':
case 'searchSli':
return new RL\FilePath( $param . '.js' );
default:
return '';
}
}
}
/**
* Register change tags.
*
* @param array &$tags
*/
public function onListDefinedTags( &$tags ): void {
if ( WikiMap::getCurrentWikiId() === 'commonswiki' ) {
$tags[] = 'cross-wiki-upload';
// For A/B test
$tags[] = 'cross-wiki-upload-1';
$tags[] = 'cross-wiki-upload-2';
$tags[] = 'cross-wiki-upload-3';
$tags[] = 'cross-wiki-upload-4';
}
}
/**
* Mark active change tags.
*
* @param array &$tags
*/
public function onChangeTagsListActive( &$tags ): void {
if ( WikiMap::getCurrentWikiId() === 'commonswiki' ) {
$tags[] = 'cross-wiki-upload';
// For A/B test
$tags[] = 'cross-wiki-upload-1';
$tags[] = 'cross-wiki-upload-2';
$tags[] = 'cross-wiki-upload-3';
$tags[] = 'cross-wiki-upload-4';
}
}
/**
* @param string $term
* @param Title $title
* @param string|null &$url
*/
public function onSpecialSearchGoResult( $term, $title, &$url ): void {
$request = RequestContext::getMain()->getRequest();
$wprov = $request->getRawVal( 'wprov' );
if ( $wprov ) {
$url = $title->getFullURL( [ 'wprov' => $wprov ] );
}
}
/**
* The javascript that records search metrics needs to know if it is on a
* SERP or not. This ends up being non-trivial due to localization, so
* make it trivial by injecting a boolean value to check.
*
* @param string $term
* @param ?ISearchResultSet &$titleMatches
* @param ?ISearchResultSet &$textMatches
*/
public function onSpecialSearchResults( $term, &$titleMatches, &$textMatches ): void {
$out = RequestContext::getMain()->getOutput();
$out->addJsConfigVars( [
'wgIsSearchResultPage' => true,
] );
}
/**
* @param RecentChange $rc
*/
public function onRecentChange_save( $rc ): void {
self::onRecentChangeSaveCrossWikiUpload( $rc );
self::onRecentChangeSaveEditCampaign( $rc );
}
/**
* Add a change tag 'cross-wiki-upload' to cross-wiki uploads to Commons, to track usage of the
* new feature. (Both to track adoption, and to let Commons editors review the uploads.) (T115328)
*
* @param RecentChange $rc
*/
public static function onRecentChangeSaveCrossWikiUpload( RecentChange $rc ): void {
if ( !defined( 'MW_API' ) || WikiMap::getCurrentWikiId() !== 'commonswiki' ) {
return;
}
if (
$rc->getAttribute( 'rc_log_type' ) !== 'upload' ||
$rc->getAttribute( 'rc_log_action' ) !== 'upload'
) {
return;
}
$request = RequestContext::getMain()->getRequest();
if ( !$request->response()->getHeader( 'Access-Control-Allow-Origin' ) ) {
return;
}
// A/B test
$bucket = $request->getRawVal( 'bucket' );
if ( !in_array( $bucket, [ '1', '2', '3', '4' ] ) ) {
$bucket = null;
}
$tags = [ 'cross-wiki-upload' ];
if ( $bucket ) {
$tags[] = "cross-wiki-upload-$bucket";
}
$rc->addTags( $tags );
}
/**
* Add a change tag 'campaign-...' to edits made via edit campaigns (identified by an URL
* parameter passed to the API via VisualEditor). (T209132)
*
* @param RecentChange $rc
*/
public static function onRecentChangeSaveEditCampaign( RecentChange $rc ): void {
global $wgWMEEditCampaigns;
if ( !defined( 'MW_API' ) ) {
return;
}
$request = RequestContext::getMain()->getRequest();
$campaign = $request->getRawVal( 'campaign' );
if ( !in_array( $campaign, $wgWMEEditCampaigns ) ) {
return;
}
$tags = [ "campaign-$campaign" ];
$rc->addTags( $tags );
}
/**
* @param ResourceLoader $rl
*/
public function onResourceLoaderRegisterModules( ResourceLoader $rl ): void {
if ( !ExtensionRegistry::getInstance()->isLoaded( 'VisualEditor' ) ) {
return;
}
$dir = dirname( __DIR__ ) . DIRECTORY_SEPARATOR;
$rl->register( "ext.wikimediaEvents.visualEditor", [
'localBasePath' => $dir . 'modules',
'remoteExtPath' => 'WikimediaEvents/modules',
"scripts" => "ext.wikimediaEvents.visualEditor/campaigns.js",
"dependencies" => "ext.visualEditor.targetLoader",
] );
}
/**
* @param Article $article
* @param bool|ParserOutput|null &$outputDone
* @param bool &$pcache
*/
public function onArticleViewHeader( $article, &$outputDone, &$pcache ) {
DeferredUpdates::addCallableUpdate( static function () {
$context = RequestContext::getMain();
$timing = $context->getTiming();
if ( ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' )
&& MobileContext::singleton()->shouldDisplayMobileView()
) {
$platform = 'mobile';
} else {
$platform = 'desktop';
}
$measure = $timing->measure( 'viewResponseTime', 'requestStart', 'requestShutdown' );
if ( $measure !== false ) {
MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
"timing.viewResponseTime.{$platform}", $measure['duration'] * 1000 );
}
} );
}
/**
* @param array &$vars
* @param OutputPage $out
*/
public function onMakeGlobalVariablesScript( &$vars, $out ): void {
$vars['wgWMESchemaEditAttemptStepOversample'] =
static::shouldSchemaEditAttemptStepOversample( $out->getContext() );
// Set page length for reading depth instrument T294777.
$length = $out->getTitle()->getLength();
$log = log10( $length );
$vars[ 'wgWMEPageLength' ] = round( $length, -intval( $log ) );
}
/**
* @param IContextSource $context
* @return bool
*/
public static function shouldSchemaEditAttemptStepOversample( IContextSource $context ) {
// The editingStatsOversample request parameter can trigger oversampling
$shouldOversample = $context->getRequest()->getBool( 'editingStatsOversample' );
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
->onWikimediaEventsShouldSchemaEditAttemptStepOversample( $context, $shouldOversample );
return $shouldOversample;
}
/**
* WMDE runs banner campaigns to encourage users to create an account and edit.
*
* The tracking already implemented in the Campaigns extension doesn't quite cover the WMDE
* use case. WMDE has a landing page that must be shown before the user progresses to
* registration. This could one day be factored out into its own extension, or made
* part of the Campaigns extension.
*
* Task for moving this to the Campaigns extension:
* https://phabricator.wikimedia.org/T174939
*
* Active WMDE campaigns tracked at:
* https://phabricator.wikimedia.org/project/subprojects/2821/
*
* @author addshore on behalf of WMDE
*
* @param Title $title
* @param mixed $unused
* @param OutputPage $output
* @param User $user
* @param WebRequest $request
* @param ActionEntryPoint $mediaWiki
*/
public function onBeforeInitialize(
$title,
$unused,
$output,
$user,
$request,
$mediaWiki
): void {
// Only run for dewiki
if ( WikiMap::getCurrentWikiId() !== 'dewiki' ) {
return;
}
// Setup the campaign prefix.
// Everything below this block is agnostic to which campaign is being run.
$campaignPrefix = 'WMDE_';
$cookieName = 'wmdecampaign-' . $campaignPrefix;
$hasCampaignCookie = $request->getCookie( $cookieName ) !== null;
$hasCampaignQuery = strpos( $request->getRawVal( 'campaign' ), $campaignPrefix ) === 0;
// Get the campaign name from either the cookie or query param
// Cookie has precedence
if ( $hasCampaignCookie ) {
$campaign = $request->getCookie( $cookieName );
} elseif ( $hasCampaignQuery ) {
$campaign = $request->getRawVal( 'campaign' );
} else {
// Request has nothing to do with our campaign
return;
}
// If an anon user clicks on the banner and doesn't yet have a session cookie then
// add a session cookie and log the click.
if ( !$hasCampaignCookie && $hasCampaignQuery && !$user->isRegistered() ) {
$request->response()->setCookie( $cookieName, $campaign, null );
wfDebugLog( 'WMDE', "$campaign - 1 - Banner click by anon user without cookie" );
}
// If an anon user with the cookie, views the create account page without a campaign
// query param, then inject it into the WebRequest object to influence the Campaigns
// extension.
if (
!$hasCampaignQuery &&
$hasCampaignCookie &&
!$user->isRegistered() &&
$title->isSpecial( 'CreateAccount' )
) {
$request->setVal( 'campaign', $campaign );
wfDebugLog( 'WMDE', "$campaign - 2 - Inject campaign value on CreateAccount" );
}
}
}