Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConvertLqtPageFromRemoteApiForTesting
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace Flow\Maintenance;
4
5use Flow\Container;
6use Flow\Exception\FlowException;
7use Flow\Import\LiquidThreadsApi\ImportSource;
8use Flow\Import\LiquidThreadsApi\RemoteApiBackend;
9use Flow\Import\SourceStore\FileImportSourceStore;
10use Flow\OccupationController;
11use MediaWiki\Maintenance\Maintenance;
12use MediaWiki\Title\Title;
13use Psr\Log\LogLevel;
14
15$IP = getenv( 'MW_INSTALL_PATH' );
16if ( $IP === false ) {
17    $IP = __DIR__ . '/../../..';
18}
19
20require_once "$IP/maintenance/Maintenance.php";
21
22/**
23 * This is *only* for use in testing, not production.  The primary purpose is to exercise
24 * the API (production also uses the API, but with FauxRequest) and Parsoid.
25 *
26 * This also does not test redirects or notification conversion.
27 */
28class ConvertLqtPageFromRemoteApiForTesting extends Maintenance {
29    public function __construct() {
30        parent::__construct();
31
32        // phpcs:disable Generic.Files.LineLength
33        $this->addDescription( 'Converts LiquidThreads data to Flow data.  Destination page is determined by ConversionStrategy' );
34        $this->addOption( 'dstpage', 'Page name of the destination page on the current wiki.  Defaults to same as source', false, true );
35        $this->addOption( 'srcpage', 'Page name of the source page to import from.', true, true );
36        $this->addOption( 'remoteapi', 'Remote API URL to read from', true, true );
37        $this->addOption( 'cacheremoteapidir', 'Cache remote api calls to the specified directory', true, true );
38        $this->addOption( 'logfile', 'File to read and store associations between imported items and their sources', true, true );
39        $this->addOption( 'debug', 'Include debug information to progress report' );
40        // phpcs:enable
41
42        $this->requireExtension( 'Flow' );
43    }
44
45    public function execute() {
46        $cacheDir = $this->getOption( 'cacheremoteapidir' );
47        if ( !is_dir( $cacheDir ) ) {
48            if ( !mkdir( $cacheDir ) ) {
49                throw new FlowException( 'Provided dir for caching remote api calls is not creatable.' );
50            }
51        }
52        if ( !is_writable( $cacheDir ) ) {
53            throw new FlowException( 'Provided dir for caching remote api calls is not writable.' );
54        }
55
56        $api = new RemoteApiBackend( $this->getOption( 'remoteapi' ), $cacheDir );
57
58        $importer = Container::get( 'importer' );
59        $importer->setAllowUnknownUsernames( true );
60
61        /** @var OccupationController $occupationController */
62        $occupationController = $this->getServiceContainer()->getService( 'FlowTalkpageManager' );
63        $talkPageManagerUser = $occupationController->getTalkpageManager();
64
65        $srcPageName = $this->getOption( 'srcpage' );
66        if ( $this->hasOption( 'dstpage' ) ) {
67            $dstPageName = $this->getOption( 'dstpage' );
68        } else {
69            $dstPageName = $srcPageName;
70        }
71
72        $dstTitle = Title::newFromText( $dstPageName );
73        $source = new ImportSource(
74            $api,
75            $srcPageName,
76            $talkPageManagerUser
77        );
78
79        $logFilename = $this->getOption( 'logfile' );
80        $sourceStore = new FileImportSourceStore( $logFilename );
81
82        $logger = new MaintenanceDebugLogger( $this );
83        if ( $this->getOption( 'debug' ) ) {
84            $logger->setMaximumLevel( LogLevel::DEBUG );
85        } else {
86            $logger->setMaximumLevel( LogLevel::INFO );
87        }
88
89        $importer->setLogger( $logger );
90        $api->setLogger( $logger );
91
92        $logger->info( "Starting LQT conversion of page $srcPageName" );
93
94        $importer->import( $source, $dstTitle, $talkPageManagerUser, $sourceStore );
95
96        $logger->info( "Finished LQT conversion of page $srcPageName" );
97    }
98}
99
100$maintClass = ConvertLqtPageFromRemoteApiForTesting::class;
101require_once RUN_MAINTENANCE_IF_MAIN;