MediaWiki  REL1_24
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         if ( !$wgUser ) {
00058             $this->error( "Invalid username", true );
00059         }
00060         if ( $wgUser->isAnon() ) {
00061             $wgUser->addToDatabase();
00062         }
00063 
00064         $title = Title::newFromText( $this->getArg() );
00065         if ( !$title ) {
00066             $this->error( "Invalid title", true );
00067         }
00068 
00069         if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) {
00070             $this->error( "Page does not exist", true );
00071         } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) {
00072             $this->error( "Page already exists", true );
00073         }
00074 
00075         $page = WikiPage::factory( $title );
00076 
00077         # Read the text
00078         $text = $this->getStdin( Maintenance::STDIN_ALL );
00079         $content = ContentHandler::makeContent( $text, $title );
00080 
00081         # Do the edit
00082         $this->output( "Saving... " );
00083         $status = $page->doEditContent( $content, $summary,
00084             ( $minor ? EDIT_MINOR : 0 ) |
00085             ( $bot ? EDIT_FORCE_BOT : 0 ) |
00086             ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) |
00087             ( $noRC ? EDIT_SUPPRESS_RC : 0 ) );
00088         if ( $status->isOK() ) {
00089             $this->output( "done\n" );
00090             $exit = 0;
00091         } else {
00092             $this->output( "failed\n" );
00093             $exit = 1;
00094         }
00095         if ( !$status->isGood() ) {
00096             $this->output( $status->getWikiText() . "\n" );
00097         }
00098         exit( $exit );
00099     }
00100 }
00101 
00102 $maintClass = "EditCLI";
00103 require_once RUN_MAINTENANCE_IF_MAIN;