Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
LinkRecommendationLink
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 10
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLinkTarget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMatchIndex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWikitextOffset
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getScore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getContextBefore
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getContextAfter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLinkIndex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\NewcomerTasks\AddLink;
4
5/**
6 * Represents an individual suggested link within a LinkRecommendation.
7 */
8class LinkRecommendationLink {
9
10    /** @var string */
11    private $linkText;
12    /** @var string */
13    private $linkTarget;
14    /** @var int */
15    private $matchIndex;
16    /** @var int */
17    private $wikitextOffset;
18    /** @var float */
19    private $score;
20    /** @var string */
21    private $contextBefore;
22    /** @var string */
23    private $contextAfter;
24    /** @var int */
25    private $linkIndex;
26
27    /**
28     * @param string $linkText The text fragment which would be linked (as plaintext).
29     *   This text is present and unlinked in the article revision that was used for generating
30     *   recommendations.
31     * @param string $linkTarget The title to link to, in any format that can be parsed by
32     *   TitleParser.
33     * @param int $matchIndex The 0-based index the link text within all matches of $text
34     *   in the article.
35     * @param int $wikitextOffset The 0-based index of the first character of the link text in the
36     *   wikitext, in Unicode characters.
37     * @param float $score The confidence score of the recommended link (a number between 0
38     *   and 1).
39     * @param string $contextBefore A few characters of text from the artcile right before the
40     *   text to be linked. Might be omitted (set to empty string) if the recommended link is
41     *   preceded by something that cannot easily be converted to plaintext (such as a template).
42     * @param string $contextAfter Like $contextBefore but the text is right after the link text.
43     * @param int $linkIndex 0-based position in the list of recommendations (in the order
44     *   they occur in the article).
45     */
46    public function __construct(
47        string $linkText,
48        string $linkTarget,
49        int $matchIndex,
50        int $wikitextOffset,
51        float $score,
52        string $contextBefore,
53        string $contextAfter,
54        int $linkIndex
55    ) {
56        $this->linkText = $linkText;
57        $this->linkTarget = $linkTarget;
58        $this->matchIndex = $matchIndex;
59        $this->wikitextOffset = $wikitextOffset;
60        $this->score = $score;
61        $this->contextBefore = $contextBefore;
62        $this->contextAfter = $contextAfter;
63        $this->linkIndex = $linkIndex;
64    }
65
66    /**
67     * The text fragment which would be linked (as plaintext). This text is present and unlinked
68     * in the article revision that was used for generating recommendations.
69     * @return string
70     */
71    public function getText(): string {
72        return $this->linkText;
73    }
74
75    /**
76     * The title to link to, in any format that can be parsed by TitleParser.
77     * @return string
78     */
79    public function getLinkTarget(): string {
80        return $this->linkTarget;
81    }
82
83    /**
84     * The 0-based index the link text within all matches of $text within the simple wikitext
85     * of the (top-level wikitext that's not part of any kind of wikitext construct). This is
86     * roughly equivalent to the match index in the text of top-level (within a `<section>`)
87     * `<p>` nodes in Parsoid HTML.
88     * @return int
89     */
90    public function getMatchIndex(): int {
91        return $this->matchIndex;
92    }
93
94    /**
95     * The 0-based index of the first character of the link text in the wikitext,
96     * in Unicode characters.
97     * @return int
98     */
99    public function getWikitextOffset(): int {
100        return $this->wikitextOffset;
101    }
102
103    /**
104     * The confidence score of the recommended link (a number between 0 and 1).
105     * @return float
106     */
107    public function getScore(): float {
108        return $this->score;
109    }
110
111    /**
112     * A few characters of text from the article right before the text to be linked. Might
113     * be omitted (set to empty string) if the recommended link is preceded by something
114     * that cannot easily be converted to plaintext (such as a template).
115     * @return string
116     */
117    public function getContextBefore(): string {
118        return $this->contextBefore;
119    }
120
121    /**
122     * A few characters of text from the article right after the text to be linked. Might
123     * be omitted (set to empty string) if the recommended link is followed by something
124     * that cannot easily be converted to plaintext (such as a template).
125     * @return string
126     */
127    public function getContextAfter(): string {
128        return $this->contextAfter;
129    }
130
131    /**
132     * 0-based position in the list of recommendations (in the order they occur in the article).
133     * @return int
134     */
135    public function getLinkIndex(): int {
136        return $this->linkIndex;
137    }
138
139    /**
140     * @return array
141     */
142    public function toArray(): array {
143        return [
144            'link_text' => $this->linkText,
145            'link_target' => $this->linkTarget,
146            'match_index' => $this->matchIndex,
147            'wikitext_offset' => $this->wikitextOffset,
148            'score' => $this->score,
149            'context_before' => $this->contextBefore,
150            'context_after' => $this->contextAfter,
151            'link_index' => $this->linkIndex,
152        ];
153    }
154
155}