42 # text with no html simulates an interface message string or a title
43 $textWithNoHtml =
'This could be an article title';
44 $textWithHtmlSm =
'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
45 $textWithHtmlLg = str_repeat(
54 if ( !$method || $method ===
'validateEmail' ) {
55 $benches[
'Sanitizer::validateEmail (valid)'] =
static function () {
56 Sanitizer::validateEmail(
'user@example.org' );
58 $benches[
'Sanitizer::validateEmail (invalid)'] =
static function () {
59 Sanitizer::validateEmail(
'username@example! org' );
62 if ( !$method || $method ===
'encodeAttribute' ) {
63 $benches[
'Sanitizer::encodeAttribute (simple)'] =
static function () {
64 Sanitizer::encodeAttribute(
'simple' );
66 $benches[
'Sanitizer::encodeAttribute (special)'] =
static function () {
67 Sanitizer::encodeAttribute(
":'\"\n https://example" );
70 if ( !$method || $method ===
'safeEncodeAttribute' ) {
71 $benches[
'Sanitizer::safeEncodeAttribute (simple)'] =
static function () {
72 Sanitizer::safeEncodeAttribute(
'simple' );
74 $benches[
'Sanitizer::safeEncodeAttribute (special)'] =
static function () {
75 Sanitizer::safeEncodeAttribute(
":'\"\n https://example" );
78 if ( !$method || $method ===
'internalRemoveHtmlTags' ) {
79 $tiny = strlen( $textWithNoHtml );
80 $sm = strlen( $textWithHtmlSm );
81 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
82 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
83 Sanitizer::internalRemoveHtmlTags( $textWithNoHtml );
85 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
86 Sanitizer::internalRemoveHtmlTags( $textWithHtmlSm );
88 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
89 Sanitizer::internalRemoveHtmlTags( $textWithHtmlLg );
92 if ( !$method || $method ===
'tidy' ) {
93 # This matches what DISPLAYTITLE was previously doing to sanitize
95 $tiny = strlen( $textWithNoHtml );
96 $sm = strlen( $textWithHtmlSm );
97 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
98 $doit =
static function ( $text ) {
99 return static function () use ( $text ) {
100 $tidy = new \MediaWiki\Tidy\RemexDriver(
101 new \
MediaWiki\Config\ServiceOptions( [ MainConfigNames::TidyConfig ], [
102 MainConfigNames::TidyConfig => [
'pwrap' =>
false ],
104 $textWithTags = $tidy->tidy( $text, [ Sanitizer::class,
'armorFrenchSpaces' ] );
105 $textWithTags = Sanitizer::normalizeCharReferences(
106 Sanitizer::internalRemoveHtmlTags( $textWithTags )
110 $benches[
"DISPLAYTITLE tidy (input: $tiny)"] = $doit( $textWithNoHtml );
111 $benches[
"DISPLAYTITLE tidy (input: $sm)"] = $doit( $textWithHtmlSm );
112 $benches[
"DISPLAYTITLE tidy (input: $lg)"] = $doit( $textWithHtmlLg );
114 if ( !$method || $method ===
'removeSomeTags' ) {
115 $tiny = strlen( $textWithNoHtml );
116 $sm = strlen( $textWithHtmlSm );
117 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
118 $benches[
"Sanitizer::removeSomeTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
119 Sanitizer::removeSomeTags( $textWithNoHtml );
121 $benches[
"Sanitizer::removeSomeTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
122 Sanitizer::removeSomeTags( $textWithHtmlSm );
124 $benches[
"Sanitizer::removeSomeTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
125 Sanitizer::removeSomeTags( $textWithHtmlLg );
128 if ( !$method || $method ===
'stripAllTags' ) {
129 $sm = strlen( $textWithHtmlSm );
130 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
131 $benches[
"Sanitizer::stripAllTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
132 Sanitizer::stripAllTags( $textWithHtmlSm );
134 $benches[
"Sanitizer::stripAllTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
135 Sanitizer::stripAllTags( $textWithHtmlLg );
139 $this->
bench( $benches );