MediaWiki master
edit.php
Go to the documentation of this file.
1<?php
32
33// @codeCoverageIgnoreStart
34require_once __DIR__ . '/Maintenance.php';
35// @codeCoverageIgnoreEnd
36
42class EditCLI extends Maintenance {
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
46 $this->addOption( 'user', 'Username', false, true, 'u' );
47 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
48 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
49 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
50 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
51 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
52 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
53 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
54 $this->addOption( 'createonly', 'Only create new pages', false, false );
55 $this->addOption( 'slot', 'Slot role name', false, true );
56 $this->addOption(
57 'parse-title',
58 'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}',
59 false, false, 'p'
60 );
61 $this->addArg( 'title', 'Title of article to edit' );
62 }
63
65 public function execute() {
66 $userName = $this->getOption( 'user', false );
67 $summary = $this->getOption( 'summary', '' );
68 $remove = $this->hasOption( 'remove' );
69 $minor = $this->hasOption( 'minor' );
70 $bot = $this->hasOption( 'bot' );
71 $autoSummary = $this->hasOption( 'autosummary' );
72 $noRC = $this->hasOption( 'no-rc' );
73 $slot = $this->getOption( 'slot', SlotRecord::MAIN );
74
75 if ( $userName === false ) {
76 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
77 } else {
78 $user = User::newFromName( $userName );
79 }
80 if ( !$user ) {
81 $this->fatalError( "Invalid username" );
82 }
83 if ( $user->isAnon() ) {
84 $user->addToDatabase();
85 }
86 StubGlobalUser::setUser( $user );
87
88 $titleInput = $this->getArg( 0 );
89
90 if ( $this->hasOption( 'parse-title' ) ) {
91 $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text();
92 }
93
94 $title = Title::newFromText( $titleInput );
95 if ( !$title ) {
96 $this->fatalError( "Invalid title" );
97 }
98
99 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
100 $this->fatalError( "Page does not exist" );
101 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
102 $this->fatalError( "Page already exists" );
103 }
104
105 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
106
107 if ( $remove ) {
108 if ( $slot === SlotRecord::MAIN ) {
109 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
110 }
111
112 $content = false;
113 } else {
114 # Read the text
115 $text = $this->getStdin( Maintenance::STDIN_ALL );
116 $content = ContentHandler::makeContent( $text, $title );
117 }
118
119 # Do the edit
120 $this->output( "Saving..." );
121 $updater = $page->newPageUpdater( $user );
122
123 $flags = ( $minor ? EDIT_MINOR : 0 ) |
124 ( $bot ? EDIT_FORCE_BOT : 0 ) |
125 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
126 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
127
128 if ( $content === false ) {
129 $updater->removeSlot( $slot );
130 } else {
131 $updater->setContent( $slot, $content );
132 }
133
134 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
135 $status = $updater->getStatus();
136
137 if ( $status->isOK() ) {
138 $this->output( "done\n" );
139 } else {
140 $this->output( "failed\n" );
141 }
142 if ( !$status->isGood() ) {
143 $this->error( $status );
144 }
145 return $status->isOK();
146 }
147}
148
149// @codeCoverageIgnoreStart
150$maintClass = EditCLI::class;
151require_once RUN_MAINTENANCE_IF_MAIN;
152// @codeCoverageIgnoreEnd
const EDIT_FORCE_BOT
Mark the edit a "bot" edit regardless of user rights.
Definition Defines.php:143
const EDIT_SUPPRESS_RC
Definition Defines.php:140
const EDIT_MINOR
Mark this edit minor, if the user is allowed to do so.
Definition Defines.php:134
const EDIT_AUTOSUMMARY
Fill in blank summaries with generated text where possible.
Definition Defines.php:149
Maintenance script to make a page edit.
Definition edit.php:42
__construct()
Default constructor.
Definition edit.php:43
execute()
Do the actual work.All child classes will need to implement thisbool|null|void True for success,...
Definition edit.php:65
Value object for a comment stored by CommentStore.
Base class for content handling.
Variant of the Message class.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
getStdin( $len=null)
Return input from stdin.
addDescription( $text)
Set the description text.
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
User class for the MediaWiki software.
Definition User.php:123
$maintClass
Definition edit.php:150