MediaWiki 1.43.1
edit.php
Go to the documentation of this file.
1<?php
31
32// @codeCoverageIgnoreStart
33require_once __DIR__ . '/Maintenance.php';
34// @codeCoverageIgnoreEnd
35
41class EditCLI extends Maintenance {
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
45 $this->addOption( 'user', 'Username', false, true, 'u' );
46 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
47 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
48 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
49 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
50 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
51 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
52 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
53 $this->addOption( 'createonly', 'Only create new pages', false, false );
54 $this->addOption( 'slot', 'Slot role name', false, true );
55 $this->addOption(
56 'parse-title',
57 'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}',
58 false, false, 'p'
59 );
60 $this->addArg( 'title', 'Title of article to edit' );
61 }
62
63 public function execute() {
64 $userName = $this->getOption( 'user', false );
65 $summary = $this->getOption( 'summary', '' );
66 $remove = $this->hasOption( 'remove' );
67 $minor = $this->hasOption( 'minor' );
68 $bot = $this->hasOption( 'bot' );
69 $autoSummary = $this->hasOption( 'autosummary' );
70 $noRC = $this->hasOption( 'no-rc' );
71 $slot = $this->getOption( 'slot', SlotRecord::MAIN );
72
73 if ( $userName === false ) {
74 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
75 } else {
76 $user = User::newFromName( $userName );
77 }
78 if ( !$user ) {
79 $this->fatalError( "Invalid username" );
80 }
81 if ( $user->isAnon() ) {
82 $user->addToDatabase();
83 }
84 StubGlobalUser::setUser( $user );
85
86 $titleInput = $this->getArg( 0 );
87
88 if ( $this->hasOption( 'parse-title' ) ) {
89 $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text();
90 }
91
92 $title = Title::newFromText( $titleInput );
93 if ( !$title ) {
94 $this->fatalError( "Invalid title" );
95 }
96
97 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
98 $this->fatalError( "Page does not exist" );
99 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
100 $this->fatalError( "Page already exists" );
101 }
102
103 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
104
105 if ( $remove ) {
106 if ( $slot === SlotRecord::MAIN ) {
107 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
108 }
109
110 $content = false;
111 } else {
112 # Read the text
113 $text = $this->getStdin( Maintenance::STDIN_ALL );
114 $content = ContentHandler::makeContent( $text, $title );
115 }
116
117 # Do the edit
118 $this->output( "Saving..." );
119 $updater = $page->newPageUpdater( $user );
120
121 $flags = ( $minor ? EDIT_MINOR : 0 ) |
122 ( $bot ? EDIT_FORCE_BOT : 0 ) |
123 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
124 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
125
126 if ( $content === false ) {
127 $updater->removeSlot( $slot );
128 } else {
129 $updater->setContent( $slot, $content );
130 }
131
132 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
133 $status = $updater->getStatus();
134
135 if ( $status->isOK() ) {
136 $this->output( "done\n" );
137 } else {
138 $this->output( "failed\n" );
139 }
140 if ( !$status->isGood() ) {
141 $this->error( $status );
142 }
143 return $status->isOK();
144 }
145}
146
147// @codeCoverageIgnoreStart
148$maintClass = EditCLI::class;
149require_once RUN_MAINTENANCE_IF_MAIN;
150// @codeCoverageIgnoreEnd
const EDIT_FORCE_BOT
Definition Defines.php:131
const EDIT_SUPPRESS_RC
Definition Defines.php:130
const EDIT_MINOR
Definition Defines.php:129
const EDIT_AUTOSUMMARY
Definition Defines.php:133
Maintenance script to make a page edit.
Definition edit.php:41
__construct()
Definition edit.php:42
execute()
Definition edit.php:63
Value object for a comment stored by CommentStore.
A content handler knows how do deal with a specific type of content on a wiki page.
Variant of the Message class.
Value object representing a content slot associated with a page revision.
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Represents a title within MediaWiki.
Definition Title.php:78
internal since 1.36
Definition User.php:94
$maintClass
Definition edit.php:148