Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / maintenance / makeTestEdits.php
blobe08dc9c0734e4547d929764e3e429a662dc157c8
1 <?php
2 /**
3 * Make test edits for a user to populate a test wiki
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.
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.
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
20 * @file
21 * @ingroup Maintenance
23 // @codeCoverageIgnoreStart
24 require_once __DIR__ . '/Maintenance.php';
25 // @codeCoverageIgnoreEnd
27 use MediaWiki\Content\ContentHandler;
28 use MediaWiki\Maintenance\Maintenance;
29 use MediaWiki\Title\Title;
30 use MediaWiki\User\User;
32 /**
33 * Make test edits for a user to populate a test wiki
35 * @ingroup Maintenance
37 class MakeTestEdits extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Make test edits for a user' );
41 $this->addOption( 'user', 'User name', true, true );
42 $this->addOption( 'count', 'Number of edits', true, true );
43 $this->addOption( 'namespace', 'Namespace number', false, true );
44 $this->setBatchSize( 100 );
47 public function execute() {
48 $user = User::newFromName( $this->getOption( 'user' ) );
49 if ( !$user->isRegistered() ) {
50 $this->fatalError( "No such user exists." );
53 $count = $this->getOption( 'count' );
54 $namespace = (int)$this->getOption( 'namespace', 0 );
55 $batchSize = $this->getBatchSize();
56 $services = $this->getServiceContainer();
57 $wikiPageFactory = $services->getWikiPageFactory();
59 /** @var iterable<Title[]> $titleBatches */
60 $titleBatches = $this->newBatchIterator(
61 static function () use ( $namespace, $count ) {
62 for ( $i = 0; $i < $count; ++$i ) {
63 yield Title::makeTitleSafe( $namespace, "Page " . wfRandomString( 2 ) );
68 foreach ( $titleBatches as $titleBatch ) {
69 $this->beginTransactionRound( __METHOD__ );
70 foreach ( $titleBatch as $title ) {
71 $page = $wikiPageFactory->newFromTitle( $title );
72 $content = ContentHandler::makeContent( wfRandomString(), $title );
73 $summary = "Change " . wfRandomString( 6 );
75 $page->doUserEditContent( $content, $user, $summary );
77 $this->output( "Edited $title\n" );
79 $this->commitTransactionRound( __METHOD__ );
82 $this->output( "Done\n" );
86 // @codeCoverageIgnoreStart
87 $maintClass = MakeTestEdits::class;
88 require_once RUN_MAINTENANCE_IF_MAIN;
89 // @codeCoverageIgnoreEnd