MediaWiki master
benchmarkSanitizer.php
Go to the documentation of this file.
1<?php
11
12// @codeCoverageIgnoreStart
13require_once __DIR__ . '/../includes/Benchmarker.php';
14// @codeCoverageIgnoreEnd
15
22 public function __construct() {
23 parent::__construct();
24 $this->addDescription( 'Benchmark for Sanitizer methods.' );
25 $this->addOption( 'method', 'One of "validateEmail", "encodeAttribute", '
26 . '"safeEncodeAttribute", "internalRemoveHtmlTags", "removeSomeTags", "tidy", or "stripAllTags". '
27 . 'Default: (All)', false, true );
28 }
29
30 public function execute() {
31 # text with no html simulates an interface message string or a title
32 $textWithNoHtml = 'This could be an article title';
33 $textWithHtmlSm = 'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
34 $textWithHtmlLg = str_repeat(
35 // 28K (28 chars * 1000)
36 wfRandomString( 3 ) . ' <tag>' . wfRandomString( 5 ) . '</tag> ' . wfRandomString( 7 ),
37 1000
38 );
39
40 $method = $this->getOption( 'method' );
41 $benches = [];
42
43 if ( !$method || $method === 'validateEmail' ) {
44 $benches['Sanitizer::validateEmail (valid)'] = static function () {
45 Sanitizer::validateEmail( 'user@example.org' );
46 };
47 $benches['Sanitizer::validateEmail (invalid)'] = static function () {
48 Sanitizer::validateEmail( 'username@example! org' );
49 };
50 }
51 if ( !$method || $method === 'encodeAttribute' ) {
52 $benches['Sanitizer::encodeAttribute (simple)'] = static function () {
53 Sanitizer::encodeAttribute( 'simple' );
54 };
55 $benches['Sanitizer::encodeAttribute (special)'] = static function () {
56 Sanitizer::encodeAttribute( ":'\"\n https://example" );
57 };
58 }
59 if ( !$method || $method === 'safeEncodeAttribute' ) {
60 $benches['Sanitizer::safeEncodeAttribute (simple)'] = static function () {
61 Sanitizer::safeEncodeAttribute( 'simple' );
62 };
63 $benches['Sanitizer::safeEncodeAttribute (special)'] = static function () {
64 Sanitizer::safeEncodeAttribute( ":'\"\n https://example" );
65 };
66 }
67 if ( !$method || $method === 'internalRemoveHtmlTags' ) {
68 $tiny = strlen( $textWithNoHtml );
69 $sm = strlen( $textWithHtmlSm );
70 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
71 $benches["Sanitizer::internalRemoveHtmlTags (input: $tiny)"] = static function () use ( $textWithNoHtml ) {
72 Sanitizer::internalRemoveHtmlTags( $textWithNoHtml );
73 };
74 $benches["Sanitizer::internalRemoveHtmlTags (input: $sm)"] = static function () use ( $textWithHtmlSm ) {
75 Sanitizer::internalRemoveHtmlTags( $textWithHtmlSm );
76 };
77 $benches["Sanitizer::internalRemoveHtmlTags (input: $lg)"] = static function () use ( $textWithHtmlLg ) {
78 Sanitizer::internalRemoveHtmlTags( $textWithHtmlLg );
79 };
80 }
81 if ( !$method || $method === 'tidy' ) {
82 # This matches what DISPLAYTITLE was previously doing to sanitize
83 # title strings
84 $tiny = strlen( $textWithNoHtml );
85 $sm = strlen( $textWithHtmlSm );
86 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
87 $doit = static function ( $text ) {
88 return static function () use ( $text ) {
89 $tidy = new \MediaWiki\Tidy\RemexDriver(
90 new \MediaWiki\Config\ServiceOptions( [ MainConfigNames::TidyConfig ], [
91 MainConfigNames::TidyConfig => [ 'pwrap' => false ],
92 ] ) );
93 $textWithTags = $tidy->tidy( $text, Sanitizer::armorFrenchSpaces( ... ) );
94 $textWithTags = Sanitizer::normalizeCharReferences(
95 Sanitizer::internalRemoveHtmlTags( $textWithTags )
96 );
97 };
98 };
99 $benches["DISPLAYTITLE tidy (input: $tiny)"] = $doit( $textWithNoHtml );
100 $benches["DISPLAYTITLE tidy (input: $sm)"] = $doit( $textWithHtmlSm );
101 $benches["DISPLAYTITLE tidy (input: $lg)"] = $doit( $textWithHtmlLg );
102 }
103 if ( !$method || $method === 'removeSomeTags' ) {
104 $tiny = strlen( $textWithNoHtml );
105 $sm = strlen( $textWithHtmlSm );
106 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
107 $benches["Sanitizer::removeSomeTags (input: $tiny)"] = static function () use ( $textWithNoHtml ) {
108 Sanitizer::removeSomeTags( $textWithNoHtml );
109 };
110 $benches["Sanitizer::removeSomeTags (input: $sm)"] = static function () use ( $textWithHtmlSm ) {
111 Sanitizer::removeSomeTags( $textWithHtmlSm );
112 };
113 $benches["Sanitizer::removeSomeTags (input: $lg)"] = static function () use ( $textWithHtmlLg ) {
114 Sanitizer::removeSomeTags( $textWithHtmlLg );
115 };
116 }
117 if ( !$method || $method === 'stripAllTags' ) {
118 $sm = strlen( $textWithHtmlSm );
119 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) . 'K';
120 $benches["Sanitizer::stripAllTags (input: $sm)"] = static function () use ( $textWithHtmlSm ) {
121 Sanitizer::stripAllTags( $textWithHtmlSm );
122 };
123 $benches["Sanitizer::stripAllTags (input: $lg)"] = static function () use ( $textWithHtmlLg ) {
124 Sanitizer::stripAllTags( $textWithHtmlLg );
125 };
126 }
127
128 $this->bench( $benches );
129 }
130}
131
132// @codeCoverageIgnoreStart
133$maintClass = BenchmarkSanitizer::class;
134require_once RUN_MAINTENANCE_IF_MAIN;
135// @codeCoverageIgnoreEnd
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Maintenance script that benchmarks Sanitizer methods.
__construct()
Default constructor.
execute()
Do the actual work.
A class containing constants representing the names of configuration variables.
Base class for benchmark scripts.
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.
addDescription( $text)
Set the description text.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:32
Helper trait for implementations \DAO.