MediaWiki  master
edit.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
34 class 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->addOption(
49  'parse-title',
50  'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}',
51  false, false, 'p'
52  );
53  $this->addArg( 'title', 'Title of article to edit' );
54  }
55 
56  public function execute() {
57  $userName = $this->getOption( 'user', false );
58  $summary = $this->getOption( 'summary', '' );
59  $remove = $this->hasOption( 'remove' );
60  $minor = $this->hasOption( 'minor' );
61  $bot = $this->hasOption( 'bot' );
62  $autoSummary = $this->hasOption( 'autosummary' );
63  $noRC = $this->hasOption( 'no-rc' );
64  $slot = $this->getOption( 'slot', SlotRecord::MAIN );
65 
66  if ( $userName === false ) {
67  $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
68  } else {
69  $user = User::newFromName( $userName );
70  }
71  if ( !$user ) {
72  $this->fatalError( "Invalid username" );
73  }
74  if ( $user->isAnon() ) {
75  $user->addToDatabase();
76  }
77  StubGlobalUser::setUser( $user );
78 
79  $titleInput = $this->getArg( 0 );
80 
81  if ( $this->hasOption( 'parse-title' ) ) {
82  $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text();
83  }
84 
85  $title = Title::newFromText( $titleInput );
86  if ( !$title ) {
87  $this->fatalError( "Invalid title" );
88  }
89 
90  if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
91  $this->fatalError( "Page does not exist" );
92  } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
93  $this->fatalError( "Page already exists" );
94  }
95 
96  $page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
97 
98  if ( $remove ) {
99  if ( $slot === SlotRecord::MAIN ) {
100  $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
101  }
102 
103  $content = false;
104  } else {
105  # Read the text
106  $text = $this->getStdin( Maintenance::STDIN_ALL );
108  }
109 
110  # Do the edit
111  $this->output( "Saving..." );
112  $updater = $page->newPageUpdater( $user );
113 
114  $flags = ( $minor ? EDIT_MINOR : 0 ) |
115  ( $bot ? EDIT_FORCE_BOT : 0 ) |
116  ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
117  ( $noRC ? EDIT_SUPPRESS_RC : 0 );
118 
119  if ( $content === false ) {
120  $updater->removeSlot( $slot );
121  } else {
122  $updater->setContent( $slot, $content );
123  }
124 
125  $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
126  $status = $updater->getStatus();
127 
128  if ( $status->isOK() ) {
129  $this->output( "done\n" );
130  } else {
131  $this->output( "failed\n" );
132  }
133  if ( !$status->isGood() ) {
134  $this->output( $status->getMessage( false, false, 'en' )->text() . "\n" );
135  }
136  return $status->isOK();
137  }
138 }
139 
140 $maintClass = EditCLI::class;
141 require_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
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
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:56
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:61
const STDIN_ALL
Definition: Maintenance.php:71
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.
Definition: SlotRecord.php:40
Variant of the Message class.
Definition: RawMessage.php:35
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:369
static newFromName( $name, $validate='valid')
Definition: User.php:599
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:807
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition: User.php:114
$maintClass
Definition: edit.php:140
$content
Definition: router.php:76