Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConvertNamespaceFromWikitext
0.00% covered (danger)
0.00%
0 / 62
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 / 18
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace Flow\Maintenance;
4
5use Flow\Container;
6use Flow\Import\Converter;
7use Flow\Import\SourceStore\NullImportSourceStore;
8use Flow\Import\Wikitext\ConversionStrategy;
9use Flow\OccupationController;
10use Flow\Utils\NamespaceIterator;
11use MediaWiki\Maintenance\Maintenance;
12use MediaWiki\Title\Title;
13
14$IP = getenv( 'MW_INSTALL_PATH' );
15if ( $IP === false ) {
16    $IP = __DIR__ . '/../../..';
17}
18
19require_once "$IP/maintenance/Maintenance.php";
20
21/**
22 * Converts a single namespace from wikitext talk pages to flow talk pages.  Does not
23 * modify LiquidThreads pages it comes across; use convertLqtPagesWithProp.php for that.  Does not
24 * modify sub-pages (except talk subpages with a corresponding subject page).
25 */
26class ConvertNamespaceFromWikitext extends Maintenance {
27    public function __construct() {
28        parent::__construct();
29        $this->addDescription( "Converts a single namespace of wikitext talk pages to Flow" );
30        $this->addArg( 'namespaceName', 'Name of the namespace to convert' );
31        $this->addOption(
32            'no-convert-templates',
33            'Comma-separated list of templates that indicate a page should not be converted',
34            false, // not required
35            true, // takes argument
36            't'
37        );
38        $this->addOption(
39            'header-suffix',
40            'Wikitext to add to the end of the header',
41            false, // not required
42            true, // takes argument
43            'a'
44        );
45        $this->requireExtension( 'Flow' );
46    }
47
48    public function execute() {
49        $lang = $this->getServiceContainer()->getContentLanguage();
50        $provided = $this->getArg( 0 );
51        $namespace = $lang->getNsIndex( $provided );
52        if ( !$namespace ) {
53            $this->error( "Invalid namespace provided: $provided" );
54            return;
55        }
56        $namespaceName = $lang->getNsText( $namespace );
57        if ( !$this->getServiceContainer()->getNamespaceInfo()->hasSubpages( $namespace ) ) {
58            $this->error( "Subpages are not enabled in the $namespaceName namespace." );
59            $this->error( "In order to convert this namespace to Flow, you must enable subpages using:" );
60            $this->error( "\$wgNamespacesWithSubpages[$namespace] = true;" );
61            return;
62        }
63
64        $noConvertTemplates = explode( ',', $this->getOption( 'no-convert-templates', '' ) );
65        if ( $noConvertTemplates === [ '' ] ) {
66            // explode( ',', '' ) returns [ '' ]
67            $noConvertTemplates = [];
68        }
69        // Convert to Title objects
70        foreach ( $noConvertTemplates as &$template ) {
71            $title = Title::newFromText( $template, NS_TEMPLATE );
72            if ( !$title ) {
73                $this->error( "Invalid template name: $template" );
74                return;
75            }
76            $template = $title;
77        }
78
79        // @todo send to prod logger?
80        $logger = new MaintenanceDebugLogger( $this );
81
82        $dbw = $this->getPrimaryDB();
83        /** @var OccupationController $occupationController */
84        $occupationController = $this->getServiceContainer()->getService( 'FlowTalkpageManager' );
85        $talkpageManager = $occupationController->getTalkpageManager();
86        $converter = new Converter(
87            $dbw,
88            Container::get( 'importer' ),
89            $logger,
90            $talkpageManager,
91
92            new ConversionStrategy(
93                $this->getServiceContainer()->getParser(),
94                new NullImportSourceStore(),
95                $logger,
96                $talkpageManager,
97                $noConvertTemplates,
98                $this->getOption( 'header-suffix', null )
99            )
100        );
101
102        $logger->info( "Starting conversion of $namespaceName namespace" );
103
104        // Iterate over all existing pages of the namespace.
105        $it = new NamespaceIterator( $dbw, $namespace );
106        // NamespaceIterator is an IteratorAggregate. Get an Iterator
107        // so we can wrap that.
108        $it = $it->getIterator();
109
110        $converter->convertAll( $it );
111
112        $logger->info( "Finished conversion of $namespaceName namespace" );
113    }
114}
115
116$maintClass = ConvertNamespaceFromWikitext::class;
117require_once RUN_MAINTENANCE_IF_MAIN;