Merge "selenium: invoke jobs to enforce eventual consistency"
[lhc/web/wiklou.git] / maintenance / edit.php
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
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to make a page edit.
28 *
29 * @ingroup Maintenance
30 */
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' );
46 }
47
48 public function execute() {
49 global $wgUser;
50
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' );
59
60 if ( $userName === false ) {
61 $wgUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
62 } else {
63 $wgUser = User::newFromName( $userName );
64 }
65 if ( !$wgUser ) {
66 $this->fatalError( "Invalid username" );
67 }
68 if ( $wgUser->isAnon() ) {
69 $wgUser->addToDatabase();
70 }
71
72 $title = Title::newFromText( $this->getArg() );
73 if ( !$title ) {
74 $this->fatalError( "Invalid title" );
75 }
76
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" );
81 }
82
83 $page = WikiPage::factory( $title );
84
85 if ( $remove ) {
86 if ( $slot === 'main' ) {
87 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
88 }
89
90 $content = false;
91 } else {
92 # Read the text
93 $text = $this->getStdin( Maintenance::STDIN_ALL );
94 $content = ContentHandler::makeContent( $text, $title );
95 }
96
97 # Do the edit
98 $this->output( "Saving... " );
99 $updater = $page->newPageUpdater( $wgUser );
100
101 $flags = ( $minor ? EDIT_MINOR : 0 ) |
102 ( $bot ? EDIT_FORCE_BOT : 0 ) |
103 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
104 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
105
106 if ( $content === false ) {
107 $updater->removeSlot( $slot );
108 } else {
109 $updater->setContent( $slot, $content );
110 }
111
112 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
113 $status = $updater->getStatus();
114
115 if ( $status->isOK() ) {
116 $this->output( "done\n" );
117 $exit = 0;
118 } else {
119 $this->output( "failed\n" );
120 $exit = 1;
121 }
122 if ( !$status->isGood() ) {
123 $this->output( $status->getWikiText( false, false, 'en' ) . "\n" );
124 }
125 exit( $exit );
126 }
127 }
128
129 $maintClass = EditCLI::class;
130 require_once RUN_MAINTENANCE_IF_MAIN;