Debian Codegraph @

extensions/Echo/includes/SummaryParser.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications;
4
5 use MediaWiki\Title\Title;
6 use MediaWiki\User\User;
7 use MediaWiki\User\UserIdentity;
8
9 class SummaryParser {
10 /** @var callable */
11 private $userLookup;
12
13 /**
14 * @param callable|null $userLookup An alternative filter function that receives a User object
15 * (see the caller in {@see parse}) and returns the user id or 0 if the user doesn't exist.
16 * Only tests should modify this.
17 */
18 public function __construct( ?callable $userLookup = null ) {
19 $this->userLookup = $userLookup ?? static fn ( UserIdentity $user ) => $user->getId();
20 }
21
22 /**
23 * Returns a list of registered users linked in an edit summary
24 *
25 * @param string $summary
26 * @return User[] Array of username => User object
27 */
28 public function parse( $summary ) {
29 // Remove section autocomments. Replace with characters that can't be in titles,
30 // to prevent fun stuff like "[[foo /* section */ bar]]".
31 $summary = preg_replace( '#/\*.*?\*/#', ' [] ', $summary );
32
33 $users = [];
34 $regex = '/\[\[([' . Title::legalChars() . ']++)(?:\|.*?)?\]\]/';
35 if ( preg_match_all( $regex, $summary, $matches ) ) {
36 foreach ( $matches[1] as $match ) {
37 if ( $match[0] === ':' ) {
38 continue;
39 }
40
41 $title = Title::newFromText( $match );
42 if ( $title
43 && $title->isLocal()
44 && $title->getNamespace() === NS_USER
45 ) {
46 $user = User::newFromName( $title->getText() );
47 if ( $user && ( $this->userLookup )( $user ) > 0 ) {
48 $users[$user->getName()] = $user;
49 }
50 }
51 }
52 }
53
54 return $users;
55 }
56 }