MediaWiki  master
benchmarkCommentFormatter.php
Go to the documentation of this file.
1 <?php
2 
7 
8 require_once __DIR__ . '/../includes/Benchmarker.php';
9 
11  public function __construct() {
12  parent::__construct();
13  $this->addDescription( 'Benchmark Linker::formatComment()' );
14  $this->addOption( 'file', 'A JSON API result from list=recentchanges',
15  false, true );
16  }
17 
18  public function execute() {
19  $file = $this->getOption( 'file',
20  __DIR__ . '/data/CommentFormatter/rc100-2021-07-29.json' );
21  $json = file_get_contents( $file );
22  if ( !$json ) {
23  $this->fatalError( "Unable to read input file \"$file\"" );
24  }
25  $result = json_decode( $json, true );
26  if ( !isset( $result['query']['recentchanges'] ) ) {
27  $this->fatalError( "Invalid JSON data" );
28  }
29  $entries = $result['query']['recentchanges'];
30  $inputs = [];
31  $comments = [];
32  foreach ( $entries as $entry ) {
33  $inputs[] = [
34  'comment' => $entry['comment'],
35  'title' => Title::newFromText( $entry['title'] )
36  ];
37  $comments[] = $entry['comment'];
38  }
39  $this->bench( [
40  'Linker::formatComment' => [
41  'function' => static function () use ( $inputs ) {
42  Title::clearCaches();
43  foreach ( $inputs as $input ) {
44  Linker::formatComment(
45  $input['comment'],
46  $input['title']
47  );
48  }
49  },
50  ],
51 
52  'CommentFormatter::createBatch' => [
53  'function' => static function () use ( $inputs ) {
54  Title::clearCaches();
55  $formatter = MediaWikiServices::getInstance()->getCommentFormatter();
56  $comments = [];
57  foreach ( $inputs as $input ) {
58  $comments[] = ( new CommentItem( $input['comment'] ) )
59  ->selfLinkTarget( $input['title'] );
60  }
61  $formatter->createBatch()
62  ->comments( $comments )
63  ->execute();
64  }
65  ],
66 
67  'CommentFormatter::formatStrings' => [
68  'function' => static function () use ( $comments ) {
69  Title::clearCaches();
70  $formatter = MediaWikiServices::getInstance()->getCommentFormatter();
71  $formatter->formatStrings( $comments );
72  }
73  ],
74 
75  ] );
76  }
77 }
78 
79 $maintClass = BenchmarkCommentFormatter::class;
80 require_once RUN_MAINTENANCE_IF_MAIN;
Base class for benchmark scripts.
Definition: Benchmarker.php:40
bench(array $benchs)
Definition: Benchmarker.php:49
addDescription( $text)
Set the description text.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
An object to represent one of the inputs to a batch formatting operation.
Definition: CommentItem.php:13
Some internal bits split of from Skin.php.
Definition: Linker.php:65
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition: Title.php:82
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42