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
21 * @ingroup Maintenance
24 use MediaWiki\Revision\SlotRecord
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script to make a page edit.
31 * @ingroup Maintenance
33 class EditCLI
extends Maintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
37 $this->addOption( 'user', 'Username', false, true, 'u' );
38 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
39 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
40 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
41 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
42 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
43 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
44 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
45 $this->addOption( 'createonly', 'Only create new pages', false, false );
46 $this->addOption( 'slot', 'Slot role name', false, true );
47 $this->addArg( 'title', 'Title of article to edit' );
50 public function execute() {
53 $userName = $this->getOption( 'user', false );
54 $summary = $this->getOption( 'summary', '' );
55 $remove = $this->hasOption( 'remove' );
56 $minor = $this->hasOption( 'minor' );
57 $bot = $this->hasOption( 'bot' );
58 $autoSummary = $this->hasOption( 'autosummary' );
59 $noRC = $this->hasOption( 'no-rc' );
60 $slot = $this->getOption( 'slot', SlotRecord
::MAIN
);
62 if ( $userName === false ) {
63 $wgUser = User
::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
65 $wgUser = User
::newFromName( $userName );
68 $this->fatalError( "Invalid username" );
70 if ( $wgUser->isAnon() ) {
71 $wgUser->addToDatabase();
74 $title = Title
::newFromText( $this->getArg( 0 ) );
76 $this->fatalError( "Invalid title" );
79 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
80 $this->fatalError( "Page does not exist" );
81 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
82 $this->fatalError( "Page already exists" );
85 $page = WikiPage
::factory( $title );
88 if ( $slot === SlotRecord
::MAIN
) {
89 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
95 $text = $this->getStdin( Maintenance
::STDIN_ALL
);
96 $content = ContentHandler
::makeContent( $text, $title );
100 $this->output( "Saving... " );
101 $updater = $page->newPageUpdater( $wgUser );
103 $flags = ( $minor ? EDIT_MINOR
: 0 ) |
104 ( $bot ? EDIT_FORCE_BOT
: 0 ) |
105 ( $autoSummary ? EDIT_AUTOSUMMARY
: 0 ) |
106 ( $noRC ? EDIT_SUPPRESS_RC
: 0 );
108 if ( $content === false ) {
109 $updater->removeSlot( $slot );
111 $updater->setContent( $slot, $content );
114 $updater->saveRevision( CommentStoreComment
::newUnsavedComment( $summary ), $flags );
115 $status = $updater->getStatus();
117 if ( $status->isOK() ) {
118 $this->output( "done\n" );
121 $this->output( "failed\n" );
124 if ( !$status->isGood() ) {
125 $this->output( $status->getWikiText( false, false, 'en' ) . "\n" );
131 $maintClass = EditCLI
::class;
132 require_once RUN_MAINTENANCE_IF_MAIN
;