MediaWiki  master
benchmarkCommentFormatter.php
Go to the documentation of this file.
1 <?php
2 
5 
6 require_once __DIR__ . '/../includes/Benchmarker.php';
7 
9  public function __construct() {
10  parent::__construct();
11  $this->addDescription( 'Benchmark Linker::formatComment()' );
12  $this->addOption( 'file', 'A JSON API result from list=recentchanges',
13  false, true );
14  }
15 
16  public function execute() {
17  $file = $this->getOption( 'file',
18  __DIR__ . '/data/CommentFormatter/rc100-2021-07-29.json' );
19  $json = file_get_contents( $file );
20  if ( !$json ) {
21  $this->fatalError( "Unable to read input file \"$file\"" );
22  }
23  $result = json_decode( $json, true );
24  if ( !isset( $result['query']['recentchanges'] ) ) {
25  $this->fatalError( "Invalid JSON data" );
26  }
27  $entries = $result['query']['recentchanges'];
28  $inputs = [];
29  $comments = [];
30  foreach ( $entries as $entry ) {
31  $inputs[] = [
32  'comment' => $entry['comment'],
33  'title' => Title::newFromText( $entry['title'] )
34  ];
35  $comments[] = $entry['comment'];
36  }
37  $this->bench( [
38  'Linker::formatComment' => [
39  'function' => static function () use ( $inputs ) {
41  foreach ( $inputs as $input ) {
43  $input['comment'],
44  $input['title']
45  );
46  }
47  },
48  ],
49 
50  'CommentFormatter::createBatch' => [
51  'function' => static function () use ( $inputs ) {
53  $formatter = MediaWikiServices::getInstance()->getCommentFormatter();
54  $comments = [];
55  foreach ( $inputs as $input ) {
56  $comments[] = ( new CommentItem( $input['comment'] ) )
57  ->selfLinkTarget( $input['title'] );
58  }
59  $formatter->createBatch()
60  ->comments( $comments )
61  ->execute();
62  }
63  ],
64 
65  'CommentFormatter::formatStrings' => [
66  'function' => static function () use ( $comments ) {
68  $formatter = MediaWikiServices::getInstance()->getCommentFormatter();
69  $formatter->formatStrings( $comments );
70  }
71  ],
72 
73  ] );
74  }
75 }
76 
77 $maintClass = BenchmarkCommentFormatter::class;
78 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
$maintClass
Definition: benchmarkCommentFormatter.php:77
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:382
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
Title\clearCaches
static clearCaches()
Definition: Title.php:3008
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Benchmarker\bench
bench(array $benchs)
Definition: Benchmarker.php:49
MediaWiki\CommentFormatter\CommentItem
An object to represent one of the inputs to a batch formatting operation.
Definition: CommentItem.php:13
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
BenchmarkCommentFormatter\execute
execute()
Do the actual work.
Definition: benchmarkCommentFormatter.php:16
BenchmarkCommentFormatter\__construct
__construct()
Default constructor.
Definition: benchmarkCommentFormatter.php:9
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
Linker\formatComment
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1372
BenchmarkCommentFormatter
Definition: benchmarkCommentFormatter.php:8
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286