Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.74% |
18 / 19 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
MorelikeBasedTopic | |
94.74% |
18 / 19 |
|
83.33% |
5 / 6 |
6.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getReferencePages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toJsonArray | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
newFromJsonArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\NewcomerTasks\Topic; |
4 | |
5 | use MediaWiki\Json\JsonUnserializer; |
6 | use MediaWiki\Language\RawMessage; |
7 | use MediaWiki\Linker\LinkTarget; |
8 | use MediaWiki\Message\Message; |
9 | use MediaWiki\Title\TitleValue; |
10 | use MessageLocalizer; |
11 | |
12 | /** |
13 | * A topic based on morelike search (text similarity with a predefined set of reference articles). |
14 | */ |
15 | class MorelikeBasedTopic extends Topic { |
16 | |
17 | /** @var LinkTarget[] */ |
18 | private $referencePages; |
19 | |
20 | /** @var string */ |
21 | private $name; |
22 | |
23 | /** |
24 | * @param string $id Topic ID, e.g. 'biology'. |
25 | * @param LinkTarget[] $referencePages The set of reference pages defining this topic. |
26 | */ |
27 | public function __construct( $id, array $referencePages ) { |
28 | parent::__construct( $id ); |
29 | $this->referencePages = $referencePages; |
30 | // If setName() is somehow not called, this is better than the RawMessage constructor |
31 | // throwing an error. |
32 | $this->name = $id; |
33 | } |
34 | |
35 | /** |
36 | * Return the set of reference pages defining this topic. These are "representative" articles |
37 | * about the topic, and articles will be classified into the given topic based on a text |
38 | * similarity metric. |
39 | * @return LinkTarget[] |
40 | */ |
41 | public function getReferencePages(): array { |
42 | return $this->referencePages; |
43 | } |
44 | |
45 | /** @inheritDoc */ |
46 | public function getName( MessageLocalizer $messageLocalizer ): Message { |
47 | // HACK we don't localize morelike-based topics because it will be replaced by ORES topics |
48 | // in production, the list of ORES topics is different, and we want to avoid wasting |
49 | // translator time. |
50 | // FIXME MessageLocalizer does not work with raw messages. The language does not matter |
51 | // for RawMessage, but we have to set something to avoid triggering session loading. |
52 | return ( new RawMessage( $this->name ) )->inLanguage( 'en' ); |
53 | } |
54 | |
55 | /** |
56 | * Hack for non-localizable topic names. |
57 | * @param string $name Topic name as raw text |
58 | */ |
59 | public function setName( $name ) { |
60 | $this->name = $name; |
61 | } |
62 | |
63 | /** @inheritDoc */ |
64 | protected function toJsonArray(): array { |
65 | return [ |
66 | 'id' => $this->getId(), |
67 | 'name' => $this->name, |
68 | 'referencePages' => array_map( static function ( LinkTarget $page ) { |
69 | return [ $page->getNamespace(), $page->getDBkey() ]; |
70 | }, $this->getReferencePages() ), |
71 | ]; |
72 | } |
73 | |
74 | /** @inheritDoc */ |
75 | public static function newFromJsonArray( JsonUnserializer $unserializer, array $json ) { |
76 | $referencePages = array_map( static function ( array $page ) { |
77 | return new TitleValue( $page[0], $page[1] ); |
78 | }, $json['referencePages'] ); |
79 | $topic = new MorelikeBasedTopic( $json['id'], $referencePages ); |
80 | $topic->setName( $json['name'] ); |
81 | return $topic; |
82 | } |
83 | |
84 | } |