MediaWiki  REL1_31
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 
99 require_once RUN_MAINTENANCE_IF_MAIN;
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
Sanitizer\stripAllTags
static stripAllTags( $html)
Take a fragment of (potentially invalid) HTML and return a version with any tags removed,...
Definition: Sanitizer.php:1981
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:291
Sanitizer\validateEmail
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:2105
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Benchmarker\bench
bench(array $benchs)
Definition: Benchmarker.php:50
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Sanitizer\safeEncodeAttribute
static safeEncodeAttribute( $text)
Encode an attribute value for HTML tags, with extra armoring against further wiki processing.
Definition: Sanitizer.php:1151
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:219
Sanitizer\encodeAttribute
static encodeAttribute( $text)
Encode an attribute value for HTML output.
Definition: Sanitizer.php:1130
BenchmarkSanitizer\execute
execute()
Do the actual work.
Definition: benchmarkSanitizer.php:38
Benchmarker
Base class for benchmark scripts.
Definition: Benchmarker.php:40
$maintClass
$maintClass
Definition: benchmarkSanitizer.php:98
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:254
BenchmarkSanitizer
Maintenance script that benchmarks Sanitizer methods.
Definition: benchmarkSanitizer.php:29
BenchmarkSanitizer\__construct
__construct()
Default constructor.
Definition: benchmarkSanitizer.php:30
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
Sanitizer\removeHTMLtags
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:477
wfRandomString
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Definition: GlobalFunctions.php:305