Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
WebABTestArticleIdStrategy
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getBucket
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace WikimediaEvents\WebABTest;
4
5use MediaWiki\Request\WebRequest;
6use MediaWiki\Title\Title;
7use WikimediaEvents\PageSplitter\PageHashGenerate;
8use WikimediaEvents\PageSplitter\PageSplitterInstrumentation;
9
10/**
11 * Integrates with PageSplitterInstrumentation and PageHashGenerate to sample
12 * and bucket based on article id. Bucket can be overriden with a query param
13 * via the $overrideName param.
14 */
15final class WebABTestArticleIdStrategy {
16    public const EXCLUDED_BUCKET_NAME = 'unsampled';
17
18    /**
19     * @var string[]
20     */
21    private $buckets;
22
23    /**
24     * @var Title
25     */
26    private $title;
27
28    /**
29     * @var WebRequest
30     */
31    private $request;
32
33    /**
34     * @var string
35     */
36    private $overrideName;
37
38    /**
39     * @var PageSplitterInstrumentation
40     */
41    private $pageSplitterInstrumentation;
42
43    /**
44     * @var PageHashGenerate
45     */
46    private $PageHashGenerate;
47
48    /**
49     * @param string[] $buckets An array of bucket name strings. E.g., ['control',
50     * 'treatment']. If the query param override is suppplied, the returned bucket
51     * will be the index of the buckets array.
52     * @param Title $title
53     * @param WebRequest $request
54     * @param string $overrideName
55     * @param PageSplitterInstrumentation $pageSplitterInstrumentation
56     * @param PageHashGenerate $PageHashGenerate
57     */
58    public function __construct(
59        array $buckets,
60        Title $title,
61        WebRequest $request,
62        string $overrideName,
63        PageSplitterInstrumentation $pageSplitterInstrumentation,
64        PageHashGenerate $PageHashGenerate
65    ) {
66        $this->buckets = $buckets;
67        $this->title = $title;
68        $this->request = $request;
69        $this->overrideName = $overrideName;
70        $this->pageSplitterInstrumentation = $pageSplitterInstrumentation;
71        $this->PageHashGenerate = $PageHashGenerate;
72    }
73
74    /**
75     * @return string|null Bucket name or null if a bucket can't be determined.
76     */
77    public function getBucket(): ?string {
78        // Check if query param exists first.
79        if ( $this->request->getCheck( $this->overrideName ) ) {
80            $queryParam = $this->request->getBool( $this->overrideName );
81
82            return $this->buckets[ (int)$queryParam ] ?? null;
83        }
84
85        $pageHash = $this->PageHashGenerate->getPageHash( $this->title->getArticleID() );
86
87        if ( !$this->pageSplitterInstrumentation->isSampled( $pageHash ) ) {
88            return self::EXCLUDED_BUCKET_NAME;
89        }
90
91        return $this->pageSplitterInstrumentation->getBucket( $pageHash );
92    }
93}