MediaWiki  master
edit.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
33 class EditCLI extends Maintenance {
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Edit an article from the command line, text is from stdin' );
37  $this->addOption( 'user', 'Username', false, true, 'u' );
38  $this->addOption( 'summary', 'Edit summary', false, true, 's' );
39  $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false );
40  $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
41  $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
42  $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
43  $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
44  $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
45  $this->addOption( 'createonly', 'Only create new pages', false, false );
46  $this->addOption( 'slot', 'Slot role name', false, true );
47  $this->addArg( 'title', 'Title of article to edit' );
48  }
49 
50  public function execute() {
51  global $wgUser;
52 
53  $userName = $this->getOption( 'user', false );
54  $summary = $this->getOption( 'summary', '' );
55  $remove = $this->hasOption( 'remove' );
56  $minor = $this->hasOption( 'minor' );
57  $bot = $this->hasOption( 'bot' );
58  $autoSummary = $this->hasOption( 'autosummary' );
59  $noRC = $this->hasOption( 'no-rc' );
60  $slot = $this->getOption( 'slot', SlotRecord::MAIN );
61 
62  if ( $userName === false ) {
63  $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
64  } else {
65  $user = User::newFromName( $userName );
66  }
67  if ( !$user ) {
68  $this->fatalError( "Invalid username" );
69  }
70  if ( $user->isAnon() ) {
71  $user->addToDatabase();
72  }
73  $wgUser = $user;
74 
75  $title = Title::newFromText( $this->getArg( 0 ) );
76  if ( !$title ) {
77  $this->fatalError( "Invalid title" );
78  }
79 
80  if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
81  $this->fatalError( "Page does not exist" );
82  } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
83  $this->fatalError( "Page already exists" );
84  }
85 
86  $page = WikiPage::factory( $title );
87 
88  if ( $remove ) {
89  if ( $slot === SlotRecord::MAIN ) {
90  $this->fatalError( "Cannot remove main slot! Use --slot to specify." );
91  }
92 
93  $content = false;
94  } else {
95  # Read the text
96  $text = $this->getStdin( Maintenance::STDIN_ALL );
98  }
99 
100  # Do the edit
101  $this->output( "Saving... " );
102  $updater = $page->newPageUpdater( $user );
103 
104  $flags = ( $minor ? EDIT_MINOR : 0 ) |
105  ( $bot ? EDIT_FORCE_BOT : 0 ) |
106  ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
107  ( $noRC ? EDIT_SUPPRESS_RC : 0 );
108 
109  if ( $content === false ) {
110  $updater->removeSlot( $slot );
111  } else {
112  $updater->setContent( $slot, $content );
113  }
114 
115  $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags );
116  $status = $updater->getStatus();
117 
118  if ( $status->isOK() ) {
119  $this->output( "done\n" );
120  $exit = 0;
121  } else {
122  $this->output( "failed\n" );
123  $exit = 1;
124  }
125  if ( !$status->isGood() ) {
126  $this->output( $status->getMessage( false, false, 'en' )->text() . "\n" );
127  }
128  exit( $exit );
129  }
130 }
131 
132 $maintClass = EditCLI::class;
133 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:67
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:328
$maintClass
$maintClass
Definition: edit.php:132
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:406
EDIT_FORCE_BOT
const EDIT_FORCE_BOT
Definition: Defines.php:145
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:542
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:772
EditCLI\__construct
__construct()
Default constructor.
Definition: edit.php:34
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:159
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$title
$title
Definition: testCompression.php:38
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:142
$content
$content
Definition: router.php:76
EditCLI
Maintenance script to make a page edit.
Definition: edit.php:33
EDIT_AUTOSUMMARY
const EDIT_AUTOSUMMARY
Definition: Defines.php:147
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
Maintenance\STDIN_ALL
const STDIN_ALL
Definition: Maintenance.php:65
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:298
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
EDIT_MINOR
const EDIT_MINOR
Definition: Defines.php:143
EDIT_SUPPRESS_RC
const EDIT_SUPPRESS_RC
Definition: Defines.php:144
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:351
EditCLI\execute
execute()
Do the actual work.
Definition: edit.php:50
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40