2014-07-29 23:54:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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
|
2014-07-31 02:31:47 +00:00
|
|
|
* @return 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
|
|
|
}
|
|
|
|
|
2015-12-15 21:33:41 +00:00
|
|
|
$it = 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
|
|
|
);
|
2016-12-05 18:51:07 +00:00
|
|
|
$it->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
|
|
|
] );
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
// flatten the result into a stream of rows
|
|
|
|
$it = new RecursiveIteratorIterator( $it );
|
|
|
|
|
|
|
|
// add callback to convert user id to user objects
|
2015-10-01 13:48:52 +00:00
|
|
|
$it = new EchoCallbackIterator( $it, function ( $row ) {
|
2014-07-30 03:18:48 +00:00
|
|
|
return User::newFromId( $row->wl_user );
|
|
|
|
} );
|
2014-07-29 23:54:00 +00:00
|
|
|
|
2014-07-30 03:18:48 +00:00
|
|
|
return $it;
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 02:31:47 +00:00
|
|
|
/**
|
|
|
|
* If the event occured on the talk page of a registered
|
|
|
|
* 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() );
|
|
|
|
if ( $user && !$user->isAnon() ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ $user->getId() => $user ];
|
2014-07-29 23:54:00 +00:00
|
|
|
} else {
|
2016-12-05 18:51:07 +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();
|
|
|
|
if ( $agent && !$agent->isAnon() ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ $agent->getId() => $agent ];
|
2014-07-31 02:31:47 +00:00
|
|
|
} else {
|
2016-12-05 18:51:07 +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 );
|
2018-03-12 15:47:02 +00:00
|
|
|
$revQuery = Revision::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
|
|
|
} else {
|
2016-12-05 18:51:07 +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;
|
|
|
|
} elseif ( !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 ) {
|
|
|
|
if ( $userId->isAnon() ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$user = $userId;
|
|
|
|
} else {
|
|
|
|
$user = User::newFromId( $userId );
|
|
|
|
}
|
|
|
|
$users[$user->getId()] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
2014-07-29 23:54:00 +00:00
|
|
|
}
|