2014-07-29 23:54:00 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 20:26:48 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Iterator\CallbackIterator;
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2014-07-29 23:54:00 +00:00
|
|
|
class EchoUserLocator {
|
|
|
|
/**
|
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
|
|
|
*
|
|
|
|
* @param EchoEvent $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
|
|
|
*/
|
2014-07-30 03:18:48 +00:00
|
|
|
public static function locateUsersWatchingTitle( EchoEvent $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.
|
|
|
|
*
|
|
|
|
* @param EchoEvent $event
|
|
|
|
* @return User[]
|
|
|
|
*/
|
2014-07-29 23:54:00 +00:00
|
|
|
public static function locateTalkPageOwner( EchoEvent $event ) {
|
|
|
|
$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
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the event agent
|
|
|
|
*
|
|
|
|
* @param EchoEvent $event
|
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
public static function locateEventAgent( EchoEvent $event ) {
|
|
|
|
$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.
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param EchoEvent $event
|
2014-07-31 02:31:47 +00:00
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
public static function locateArticleCreator( EchoEvent $event ) {
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
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'] ],
|
2016-12-05 18:51:07 +00:00
|
|
|
[ 'rev_page' => $title->getArticleID() ],
|
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 ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2014-07-31 02:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::newFromId( $res->rev_user );
|
|
|
|
if ( $user ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ $user->getId() => $user ];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch user ids from the event extra data. Requires additional
|
|
|
|
* parameter. Example $wgEchoNotifications parameter:
|
|
|
|
*
|
|
|
|
* 'user-locator' => array( array( 'event-extra', 'mentions' ) ),
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param EchoEvent $event
|
|
|
|
* @param string[] $keys one or more keys to check for user ids
|
|
|
|
* @return User[]
|
|
|
|
*/
|
|
|
|
public static function locateFromEventExtra( EchoEvent $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
|
|
|
}
|