blob: a701b101b9c56563b122b7a7f0e86fd0f5f5886c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<?php
class EchoSummaryParser {
/** @var callable */
private $userLookup;
/**
* @param callable|null $userLookup Function that receives User object and returns its id
* or 0 if the user doesn't exist. Passing null to this parameter will result in default
* implementation being used.
*/
public function __construct( callable $userLookup = null ) {
$this->userLookup = $userLookup;
if ( !$this->userLookup ) {
$this->userLookup = function ( User $user ) {
return $user->getId();
};
}
}
/**
* 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 = [];
$regex = '/\[\[([' . Title::legalChars() . ']*+)(?:\|.*?)?\]\]/';
if ( preg_match_all( $regex, $summary, $matches ) ) {
foreach ( $matches[1] as $match ) {
if ( preg_match( '/^:/', $match ) ) {
continue;
}
$title = Title::newFromText( $match );
if ( $title
&& $title->isLocal()
&& $title->getNamespace() === NS_USER
) {
$user = User::newFromName( $title->getText() );
$lookup = $this->userLookup;
if ( $user && $lookup( $user ) > 0 ) {
$users[$user->getName()] = $user;
}
}
}
}
return $users;
}
}
|