2014-07-29 23:54:00 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
|
|
|
use BatchRowIterator;
|
|
|
|
use Iterator;
|
2022-11-02 20:26:48 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Iterator\CallbackIterator;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-11-12 07:19:00 +00:00
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
use User;
|
2019-04-17 15:46:06 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
class UserLocator {
|
2014-07-29 23:54:00 +00:00
|
|
|
/**
|
2014-07-31 02:31:47 +00:00
|
|
|
* Return all users watching the event title.
|
|
|
|
*
|
2014-07-29 23:54:00 +00:00
|
|
|
* The echo job queue must be enabled to prevent timeouts submitting to
|
|
|
|
* heavily watched pages when this is used.
|
2014-07-31 02:31:47 +00:00
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $batchSize
|
2019-02-15 20:23:02 +00:00
|
|
|
* @return User[]|Iterator<User>
|
2014-07-29 23:54:00 +00:00
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
public static function locateUsersWatchingTitle( Event $event, $batchSize = 500 ) {
|
2014-07-29 23:54:00 +00:00
|
|
|
$title = $event->getTitle();
|
|
|
|
if ( !$title ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 16:36:17 +00:00
|
|
|
$batchRowIt = new BatchRowIterator(
|
2017-09-24 05:23:47 +00:00
|
|
|
wfGetDB( DB_REPLICA, 'watchlist' ),
|
2014-07-30 03:18:48 +00:00
|
|
|
/* $table = */ 'watchlist',
|
2016-12-05 18:51:07 +00:00
|
|
|
/* $primaryKeys = */ [ 'wl_user' ],
|
2014-07-30 03:18:48 +00:00
|
|
|
$batchSize
|
2014-07-29 23:54:00 +00:00
|
|
|
);
|
2021-02-23 16:36:17 +00:00
|
|
|
$batchRowIt->addConditions( [
|
2014-07-30 03:18:48 +00:00
|
|
|
'wl_namespace' => $title->getNamespace(),
|
|
|
|
'wl_title' => $title->getDBkey(),
|
2016-12-05 18:51:07 +00:00
|
|
|
] );
|
2021-02-23 16:36:17 +00:00
|
|
|
$batchRowIt->setCaller( __METHOD__ );
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
// flatten the result into a stream of rows
|
2021-02-23 16:36:17 +00:00
|
|
|
$recursiveIt = new RecursiveIteratorIterator( $batchRowIt );
|
2014-07-30 03:18:48 +00:00
|
|
|
|
|
|
|
// add callback to convert user id to user objects
|
2022-11-02 20:26:48 +00:00
|
|
|
$echoCallbackIt = new CallbackIterator( $recursiveIt, static function ( $row ) {
|
2014-07-30 03:18:48 +00:00
|
|
|
return User::newFromId( $row->wl_user );
|
|
|
|
} );
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2021-02-23 16:36:17 +00:00
|
|
|
return $echoCallbackIt;
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 02:31:47 +00:00
|
|
|
/**
|
2019-03-08 13:35:25 +00:00
|
|
|
* If the event occurred on the talk page of a registered
|
2014-07-31 02:31:47 +00:00
|
|
|
* user return that user.
|
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2014-07-31 02:31:47 +00:00
|
|
|
* @return User[]
|
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
public static function locateTalkPageOwner( Event $event ) {
|
2014-07-29 23:54:00 +00:00
|
|
|
$title = $event->getTitle();
|
|
|
|
if ( !$title || $title->getNamespace() !== NS_USER_TALK ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::newFromName( $title->getDBkey() );
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( $user && $user->isRegistered() ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ $user->getId() => $user ];
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
2019-03-01 22:34:38 +00:00
|
|
|
|
|
|
|
return [];
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
2014-07-31 02:31:47 +00:00
|
|
|
|
2023-03-09 14:18:07 +00:00
|
|
|
/**
|
|
|
|
* If the event occurred on the user page of a registered
|
|
|
|
* user return that user.
|
|
|
|
*
|
2022-11-12 07:19:00 +00:00
|
|
|
* @param Event $event
|
2023-03-09 14:18:07 +00:00
|
|
|
* @return User[]
|
|
|
|
*/
|
2022-11-12 07:19:00 +00:00
|
|
|
public static function locateUserPageOwner( Event $event ) {
|
2023-03-09 14:18:07 +00:00
|
|
|
$title = $event->getTitle();
|
|
|
|
if ( !$title || !$title->inNamespace( NS_USER ) ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::newFromName( $title->getDBkey() );
|
|
|
|
if ( $user && $user->isRegistered() ) {
|
|
|
|
return [ $user->getId() => $user ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2014-07-31 02:31:47 +00:00
|
|
|
/**
|
|
|
|
* Return the event agent
|
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2014-07-31 02:31:47 +00:00
|
|
|
* @return User[]
|
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
public static function locateEventAgent( Event $event ) {
|
2014-07-31 02:31:47 +00:00
|
|
|
$agent = $event->getAgent();
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( $agent && $agent->isRegistered() ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ $agent->getId() => $agent ];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
2019-03-01 22:34:38 +00:00
|
|
|
|
|
|
|
return [];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the user that created the first revision of the
|
|
|
|
* associated title.
|
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2014-07-31 02:31:47 +00:00
|
|
|
* @return User[]
|
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
public static function locateArticleCreator( Event $event ) {
|
2014-07-31 02:31:47 +00:00
|
|
|
$title = $event->getTitle();
|
|
|
|
|
|
|
|
if ( !$title || $title->getArticleID() <= 0 ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 12:43:49 +00:00
|
|
|
$user = self::getArticleAuthorByArticleId( $title->getArticleID() );
|
|
|
|
if ( $user ) {
|
|
|
|
// T318523: Don't send page-linked notifications for pages created by bot users.
|
|
|
|
if ( $event->getType() === 'page-linked' && $user->isBot() ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return [ $user->getId() => $user ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $articleId
|
|
|
|
* @return User|null
|
|
|
|
*/
|
|
|
|
public static function getArticleAuthorByArticleId( int $articleId ): ?User {
|
2017-09-24 05:23:47 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2019-04-17 15:46:06 +00:00
|
|
|
$revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo();
|
2014-07-31 02:31:47 +00:00
|
|
|
$res = $dbr->selectRow(
|
2018-03-12 15:47:02 +00:00
|
|
|
$revQuery['tables'],
|
|
|
|
[ 'rev_user' => $revQuery['fields']['rev_user'] ],
|
2023-05-16 12:43:49 +00:00
|
|
|
[ 'rev_page' => $articleId ],
|
2014-07-31 02:31:47 +00:00
|
|
|
__METHOD__,
|
2018-03-12 15:47:02 +00:00
|
|
|
[ 'LIMIT' => 1, 'ORDER BY' => 'rev_timestamp, rev_id' ],
|
|
|
|
$revQuery['joins']
|
2014-07-31 02:31:47 +00:00
|
|
|
);
|
|
|
|
if ( !$res || !$res->rev_user ) {
|
2023-05-16 12:43:49 +00:00
|
|
|
return null;
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 12:43:49 +00:00
|
|
|
return User::newFromId( $res->rev_user );
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch user ids from the event extra data. Requires additional
|
|
|
|
* parameter. Example $wgEchoNotifications parameter:
|
|
|
|
*
|
2023-08-16 15:46:59 +00:00
|
|
|
* 'user-locators' => [ [ 'event-extra', 'mentions' ] ],
|
2014-07-31 02:31:47 +00:00
|
|
|
*
|
|
|
|
* The above will look in the 'mentions' parameter for a user id or
|
|
|
|
* array of user ids. It will return all these users as notification
|
|
|
|
* targets.
|
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2014-07-31 02:31:47 +00:00
|
|
|
* @param string[] $keys one or more keys to check for user ids
|
|
|
|
* @return User[]
|
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
public static function locateFromEventExtra( Event $event, array $keys ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$users = [];
|
2014-07-31 02:31:47 +00:00
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
$userIds = $event->getExtraParam( $key );
|
|
|
|
if ( !$userIds ) {
|
|
|
|
continue;
|
2019-03-01 22:34:38 +00:00
|
|
|
}
|
|
|
|
if ( !is_array( $userIds ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$userIds = [ $userIds ];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
foreach ( $userIds as $userId ) {
|
|
|
|
// we shouldn't receive User instances, but allow
|
|
|
|
// it for backward compatability
|
|
|
|
if ( $userId instanceof User ) {
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( !$userId->isRegistered() ) {
|
2014-07-31 02:31:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$user = $userId;
|
|
|
|
} else {
|
|
|
|
$user = User::newFromId( $userId );
|
|
|
|
}
|
|
|
|
$users[$user->getId()] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
2022-11-12 07:19:00 +00:00
|
|
|
|
|
|
|
class_alias( UserLocator::class, 'EchoUserLocator' );
|