2018-01-19 21:26:49 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
2023-08-19 04:14:59 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2024-03-15 08:27:36 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2022-11-13 07:48:43 +00:00
|
|
|
|
|
|
|
class SummaryParser {
|
2018-01-19 21:26:49 +00:00
|
|
|
/** @var callable */
|
|
|
|
private $userLookup;
|
|
|
|
|
|
|
|
/**
|
2024-03-15 08:27:36 +00:00
|
|
|
* @param callable|null $userLookup An alternative filter function that receives a User object
|
|
|
|
* (see the caller in {@see parse}) and returns the user id or 0 if the user doesn't exist.
|
|
|
|
* Only tests should modify this.
|
2018-01-19 21:26:49 +00:00
|
|
|
*/
|
|
|
|
public function __construct( callable $userLookup = null ) {
|
2024-03-15 08:27:36 +00:00
|
|
|
$this->userLookup = $userLookup ?? static fn ( UserIdentity $user ) => $user->getId();
|
2018-01-19 21:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of registered users linked in an edit summary
|
|
|
|
*
|
|
|
|
* @param string $summary
|
|
|
|
* @return User[] Array of username => User object
|
|
|
|
*/
|
|
|
|
public function parse( $summary ) {
|
|
|
|
// Remove section autocomments. Replace with characters that can't be in titles,
|
|
|
|
// to prevent fun stuff like "[[foo /* section */ bar]]".
|
|
|
|
$summary = preg_replace( '#/\*.*?\*/#', ' [] ', $summary );
|
|
|
|
|
|
|
|
$users = [];
|
2018-10-11 08:59:04 +00:00
|
|
|
$regex = '/\[\[([' . Title::legalChars() . ']++)(?:\|.*?)?\]\]/';
|
2018-01-19 21:26:49 +00:00
|
|
|
if ( preg_match_all( $regex, $summary, $matches ) ) {
|
|
|
|
foreach ( $matches[1] as $match ) {
|
2018-10-11 08:59:04 +00:00
|
|
|
if ( $match[0] === ':' ) {
|
2018-03-07 20:50:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-10-11 08:59:04 +00:00
|
|
|
|
2018-01-19 21:26:49 +00:00
|
|
|
$title = Title::newFromText( $match );
|
|
|
|
if ( $title
|
|
|
|
&& $title->isLocal()
|
2018-03-08 22:36:58 +00:00
|
|
|
&& $title->getNamespace() === NS_USER
|
2018-01-19 21:26:49 +00:00
|
|
|
) {
|
|
|
|
$user = User::newFromName( $title->getText() );
|
2024-03-15 08:27:36 +00:00
|
|
|
if ( $user && ( $this->userLookup )( $user ) > 0 ) {
|
2018-01-19 21:26:49 +00:00
|
|
|
$users[$user->getName()] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
}
|