Updating composer/semver (1.5.1 => 1.5.2)
[mediawiki.git] / maintenance / edit.php
blobc1c55920d71d731d1b8c4bd2a6b331b63eb39b8c
1 <?php
2 /**
3 * Make a page edit.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Revision\SlotRecord;
27 require_once __DIR__ . '/Maintenance.php';
29 /**
30 * Maintenance script to make a page edit.
32 * @ingroup Maintenance
34 class EditCLI extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
38 $this->addOption( 'user', 'Username', false, true, 'u' );
39 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
40 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
41 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
42 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
43 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
44 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
45 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
46 $this->addOption( 'createonly', 'Only create new pages', false, false );
47 $this->addOption( 'slot', 'Slot role name', false, true );
48 $this->addArg( 'title', 'Title of article to edit' );
51 public function execute() {
52 global $wgUser;
54 $userName = $this->getOption( 'user', false );
55 $summary = $this->getOption( 'summary', '' );
56 $remove = $this->hasOption( 'remove' );
57 $minor = $this->hasOption( 'minor' );
58 $bot = $this->hasOption( 'bot' );
59 $autoSummary = $this->hasOption( 'autosummary' );
60 $noRC = $this->hasOption( 'no-rc' );
61 $slot = $this->getOption( 'slot', SlotRecord::MAIN );
63 if ( $userName === false ) {
64 $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
65 } else {
66 $user = User::newFromName( $userName );
68 if ( !$user ) {
69 $this->fatalError( "Invalid username" );
71 if ( $user->isAnon() ) {
72 $user->addToDatabase();
74 $wgUser = $user;
76 $title = Title::newFromText( $this->getArg( 0 ) );
77 if ( !$title ) {
78 $this->fatalError( "Invalid title" );
81 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
82 $this->fatalError( "Page does not exist" );
83 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
84 $this->fatalError( "Page already exists" );
87 $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
89 if ( $remove ) {
90 if ( $slot === SlotRecord::MAIN ) {
91 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
94 $content = false;
95 } else {
96 # Read the text
97 $text = $this->getStdin( Maintenance::STDIN_ALL );
98 $content = ContentHandler::makeContent( $text, $title );
101 # Do the edit
102 $this->output( "Saving... " );
103 $updater = $page->newPageUpdater( $user );
105 $flags = ( $minor ? EDIT_MINOR : 0 ) |
106 ( $bot ? EDIT_FORCE_BOT : 0 ) |
107 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
108 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
110 if ( $content === false ) {
111 $updater->removeSlot( $slot );
112 } else {
113 $updater->setContent( $slot, $content );
116 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
117 $status = $updater->getStatus();
119 if ( $status->isOK() ) {
120 $this->output( "done\n" );
121 $exit = 0;
122 } else {
123 $this->output( "failed\n" );
124 $exit = 1;
126 if ( !$status->isGood() ) {
127 $this->output( $status->getMessage( false, false, 'en' )->text() . "\n" );
129 exit( $exit );
133 $maintClass = EditCLI::class;
134 require_once RUN_MAINTENANCE_IF_MAIN;