Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
InsertLinkRecommendation
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 execute
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace GrowthExperiments\Maintenance;
4
5use GrowthExperiments\GrowthExperimentsServices;
6use GrowthExperiments\NewcomerTasks\AddLink\LinkRecommendation;
7use Maintenance;
8use MediaWiki\MediaWikiServices;
9
10$IP = getenv( 'MW_INSTALL_PATH' );
11if ( $IP === false ) {
12    $IP = __DIR__ . '/../../..';
13}
14require_once "$IP/maintenance/Maintenance.php";
15
16class InsertLinkRecommendation extends Maintenance {
17
18    public function __construct() {
19        parent::__construct();
20        $this->requireExtension( 'GrowthExperiments' );
21        $this->addDescription(
22            'Insert a link recommendation from JSON file for a particular page. For testing and local development.'
23        );
24        $this->addOption( 'title', 'The title to insert a link recommendation for', true );
25        $this->addOption( 'json-file', 'The JSON file containing the link recommendation data', true );
26    }
27
28    public function init() {
29        $services = MediaWikiServices::getInstance();
30        $growthServices = GrowthExperimentsServices::wrap( $services );
31        if ( !$growthServices->getGrowthConfig()->get( 'GEDeveloperSetup' ) ) {
32            $this->fatalError( 'This script cannot be safely run in production.' );
33        }
34    }
35
36    public function execute() {
37        $this->init();
38        $services = MediaWikiServices::getInstance();
39        $growthServices = GrowthExperimentsServices::wrap( $services );
40        $json = file_get_contents( $this->getOption( 'json-file' ) );
41        $data = json_decode( $json, true );
42        if ( !$data ) {
43            $this->fatalError( 'Unable to decode JSON' );
44        }
45        $title = $services->getTitleFactory()->newFromText( $this->getOption( 'title' ) );
46        if ( !$title ) {
47            $this->fatalError( 'Unable to get a title for ' . $this->getOption( 'title' ) );
48        }
49        $linkRecommendation = new LinkRecommendation(
50            $title,
51            $title->getId(),
52            $title->getLatestRevID(),
53            LinkRecommendation::getLinksFromArray( $data['links'] ),
54            LinkRecommendation::getMetadataFromArray( $data['meta'] )
55        );
56        $linkRecommendationStore = $growthServices->getLinkRecommendationStore();
57        $linkRecommendationStore->insert( $linkRecommendation );
58    }
59}
60
61$maintClass = InsertLinkRecommendation::class;
62require_once RUN_MAINTENANCE_IF_MAIN;