MediaWiki  REL1_22
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->addArg( 'title', 'Title of article to edit' );
00042     }
00043 
00044     public function execute() {
00045         global $wgUser, $wgTitle;
00046 
00047         $userName = $this->getOption( 'user', 'Maintenance script' );
00048         $summary = $this->getOption( 'summary', '' );
00049         $minor = $this->hasOption( 'minor' );
00050         $bot = $this->hasOption( 'bot' );
00051         $autoSummary = $this->hasOption( 'autosummary' );
00052         $noRC = $this->hasOption( 'no-rc' );
00053 
00054         $wgUser = User::newFromName( $userName );
00055         $context = RequestContext::getMain();
00056         $context->setUser( $wgUser );
00057         if ( !$wgUser ) {
00058             $this->error( "Invalid username", true );
00059         }
00060         if ( $wgUser->isAnon() ) {
00061             $wgUser->addToDatabase();
00062         }
00063 
00064         $wgTitle = Title::newFromText( $this->getArg() );
00065         if ( !$wgTitle ) {
00066             $this->error( "Invalid title", true );
00067         }
00068         $context->setTitle( $wgTitle );
00069 
00070         $page = WikiPage::factory( $wgTitle );
00071 
00072         # Read the text
00073         $text = $this->getStdin( Maintenance::STDIN_ALL );
00074         $content = ContentHandler::makeContent( $text, $wgTitle );
00075 
00076         # Do the edit
00077         $this->output( "Saving... " );
00078         $status = $page->doEditContent( $content, $summary,
00079             ( $minor ? EDIT_MINOR : 0 ) |
00080             ( $bot ? EDIT_FORCE_BOT : 0 ) |
00081             ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
00082             ( $noRC ? EDIT_SUPPRESS_RC : 0 ) );
00083         if ( $status->isOK() ) {
00084             $this->output( "done\n" );
00085             $exit = 0;
00086         } else {
00087             $this->output( "failed\n" );
00088             $exit = 1;
00089         }
00090         if ( !$status->isGood() ) {
00091             $this->output( $status->getWikiText() . "\n" );
00092         }
00093         exit( $exit );
00094     }
00095 }
00096 
00097 $maintClass = "EditCLI";
00098 require_once RUN_MAINTENANCE_IF_MAIN;