MediaWiki  master
benchmarkSanitizer.php
Go to the documentation of this file.
1 <?php
22 require_once __DIR__ . '/Benchmarker.php';
23 
30  public function __construct() {
31  parent::__construct();
32  $this->addDescription( 'Benchmark for Sanitizer methods.' );
33  $this->addOption( 'method', 'One of "validateEmail", "encodeAttribute", '
34  . '"safeEncodeAttribute", "removeHTMLtags", or "stripAllTags". '
35  . 'Default: (All)', false, true );
36  }
37 
38  public function execute() {
39  $textWithHtmlSm = 'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
40  $textWithHtmlLg = str_repeat(
41  // 28K (28 chars * 1000)
42  wfRandomString( 3 ) . ' <tag>' . wfRandomString( 5 ) . '</tag> ' . wfRandomString( 7 ),
43  1000
44  );
45 
46  $method = $this->getOption( 'method' );
47  $benches = [];
48 
49  if ( !$method || $method === 'validateEmail' ) {
50  $benches['Sanitizer::validateEmail (valid)'] = function () {
51  Sanitizer::validateEmail( 'user@example.org' );
52  };
53  $benches['Sanitizer::validateEmail (invalid)'] = function () {
54  Sanitizer::validateEmail( 'username@example! org' );
55  };
56  }
57  if ( !$method || $method === 'encodeAttribute' ) {
58  $benches['Sanitizer::encodeAttribute (simple)'] = function () {
59  Sanitizer::encodeAttribute( 'simple' );
60  };
61  $benches['Sanitizer::encodeAttribute (special)'] = function () {
62  Sanitizer::encodeAttribute( ":'\"\n https://example" );
63  };
64  }
65  if ( !$method || $method === 'safeEncodeAttribute' ) {
66  $benches['Sanitizer::safeEncodeAttribute (simple)'] = function () {
68  };
69  $benches['Sanitizer::safeEncodeAttribute (special)'] = function () {
70  Sanitizer::safeEncodeAttribute( ":'\"\n https://example" );
71  };
72  }
73  if ( !$method || $method === 'removeHTMLtags' ) {
74  $sm = strlen( $textWithHtmlSm );
75  $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
76  $benches["Sanitizer::removeHTMLtags (input: $sm)"] = function () use ( $textWithHtmlSm ) {
77  Sanitizer::removeHTMLtags( $textWithHtmlSm );
78  };
79  $benches["Sanitizer::removeHTMLtags (input: $lg)"] = function () use ( $textWithHtmlLg ) {
80  Sanitizer::removeHTMLtags( $textWithHtmlLg );
81  };
82  }
83  if ( !$method || $method === 'stripAllTags' ) {
84  $sm = strlen( $textWithHtmlSm );
85  $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
86  $benches["Sanitizer::stripAllTags (input: $sm)"] = function () use ( $textWithHtmlSm ) {
87  Sanitizer::stripAllTags( $textWithHtmlSm );
88  };
89  $benches["Sanitizer::stripAllTags (input: $lg)"] = function () use ( $textWithHtmlLg ) {
90  Sanitizer::stripAllTags( $textWithHtmlLg );
91  };
92  }
93 
94  $this->bench( $benches );
95  }
96 }
97 
98 $maintClass = BenchmarkSanitizer::class;
99 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script that benchmarks Sanitizer methods.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
getOption( $name, $default=null)
Get an option, or return the default.
bench(array $benchs)
Definition: Benchmarker.php:50
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:2165
static stripAllTags( $html)
Take a fragment of (potentially invalid) HTML and return a version with any tags removed, encoded as plain text.
Definition: Sanitizer.php:2041
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
addDescription( $text)
Set the description text.
static encodeAttribute( $text)
Encode an attribute value for HTML output.
Definition: Sanitizer.php:1156
Base class for benchmark scripts.
Definition: Benchmarker.php:40
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static removeHTMLtags( $text, $processCallback=null, $args=[], $extratags=[], $removetags=[], $warnCallback=null)
Cleans up HTML, removes dangerous tags and attributes, and removes HTML comments. ...
Definition: Sanitizer.php:497
static safeEncodeAttribute( $text)
Encode an attribute value for HTML tags, with extra armoring against further wiki processing...
Definition: Sanitizer.php:1199