| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 25 |
use MediaWiki\Content\ContentHandler; |
| 26 |
use MediaWiki\Language\RawMessage; |
| 27 |
use MediaWiki\Revision\SlotRecord; |
| 28 |
use MediaWiki\StubObject\StubGlobalUser; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use MediaWiki\User\User; |
| 31 |
|
| 32 |
|
| 33 |
require_once __DIR__ . '/Maintenance.php'; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class EditCLI extends Maintenance { |
| 42 |
public function __construct() { |
| 43 |
parent::__construct(); |
| 44 |
$this->addDescription( 'Edit an article from the command line, text is from stdin' ); |
| 45 |
$this->addOption( 'user', 'Username', false, true, 'u' ); |
| 46 |
$this->addOption( 'summary', 'Edit summary', false, true, 's' ); |
| 47 |
$this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false ); |
| 48 |
$this->addOption( 'minor', 'Minor edit', false, false, 'm' ); |
| 49 |
$this->addOption( 'bot', 'Bot edit', false, false, 'b' ); |
| 50 |
$this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' ); |
| 51 |
$this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' ); |
| 52 |
$this->addOption( 'nocreate', 'Don\'t create new pages', false, false ); |
| 53 |
$this->addOption( 'createonly', 'Only create new pages', false, false ); |
| 54 |
$this->addOption( 'slot', 'Slot role name', false, true ); |
| 55 |
$this->addOption( |
| 56 |
'parse-title', |
| 57 |
'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}', |
| 58 |
false, false, 'p' |
| 59 |
); |
| 60 |
$this->addArg( 'title', 'Title of article to edit' ); |
| 61 |
} |
| 62 |
|
| 63 |
public function execute() { |
| 64 |
$userName = $this->getOption( 'user', false ); |
| 65 |
$summary = $this->getOption( 'summary', '' ); |
| 66 |
$remove = $this->hasOption( 'remove' ); |
| 67 |
$minor = $this->hasOption( 'minor' ); |
| 68 |
$bot = $this->hasOption( 'bot' ); |
| 69 |
$autoSummary = $this->hasOption( 'autosummary' ); |
| 70 |
$noRC = $this->hasOption( 'no-rc' ); |
| 71 |
$slot = $this->getOption( 'slot', SlotRecord::MAIN ); |
| 72 |
|
| 73 |
if ( $userName === false ) { |
| 74 |
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 75 |
} else { |
| 76 |
$user = User::newFromName( $userName ); |
| 77 |
} |
| 78 |
if ( !$user ) { |
| 79 |
$this->fatalError( "Invalid username" ); |
| 80 |
} |
| 81 |
if ( $user->isAnon() ) { |
| 82 |
$user->addToDatabase(); |
| 83 |
} |
| 84 |
StubGlobalUser::setUser( $user ); |
| 85 |
|
| 86 |
$titleInput = $this->getArg( 0 ); |
| 87 |
|
| 88 |
if ( $this->hasOption( 'parse-title' ) ) { |
| 89 |
$titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text(); |
| 90 |
} |
| 91 |
|
| 92 |
$title = Title::newFromText( $titleInput ); |
| 93 |
if ( !$title ) { |
| 94 |
$this->fatalError( "Invalid title" ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) { |
| 98 |
$this->fatalError( "Page does not exist" ); |
| 99 |
} elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) { |
| 100 |
$this->fatalError( "Page already exists" ); |
| 101 |
} |
| 102 |
|
| 103 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 104 |
|
| 105 |
if ( $remove ) { |
| 106 |
if ( $slot === SlotRecord::MAIN ) { |
| 107 |
$this->fatalError( "Cannot remove main slot! Use --slot to specify." ); |
| 108 |
} |
| 109 |
|
| 110 |
$content = false; |
| 111 |
} else { |
| 112 |
|
| 113 |
$text = $this->getStdin( Maintenance::STDIN_ALL ); |
| 114 |
$content = ContentHandler::makeContent( $text, $title ); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
$this->output( "Saving..." ); |
| 119 |
$updater = $page->newPageUpdater( $user ); |
| 120 |
|
| 121 |
$flags = ( $minor ? EDIT_MINOR : 0 ) | |
| 122 |
( $bot ? EDIT_FORCE_BOT : 0 ) | |
| 123 |
( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) | |
| 124 |
( $noRC ? EDIT_SUPPRESS_RC : 0 ); |
| 125 |
|
| 126 |
if ( $content === false ) { |
| 127 |
$updater->removeSlot( $slot ); |
| 128 |
} else { |
| 129 |
$updater->setContent( $slot, $content ); |
| 130 |
} |
| 131 |
|
| 132 |
$updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags ); |
| 133 |
$status = $updater->getStatus(); |
| 134 |
|
| 135 |
if ( $status->isOK() ) { |
| 136 |
$this->output( "done\n" ); |
| 137 |
} else { |
| 138 |
$this->output( "failed\n" ); |
| 139 |
} |
| 140 |
if ( !$status->isGood() ) { |
| 141 |
$this->error( $status ); |
| 142 |
} |
| 143 |
return $status->isOK(); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
$maintClass = EditCLI::class; |
| 149 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 150 |
|