MediaWiki REL1_37
edit.php
Go to the documentation of this file.
1<?php
26
27require_once __DIR__ . '/Maintenance.php';
28
34class EditCLI extends Maintenance {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Edit an article from the command line, text is from stdin' );
38 $this->addOption( 'user', 'Username', false, true, 'u' );
39 $this->addOption( 'summary', 'Edit summary', false, true, 's' );
40 $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
41 $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
42 $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
43 $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
44 $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
45 $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
46 $this->addOption( 'createonly', 'Only create new pages', false, false );
47 $this->addOption( 'slot', 'Slot role name', false, true );
48 $this->addArg( 'title', 'Title of article to edit' );
49 }
50
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 }
73
74 $title = Title::newFromText( $this->getArg( 0 ) );
75 if ( !$title ) {
76 $this->fatalError( "Invalid title" );
77 }
78
79 if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
80 $this->fatalError( "Page does not exist" );
81 } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
82 $this->fatalError( "Page already exists" );
83 }
84
85 $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
86
87 if ( $remove ) {
88 if ( $slot === SlotRecord::MAIN ) {
89 $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
90 }
91
92 $content = false;
93 } else {
94 # Read the text
95 $text = $this->getStdin( Maintenance::STDIN_ALL );
96 $content = ContentHandler::makeContent( $text, $title );
97 }
98
99 # Do the edit
100 $this->output( "Saving..." );
101 $updater = $page->newPageUpdater( $user );
102
103 $flags = ( $minor ? EDIT_MINOR : 0 ) |
104 ( $bot ? EDIT_FORCE_BOT : 0 ) |
105 ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
106 ( $noRC ? EDIT_SUPPRESS_RC : 0 );
107
108 if ( $content === false ) {
109 $updater->removeSlot( $slot );
110 } else {
111 $updater->setContent( $slot, $content );
112 }
113
114 $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
115 $status = $updater->getStatus();
116
117 if ( $status->isOK() ) {
118 $this->output( "done\n" );
119 } else {
120 $this->output( "failed\n" );
121 }
122 if ( !$status->isGood() ) {
123 $this->output( $status->getMessage( false, false, 'en' )->text() . "\n" );
124 }
125 return $status->isOK();
126 }
127}
128
129$maintClass = EditCLI::class;
130require_once RUN_MAINTENANCE_IF_MAIN;
const EDIT_FORCE_BOT
Definition Defines.php:129
const EDIT_SUPPRESS_RC
Definition Defines.php:128
const EDIT_MINOR
Definition Defines.php:127
const EDIT_AUTOSUMMARY
Definition Defines.php:131
Maintenance script to make a page edit.
Definition edit.php:34
__construct()
Default constructor.
Definition edit.php:35
execute()
Do the actual work.
Definition edit.php:51
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
const STDIN_ALL
addArg( $arg, $description, $required=true)
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Value object representing a content slot associated with a page revision.
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newFromName( $name, $validate='valid')
Definition User.php:607
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:810
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:122
$maintClass
Definition edit.php:129
$content
Definition router.php:76