MediaWiki master
edit.php
Go to the documentation of this file.
1<?php
18
19// @codeCoverageIgnoreStart
20require_once __DIR__ . '/Maintenance.php';
21// @codeCoverageIgnoreEnd
22
28class EditCLI extends Maintenance {
29 public function __construct() {
30 parent::__construct();
31 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
32 $this->addOption( 'user', 'Username', false, true, 'u' );
33 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
34 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
35 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
36 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
37 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
38 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
39 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
40 $this->addOption( 'createonly', 'Only create new pages', false, false );
41 $this->addOption( 'slot', 'Slot role name', false, true );
42 $this->addOption(
43 'parse-title',
44 'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}',
45 false, false, 'p'
46 );
47 $this->addArg( 'title', 'Title of article to edit' );
48 }
49
51 public function execute() {
52 $userName = $this->getOption( 'user', false );
53 $summary = $this->getOption( 'summary', '' );
54 $remove = $this->hasOption( 'remove' );
55 $minor = $this->hasOption( 'minor' );
56 $bot = $this->hasOption( 'bot' );
57 $autoSummary = $this->hasOption( 'autosummary' );
58 $noRC = $this->hasOption( 'no-rc' );
59 $slot = $this->getOption( 'slot', SlotRecord::MAIN );
60
61 if ( $userName === false ) {
62 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
63 } else {
64 $user = User::newFromName( $userName );
65 }
66 if ( !$user ) {
67 $this->fatalError( "Invalid username" );
68 }
69 if ( $user->isAnon() ) {
70 $user->addToDatabase();
71 }
72 StubGlobalUser::setUser( $user );
73
74 $titleInput = $this->getArg( 0 );
75
76 if ( $this->hasOption( 'parse-title' ) ) {
77 $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text();
78 }
79
80 $title = Title::newFromText( $titleInput );
81 if ( !$title ) {
82 $this->fatalError( "Invalid title" );
83 }
84
85 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
86 $this->fatalError( "Page does not exist" );
87 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
88 $this->fatalError( "Page already exists" );
89 }
90
91 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
92
93 if ( $remove ) {
94 if ( $slot === SlotRecord::MAIN ) {
95 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
96 }
97
98 $content = false;
99 } else {
100 # Read the text
101 $text = $this->getStdin( Maintenance::STDIN_ALL );
102 $content = ContentHandler::makeContent( $text, $title );
103 }
104
105 # Do the edit
106 $this->output( "Saving..." );
107 $updater = $page->newPageUpdater( $user );
108
109 $flags = ( $minor ? EDIT_MINOR : 0 ) |
110 ( $bot ? EDIT_FORCE_BOT : 0 ) |
111 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
112 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
113
114 if ( $content === false ) {
115 $updater->removeSlot( $slot );
116 } else {
117 $updater->setContent( $slot, $content );
118 }
119
120 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
121 $status = $updater->getStatus();
122
123 if ( $status->isOK() ) {
124 $this->output( "done\n" );
125 } else {
126 $this->output( "failed\n" );
127 }
128 if ( !$status->isGood() ) {
129 $this->error( $status );
130 }
131 return $status->isOK();
132 }
133}
134
135// @codeCoverageIgnoreStart
136$maintClass = EditCLI::class;
137require_once RUN_MAINTENANCE_IF_MAIN;
138// @codeCoverageIgnoreEnd
const EDIT_FORCE_BOT
Mark the edit a "bot" edit regardless of user rights.
Definition Defines.php:129
const EDIT_SUPPRESS_RC
Definition Defines.php:126
const EDIT_MINOR
Mark this edit minor, if the user is allowed to do so.
Definition Defines.php:120
const EDIT_AUTOSUMMARY
Fill in blank summaries with generated text where possible.
Definition Defines.php:135
Maintenance script to make a page edit.
Definition edit.php:28
__construct()
Default constructor.
Definition edit.php:29
execute()
Do the actual work.All child classes will need to implement thisbool|null|void True for success,...
Definition edit.php:51
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:69
User class for the MediaWiki software.
Definition User.php:130
$maintClass
Definition edit.php:136