MediaWiki master
makeTestEdits.php
Go to the documentation of this file.
1<?php
9// @codeCoverageIgnoreStart
10require_once __DIR__ . '/Maintenance.php';
11// @codeCoverageIgnoreEnd
12
17
24 public function __construct() {
25 parent::__construct();
26 $this->addDescription( 'Make test edits for a user' );
27 $this->addOption( 'user', 'User name', true, true );
28 $this->addOption( 'count', 'Number of edits', true, true );
29 $this->addOption( 'namespace', 'Namespace number', false, true );
30 $this->setBatchSize( 100 );
31 }
32
33 public function execute() {
34 $user = User::newFromName( $this->getOption( 'user' ) );
35 if ( !$user->isRegistered() ) {
36 $this->fatalError( "No such user exists." );
37 }
38
39 $count = $this->getOption( 'count' );
40 $namespace = (int)$this->getOption( 'namespace', 0 );
41 $batchSize = $this->getBatchSize();
42 $services = $this->getServiceContainer();
43 $wikiPageFactory = $services->getWikiPageFactory();
44
46 $titleBatches = $this->newBatchIterator(
47 static function () use ( $namespace, $count ) {
48 for ( $i = 0; $i < $count; ++$i ) {
49 yield Title::makeTitleSafe( $namespace, "Page " . wfRandomString( 2 ) );
50 }
51 }
52 );
53
54 foreach ( $titleBatches as $titleBatch ) {
55 $this->beginTransactionRound( __METHOD__ );
56 foreach ( $titleBatch as $title ) {
57 $page = $wikiPageFactory->newFromTitle( $title );
58 $content = ContentHandler::makeContent( wfRandomString(), $title );
59 $summary = "Change " . wfRandomString( 6 );
60
61 $page->doUserEditContent( $content, $user, $summary );
62
63 $this->output( "Edited $title\n" );
64 }
65 $this->commitTransactionRound( __METHOD__ );
66 }
67
68 $this->output( "Done\n" );
69 }
70}
71
72// @codeCoverageIgnoreStart
73$maintClass = MakeTestEdits::class;
74require_once RUN_MAINTENANCE_IF_MAIN;
75// @codeCoverageIgnoreEnd
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Make test edits for a user to populate a test wiki.
execute()
Do the actual work.
__construct()
Default constructor.
Base class for content handling.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
newBatchIterator( $source)
Wrap an entry iterator into a generator that returns batches of said entries.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Represents a title within MediaWiki.
Definition Title.php:69
User class for the MediaWiki software.
Definition User.php:130
$maintClass