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 () {
55 Sanitizer::validateEmail(
'user@example.org' );
57 $benches[
'Sanitizer::validateEmail (invalid)'] =
static function () {
58 Sanitizer::validateEmail(
'username@example! org' );
61 if ( !$method || $method ===
'encodeAttribute' ) {
62 $benches[
'Sanitizer::encodeAttribute (simple)'] =
static function () {
63 Sanitizer::encodeAttribute(
'simple' );
65 $benches[
'Sanitizer::encodeAttribute (special)'] =
static function () {
66 Sanitizer::encodeAttribute(
":'\"\n https://example" );
69 if ( !$method || $method ===
'safeEncodeAttribute' ) {
70 $benches[
'Sanitizer::safeEncodeAttribute (simple)'] =
static function () {
71 Sanitizer::safeEncodeAttribute(
'simple' );
73 $benches[
'Sanitizer::safeEncodeAttribute (special)'] =
static function () {
74 Sanitizer::safeEncodeAttribute(
":'\"\n https://example" );
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 ) {
82 Sanitizer::internalRemoveHtmlTags( $textWithNoHtml );
84 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
85 Sanitizer::internalRemoveHtmlTags( $textWithHtmlSm );
87 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
88 Sanitizer::internalRemoveHtmlTags( $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' ] );
104 $textWithTags = Sanitizer::normalizeCharReferences(
105 Sanitizer::internalRemoveHtmlTags( $textWithTags )
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 ) {
118 Sanitizer::removeSomeTags( $textWithNoHtml );
120 $benches[
"Sanitizer::removeSomeTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
121 Sanitizer::removeSomeTags( $textWithHtmlSm );
123 $benches[
"Sanitizer::removeSomeTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
124 Sanitizer::removeSomeTags( $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 ) {
131 Sanitizer::stripAllTags( $textWithHtmlSm );
133 $benches[
"Sanitizer::stripAllTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
134 Sanitizer::stripAllTags( $textWithHtmlLg );
138 $this->
bench( $benches );