13require_once __DIR__ .
'/../includes/Benchmarker.php';
23 parent::__construct();
25 $this->
addOption(
'method',
'One of "validateEmail", "encodeAttribute", '
26 .
'"safeEncodeAttribute", "internalRemoveHtmlTags", "removeSomeTags", "tidy", or "stripAllTags". '
27 .
'Default: (All)',
false,
true );
31 # text with no html simulates an interface message string or a title
32 $textWithNoHtml =
'This could be an article title';
33 $textWithHtmlSm =
'Before <wrap><in>and</in> another <unclose> <in>word</in></wrap>.';
34 $textWithHtmlLg = str_repeat(
43 if ( !$method || $method ===
'validateEmail' ) {
44 $benches[
'Sanitizer::validateEmail (valid)'] =
static function () {
47 $benches[
'Sanitizer::validateEmail (invalid)'] =
static function () {
51 if ( !$method || $method ===
'encodeAttribute' ) {
52 $benches[
'Sanitizer::encodeAttribute (simple)'] =
static function () {
55 $benches[
'Sanitizer::encodeAttribute (special)'] =
static function () {
59 if ( !$method || $method ===
'safeEncodeAttribute' ) {
60 $benches[
'Sanitizer::safeEncodeAttribute (simple)'] =
static function () {
63 $benches[
'Sanitizer::safeEncodeAttribute (special)'] =
static function () {
67 if ( !$method || $method ===
'internalRemoveHtmlTags' ) {
68 $tiny = strlen( $textWithNoHtml );
69 $sm = strlen( $textWithHtmlSm );
70 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
71 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
74 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
77 $benches[
"Sanitizer::internalRemoveHtmlTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
81 if ( !$method || $method ===
'tidy' ) {
82 # This matches what DISPLAYTITLE was previously doing to sanitize
84 $tiny = strlen( $textWithNoHtml );
85 $sm = strlen( $textWithHtmlSm );
86 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
87 $doit =
static function ( $text ) {
88 return static function () use ( $text ) {
89 $tidy = new \MediaWiki\Tidy\RemexDriver(
99 $benches[
"DISPLAYTITLE tidy (input: $tiny)"] = $doit( $textWithNoHtml );
100 $benches[
"DISPLAYTITLE tidy (input: $sm)"] = $doit( $textWithHtmlSm );
101 $benches[
"DISPLAYTITLE tidy (input: $lg)"] = $doit( $textWithHtmlLg );
103 if ( !$method || $method ===
'removeSomeTags' ) {
104 $tiny = strlen( $textWithNoHtml );
105 $sm = strlen( $textWithHtmlSm );
106 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
107 $benches[
"Sanitizer::removeSomeTags (input: $tiny)"] =
static function () use ( $textWithNoHtml ) {
110 $benches[
"Sanitizer::removeSomeTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
113 $benches[
"Sanitizer::removeSomeTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
117 if ( !$method || $method ===
'stripAllTags' ) {
118 $sm = strlen( $textWithHtmlSm );
119 $lg = round( strlen( $textWithHtmlLg ) / 1000 ) .
'K';
120 $benches[
"Sanitizer::stripAllTags (input: $sm)"] =
static function () use ( $textWithHtmlSm ) {
123 $benches[
"Sanitizer::stripAllTags (input: $lg)"] =
static function () use ( $textWithHtmlLg ) {
128 $this->
bench( $benches );
134require_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.
A class containing constants representing the names of configuration variables.
const TidyConfig
Name constant for the TidyConfig setting, for use with Config\get().
Base class for benchmark scripts.
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.
addDescription( $text)
Set the description text.