MediaWiki REL1_40
edit.php
Go to the documentation of this file.
1<?php
30
31require_once __DIR__ . '/Maintenance.php';
32
38class EditCLI extends Maintenance {
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
42 $this->addOption( 'user', 'Username', false, true, 'u' );
43 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
44 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
45 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
46 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
47 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
48 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
49 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
50 $this->addOption( 'createonly', 'Only create new pages', false, false );
51 $this->addOption( 'slot', 'Slot role name', false, true );
52 $this->addOption(
53 'parse-title',
54 'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}',
55 false, false, 'p'
56 );
57 $this->addArg( 'title', 'Title of article to edit' );
58 }
59
60 public function execute() {
61 $userName = $this->getOption( 'user', false );
62 $summary = $this->getOption( 'summary', '' );
63 $remove = $this->hasOption( 'remove' );
64 $minor = $this->hasOption( 'minor' );
65 $bot = $this->hasOption( 'bot' );
66 $autoSummary = $this->hasOption( 'autosummary' );
67 $noRC = $this->hasOption( 'no-rc' );
68 $slot = $this->getOption( 'slot', SlotRecord::MAIN );
69
70 if ( $userName === false ) {
71 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
72 } else {
73 $user = User::newFromName( $userName );
74 }
75 if ( !$user ) {
76 $this->fatalError( "Invalid username" );
77 }
78 if ( $user->isAnon() ) {
79 $user->addToDatabase();
80 }
81 StubGlobalUser::setUser( $user );
82
83 $titleInput = $this->getArg( 0 );
84
85 if ( $this->hasOption( 'parse-title' ) ) {
86 $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text();
87 }
88
89 $title = Title::newFromText( $titleInput );
90 if ( !$title ) {
91 $this->fatalError( "Invalid title" );
92 }
93
94 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
95 $this->fatalError( "Page does not exist" );
96 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
97 $this->fatalError( "Page already exists" );
98 }
99
100 $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
101
102 if ( $remove ) {
103 if ( $slot === SlotRecord::MAIN ) {
104 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
105 }
106
107 $content = false;
108 } else {
109 # Read the text
110 $text = $this->getStdin( Maintenance::STDIN_ALL );
111 $content = ContentHandler::makeContent( $text, $title );
112 }
113
114 # Do the edit
115 $this->output( "Saving..." );
116 $updater = $page->newPageUpdater( $user );
117
118 $flags = ( $minor ? EDIT_MINOR : 0 ) |
119 ( $bot ? EDIT_FORCE_BOT : 0 ) |
120 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
121 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
122
123 if ( $content === false ) {
124 $updater->removeSlot( $slot );
125 } else {
126 $updater->setContent( $slot, $content );
127 }
128
129 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
130 $status = $updater->getStatus();
131
132 if ( $status->isOK() ) {
133 $this->output( "done\n" );
134 } else {
135 $this->output( "failed\n" );
136 }
137 if ( !$status->isGood() ) {
138 $this->output( $status->getMessage( false, false, 'en' )->text() . "\n" );
139 }
140 return $status->isOK();
141 }
142}
143
144$maintClass = EditCLI::class;
145require_once RUN_MAINTENANCE_IF_MAIN;
const EDIT_FORCE_BOT
Definition Defines.php:130
const EDIT_SUPPRESS_RC
Definition Defines.php:129
const EDIT_MINOR
Definition Defines.php:128
const EDIT_AUTOSUMMARY
Definition Defines.php:132
Maintenance script to make a page edit.
Definition edit.php:38
__construct()
Default constructor.
Definition edit.php:39
execute()
Do the actual work.
Definition edit.php:60
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
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.
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.
Service locator for MediaWiki core services.
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:82
static newFromName( $name, $validate='valid')
Definition User.php:592
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:793
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:117
$maintClass
Definition edit.php:144
$content
Definition router.php:76