Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.59% |
65 / 68 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
EditCLI | |
95.59% |
65 / 68 |
|
50.00% |
1 / 2 |
20 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
94.00% |
47 / 50 |
|
0.00% |
0 / 1 |
19.08 |
1 | <?php |
2 | /** |
3 | * Make a page edit. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Maintenance |
22 | */ |
23 | |
24 | use MediaWiki\CommentStore\CommentStoreComment; |
25 | use MediaWiki\Content\ContentHandler; |
26 | use MediaWiki\Language\RawMessage; |
27 | use MediaWiki\Maintenance\Maintenance; |
28 | use MediaWiki\Revision\SlotRecord; |
29 | use MediaWiki\StubObject\StubGlobalUser; |
30 | use MediaWiki\Title\Title; |
31 | use MediaWiki\User\User; |
32 | |
33 | // @codeCoverageIgnoreStart |
34 | require_once __DIR__ . '/Maintenance.php'; |
35 | // @codeCoverageIgnoreEnd |
36 | |
37 | /** |
38 | * Maintenance script to make a page edit. |
39 | * |
40 | * @ingroup Maintenance |
41 | */ |
42 | class EditCLI extends Maintenance { |
43 | public function __construct() { |
44 | parent::__construct(); |
45 | $this->addDescription( 'Edit an article from the command line, text is from stdin' ); |
46 | $this->addOption( 'user', 'Username', false, true, 'u' ); |
47 | $this->addOption( 'summary', 'Edit summary', false, true, 's' ); |
48 | $this->addOption( 'remove', 'Remove a slot (requires --slot).', false, false ); |
49 | $this->addOption( 'minor', 'Minor edit', false, false, 'm' ); |
50 | $this->addOption( 'bot', 'Bot edit', false, false, 'b' ); |
51 | $this->addOption( 'autosummary', 'Enable autosummary', false, false, 'a' ); |
52 | $this->addOption( 'no-rc', 'Do not show the change in recent changes', false, false, 'r' ); |
53 | $this->addOption( 'nocreate', 'Don\'t create new pages', false, false ); |
54 | $this->addOption( 'createonly', 'Only create new pages', false, false ); |
55 | $this->addOption( 'slot', 'Slot role name', false, true ); |
56 | $this->addOption( |
57 | 'parse-title', |
58 | 'Parse title input as a message, e.g. "{{int:mainpage}}" or "News_{{CURRENTYEAR}}', |
59 | false, false, 'p' |
60 | ); |
61 | $this->addArg( 'title', 'Title of article to edit' ); |
62 | } |
63 | |
64 | public function execute() { |
65 | $userName = $this->getOption( 'user', false ); |
66 | $summary = $this->getOption( 'summary', '' ); |
67 | $remove = $this->hasOption( 'remove' ); |
68 | $minor = $this->hasOption( 'minor' ); |
69 | $bot = $this->hasOption( 'bot' ); |
70 | $autoSummary = $this->hasOption( 'autosummary' ); |
71 | $noRC = $this->hasOption( 'no-rc' ); |
72 | $slot = $this->getOption( 'slot', SlotRecord::MAIN ); |
73 | |
74 | if ( $userName === false ) { |
75 | $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
76 | } else { |
77 | $user = User::newFromName( $userName ); |
78 | } |
79 | if ( !$user ) { |
80 | $this->fatalError( "Invalid username" ); |
81 | } |
82 | if ( $user->isAnon() ) { |
83 | $user->addToDatabase(); |
84 | } |
85 | StubGlobalUser::setUser( $user ); |
86 | |
87 | $titleInput = $this->getArg( 0 ); |
88 | |
89 | if ( $this->hasOption( 'parse-title' ) ) { |
90 | $titleInput = ( new RawMessage( '$1' ) )->params( $titleInput )->text(); |
91 | } |
92 | |
93 | $title = Title::newFromText( $titleInput ); |
94 | if ( !$title ) { |
95 | $this->fatalError( "Invalid title" ); |
96 | } |
97 | |
98 | if ( $this->hasOption( 'nocreate' ) && !$title->exists() ) { |
99 | $this->fatalError( "Page does not exist" ); |
100 | } elseif ( $this->hasOption( 'createonly' ) && $title->exists() ) { |
101 | $this->fatalError( "Page already exists" ); |
102 | } |
103 | |
104 | $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
105 | |
106 | if ( $remove ) { |
107 | if ( $slot === SlotRecord::MAIN ) { |
108 | $this->fatalError( "Cannot remove main slot! Use --slot to specify." ); |
109 | } |
110 | |
111 | $content = false; |
112 | } else { |
113 | # Read the text |
114 | $text = $this->getStdin( Maintenance::STDIN_ALL ); |
115 | $content = ContentHandler::makeContent( $text, $title ); |
116 | } |
117 | |
118 | # Do the edit |
119 | $this->output( "Saving..." ); |
120 | $updater = $page->newPageUpdater( $user ); |
121 | |
122 | $flags = ( $minor ? EDIT_MINOR : 0 ) | |
123 | ( $bot ? EDIT_FORCE_BOT : 0 ) | |
124 | ( $autoSummary ? EDIT_AUTOSUMMARY : 0 ) | |
125 | ( $noRC ? EDIT_SUPPRESS_RC : 0 ); |
126 | |
127 | if ( $content === false ) { |
128 | $updater->removeSlot( $slot ); |
129 | } else { |
130 | $updater->setContent( $slot, $content ); |
131 | } |
132 | |
133 | $updater->saveRevision( CommentStoreComment::newUnsavedComment( $summary ), $flags ); |
134 | $status = $updater->getStatus(); |
135 | |
136 | if ( $status->isOK() ) { |
137 | $this->output( "done\n" ); |
138 | } else { |
139 | $this->output( "failed\n" ); |
140 | } |
141 | if ( !$status->isGood() ) { |
142 | $this->error( $status ); |
143 | } |
144 | return $status->isOK(); |
145 | } |
146 | } |
147 | |
148 | // @codeCoverageIgnoreStart |
149 | $maintClass = EditCLI::class; |
150 | require_once RUN_MAINTENANCE_IF_MAIN; |
151 | // @codeCoverageIgnoreEnd |