Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialEditGrowthConfigLogger
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 doLogView
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 doLogSave
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 logAction
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace GrowthExperiments\EventLogging;
4
5use ExtensionRegistry;
6use GrowthExperiments\Specials\SpecialEditGrowthConfig;
7use InvalidArgumentException;
8use MediaWiki\Deferred\DeferredUpdates;
9use MediaWiki\Extension\EventLogging\EventLogging;
10use MediaWiki\Permissions\Authority;
11use MediaWiki\WikiMap\WikiMap;
12
13class SpecialEditGrowthConfigLogger {
14
15    /** @var string Versioned schema URL for $schema field */
16    private const SCHEMA_VERSIONED = '/analytics/mediawiki/editgrowthconfig/1.0.2';
17
18    /** @var string Stream name for EventLogging::submit */
19    private const STREAM = 'mediawiki.editgrowthconfig';
20
21    /** @var string */
22    public const ACTION_VIEW = 'view';
23
24    /** @var string */
25    public const ACTION_SAVE = 'save';
26
27    /**
28     * Log an `view` event
29     *
30     * @param Authority $authority
31     */
32    private function doLogView(
33        Authority $authority
34    ): void {
35        EventLogging::submit(
36            self::STREAM,
37            [
38                '$schema' => self::SCHEMA_VERSIONED,
39                'database' => WikiMap::getCurrentWikiId(),
40                'action' => self::ACTION_VIEW,
41                'is_privileged_user' => $authority->isAllowed(
42                    SpecialEditGrowthConfig::REQUIRED_RIGHT_TO_WRITE
43                ),
44                'is_registered_user' => $authority->isNamed(),
45            ]
46        );
47    }
48
49    /**
50     * Log a `save` interaction
51     *
52     * @param Authority $authority
53     */
54    private function doLogSave(
55        Authority $authority
56    ): void {
57        EventLogging::submit(
58            self::STREAM,
59            [
60                '$schema' => self::SCHEMA_VERSIONED,
61                'database' => WikiMap::getCurrentWikiId(),
62                'action' => self::ACTION_SAVE,
63                'is_privileged_user' => $authority->isAllowed(
64                    SpecialEditGrowthConfig::REQUIRED_RIGHT_TO_WRITE
65                ),
66                'is_registered_user' => $authority->isNamed(),
67                'performer' => [
68                    'user_id' => $authority->getUser()->getId(),
69                    'user_text' => $authority->getUser()->getName(),
70                ],
71            ]
72        );
73    }
74
75    /**
76     * If EventLogging is enabled, log an SpecialEditGrowthConfig-related action
77     *
78     * @param string $action One of ACTION_* constants
79     * @param Authority $authority
80     */
81    public function logAction(
82        string $action,
83        Authority $authority
84    ): void {
85        if ( ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) {
86            DeferredUpdates::addCallableUpdate( function () use ( $action, $authority ) {
87                switch ( $action ) {
88                    case self::ACTION_VIEW:
89                        $this->doLogView( $authority );
90                        break;
91                    case self::ACTION_SAVE:
92                        $this->doLogSave( $authority );
93                        break;
94                    default:
95                        throw new InvalidArgumentException( 'Unsupported value passed as $action' );
96                }
97            } );
98        }
99    }
100}