Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImplementLqtFixes
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2/**
3 * Maintenance script to apply LQT porting fixes from a JSON file. (T397426)
4 * Usage: php implementLqtFixes.php [--dryrun] < fixes.json
5 */
6
7use MediaWiki\Content\ContentHandler;
8use MediaWiki\Content\TextContent;
9use MediaWiki\Maintenance\Maintenance;
10use MediaWiki\Title\Title;
11
12$IP = getenv( 'MW_INSTALL_PATH' );
13if ( $IP === false ) {
14    $IP = __DIR__ . '/../../..';
15}
16
17require_once "$IP/maintenance/Maintenance.php";
18
19class ImplementLqtFixes extends Maintenance {
20    public function __construct() {
21        parent::__construct();
22        $this->addOption( 'dryrun', 'Dry run mode (do not perform edits)' );
23    }
24
25    public function execute() {
26        $dryRun = $this->hasOption( 'dryrun' );
27
28        // Read JSON proposals from stdin
29        $json = stream_get_contents( STDIN );
30        $proposals = json_decode( $json, true );
31        if ( !is_array( $proposals ) ) {
32            $this->fatalError( "Invalid JSON on stdin" );
33        }
34
35        // Use FlowTalkpageManager user for edits, as in convertLqtPageOnLocalWiki.php
36        $occupationController = $this->getServiceContainer()->getService( 'FlowTalkpageManager' );
37        $user = $occupationController->getTalkpageManager();
38        $summary = 'Automated LQT porting fix';
39
40        foreach ( $proposals as $proposal ) {
41            $titleText = $proposal['title'];
42            $expectedRevision = intval( $proposal['expected_revision'] );
43            $content = $proposal['content'];
44            $pageId = intval( $proposal['id'] );
45
46            $title = Title::newFromText( $titleText );
47            if ( !$title || !$title->exists() ) {
48                $this->output( "!!! Skipping: $titleText (page does not exist)\n" );
49                continue;
50            }
51
52            $wikiPage = $this->getServiceContainer()
53                ->getWikiPageFactory()
54                ->newFromTitle( $title );
55            $dbPageId = $wikiPage->getId();
56            if ( $dbPageId != $pageId ) {
57                $this->output( "!!! Skipping: $titleText (pageid mismatch: JSON $pageId, DB $dbPageId)\n" );
58                continue;
59            }
60            $currentRevision = $wikiPage->getLatest();
61            $contentObj = $wikiPage->getContent();
62            if ( !( $contentObj instanceof TextContent ) ) {
63                $this->output( "!!! Skipping: $titleText (not text content)\n" );
64                continue;
65            }
66            $currentContent = $contentObj->getText();
67
68            if ( $currentRevision === $expectedRevision ) {
69                if ( $dryRun ) {
70                    $this->output( "+++ Would edit: $titleText\n" );
71                } else {
72                    $contentObj = ContentHandler::makeContent( $content, $title );
73                    $status = $wikiPage->doUserEditContent(
74                        $contentObj,
75                        $user,
76                        $summary,
77                        EDIT_FORCE_BOT | EDIT_SUPPRESS_RC,
78                        $expectedRevision
79                    );
80                    if ( $status->isOK() ) {
81                        $this->output( "+++ Edited: $titleText\n" );
82                    } else {
83                        $this->output( "!!! Error editing $titleText" . $status->getWikiText() . "\n" );
84                    }
85                }
86            } else {
87                if ( $currentContent === $content ) {
88                    $this->output( "--- Complete: $titleText\n" );
89                } else {
90                    $this->output(
91                        "!!! Conflict: $titleText," .
92                        " expected revision: $expectedRevision," .
93                        " current revision: $currentRevision\n"
94                    );
95                }
96            }
97        }
98    }
99}
100
101$maintClass = ImplementLqtFixes::class;
102require_once RUN_MAINTENANCE_IF_MAIN;