MediaWiki  REL1_23
edit.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/Maintenance.php';
00025 
00031 class EditCLI extends Maintenance {
00032     public function __construct() {
00033         parent::__construct();
00034         $this->mDescription = "Edit an article from the command line, text is from stdin";
00035         $this->addOption( 'user', 'Username', false, true, 'u' );
00036         $this->addOption( 'summary', 'Edit summary', false, true, 's' );
00037         $this->addOption( 'minor', 'Minor edit', false, false, 'm' );
00038         $this->addOption( 'bot', 'Bot edit', false, false, 'b' );
00039         $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' );
00040         $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' );
00041         $this->addOption( 'nocreate', 'Don\'t create new pages', false, false );
00042         $this->addOption( 'createonly', 'Only create new pages', false, false );
00043         $this->addArg( 'title', 'Title of article to edit' );
00044     }
00045 
00046     public function execute() {
00047         global $wgUser;
00048 
00049         $userName = $this->getOption( 'user', 'Maintenance script' );
00050         $summary = $this->getOption( 'summary', '' );
00051         $minor = $this->hasOption( 'minor' );
00052         $bot = $this->hasOption( 'bot' );
00053         $autoSummary = $this->hasOption( 'autosummary' );
00054         $noRC = $this->hasOption( 'no-rc' );
00055 
00056         $wgUser = User::newFromName( $userName );
00057         $context = RequestContext::getMain();
00058         $context->setUser( $wgUser );
00059         if ( !$wgUser ) {
00060             $this->error( "Invalid username", true );
00061         }
00062         if ( $wgUser->isAnon() ) {
00063             $wgUser->addToDatabase();
00064         }
00065 
00066         $title = Title::newFromText( $this->getArg() );
00067         if ( !$title ) {
00068             $this->error( "Invalid title", true );
00069         }
00070         $context->setTitle( $title );
00071 
00072         if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
00073             $this->error( "Page does not exist", true );
00074         } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
00075             $this->error( "Page already exists", true );
00076         }
00077 
00078         $page = WikiPage::factory( $title );
00079 
00080         # Read the text
00081         $text = $this->getStdin( Maintenance::STDIN_ALL );
00082         $content = ContentHandler::makeContent( $text, $title );
00083 
00084         # Do the edit
00085         $this->output( "Saving... " );
00086         $status = $page->doEditContent( $content, $summary,
00087             ( $minor ? EDIT_MINOR : 0 ) |
00088             ( $bot ? EDIT_FORCE_BOT : 0 ) |
00089             ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
00090             ( $noRC ? EDIT_SUPPRESS_RC : 0 ) );
00091         if ( $status->isOK() ) {
00092             $this->output( "done\n" );
00093             $exit = 0;
00094         } else {
00095             $this->output( "failed\n" );
00096             $exit = 1;
00097         }
00098         if ( !$status->isGood() ) {
00099             $this->output( $status->getWikiText() . "\n" );
00100         }
00101         exit( $exit );
00102     }
00103 }
00104 
00105 $maintClass = "EditCLI";
00106 require_once RUN_MAINTENANCE_IF_MAIN;