MediaWiki  1.23.2
edit.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class EditCLI extends Maintenance {
32  public function __construct() {
33  parent::__construct();
34  $this->mDescription = "Edit an article from the command line, text is from stdin";
35  $this->addOption( 'user', 'Username', false, true, 'u' );
36  $this->addOption( 'summary', 'Edit summary', false, true, 's' );
37  $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
38  $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
39  $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
40  $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
41  $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
42  $this->addOption( 'createonly', 'Only create new pages', false, false );
43  $this->addArg( 'title', 'Title of article to edit' );
44  }
45 
46  public function execute() {
48 
49  $userName = $this->getOption( 'user', 'Maintenance script' );
50  $summary = $this->getOption( 'summary', '' );
51  $minor = $this->hasOption( 'minor' );
52  $bot = $this->hasOption( 'bot' );
53  $autoSummary = $this->hasOption( 'autosummary' );
54  $noRC = $this->hasOption( 'no-rc' );
55 
56  $wgUser = User::newFromName( $userName );
57  $context = RequestContext::getMain();
58  $context->setUser( $wgUser );
59  if ( !$wgUser ) {
60  $this->error( "Invalid username", true );
61  }
62  if ( $wgUser->isAnon() ) {
63  $wgUser->addToDatabase();
64  }
65 
66  $title = Title::newFromText( $this->getArg() );
67  if ( !$title ) {
68  $this->error( "Invalid title", true );
69  }
70  $context->setTitle( $title );
71 
72  if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
73  $this->error( "Page does not exist", true );
74  } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
75  $this->error( "Page already exists", true );
76  }
77 
78  $page = WikiPage::factory( $title );
79 
80  # Read the text
81  $text = $this->getStdin( Maintenance::STDIN_ALL );
82  $content = ContentHandler::makeContent( $text, $title );
83 
84  # Do the edit
85  $this->output( "Saving... " );
86  $status = $page->doEditContent( $content, $summary,
87  ( $minor ? EDIT_MINOR : 0 ) |
88  ( $bot ? EDIT_FORCE_BOT : 0 ) |
89  ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
90  ( $noRC ? EDIT_SUPPRESS_RC : 0 ) );
91  if ( $status->isOK() ) {
92  $this->output( "done\n" );
93  $exit = 0;
94  } else {
95  $this->output( "failed\n" );
96  $exit = 1;
97  }
98  if ( !$status->isGood() ) {
99  $this->output( $status->getWikiText() . "\n" );
100  }
101  exit( $exit );
102  }
103 }
104 
105 $maintClass = "EditCLI";
106 require_once RUN_MAINTENANCE_IF_MAIN;
$wgUser
$wgUser
Definition: Setup.php:552
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:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$maintClass
$maintClass
Definition: edit.php:105
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:287
EDIT_FORCE_BOT
const EDIT_FORCE_BOT
Definition: Defines.php:193
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:388
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
EditCLI\__construct
__construct()
Default constructor.
Definition: edit.php:32
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
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:144
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
EditCLI
Maintenance script to make a page edit.
Definition: edit.php:31
$summary
$summary
Definition: importImages.php:120
EDIT_AUTOSUMMARY
const EDIT_AUTOSUMMARY
Definition: Defines.php:195
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
Maintenance\STDIN_ALL
const STDIN_ALL
Definition: Maintenance.php:62
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:207
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
EDIT_MINOR
const EDIT_MINOR
Definition: Defines.php:191
EDIT_SUPPRESS_RC
const EDIT_SUPPRESS_RC
Definition: Defines.php:192
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246
EditCLI\execute
execute()
Do the actual work.
Definition: edit.php:46