24 require_once __DIR__ .
'/../includes/Benchmarker.php';
33 parent::__construct();
35 $this->
addOption(
'method',
'One of "validateEmail", "encodeAttribute", '
36 .
'"safeEncodeAttribute", "internalRemoveHtmlTags", "removeSomeTags", "tidy", or "stripAllTags". '
37 .
'Default: (All)',
false,
true );
41 # text with no html simulates an interface message string or a title
42 $textWithNoHtml =
'This could be an article title';
43 $textWithHtmlSm =
'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
44 $textWithHtmlLg = str_repeat(
53 if ( !$method || $method ===
'validateEmail' ) {
54 $benches[
'Sanitizer::validateEmail (valid)'] =
static function () {
57 $benches[
'Sanitizer::validateEmail (invalid)'] =
static function () {
61 if ( !$method || $method ===
'encodeAttribute' ) {
62 $benches[
'Sanitizer::encodeAttribute (simple)'] =
static function () {
65 $benches[
'Sanitizer::encodeAttribute (special)'] =
static function () {
69 if ( !$method || $method ===
'safeEncodeAttribute' ) {
70 $benches[
'Sanitizer::safeEncodeAttribute (simple)'] =
static function () {
73 $benches[
'Sanitizer::safeEncodeAttribute (special)'] =
static function () {
77 if ( !$method || $method ===
'internalRemoveHtmlTags' ) {
78 $tiny = strlen( $textWithNoHtml );
79 $sm = strlen( $textWithHtmlSm );
80 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
81 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
84 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
87 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
91 if ( !$method || $method ===
'tidy' ) {
92 # This matches what DISPLAYTITLE was previously doing to sanitize
94 $tiny = strlen( $textWithNoHtml );
95 $sm = strlen( $textWithHtmlSm );
96 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
97 $doit =
static function ( $text ) {
98 return static function () use ( $text ) {
99 $tidy = new \MediaWiki\Tidy\RemexDriver(
100 new \
MediaWiki\
Config\ServiceOptions( [ MainConfigNames::TidyConfig ], [
101 MainConfigNames::TidyConfig => [
'pwrap' =>
false ],
103 $textWithTags = $tidy->tidy( $text, [ Sanitizer::class,
'armorFrenchSpaces' ] );
109 $benches[
"DISPLAYTITLE tidy (input: $tiny)"] = $doit( $textWithNoHtml );
110 $benches[
"DISPLAYTITLE tidy (input: $sm)"] = $doit( $textWithHtmlSm );
111 $benches[
"DISPLAYTITLE tidy (input: $lg)"] = $doit( $textWithHtmlLg );
113 if ( !$method || $method ===
'removeSomeTags' ) {
114 $tiny = strlen( $textWithNoHtml );
115 $sm = strlen( $textWithHtmlSm );
116 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
117 $benches[
"Sanitizer::removeSomeTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
120 $benches[
"Sanitizer::removeSomeTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
123 $benches[
"Sanitizer::removeSomeTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
127 if ( !$method || $method ===
'stripAllTags' ) {
128 $sm = strlen( $textWithHtmlSm );
129 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
130 $benches[
"Sanitizer::stripAllTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
133 $benches[
"Sanitizer::stripAllTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
138 $this->
bench( $benches );
143 require_once RUN_MAINTENANCE_IF_MAIN;
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.
Base class for benchmark scripts.
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.
A class containing constants representing the names of configuration variables.
static encodeAttribute( $text)
Encode an attribute value for HTML output.
static removeSomeTags(string $text, array $options=[])
Cleans up HTML, removes dangerous tags and attributes, and removes HTML comments; the result will alw...
static internalRemoveHtmlTags( $text, $processCallback=null, $args=[], $extratags=[], $removetags=[])
Cleans up HTML, removes dangerous tags and attributes, and removes HTML comments; BEWARE there may be...
static normalizeCharReferences( $text)
Ensure that any entities and character references are legal for XML and XHTML specifically.
static stripAllTags( $html)
Take a fragment of (potentially invalid) HTML and return a version with any tags removed,...
static validateEmail( $addr)
Does a string look like an e-mail address?
static safeEncodeAttribute( $text)
Encode an attribute value for HTML tags, with extra armoring against further wiki processing.
Interface for configuration instances.