45 # text with no html simulates an interface message string or a title
46 $textWithNoHtml =
'This could be an article title';
47 $textWithHtmlSm =
'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
48 $textWithHtmlLg = str_repeat(
57 if ( !$method || $method ===
'validateEmail' ) {
58 $benches[
'Sanitizer::validateEmail (valid)'] =
static function () {
59 Sanitizer::validateEmail(
'user@example.org' );
61 $benches[
'Sanitizer::validateEmail (invalid)'] =
static function () {
62 Sanitizer::validateEmail(
'username@example! org' );
65 if ( !$method || $method ===
'encodeAttribute' ) {
66 $benches[
'Sanitizer::encodeAttribute (simple)'] =
static function () {
67 Sanitizer::encodeAttribute(
'simple' );
69 $benches[
'Sanitizer::encodeAttribute (special)'] =
static function () {
70 Sanitizer::encodeAttribute(
":'\"\n https://example" );
73 if ( !$method || $method ===
'safeEncodeAttribute' ) {
74 $benches[
'Sanitizer::safeEncodeAttribute (simple)'] =
static function () {
75 Sanitizer::safeEncodeAttribute(
'simple' );
77 $benches[
'Sanitizer::safeEncodeAttribute (special)'] =
static function () {
78 Sanitizer::safeEncodeAttribute(
":'\"\n https://example" );
81 if ( !$method || $method ===
'internalRemoveHtmlTags' ) {
82 $tiny = strlen( $textWithNoHtml );
83 $sm = strlen( $textWithHtmlSm );
84 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
85 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
86 Sanitizer::internalRemoveHtmlTags( $textWithNoHtml );
88 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
89 Sanitizer::internalRemoveHtmlTags( $textWithHtmlSm );
91 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
92 Sanitizer::internalRemoveHtmlTags( $textWithHtmlLg );
95 if ( !$method || $method ===
'tidy' ) {
96 # This matches what DISPLAYTITLE was previously doing to sanitize
98 $tiny = strlen( $textWithNoHtml );
99 $sm = strlen( $textWithHtmlSm );
100 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
101 $doit =
static function ( $text ) {
102 return static function () use ( $text ) {
103 $tidy = new \MediaWiki\Tidy\RemexDriver(
104 new \
MediaWiki\Config\ServiceOptions( [ MainConfigNames::TidyConfig ], [
105 MainConfigNames::TidyConfig => [
'pwrap' =>
false ],
107 $textWithTags = $tidy->tidy( $text, [ Sanitizer::class,
'armorFrenchSpaces' ] );
108 $textWithTags = Sanitizer::normalizeCharReferences(
109 Sanitizer::internalRemoveHtmlTags( $textWithTags )
113 $benches[
"DISPLAYTITLE tidy (input: $tiny)"] = $doit( $textWithNoHtml );
114 $benches[
"DISPLAYTITLE tidy (input: $sm)"] = $doit( $textWithHtmlSm );
115 $benches[
"DISPLAYTITLE tidy (input: $lg)"] = $doit( $textWithHtmlLg );
117 if ( !$method || $method ===
'removeSomeTags' ) {
118 $tiny = strlen( $textWithNoHtml );
119 $sm = strlen( $textWithHtmlSm );
120 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
121 $benches[
"Sanitizer::removeSomeTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
122 Sanitizer::removeSomeTags( $textWithNoHtml );
124 $benches[
"Sanitizer::removeSomeTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
125 Sanitizer::removeSomeTags( $textWithHtmlSm );
127 $benches[
"Sanitizer::removeSomeTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
128 Sanitizer::removeSomeTags( $textWithHtmlLg );
131 if ( !$method || $method ===
'stripAllTags' ) {
132 $sm = strlen( $textWithHtmlSm );
133 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
134 $benches[
"Sanitizer::stripAllTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
135 Sanitizer::stripAllTags( $textWithHtmlSm );
137 $benches[
"Sanitizer::stripAllTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
138 Sanitizer::stripAllTags( $textWithHtmlLg );
142 $this->
bench( $benches );