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
64 public function execute() {
65 $userName = $this->getOption( 'user', false );
66 $summary = $this->getOption( 'summary', '' );
67 $remove = $this->hasOption( 'remove' );
68 $minor = $this->hasOption( 'minor' );
69 $bot = $this->hasOption( 'bot' );
70 $autoSummary = $this->hasOption( 'autosummary' );
71 $noRC = $this->hasOption( 'no-rc' );
72 $slot = $this->getOption( 'slot', SlotRecord::MAIN );
73
74 if ( $userName === false ) {
75 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
76 } else {
77 $user = User::newFromName( $userName );
78 }
79 if ( !$user ) {
80 $this->fatalError( "Invalid username" );
81 }
82 if ( $user->isAnon() ) {
83 $user->addToDatabase();
84 }
85 StubGlobalUser::setUser( $user );
86
87 $titleInput = $this->getArg( 0 );
88
89 if ( $this->hasOption( 'parse-title' ) ) {
90 $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text();
91 }
92
93 $title = Title::newFromText( $titleInput );
94 if ( !$title ) {
95 $this->fatalError( "Invalid title" );
96 }
97
98 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
99 $this->fatalError( "Page does not exist" );
100 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
101 $this->fatalError( "Page already exists" );
102 }
103
104 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
105
106 if ( $remove ) {
107 if ( $slot === SlotRecord::MAIN ) {
108 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
109 }
110
111 $content = false;
112 } else {
113 # Read the text
114 $text = $this->getStdin( Maintenance::STDIN_ALL );
115 $content = ContentHandler::makeContent( $text, $title );
116 }
117
118 # Do the edit
119 $this->output( "Saving..." );
120 $updater = $page->newPageUpdater( $user );
121
122 $flags = ( $minor ? EDIT_MINOR : 0 ) |
123 ( $bot ? EDIT_FORCE_BOT : 0 ) |
124 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
125 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
126
127 if ( $content === false ) {
128 $updater->removeSlot( $slot );
129 } else {
130 $updater->setContent( $slot, $content );
131 }
132
133 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
134 $status = $updater->getStatus();
135
136 if ( $status->isOK() ) {
137 $this->output( "done\n" );
138 } else {
139 $this->output( "failed\n" );
140 }
141 if ( !$status->isGood() ) {
142 $this->error( $status );
143 }
144 return $status->isOK();
145 }
146}
147
148// @codeCoverageIgnoreStart
149$maintClass = EditCLI::class;
150require_once RUN_MAINTENANCE_IF_MAIN;
151// @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:42
__construct()
Default constructor.
Definition edit.php:43
execute()
Do the actual work.
Definition edit.php:64
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.
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:119
$maintClass
Definition edit.php:149