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