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 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to make a page edit.
29 * @ingroup Maintenance
31 class EditCLI
extends Maintenance
{
32 public function __construct() {
33 parent
::__construct();
34 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
35 $this->addOption( 'user', 'Username', false, true, 'u' );
36 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
37 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
38 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
39 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
40 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
41 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
42 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
43 $this->addOption( 'createonly', 'Only create new pages', false, false );
44 $this->addOption( 'slot', 'Slot role name', false, true );
45 $this->addArg( 'title', 'Title of article to edit' );
48 public function execute() {
51 $userName = $this->getOption( 'user', false );
52 $summary = $this->getOption( 'summary', '' );
53 $remove = $this->hasOption( 'remove' );
54 $minor = $this->hasOption( 'minor' );
55 $bot = $this->hasOption( 'bot' );
56 $autoSummary = $this->hasOption( 'autosummary' );
57 $noRC = $this->hasOption( 'no-rc' );
58 $slot = $this->getOption( 'slot', 'main' );
60 if ( $userName === false ) {
61 $wgUser = User
::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
63 $wgUser = User
::newFromName( $userName );
66 $this->fatalError( "Invalid username" );
68 if ( $wgUser->isAnon() ) {
69 $wgUser->addToDatabase();
72 $title = Title
::newFromText( $this->getArg() );
74 $this->fatalError( "Invalid title" );
77 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
78 $this->fatalError( "Page does not exist" );
79 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
80 $this->fatalError( "Page already exists" );
83 $page = WikiPage
::factory( $title );
86 if ( $slot === 'main' ) {
87 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
93 $text = $this->getStdin( Maintenance
::STDIN_ALL
);
94 $content = ContentHandler
::makeContent( $text, $title );
98 $this->output( "Saving... " );
99 $updater = $page->newPageUpdater( $wgUser );
101 $flags = ( $minor ? EDIT_MINOR
: 0 ) |
102 ( $bot ? EDIT_FORCE_BOT
: 0 ) |
103 ( $autoSummary ? EDIT_AUTOSUMMARY
: 0 ) |
104 ( $noRC ? EDIT_SUPPRESS_RC
: 0 );
106 if ( $content === false ) {
107 $updater->removeSlot( $slot );
109 $updater->setContent( $slot, $content );
112 $updater->saveRevision( CommentStoreComment
::newUnsavedComment( $summary ), $flags );
113 $status = $updater->getStatus();
115 if ( $status->isOK() ) {
116 $this->output( "done\n" );
119 $this->output( "failed\n" );
122 if ( !$status->isGood() ) {
123 $this->output( $status->getWikiText( false, false, 'en' ) . "\n" );
129 $maintClass = EditCLI
::class;
130 require_once RUN_MAINTENANCE_IF_MAIN
;