Created
December 15, 2023 19:59
-
-
Save jacob-g/8fb0fe1b056fd0fa2d8499e8b30292e0 to your computer and use it in GitHub Desktop.
Modified MediaWiki Echo DiscussionParser to support ScratchSig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* From a line in the signature, extract all the users linked to | |
* | |
* @param string $line Line of text potentially including linked user, user talk, | |
* and contribution pages | |
* @return string[] array of usernames, empty array for none detected | |
*/ | |
public static function extractUsersFromLine( $line ) { | |
/* | |
* Signatures can look like anything (as defined by i18n messages | |
* "signature" & "signature-anon"). | |
* A signature can, e.g., be both a link to user & user-talk page. | |
*/ | |
// match all title-like excerpts in this line | |
preg_match_all( '/\[\[([^\[]+)\]\]/', $line, $matches ); | |
$matches = $matches[1]; | |
$usernames = []; | |
foreach ( $matches as $match ) { | |
/* | |
* Create an object out of the link title. | |
* In theory, links can be [[text]], [[text|text]] or pipe tricks | |
* [[text|]] or [[|text]]. | |
* In the case of reverse pipe trick, the value we use *could* be | |
* empty, but Parser::pstPass2 should have normalized that for us | |
* already. | |
*/ | |
$match = explode( '|', $match, 2 ); | |
$title = Title::newFromText( $match[0] ); | |
// figure out if we the link is related to a user | |
if ( | |
$title && | |
( $title->getNamespace() === NS_USER || $title->getNamespace() === NS_USER_TALK ) | |
) { | |
$usernames[] = $title->getText(); | |
} elseif ( $title && $title->isSpecial( 'Contributions' ) ) { | |
$parts = explode( '/', $title->getText(), 2 ); | |
$usernames[] = end( $parts ); | |
} else { | |
// move on to next matched title-like excerpt | |
continue; | |
} | |
} | |
preg_match_all('%<scratchsig>(.*?)</scratchsig>%', $line, $matches); | |
foreach ($matches[1] as $username) { | |
$usernames[] = User::newFromName($username)->getName(); | |
} | |
return $usernames; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment