Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 67
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 / 61
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 / 43
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace Flow\Maintenance;
4
5use Flow\Container;
6use Flow\Hooks;
7use Flow\Import\Converter;
8use Flow\Import\SourceStore\NullImportSourceStore;
9use Flow\Import\Wikitext\ConversionStrategy;
10use Flow\Utils\NamespaceIterator;
11use 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        global $wgLang;
50
51        $provided = $this->getArg( 0 );
52        $namespace = $wgLang->getNsIndex( $provided );
53        if ( !$namespace ) {
54            $this->error( "Invalid namespace provided: $provided" );
55            return;
56        }
57        $namespaceName = $wgLang->getNsText( $namespace );
58        if ( !$this->getServiceContainer()->getNamespaceInfo()->hasSubpages( $namespace ) ) {
59            $this->error( "Subpages are not enabled in the $namespaceName namespace." );
60            $this->error( "In order to convert this namespace to Flow, you must enable subpages using:" );
61            $this->error( "\$wgNamespacesWithSubpages[$namespace] = true;" );
62            return;
63        }
64
65        $noConvertTemplates = explode( ',', $this->getOption( 'no-convert-templates', '' ) );
66        if ( $noConvertTemplates === [ '' ] ) {
67            // explode( ',', '' ) returns [ '' ]
68            $noConvertTemplates = [];
69        }
70        // Convert to Title objects
71        foreach ( $noConvertTemplates as &$template ) {
72            $title = Title::newFromText( $template, NS_TEMPLATE );
73            if ( !$title ) {
74                $this->error( "Invalid template name: $template" );
75                return;
76            }
77            $template = $title;
78        }
79
80        // @todo send to prod logger?
81        $logger = new MaintenanceDebugLogger( $this );
82
83        $dbw = $this->getPrimaryDB();
84        $talkpageManager = Hooks::getOccupationController()->getTalkpageManager();
85        $converter = new Converter(
86            $dbw,
87            Container::get( 'importer' ),
88            $logger,
89            $talkpageManager,
90
91            new ConversionStrategy(
92                $this->getServiceContainer()->getParser(),
93                new NullImportSourceStore(),
94                $logger,
95                $talkpageManager,
96                $noConvertTemplates,
97                $this->getOption( 'header-suffix', null )
98            )
99        );
100
101        $logger->info( "Starting conversion of $namespaceName namespace" );
102
103        // Iterate over all existing pages of the namespace.
104        $it = new NamespaceIterator( $dbw, $namespace );
105        // NamespaceIterator is an IteratorAggregate. Get an Iterator
106        // so we can wrap that.
107        $it = $it->getIterator();
108
109        $converter->convertAll( $it );
110
111        $logger->info( "Finished conversion of $namespaceName namespace" );
112    }
113}
114
115$maintClass = ConvertNamespaceFromWikitext::class;
116require_once RUN_MAINTENANCE_IF_MAIN;