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