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  $wgUser = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
64  } else {
65  $wgUser = User::newFromName( $userName );
66  }
67  if ( !$wgUser ) {
68  $this->fatalError( "Invalid username" );
69  }
70  if ( $wgUser->isAnon() ) {
71  $wgUser->addToDatabase();
72  }
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 = WikiPage::factory( $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( $wgUser );
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  $exit = 0;
120  } else {
121  $this->output( "failed\n" );
122  $exit = 1;
123  }
124  if ( !$status->isGood() ) {
125  $this->output( $status->getWikiText( false, false, 'en' ) . "\n" );
126  }
127  exit( $exit );
128  }
129 }
130 
131 $maintClass = EditCLI::class;
132 require_once RUN_MAINTENANCE_IF_MAIN;
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
const STDIN_ALL
Definition: Maintenance.php:92
getArg( $argId=0, $default=null)
Get an argument.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
__construct()
Definition: edit.php:34
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
const EDIT_MINOR
Definition: Defines.php:134
hasOption( $name)
Checks to see if a particular option exists.
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
const EDIT_FORCE_BOT
Definition: Defines.php:136
execute()
Definition: edit.php:50
addDescription( $text)
Set the description text.
const EDIT_AUTOSUMMARY
Definition: Defines.php:138
const EDIT_SUPPRESS_RC
Definition: Defines.php:135
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
$maintClass
Definition: edit.php:131
Maintenance script to make a page edit.
Definition: edit.php:33
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getStdin( $len=null)
Return input from stdin.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:737
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316