Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EditCLI
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 2
420
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 / 50
0.00% covered (danger)
0.00%
0 / 1
380
1<?php
2/**
3 * Make a page edit.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\CommentStore\CommentStoreComment;
25use MediaWiki\Language\RawMessage;
26use MediaWiki\Revision\SlotRecord;
27use MediaWiki\StubObject\StubGlobalUser;
28use MediaWiki\Title\Title;
29use MediaWiki\User\User;
30
31require_once __DIR__ . '/Maintenance.php';
32
33/**
34 * Maintenance script to make a page edit.
35 *
36 * @ingroup Maintenance
37 */
38class EditCLI extends Maintenance {
39    public function __construct() {
40        parent::__construct();
41        $this->addDescription( 'Edit an article from the command line, text is from stdin' );
42        $this->addOption( 'user', 'Username', false, true, 'u' );
43        $this->addOption( 'summary', 'Edit summary', false, true, 's' );
44        $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
45        $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
46        $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
47        $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
48        $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
49        $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
50        $this->addOption( 'createonly', 'Only create new pages', false, false );
51        $this->addOption( 'slot', 'Slot role name', false, true );
52        $this->addOption(
53            'parse-title',
54            'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}',
55            false, false, 'p'
56        );
57        $this->addArg( 'title', 'Title of article to edit' );
58    }
59
60    public function execute() {
61        $userName = $this->getOption( 'user', false );
62        $summary = $this->getOption( 'summary', '' );
63        $remove = $this->hasOption( 'remove' );
64        $minor = $this->hasOption( 'minor' );
65        $bot = $this->hasOption( 'bot' );
66        $autoSummary = $this->hasOption( 'autosummary' );
67        $noRC = $this->hasOption( 'no-rc' );
68        $slot = $this->getOption( 'slot', SlotRecord::MAIN );
69
70        if ( $userName === false ) {
71            $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
72        } else {
73            $user = User::newFromName( $userName );
74        }
75        if ( !$user ) {
76            $this->fatalError( "Invalid username" );
77        }
78        if ( $user->isAnon() ) {
79            $user->addToDatabase();
80        }
81        StubGlobalUser::setUser( $user );
82
83        $titleInput = $this->getArg( 0 );
84
85        if ( $this->hasOption( 'parse-title' ) ) {
86            $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text();
87        }
88
89        $title = Title::newFromText( $titleInput );
90        if ( !$title ) {
91            $this->fatalError( "Invalid title" );
92        }
93
94        if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
95            $this->fatalError( "Page does not exist" );
96        } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
97            $this->fatalError( "Page already exists" );
98        }
99
100        $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
101
102        if ( $remove ) {
103            if ( $slot === SlotRecord::MAIN ) {
104                $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
105            }
106
107            $content = false;
108        } else {
109            # Read the text
110            $text = $this->getStdin( Maintenance::STDIN_ALL );
111            $content = ContentHandler::makeContent( $text, $title );
112        }
113
114        # Do the edit
115        $this->output( "Saving..." );
116        $updater = $page->newPageUpdater( $user );
117
118        $flags = ( $minor ? EDIT_MINOR : 0 ) |
119            ( $bot ? EDIT_FORCE_BOT : 0 ) |
120            ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
121            ( $noRC ? EDIT_SUPPRESS_RC : 0 );
122
123        if ( $content === false ) {
124            $updater->removeSlot( $slot );
125        } else {
126            $updater->setContent( $slot, $content );
127        }
128
129        $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
130        $status = $updater->getStatus();
131
132        if ( $status->isOK() ) {
133            $this->output( "done\n" );
134        } else {
135            $this->output( "failed\n" );
136        }
137        if ( !$status->isGood() ) {
138            $this->output( $status->getMessage( false, false, 'en' )->text() . "\n" );
139        }
140        return $status->isOK();
141    }
142}
143
144$maintClass = EditCLI::class;
145require_once RUN_MAINTENANCE_IF_MAIN;