2012-07-27 22:16:19 +00:00
|
|
|
<?php
|
|
|
|
|
2016-07-15 16:02:34 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2021-02-21 23:01:37 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2016-07-15 16:02:34 +00:00
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
abstract class EchoDiscussionParser {
|
2020-09-19 18:55:32 +00:00
|
|
|
private const HEADER_REGEX = '^(==+)\h*([^=].*)\h*\1$';
|
2014-09-12 03:07:29 +00:00
|
|
|
|
2020-09-19 18:55:32 +00:00
|
|
|
public const DEFAULT_SNIPPET_LENGTH = 150;
|
2020-07-22 22:03:44 +00:00
|
|
|
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var string|null */
|
2019-02-06 13:01:32 +00:00
|
|
|
protected static $timestampRegex;
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var array[][] */
|
2019-02-06 13:01:32 +00:00
|
|
|
protected static $revisionInterpretationCache = [];
|
2020-12-16 21:31:09 +00:00
|
|
|
/** @var EchoDiffParser|null */
|
2019-02-06 13:01:32 +00:00
|
|
|
protected static $diffParser;
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-17 15:46:06 +00:00
|
|
|
* Given a RevisionRecord object, generates EchoEvent objects for
|
2012-07-27 22:16:19 +00:00
|
|
|
* the discussion-related actions that occurred in that Revision.
|
|
|
|
*
|
2019-04-17 15:46:06 +00:00
|
|
|
* @param RevisionRecord $revision
|
2018-01-19 21:26:49 +00:00
|
|
|
* @param bool $isRevert
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2019-04-17 15:46:06 +00:00
|
|
|
public static function generateEventsForRevision( RevisionRecord $revision, $isRevert ) {
|
2016-08-09 16:20:50 +00:00
|
|
|
global $wgEchoMentionsOnMultipleSectionEdits;
|
2016-09-05 12:53:30 +00:00
|
|
|
global $wgEchoMentionOnChanges;
|
2019-04-17 15:46:06 +00:00
|
|
|
$store = MediaWikiServices::getInstance()->getRevisionStore();
|
2016-09-05 12:53:30 +00:00
|
|
|
|
2020-06-08 23:47:29 +00:00
|
|
|
// use replica database if there is a previous revision
|
2019-04-17 15:46:06 +00:00
|
|
|
if ( $store->getPreviousRevision( $revision ) ) {
|
|
|
|
$title = Title::newFromID( $revision->getPageId() );
|
2018-01-19 21:26:49 +00:00
|
|
|
// use master database for new page
|
2012-11-14 21:56:58 +00:00
|
|
|
} else {
|
2019-04-17 15:46:06 +00:00
|
|
|
$title = Title::newFromID( $revision->getPageId(), Title::GAID_FOR_UPDATE );
|
2012-11-14 21:56:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// not a valid title
|
|
|
|
if ( !$title ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2021-03-10 16:39:21 +00:00
|
|
|
$events = [];
|
|
|
|
|
2016-07-13 15:31:06 +00:00
|
|
|
$interpretation = self::getChangeInterpretationForRevision( $revision );
|
|
|
|
|
2019-04-17 15:46:06 +00:00
|
|
|
$userID = $revision->getUser()->getId();
|
|
|
|
$userName = $revision->getUser()->getName();
|
2020-07-22 20:49:49 +00:00
|
|
|
$user = $userID !== 0 ? User::newFromId( $userID ) : User::newFromName( $userName, false );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $interpretation as $action ) {
|
2020-07-22 20:49:49 +00:00
|
|
|
if ( $action['type'] === 'add-comment' ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
$fullSection = $action['full-section'];
|
|
|
|
$header = self::extractHeader( $fullSection );
|
2016-08-16 13:51:17 +00:00
|
|
|
$userLinks = self::getUserLinks( $action['content'], $title );
|
2021-03-10 16:39:21 +00:00
|
|
|
$events = array_merge(
|
|
|
|
$events,
|
|
|
|
self::collectMentionEvents( $header, $userLinks, $action['content'], $revision, $user )
|
|
|
|
);
|
2020-07-22 20:49:49 +00:00
|
|
|
} elseif ( $action['type'] === 'new-section-with-comment' ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
$content = $action['content'];
|
|
|
|
$header = self::extractHeader( $content );
|
2016-08-16 13:51:17 +00:00
|
|
|
$userLinks = self::getUserLinks( $content, $title );
|
2021-03-10 16:39:21 +00:00
|
|
|
$events = array_merge(
|
|
|
|
$events,
|
|
|
|
self::collectMentionEvents( $header, $userLinks, $content, $revision, $user )
|
|
|
|
);
|
2020-07-22 20:49:49 +00:00
|
|
|
} elseif ( $action['type'] === 'add-section-multiple' && $wgEchoMentionsOnMultipleSectionEdits ) {
|
2016-08-09 16:20:50 +00:00
|
|
|
$content = self::stripHeader( $action['content'] );
|
|
|
|
$content = self::stripSignature( $content );
|
|
|
|
$userLinks = self::getUserLinks( $content, $title );
|
2021-03-10 16:39:21 +00:00
|
|
|
$events = array_merge(
|
|
|
|
$events,
|
|
|
|
self::collectMentionEvents( $action['header'], $userLinks, $content, $revision, $user )
|
|
|
|
);
|
2016-08-16 13:51:17 +00:00
|
|
|
} elseif ( $action['type'] === 'unknown-signed-change' ) {
|
|
|
|
$userLinks = array_diff_key(
|
2019-11-27 01:57:16 +00:00
|
|
|
self::getUserLinks( $action['new_content'], $title ),
|
|
|
|
self::getUserLinks( $action['old_content'], $title )
|
2016-08-16 13:51:17 +00:00
|
|
|
);
|
2016-09-05 12:53:30 +00:00
|
|
|
$header = self::extractHeader( $action['full-section'] );
|
|
|
|
|
|
|
|
if ( $wgEchoMentionOnChanges ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
$events = array_merge(
|
|
|
|
$events,
|
|
|
|
self::collectMentionEvents( $header, $userLinks, $action['new_content'], $revision, $user )
|
|
|
|
);
|
2016-08-16 13:51:17 +00:00
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 20:49:49 +00:00
|
|
|
if ( $title->getNamespace() === NS_USER_TALK ) {
|
2012-08-01 19:53:05 +00:00
|
|
|
$notifyUser = User::newFromName( $title->getText() );
|
2013-03-20 01:10:23 +00:00
|
|
|
// If the recipient is a valid non-anonymous user and hasn't turned
|
2014-08-28 08:50:28 +00:00
|
|
|
// off their notifications, generate a talk page post Echo notification.
|
2016-05-27 15:38:06 +00:00
|
|
|
if ( $notifyUser && $notifyUser->getId() ) {
|
2020-03-05 19:55:54 +00:00
|
|
|
$permManager = MediaWikiServices::getInstance()->getPermissionManager();
|
2016-05-27 15:38:06 +00:00
|
|
|
// If this is a minor edit, only notify if the agent doesn't have talk page minor
|
|
|
|
// edit notification blocked
|
2020-03-05 19:55:54 +00:00
|
|
|
if ( !$revision->isMinor() || !$permManager->userHasRight( $user, 'nominornewtalk' ) ) {
|
2015-02-17 10:10:49 +00:00
|
|
|
$section = self::detectSectionTitleAndText( $interpretation, $title );
|
2015-04-26 13:50:44 +00:00
|
|
|
if ( $section['section-text'] === '' ) {
|
2019-05-21 15:31:12 +00:00
|
|
|
$comment = $revision->getComment( RevisionRecord::FOR_PUBLIC, $notifyUser );
|
|
|
|
if ( $comment ) {
|
|
|
|
$section['section-text'] = $comment->text;
|
|
|
|
}
|
2015-04-26 13:50:44 +00:00
|
|
|
}
|
2021-03-10 16:39:21 +00:00
|
|
|
$events[] = [
|
2013-05-01 19:27:32 +00:00
|
|
|
'type' => 'edit-user-talk',
|
|
|
|
'title' => $title,
|
2016-12-05 18:51:07 +00:00
|
|
|
'extra' => [
|
2016-05-27 15:38:06 +00:00
|
|
|
'revid' => $revision->getId(),
|
2013-05-14 22:22:52 +00:00
|
|
|
'minoredit' => $revision->isMinor(),
|
2013-07-24 03:50:43 +00:00
|
|
|
'section-title' => $section['section-title'],
|
2015-09-29 13:17:35 +00:00
|
|
|
'section-text' => $section['section-text'],
|
|
|
|
'target-page' => $title->getArticleID(),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2013-05-01 19:27:32 +00:00
|
|
|
'agent' => $user,
|
2021-03-10 16:39:21 +00:00
|
|
|
];
|
2013-05-01 19:27:32 +00:00
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 21:26:49 +00:00
|
|
|
|
|
|
|
// Notify users mentioned in edit summary
|
|
|
|
global $wgEchoMaxMentionsInEditSummary;
|
|
|
|
|
2018-03-07 02:27:17 +00:00
|
|
|
if ( $wgEchoMaxMentionsInEditSummary > 0 && !$user->isBot() && !$isRevert ) {
|
2018-01-19 21:26:49 +00:00
|
|
|
$summaryParser = new EchoSummaryParser();
|
2019-04-17 15:46:06 +00:00
|
|
|
$usersInSummary = $summaryParser->parse( $revision->getComment()->text );
|
2018-01-19 21:26:49 +00:00
|
|
|
|
|
|
|
// Don't allow pinging yourself
|
|
|
|
unset( $usersInSummary[$userName] );
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$mentionedUsers = [];
|
|
|
|
foreach ( $usersInSummary as $summaryUser ) {
|
|
|
|
if ( $summaryUser->getTalkPage()->equals( $title ) ) {
|
|
|
|
// Users already get a ping when their talk page is edited
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( $count >= $wgEchoMaxMentionsInEditSummary ) {
|
|
|
|
break;
|
|
|
|
}
|
2020-05-05 04:31:24 +00:00
|
|
|
$mentionedUsers[$summaryUser->getId()] = $summaryUser->getId();
|
2018-01-19 21:26:49 +00:00
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $mentionedUsers ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
$events[] = [
|
2018-01-19 21:26:49 +00:00
|
|
|
'type' => 'mention-summary',
|
|
|
|
'title' => $title,
|
|
|
|
'extra' => [
|
|
|
|
'revid' => $revision->getId(),
|
|
|
|
'mentioned-users' => $mentionedUsers,
|
|
|
|
],
|
|
|
|
'agent' => $user,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 16:39:21 +00:00
|
|
|
|
|
|
|
// Allow extensions to generate more events for a revision, and de-duplicate
|
|
|
|
// against the standard events created above.
|
|
|
|
Hooks::run( 'EchoGetEventsForRevision', [ &$events, $revision, $isRevert ] );
|
|
|
|
|
|
|
|
// Create events
|
|
|
|
foreach ( $events as $event ) {
|
|
|
|
EchoEvent::create( $event );
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
/**
|
|
|
|
* Attempts to determine what section title the edit was performed under (if any)
|
|
|
|
*
|
2018-08-13 07:17:15 +00:00
|
|
|
* @param array[] $interpretation Results of {@see getChangeInterpretationForRevision}
|
2018-04-04 15:11:39 +00:00
|
|
|
* @param Title|null $title
|
2018-08-13 07:17:15 +00:00
|
|
|
* @return string[] Array containing section title and text
|
2013-05-14 22:22:52 +00:00
|
|
|
*/
|
2015-02-17 10:10:49 +00:00
|
|
|
public static function detectSectionTitleAndText( array $interpretation, Title $title = null ) {
|
2013-07-24 03:50:43 +00:00
|
|
|
$header = $snippet = '';
|
2013-05-17 19:16:46 +00:00
|
|
|
$found = false;
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
foreach ( $interpretation as $action ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
switch ( $action['type'] ) {
|
2013-07-24 03:50:43 +00:00
|
|
|
case 'add-comment':
|
2015-10-01 13:48:52 +00:00
|
|
|
$header = self::extractHeader( $action['full-section'] );
|
2015-09-02 18:09:11 +00:00
|
|
|
$snippet = self::getTextSnippet(
|
2015-10-01 13:48:52 +00:00
|
|
|
self::stripSignature( self::stripHeader( $action['content'] ), $title ),
|
2017-10-03 22:39:23 +00:00
|
|
|
RequestContext::getMain()->getLanguage(),
|
2020-07-22 22:03:44 +00:00
|
|
|
self::DEFAULT_SNIPPET_LENGTH,
|
2016-09-01 20:48:56 +00:00
|
|
|
$title );
|
2013-07-24 03:50:43 +00:00
|
|
|
break;
|
|
|
|
case 'new-section-with-comment':
|
2015-10-01 13:48:52 +00:00
|
|
|
$header = self::extractHeader( $action['content'] );
|
2015-09-02 18:09:11 +00:00
|
|
|
$snippet = self::getTextSnippet(
|
2015-10-01 13:48:52 +00:00
|
|
|
self::stripSignature( self::stripHeader( $action['content'] ), $title ),
|
2017-10-03 22:39:23 +00:00
|
|
|
RequestContext::getMain()->getLanguage(),
|
2020-07-22 22:03:44 +00:00
|
|
|
self::DEFAULT_SNIPPET_LENGTH,
|
2016-09-01 20:48:56 +00:00
|
|
|
$title );
|
2013-07-24 03:50:43 +00:00
|
|
|
break;
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
if ( $header ) {
|
2015-04-26 13:37:38 +00:00
|
|
|
// If we find a second header within the same change interpretation then
|
2013-05-17 19:16:46 +00:00
|
|
|
// we cannot choose just 1 to link to
|
|
|
|
if ( $found ) {
|
2015-04-26 13:37:38 +00:00
|
|
|
$found = false;
|
|
|
|
break;
|
2013-05-17 19:16:46 +00:00
|
|
|
}
|
2015-04-26 13:37:38 +00:00
|
|
|
$found = true;
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-26 13:37:38 +00:00
|
|
|
if ( $found === false ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 'section-title' => '', 'section-text' => '' ];
|
2013-05-17 19:16:46 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 'section-title' => $header, 'section-text' => $snippet ];
|
2013-05-14 22:22:52 +00:00
|
|
|
}
|
|
|
|
|
2012-10-28 16:47:41 +00:00
|
|
|
/**
|
|
|
|
* For an action taken on a talk page, notify users whose user pages
|
|
|
|
* are linked.
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $header The subject line for the discussion.
|
2018-08-13 07:25:22 +00:00
|
|
|
* @param int[] $userLinks
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $content The content of the post, as a wikitext string.
|
2019-04-17 15:46:06 +00:00
|
|
|
* @param RevisionRecord $revision
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param User $agent The user who made the comment.
|
2012-10-28 16:47:41 +00:00
|
|
|
*/
|
2016-05-27 15:38:06 +00:00
|
|
|
public static function generateMentionEvents(
|
|
|
|
$header,
|
2019-10-23 10:44:35 +00:00
|
|
|
array $userLinks,
|
2016-05-27 15:38:06 +00:00
|
|
|
$content,
|
2019-04-17 15:46:06 +00:00
|
|
|
RevisionRecord $revision,
|
2016-05-27 15:38:06 +00:00
|
|
|
User $agent
|
2021-03-10 16:39:21 +00:00
|
|
|
) {
|
|
|
|
$events = self::collectMentionEvents( $header, $userLinks, $content, $revision, $agent );
|
|
|
|
foreach ( $events as $event ) {
|
|
|
|
EchoEvent::create( $event );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate mention event data for a talk page action
|
|
|
|
* @param string $header The subject line for the discussion.
|
|
|
|
* @param int[] $userLinks
|
|
|
|
* @param string $content The content of the post, as a wikitext string.
|
|
|
|
* @param RevisionRecord $revision
|
|
|
|
* @param User $agent The user who made the comment.
|
|
|
|
* @return array List of event info arrays
|
|
|
|
*/
|
|
|
|
protected static function collectMentionEvents(
|
|
|
|
$header,
|
|
|
|
array $userLinks,
|
|
|
|
$content,
|
|
|
|
RevisionRecord $revision,
|
|
|
|
User $agent
|
2016-05-27 15:38:06 +00:00
|
|
|
) {
|
2016-06-21 14:42:56 +00:00
|
|
|
global $wgEchoMaxMentionsCount, $wgEchoMentionStatusNotifications;
|
|
|
|
|
2019-04-17 15:46:06 +00:00
|
|
|
$title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
|
2012-10-28 16:47:41 +00:00
|
|
|
if ( !$title ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
return [];
|
2012-10-28 16:47:41 +00:00
|
|
|
}
|
2016-06-28 13:47:38 +00:00
|
|
|
$content = self::stripHeader( $content );
|
2016-08-12 09:03:30 +00:00
|
|
|
$content = self::stripSignature( $content, $title );
|
2012-10-28 16:47:41 +00:00
|
|
|
|
2016-07-05 14:45:14 +00:00
|
|
|
if ( !$userLinks ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
return [];
|
2013-04-08 23:43:18 +00:00
|
|
|
}
|
2012-10-28 16:47:41 +00:00
|
|
|
|
2019-04-17 15:46:06 +00:00
|
|
|
$userMentions = self::getUserMentions(
|
|
|
|
$title, $revision->getUser( RevisionRecord::RAW )->getId(), $userLinks
|
|
|
|
);
|
2016-07-05 14:45:14 +00:00
|
|
|
$overallMentionsCount = self::getOverallUserMentionsCount( $userMentions );
|
|
|
|
if ( $overallMentionsCount === 0 ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
return [];
|
2016-07-05 14:45:14 +00:00
|
|
|
}
|
2016-05-22 12:26:22 +00:00
|
|
|
|
2021-03-10 16:39:21 +00:00
|
|
|
$events = [];
|
2016-09-14 11:35:03 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
|
2016-07-05 14:45:14 +00:00
|
|
|
if ( $overallMentionsCount > $wgEchoMaxMentionsCount ) {
|
|
|
|
if ( $wgEchoMentionStatusNotifications ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
$events[] = [
|
2016-07-05 14:45:14 +00:00
|
|
|
'type' => 'mention-failure-too-many',
|
|
|
|
'title' => $title,
|
2016-12-05 18:51:07 +00:00
|
|
|
'extra' => [
|
2016-07-05 14:45:14 +00:00
|
|
|
'max-mentions' => $wgEchoMaxMentionsCount,
|
|
|
|
'section-title' => $header,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-07-05 14:45:14 +00:00
|
|
|
'agent' => $agent,
|
2021-03-10 16:39:21 +00:00
|
|
|
];
|
2016-09-14 11:35:03 +00:00
|
|
|
$stats->increment( 'echo.event.mention.notification.failure-too-many' );
|
2013-03-13 19:30:20 +00:00
|
|
|
}
|
2021-03-10 16:39:21 +00:00
|
|
|
return $events;
|
2012-10-28 16:47:41 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 10:15:34 +00:00
|
|
|
if ( $userMentions['validMentions'] ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
$events[] = [
|
2016-07-29 10:15:34 +00:00
|
|
|
'type' => 'mention',
|
|
|
|
'title' => $title,
|
2016-12-05 18:51:07 +00:00
|
|
|
'extra' => [
|
2016-07-29 10:15:34 +00:00
|
|
|
'content' => $content,
|
|
|
|
'section-title' => $header,
|
|
|
|
'revid' => $revision->getId(),
|
|
|
|
'mentioned-users' => $userMentions['validMentions'],
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-07-29 10:15:34 +00:00
|
|
|
'agent' => $agent,
|
2021-03-10 16:39:21 +00:00
|
|
|
];
|
2016-07-29 10:15:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 14:42:56 +00:00
|
|
|
if ( $wgEchoMentionStatusNotifications ) {
|
|
|
|
// TODO batch?
|
2016-07-29 10:15:34 +00:00
|
|
|
foreach ( $userMentions['validMentions'] as $mentionedUserId ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
$events[] = [
|
2016-07-29 10:15:34 +00:00
|
|
|
'type' => 'mention-success',
|
2016-06-21 14:42:56 +00:00
|
|
|
'title' => $title,
|
2016-12-05 18:51:07 +00:00
|
|
|
'extra' => [
|
2016-07-29 10:15:34 +00:00
|
|
|
'subject-name' => User::newFromId( $mentionedUserId )->getName(),
|
2016-06-21 14:42:56 +00:00
|
|
|
'section-title' => $header,
|
2016-07-29 10:15:34 +00:00
|
|
|
'revid' => $revision->getId(),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-06-21 14:42:56 +00:00
|
|
|
'agent' => $agent,
|
2021-03-10 16:39:21 +00:00
|
|
|
];
|
2016-09-14 11:35:03 +00:00
|
|
|
$stats->increment( 'echo.event.mention.notification.success' );
|
2016-06-21 14:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO batch?
|
2016-07-05 14:45:14 +00:00
|
|
|
foreach ( $userMentions['anonymousUsers'] as $anonymousUser ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
$events[] = [
|
2016-06-21 14:42:56 +00:00
|
|
|
'type' => 'mention-failure',
|
|
|
|
'title' => $title,
|
2016-12-05 18:51:07 +00:00
|
|
|
'extra' => [
|
2016-06-21 14:42:56 +00:00
|
|
|
'failure-type' => 'user-anonymous',
|
|
|
|
'subject-name' => $anonymousUser,
|
|
|
|
'section-title' => $header,
|
2016-07-29 10:15:34 +00:00
|
|
|
'revid' => $revision->getId(),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-06-21 14:42:56 +00:00
|
|
|
'agent' => $agent,
|
2021-03-10 16:39:21 +00:00
|
|
|
];
|
2016-09-14 11:35:03 +00:00
|
|
|
$stats->increment( 'echo.event.mention.notification.failure-user-anonymous' );
|
2016-06-21 14:42:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 13:00:54 +00:00
|
|
|
// TODO batch?
|
2016-07-29 10:15:34 +00:00
|
|
|
foreach ( $userMentions['unknownUsers'] as $unknownUser ) {
|
2021-03-10 16:39:21 +00:00
|
|
|
$events[] = [
|
2016-07-29 10:15:34 +00:00
|
|
|
'type' => 'mention-failure',
|
2016-07-21 13:00:54 +00:00
|
|
|
'title' => $title,
|
2016-12-05 18:51:07 +00:00
|
|
|
'extra' => [
|
2016-07-29 10:15:34 +00:00
|
|
|
'failure-type' => 'user-unknown',
|
|
|
|
'subject-name' => $unknownUser,
|
2016-07-21 13:00:54 +00:00
|
|
|
'section-title' => $header,
|
2016-07-29 10:15:34 +00:00
|
|
|
'revid' => $revision->getId(),
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
2016-07-21 13:00:54 +00:00
|
|
|
'agent' => $agent,
|
2021-03-10 16:39:21 +00:00
|
|
|
];
|
2016-09-14 11:35:03 +00:00
|
|
|
$stats->increment( 'echo.event.mention.notification.failure-user-unknown' );
|
2016-07-21 13:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-10 16:39:21 +00:00
|
|
|
|
|
|
|
return $events;
|
2012-10-28 16:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 10:44:35 +00:00
|
|
|
private static function getOverallUserMentionsCount( array $userMentions ) {
|
2016-07-05 14:45:14 +00:00
|
|
|
return count( $userMentions, COUNT_RECURSIVE ) - count( $userMentions );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-07 16:06:17 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param int $revisionUserId
|
2018-08-13 07:25:22 +00:00
|
|
|
* @param int[] $userLinks
|
2016-07-05 14:45:14 +00:00
|
|
|
* @return array[]
|
|
|
|
* Set of arrays containing valid mentions and possible intended but failed mentions.
|
|
|
|
* - [validMentions]: An array of valid users to mention with ID => ID.
|
|
|
|
* - [unknownUsers]: An array of DBKey strings representing unknown users.
|
2017-05-20 11:09:06 +00:00
|
|
|
* - [anonymousUsers]: An array of DBKey strings representing anonymous IP users.
|
2016-07-05 14:45:14 +00:00
|
|
|
*/
|
|
|
|
private static function getUserMentions( Title $title, $revisionUserId, array $userLinks ) {
|
|
|
|
global $wgEchoMaxMentionsCount;
|
2016-12-05 18:51:07 +00:00
|
|
|
$userMentions = [
|
|
|
|
'validMentions' => [],
|
|
|
|
'unknownUsers' => [],
|
|
|
|
'anonymousUsers' => [],
|
|
|
|
];
|
2016-07-05 14:45:14 +00:00
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
|
|
|
|
foreach ( $userLinks as $dbk => $page_id ) {
|
|
|
|
// If more users are being pinged this is likely a spam/attack vector
|
|
|
|
// Don't send any mention notifications.
|
|
|
|
if ( $count > $wgEchoMaxMentionsCount ) {
|
|
|
|
$stats->increment( 'echo.event.mention.error.tooMany' );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we should not add user to 'mention' notification list if
|
|
|
|
// 1. the user link links to a subpage
|
|
|
|
if ( self::hasSubpage( $dbk ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-02-24 13:31:50 +00:00
|
|
|
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
|
2016-07-05 14:45:14 +00:00
|
|
|
// 2. user is an anonymous IP
|
2021-02-24 13:31:50 +00:00
|
|
|
if ( $userNameUtils->isIP( $dbk ) ) {
|
2016-07-05 14:45:14 +00:00
|
|
|
$userMentions['anonymousUsers'][] = $dbk;
|
|
|
|
$count++;
|
|
|
|
$stats->increment( 'echo.event.mention.error.anonUser' );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = User::newFromName( $dbk );
|
|
|
|
// 3. the user name is not valid
|
|
|
|
if ( !$user ) {
|
|
|
|
$userMentions['unknownUsers'][] = str_replace( '_', ' ', $dbk );
|
|
|
|
$count++;
|
|
|
|
$stats->increment( 'echo.event.mention.error.invalidUser' );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-12 01:41:08 +00:00
|
|
|
// 4. the user mentions themselves
|
|
|
|
if ( $user->getId() === $revisionUserId ) {
|
|
|
|
$stats->increment( 'echo.event.mention.error.sameUser' );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. the user is the owner of the talk page
|
2016-07-05 14:45:14 +00:00
|
|
|
if ( $title->getNamespace() === NS_USER_TALK && $title->getDBkey() === $dbk ) {
|
|
|
|
$stats->increment( 'echo.event.mention.error.ownPage' );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-12 01:41:08 +00:00
|
|
|
// 6. user does not exist
|
2016-07-05 14:45:14 +00:00
|
|
|
if ( $user->getId() === 0 ) {
|
|
|
|
$userMentions['unknownUsers'][] = str_replace( '_', ' ', $dbk );
|
|
|
|
$count++;
|
|
|
|
$stats->increment( 'echo.event.mention.error.unknownUser' );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$userMentions['validMentions'][$user->getId()] = $user->getId();
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $userMentions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-07 16:06:17 +00:00
|
|
|
* @param string $content
|
|
|
|
* @param Title $title
|
2019-11-27 01:57:16 +00:00
|
|
|
* @return int[]
|
2016-07-05 14:45:14 +00:00
|
|
|
* Array of links in the user namespace with DBKey => ID.
|
|
|
|
*/
|
|
|
|
private static function getUserLinks( $content, Title $title ) {
|
|
|
|
$output = self::parseNonEditWikitext( $content, new Article( $title ) );
|
|
|
|
$links = $output->getLinks();
|
|
|
|
|
|
|
|
if ( !isset( $links[NS_USER] ) || !is_array( $links[NS_USER] ) ) {
|
2019-11-27 01:57:16 +00:00
|
|
|
return [];
|
2016-07-05 14:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $links[NS_USER];
|
|
|
|
}
|
|
|
|
|
2016-06-21 14:42:56 +00:00
|
|
|
private static function hasSubpage( $dbk ) {
|
|
|
|
return strpos( $dbk, '/' ) !== false;
|
|
|
|
}
|
|
|
|
|
2012-10-28 16:47:41 +00:00
|
|
|
/**
|
|
|
|
* It's like Article::prepareTextForEdit,
|
|
|
|
* but not for editing (old wikitext usually)
|
|
|
|
* Stolen from AbuseFilterVariableHolder
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $wikitext
|
|
|
|
* @param Article $article
|
2012-10-28 16:47:41 +00:00
|
|
|
*
|
2015-01-31 01:00:06 +00:00
|
|
|
* @return ParserOutput
|
2012-10-28 16:47:41 +00:00
|
|
|
*/
|
2019-12-28 20:16:38 +00:00
|
|
|
private static function parseNonEditWikitext( $wikitext, Article $article ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
static $cache = [];
|
2012-10-28 16:47:41 +00:00
|
|
|
|
|
|
|
$cacheKey = md5( $wikitext ) . ':' . $article->getTitle()->getPrefixedText();
|
|
|
|
|
|
|
|
if ( isset( $cache[$cacheKey] ) ) {
|
|
|
|
return $cache[$cacheKey];
|
|
|
|
}
|
|
|
|
|
2019-07-01 17:49:31 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
|
|
|
|
2020-03-04 18:06:30 +00:00
|
|
|
$options = new ParserOptions( $article->getContext()->getUser() );
|
2019-07-01 17:49:31 +00:00
|
|
|
$output = $parser->parse( $wikitext, $article->getTitle(), $options );
|
2012-10-28 16:47:41 +00:00
|
|
|
$cache[$cacheKey] = $output;
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
/**
|
|
|
|
* Given a Revision object, returns a talk-page-centric interpretation
|
|
|
|
* of the changes made in it.
|
|
|
|
*
|
2019-04-19 20:57:42 +00:00
|
|
|
* @param RevisionRecord $revision
|
2012-07-27 22:16:19 +00:00
|
|
|
* @see EchoDiscussionParser::interpretDiff
|
2018-08-13 07:17:15 +00:00
|
|
|
* @return array[] See {@see interpretDiff} for details.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2019-04-19 20:57:42 +00:00
|
|
|
private static function getChangeInterpretationForRevision( RevisionRecord $revision ) {
|
2016-05-27 15:38:06 +00:00
|
|
|
if ( $revision->getId() && isset( self::$revisionInterpretationCache[$revision->getId()] ) ) {
|
|
|
|
return self::$revisionInterpretationCache[$revision->getId()];
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 19:35:03 +00:00
|
|
|
$userIdentity = $revision->getUser();
|
2020-01-29 04:01:51 +00:00
|
|
|
$userID = $userIdentity ? $userIdentity->getId() : 0;
|
|
|
|
$userName = $userIdentity ? $userIdentity->getName() : '';
|
2020-07-22 20:49:49 +00:00
|
|
|
$user = $userID !== 0 ? User::newFromId( $userID ) : User::newFromName( $userName, false );
|
2019-04-17 15:46:06 +00:00
|
|
|
|
2012-08-13 14:57:57 +00:00
|
|
|
$prevText = '';
|
|
|
|
if ( $revision->getParentId() ) {
|
2019-04-17 15:46:06 +00:00
|
|
|
$store = MediaWikiServices::getInstance()->getRevisionStore();
|
|
|
|
$prevRevision = $store->getRevisionById( $revision->getParentId() );
|
2012-08-13 14:57:57 +00:00
|
|
|
if ( $prevRevision ) {
|
2019-12-21 05:45:14 +00:00
|
|
|
$prevText = ContentHandler::getContentText( $prevRevision->getContent( SlotRecord::MAIN ) ) ?: '';
|
2012-08-13 14:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2016-10-04 23:42:05 +00:00
|
|
|
$changes = self::getMachineReadableDiff(
|
|
|
|
$prevText,
|
2019-04-17 15:46:06 +00:00
|
|
|
ContentHandler::getContentText( $revision->getContent( SlotRecord::MAIN ) )
|
|
|
|
);
|
|
|
|
$output = self::interpretDiff(
|
|
|
|
$changes,
|
|
|
|
$user->getName(),
|
|
|
|
Title::newFromLinkTarget( $revision->getPageAsLinkTarget() )
|
2016-10-04 23:42:05 +00:00
|
|
|
);
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2016-05-27 15:38:06 +00:00
|
|
|
self::$revisionInterpretationCache[$revision->getId()] = $output;
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a machine-readable diff, interprets the changes
|
|
|
|
* in terms of discussion page actions
|
|
|
|
*
|
|
|
|
* @todo Expand recognisable actions.
|
2016-05-27 15:38:06 +00:00
|
|
|
*
|
2019-11-07 19:35:03 +00:00
|
|
|
* @param array[] $changes Output of EchoEvent::getMachineReadableDiff
|
2018-06-17 16:56:02 +00:00
|
|
|
* @param string $username
|
2018-04-04 15:11:39 +00:00
|
|
|
* @param Title|null $title
|
2016-05-27 15:38:06 +00:00
|
|
|
* @return array[] Array of associative arrays.
|
|
|
|
*
|
2012-07-27 22:16:19 +00:00
|
|
|
* Each entry represents an action, which is classified in the 'action' field.
|
|
|
|
* All types contain a 'content' field except 'unknown'
|
|
|
|
* (which instead passes through the machine-readable diff in 'details')
|
|
|
|
* and 'unknown-change' (which provides 'new_content' and 'old_content')
|
|
|
|
* action may be:
|
|
|
|
* - add-comment: A comment signed by the user is added to an
|
|
|
|
* existing section.
|
|
|
|
* - new-section-with-comment: A new section is added, containing
|
|
|
|
* a single comment signed by the user in question.
|
2016-08-09 16:20:50 +00:00
|
|
|
* - add-section-multiple: A new section or additions to a section
|
|
|
|
* while editing multiple sections at once.
|
2012-07-27 22:16:19 +00:00
|
|
|
* - unknown-multi-signed-addition: Some signed content is added,
|
|
|
|
* but it contains multiple signatures.
|
|
|
|
* - unknown-unsigned-addition: Some content is added, but it is
|
|
|
|
* unsigned.
|
|
|
|
* - unknown-subtraction: Some content was removed. These actions are
|
|
|
|
* not currently analysed.
|
|
|
|
* - unknown-change: Some content was replaced with other content.
|
2016-08-16 13:51:17 +00:00
|
|
|
* - unknown-signed-change: Same as unknown-change, but signed.
|
2017-02-07 18:34:51 +00:00
|
|
|
* - unknown-multi-signed-change: Same as unknown-change,
|
|
|
|
* but it contains multiple signatures.
|
2012-07-27 22:16:19 +00:00
|
|
|
* - unknown: Unrecognised change type.
|
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public static function interpretDiff( array $changes, $username, Title $title = null ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
// One extra item in $changes for _info
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions = [];
|
|
|
|
$signedSections = [];
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $changes as $index => $change ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
if ( !is_numeric( $index ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$change['action'] ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
// Unknown action; skip
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
|
2020-07-22 20:49:49 +00:00
|
|
|
if ( $change['action'] === 'add' ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
$content = trim( $change['content'] );
|
2016-06-22 12:37:37 +00:00
|
|
|
// The \A means the regex must match at the beginning of the string.
|
|
|
|
// This is slightly different than ^ which matches beginning of each
|
2014-09-12 03:07:29 +00:00
|
|
|
// line in multiline mode.
|
2018-09-25 16:05:40 +00:00
|
|
|
$startSection = preg_match( '/\A' . self::HEADER_REGEX . '/um', $content );
|
2012-07-27 22:16:19 +00:00
|
|
|
$sectionCount = self::getSectionCount( $content );
|
2015-02-17 10:10:49 +00:00
|
|
|
$signedUsers = array_keys( self::extractSignatures( $content, $title ) );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
if (
|
2018-10-19 08:04:24 +00:00
|
|
|
count( $signedUsers ) === 1 &&
|
2016-05-27 15:38:06 +00:00
|
|
|
in_array( $username, $signedUsers )
|
2012-07-27 22:16:19 +00:00
|
|
|
) {
|
|
|
|
if ( $sectionCount === 0 ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
$signedSections[] = self::getSectionSpan( $change['right-pos'], $changes['_info']['rhs'] );
|
2012-07-27 22:16:19 +00:00
|
|
|
$fullSection = self::getFullSection( $changes['_info']['rhs'], $change['right-pos'] );
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2012-07-27 22:16:19 +00:00
|
|
|
'type' => 'add-comment',
|
|
|
|
'content' => $content,
|
|
|
|
'full-section' => $fullSection,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2012-07-27 22:16:19 +00:00
|
|
|
} elseif ( $startSection && $sectionCount === 1 ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
$signedSections[] = self::getSectionSpan( $change['right-pos'], $changes['_info']['rhs'] );
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2012-07-27 22:16:19 +00:00
|
|
|
'type' => 'new-section-with-comment',
|
|
|
|
'content' => $content,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2012-07-27 22:16:19 +00:00
|
|
|
} else {
|
2016-08-16 13:51:17 +00:00
|
|
|
$nextSectionStart = $change['right-pos'];
|
2016-08-09 16:20:50 +00:00
|
|
|
$sectionData = self::extractSections( $content );
|
|
|
|
foreach ( $sectionData as $section ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
$sectionSpan = self::getSectionSpan( $nextSectionStart, $changes['_info']['rhs'] );
|
|
|
|
$nextSectionStart = $sectionSpan[1] + 1;
|
2016-08-09 16:20:50 +00:00
|
|
|
$sectionSignedUsers = self::extractSignatures( $section['content'], $title );
|
|
|
|
if ( !empty( $sectionSignedUsers ) ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
$signedSections[] = $sectionSpan;
|
2016-08-09 16:20:50 +00:00
|
|
|
if ( !$section['header'] ) {
|
2020-06-27 10:05:03 +00:00
|
|
|
$fullSection = self::getFullSection(
|
|
|
|
$changes['_info']['rhs'],
|
|
|
|
$change['right-pos']
|
|
|
|
);
|
2016-08-09 16:20:50 +00:00
|
|
|
$section['header'] = self::extractHeader( $fullSection );
|
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2016-08-09 16:20:50 +00:00
|
|
|
'type' => 'add-section-multiple',
|
|
|
|
'content' => $section['content'],
|
|
|
|
'header' => $section['header'],
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-08-09 16:20:50 +00:00
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2016-08-09 16:20:50 +00:00
|
|
|
'type' => 'unknown-unsigned-addition',
|
|
|
|
'content' => $section['content'],
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-08-09 16:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
2018-06-17 16:59:03 +00:00
|
|
|
} elseif ( $signedUsers !== [] ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2012-07-27 22:16:19 +00:00
|
|
|
'type' => 'unknown-multi-signed-addition',
|
|
|
|
'content' => $content,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2012-07-27 22:16:19 +00:00
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2012-07-27 22:16:19 +00:00
|
|
|
'type' => 'unknown-unsigned-addition',
|
|
|
|
'content' => $content,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
2020-07-22 20:49:49 +00:00
|
|
|
} elseif ( $change['action'] === 'subtract' ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2012-07-27 22:16:19 +00:00
|
|
|
'type' => 'unknown-subtraction',
|
|
|
|
'content' => $change['content'],
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2020-07-22 20:49:49 +00:00
|
|
|
} elseif ( $change['action'] === 'change' ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2012-07-27 22:16:19 +00:00
|
|
|
'type' => 'unknown-change',
|
|
|
|
'old_content' => $change['old_content'],
|
|
|
|
'new_content' => $change['new_content'],
|
2016-08-16 13:51:17 +00:00
|
|
|
'right-pos' => $change['right-pos'],
|
|
|
|
'full-section' => self::getFullSection( $changes['_info']['rhs'], $change['right-pos'] ),
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-08-16 13:51:17 +00:00
|
|
|
|
|
|
|
if ( self::hasNewSignature(
|
|
|
|
$change['old_content'],
|
|
|
|
$change['new_content'],
|
|
|
|
$username,
|
|
|
|
$title
|
|
|
|
) ) {
|
|
|
|
$signedSections[] = self::getSectionSpan( $change['right-pos'], $changes['_info']['rhs'] );
|
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
$actions[] = [
|
2012-07-27 22:16:19 +00:00
|
|
|
'type' => 'unknown',
|
|
|
|
'details' => $change,
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 13:51:17 +00:00
|
|
|
if ( !empty( $signedSections ) ) {
|
|
|
|
$actions = self::convertToUnknownSignedChanges( $signedSections, $actions );
|
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function getSignedUsers( $content, $title ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
return array_keys( self::extractSignatures( $content, $title ) );
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function hasNewSignature( $oldContent, $newContent, $username, $title ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
$oldSignedUsers = self::getSignedUsers( $oldContent, $title );
|
|
|
|
$newSignedUsers = self::getSignedUsers( $newContent, $title );
|
|
|
|
|
|
|
|
return !in_array( $username, $oldSignedUsers ) && in_array( $username, $newSignedUsers );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts actions of type "unknown-change" to "unknown-signed-change" if the change is in a signed section.
|
|
|
|
*
|
2018-08-13 07:25:22 +00:00
|
|
|
* @param array[] $signedSections Array of arrays containing first and last line number of signed sections
|
|
|
|
* @param array[] $actions
|
|
|
|
* @return array[] Converted actions
|
2016-08-16 13:51:17 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function convertToUnknownSignedChanges( array $signedSections, array $actions ) {
|
2017-06-20 02:41:30 +00:00
|
|
|
return array_map( function ( $action ) use( $signedSections ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
if (
|
|
|
|
$action['type'] === 'unknown-change' &&
|
|
|
|
self::isInSignedSection( $action['right-pos'], $signedSections )
|
|
|
|
) {
|
2017-02-07 18:34:51 +00:00
|
|
|
$signedUsers = self::getSignedUsers( $action['new_content'], null );
|
|
|
|
if ( count( $signedUsers ) === 1 ) {
|
|
|
|
$action['type'] = 'unknown-signed-change';
|
|
|
|
} else {
|
|
|
|
$action['type'] = 'unknown-multi-signed-change';
|
|
|
|
}
|
2016-08-16 13:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $action;
|
|
|
|
}, $actions );
|
|
|
|
}
|
|
|
|
|
2019-11-07 19:35:03 +00:00
|
|
|
/**
|
|
|
|
* @param int $line
|
|
|
|
* @param array[] $signedSections
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function isInSignedSection( $line, array $signedSections ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
foreach ( $signedSections as $section ) {
|
|
|
|
if ( $line > $section[0] && $line <= $section[1] ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
/**
|
|
|
|
* Finds the section that a given line is in.
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param array $lines of lines in the page.
|
|
|
|
* @param int $offset The line to find the full section for.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return string Content of the section.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
public static function getFullSection( array $lines, $offset ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
$start = self::getSectionStartIndex( $offset, $lines );
|
|
|
|
$end = self::getSectionEndIndex( $offset, $lines );
|
|
|
|
$content = implode( "\n", array_slice( $lines, $start, $end - $start ) );
|
|
|
|
|
|
|
|
return trim( $content, "\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a line number and a text, find the first and last line of the section the line number is in.
|
|
|
|
* If there are subsections, the last line index will be the line before the beginning of the first subsection.
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param int $offset line number
|
2018-08-13 07:25:22 +00:00
|
|
|
* @param string[] $lines
|
|
|
|
* @return int[] Tuple [$firstLine, $lastLine]
|
2016-08-16 13:51:17 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
private static function getSectionSpan( $offset, array $lines ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2016-08-16 13:51:17 +00:00
|
|
|
self::getSectionStartIndex( $offset, $lines ),
|
|
|
|
self::getSectionEndIndex( $offset, $lines )
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-08-16 13:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the line number of the start of the section that $offset is in.
|
2016-10-05 22:27:02 +00:00
|
|
|
* @param int $offset
|
2018-08-13 07:25:22 +00:00
|
|
|
* @param string[] $lines
|
2016-08-16 13:51:17 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function getSectionStartIndex( $offset, array $lines ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
for ( $i = $offset - 1; $i >= 0; $i-- ) {
|
|
|
|
if ( self::getSectionCount( $lines[$i] ) ) {
|
|
|
|
break;
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 13:51:17 +00:00
|
|
|
return $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the line number of the end of the section that $offset is in.
|
2016-10-05 22:27:02 +00:00
|
|
|
* @param int $offset
|
|
|
|
* @param array $lines
|
2016-08-16 13:51:17 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function getSectionEndIndex( $offset, array $lines ) {
|
2016-08-16 13:51:17 +00:00
|
|
|
$lastLine = count( $lines );
|
|
|
|
for ( $i = $offset; $i < $lastLine; $i++ ) {
|
|
|
|
if ( self::getSectionCount( $lines[$i] ) ) {
|
2016-07-13 15:31:06 +00:00
|
|
|
break;
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 13:51:17 +00:00
|
|
|
return $i;
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of section headers in a string.
|
|
|
|
*
|
2021-01-29 12:55:41 +00:00
|
|
|
* @param string $text
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return int Number of section headers found.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
public static function getSectionCount( $text ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$text = trim( $text );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2019-02-19 08:54:56 +00:00
|
|
|
return (int)preg_match_all( '/' . self::HEADER_REGEX . '/um', $text );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-17 19:16:46 +00:00
|
|
|
* Gets the title of a section or sub section
|
2012-07-27 22:16:19 +00:00
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text The text of the section.
|
2016-08-03 12:31:09 +00:00
|
|
|
* @return string|false The title of the section or false if not found
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
public static function extractHeader( $text ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$text = trim( $text );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$matches = [];
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2014-09-12 03:07:29 +00:00
|
|
|
if ( !preg_match_all( '/' . self::HEADER_REGEX . '/um', $text, $matches ) ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-17 19:16:46 +00:00
|
|
|
return trim( end( $matches[2] ) );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 16:20:50 +00:00
|
|
|
/**
|
|
|
|
* Extracts sections and their contents from text.
|
|
|
|
*
|
|
|
|
* @param string $text The text to parse.
|
|
|
|
* @return array[]
|
|
|
|
* Array of arrays containing sections with header and content.
|
|
|
|
* - [header]: The full header string of the section or false if there is preceding text without header.
|
|
|
|
* - [content]: The content of the section including the header string.
|
|
|
|
*/
|
|
|
|
private static function extractSections( $text ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$matches = [];
|
2016-08-09 16:20:50 +00:00
|
|
|
|
|
|
|
if ( !preg_match_all( '/' . self::HEADER_REGEX . '/um', $text, $matches, PREG_OFFSET_CAPTURE ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ [
|
2016-08-09 16:20:50 +00:00
|
|
|
'header' => false,
|
|
|
|
'content' => $text
|
2016-12-05 18:51:07 +00:00
|
|
|
] ];
|
2016-08-09 16:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$sectionNum = count( $matches[0] );
|
2016-12-05 18:51:07 +00:00
|
|
|
$sections = [];
|
2016-08-09 16:20:50 +00:00
|
|
|
|
|
|
|
if ( $matches[0][0][1] > 1 ) { // is there text before the first headline?
|
2016-12-05 18:51:07 +00:00
|
|
|
$sections[] = [
|
2016-08-09 16:20:50 +00:00
|
|
|
'header' => false,
|
2017-08-11 03:22:40 +00:00
|
|
|
'content' => substr( $text, 0, $matches[0][0][1] - 1 )
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-08-09 16:20:50 +00:00
|
|
|
}
|
|
|
|
for ( $i = 0; $i < $sectionNum; $i++ ) {
|
|
|
|
if ( $i + 1 < $sectionNum ) {
|
|
|
|
$content = substr( $text, $matches[0][$i][1], $matches[0][$i + 1][1] - $matches[0][$i][1] );
|
|
|
|
} else {
|
|
|
|
$content = substr( $text, $matches[0][$i][1] );
|
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
$sections[] = [
|
2016-08-09 16:20:50 +00:00
|
|
|
'header' => self::extractHeader( $matches[0][$i][0] ),
|
2017-08-11 03:22:40 +00:00
|
|
|
'content' => trim( $content )
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2016-08-09 16:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $sections;
|
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
/**
|
|
|
|
* Strips out a signature if possible.
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text The wikitext to strip
|
2018-04-04 15:11:39 +00:00
|
|
|
* @param Title|null $title
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return string
|
2012-07-31 00:29:49 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function stripSignature( $text, Title $title = null ) {
|
2015-02-17 10:10:49 +00:00
|
|
|
$output = self::getUserFromLine( $text, $title );
|
2012-07-31 00:29:49 +00:00
|
|
|
if ( $output === false ) {
|
2015-01-31 01:00:06 +00:00
|
|
|
$timestampPos = self::getTimestampPosition( $text );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
return substr( $text, 0, $timestampPos );
|
|
|
|
}
|
|
|
|
|
2018-06-15 20:01:16 +00:00
|
|
|
// Use truncateForDatabase() instead of truncateHTML() because
|
|
|
|
// truncateHTML() would not strip signature if the text contains
|
|
|
|
// < or &. (And we can't use truncateForVisual() because
|
|
|
|
// self::getUserFromLine() returns byte offsets, not character
|
|
|
|
// offsets.)
|
2018-08-13 21:29:50 +00:00
|
|
|
return MediaWikiServices::getInstance()->getContentLanguage()
|
|
|
|
->truncateForDatabase( $text, $output[0], '' );
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
/**
|
|
|
|
* Strips out a section header
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text The text to strip out the section header from.
|
|
|
|
* @return string The same text, with the section header stripped out.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function stripHeader( $text ) {
|
2014-09-12 03:07:29 +00:00
|
|
|
$text = preg_replace( '/' . self::HEADER_REGEX . '/um', '', $text );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the input is a signed comment.
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text The text to check.
|
|
|
|
* @param User|bool $user If set, will only return true if the comment is
|
2012-07-27 22:16:19 +00:00
|
|
|
* signed by this user.
|
2018-04-04 15:11:39 +00:00
|
|
|
* @param Title|null $title
|
2016-05-27 15:38:06 +00:00
|
|
|
* @return bool
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
public static function isSignedComment( $text, $user = false, Title $title = null ) {
|
2015-02-17 10:10:49 +00:00
|
|
|
$userData = self::getUserFromLine( $text, $title );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
if ( $userData === false ) {
|
|
|
|
return false;
|
|
|
|
} elseif ( $user === false ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-27 18:28:37 +00:00
|
|
|
list( , $foundUser ) = $userData;
|
2021-02-21 23:01:37 +00:00
|
|
|
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2021-02-21 23:01:37 +00:00
|
|
|
return $userNameUtils->getCanonical( $foundUser, UserNameUtils::RIGOR_NONE ) ===
|
|
|
|
$userNameUtils->getCanonical( $user, UserNameUtils::RIGOR_NONE );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
/**
|
|
|
|
* Finds the start position, if any, of the timestamp on a line
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $line The line to search for a signature on
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return int|false Integer position
|
2012-07-31 00:29:49 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
public static function getTimestampPosition( $line ) {
|
2012-07-31 00:29:49 +00:00
|
|
|
$timestampRegex = self::getTimestampRegex();
|
2016-12-05 18:51:07 +00:00
|
|
|
$tsMatches = [];
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !preg_match(
|
2018-09-25 16:15:49 +00:00
|
|
|
"/$timestampRegex/mu",
|
2012-08-31 21:50:46 +00:00
|
|
|
$line,
|
|
|
|
$tsMatches,
|
|
|
|
PREG_OFFSET_CAPTURE
|
2012-07-31 00:29:49 +00:00
|
|
|
) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-19 13:41:22 +00:00
|
|
|
return $tsMatches[0][1];
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
/**
|
|
|
|
* Finds differences between $oldText and $newText
|
|
|
|
* and returns the result in a machine-readable format.
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $oldText The "left hand side" of the diff.
|
|
|
|
* @param string $newText The "right hand side" of the diff.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return array[] Array of changes.
|
2012-07-27 22:16:19 +00:00
|
|
|
* Each change consists of:
|
|
|
|
* * An 'action', one of:
|
|
|
|
* - add
|
|
|
|
* - subtract
|
|
|
|
* - change
|
|
|
|
* * 'content' that was added or removed, or in the case
|
|
|
|
* of a change, 'old_content' and 'new_content'
|
|
|
|
* * 'left_pos' and 'right_pos' (in lines) of the change.
|
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
public static function getMachineReadableDiff( $oldText, $newText ) {
|
2013-05-19 04:15:45 +00:00
|
|
|
if ( !isset( self::$diffParser ) ) {
|
|
|
|
self::$diffParser = new EchoDiffParser;
|
2013-04-29 23:24:46 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2013-05-19 04:15:45 +00:00
|
|
|
return self::$diffParser->getChangeSet( $oldText, $newText );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds and extracts signatures in $text
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text The text in which to look for signed comments.
|
2018-04-04 15:11:39 +00:00
|
|
|
* @param Title|null $title
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return string[] Associative array, the key is the username, the value
|
2012-07-27 22:16:19 +00:00
|
|
|
* is the last signature that was found.
|
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function extractSignatures( $text, Title $title = null ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
$lines = explode( "\n", $text );
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$output = [];
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
$lineNumber = 0;
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $lines as $line ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
++$lineNumber;
|
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
// Look for the last user link on the line.
|
2015-02-17 10:10:49 +00:00
|
|
|
$userData = self::getUserFromLine( $line, $title );
|
2012-07-27 22:16:19 +00:00
|
|
|
if ( $userData === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
list( $signaturePos, $user ) = $userData;
|
|
|
|
|
|
|
|
$signature = substr( $line, $signaturePos );
|
|
|
|
$output[$user] = $signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-13 01:16:59 +00:00
|
|
|
* From a line in the signature, extract all the users linked to
|
2012-07-27 22:16:19 +00:00
|
|
|
*
|
2015-06-13 01:16:59 +00:00
|
|
|
* @param string $line Line of text potentially including linked user, user talk,
|
|
|
|
* and contribution pages
|
2018-04-04 15:11:39 +00:00
|
|
|
* @return string[] array of usernames, empty array for none detected
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2015-10-29 11:25:14 +00:00
|
|
|
public static function extractUsersFromLine( $line ) {
|
2015-01-31 01:00:06 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-06-13 01:16:59 +00:00
|
|
|
// match all title-like excerpts in this line
|
|
|
|
if ( !preg_match_all( '/\[\[([^\[]+)\]\]/', $line, $matches ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2015-06-13 01:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$matches = $matches[1];
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$usernames = [];
|
2015-06-13 01:16:59 +00:00
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
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.
|
|
|
|
*/
|
2018-10-19 08:06:53 +00:00
|
|
|
$match = explode( '|', $match, 2 );
|
2015-01-31 01:00:06 +00:00
|
|
|
$title = Title::newFromText( $match[0] );
|
|
|
|
|
|
|
|
// figure out if we the link is related to a user
|
2016-05-27 15:38:06 +00:00
|
|
|
if (
|
|
|
|
$title &&
|
|
|
|
( $title->getNamespace() === NS_USER || $title->getNamespace() === NS_USER_TALK )
|
|
|
|
) {
|
2015-06-13 01:16:59 +00:00
|
|
|
$usernames[] = $title->getText();
|
2015-01-31 01:00:06 +00:00
|
|
|
} elseif ( $title && $title->isSpecial( 'Contributions' ) ) {
|
|
|
|
$parts = explode( '/', $title->getText(), 2 );
|
2015-06-13 01:16:59 +00:00
|
|
|
$usernames[] = end( $parts );
|
2015-01-31 01:00:06 +00:00
|
|
|
} else {
|
|
|
|
// move on to next matched title-like excerpt
|
2012-07-27 22:16:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-13 01:16:59 +00:00
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2015-06-13 01:16:59 +00:00
|
|
|
return $usernames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From a line in a wiki page, determine which user, if any,
|
|
|
|
* has signed it.
|
|
|
|
*
|
2021-01-29 12:55:41 +00:00
|
|
|
* @param string $line
|
2018-04-04 15:11:39 +00:00
|
|
|
* @param Title|null $title
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return array|false False for none, array for success.
|
2015-06-13 01:16:59 +00:00
|
|
|
* - First element is the position of the signature.
|
|
|
|
* - Second element is the normalised user name.
|
|
|
|
*/
|
2015-10-29 11:25:14 +00:00
|
|
|
public static function getUserFromLine( $line, Title $title = null ) {
|
2019-07-01 17:49:31 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
2015-06-13 01:16:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First we call extractUsersFromLine to get all the potential usernames
|
|
|
|
* from the line. Then, we loop backwards through them, figure out which
|
|
|
|
* match to a user, regenera the signature based on that user, and
|
|
|
|
* see if it matches!
|
|
|
|
*/
|
|
|
|
$usernames = self::extractUsersFromLine( $line );
|
|
|
|
$usernames = array_reverse( $usernames );
|
|
|
|
foreach ( $usernames as $username ) {
|
2015-01-31 01:00:06 +00:00
|
|
|
// generate (dateless) signature from the user we think we've
|
|
|
|
// discovered the signature from
|
|
|
|
// don't validate the username - anon (IP) is fine!
|
|
|
|
$user = User::newFromName( $username, false );
|
2019-07-01 17:49:31 +00:00
|
|
|
$sig = $parser->preSaveTransform(
|
2016-05-27 15:38:06 +00:00
|
|
|
'~~~',
|
|
|
|
$title ?: Title::newMainPage(),
|
|
|
|
$user,
|
2020-03-04 18:06:30 +00:00
|
|
|
new ParserOptions( $user )
|
2016-05-27 15:38:06 +00:00
|
|
|
);
|
2014-09-29 19:38:01 +00:00
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
// see if we can find this user's generated signature in the content
|
|
|
|
$pos = strrpos( $line, $sig );
|
|
|
|
if ( $pos !== false ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ $pos, $username ];
|
2015-01-31 01:00:06 +00:00
|
|
|
}
|
|
|
|
// couldn't find sig, move on to next link excerpt and try there
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 01:00:06 +00:00
|
|
|
// couldn't find any matching signature
|
|
|
|
return false;
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the last link beginning with a given prefix on a line.
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $line The line to search.
|
|
|
|
* @param string $linkPrefix The prefix to search for.
|
2019-12-21 05:45:14 +00:00
|
|
|
* @param int|false $failureOffset
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return array|false False for failure, array for success.
|
2012-07-27 22:16:19 +00:00
|
|
|
* - First element is the string offset of the link.
|
|
|
|
* - Second element is the user the link refers to.
|
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function getLinkFromLine( $line, $linkPrefix, $failureOffset = false ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
$offset = 0;
|
|
|
|
|
|
|
|
// If extraction failed at another offset, try again.
|
|
|
|
if ( $failureOffset !== false ) {
|
|
|
|
$offset = $failureOffset - strlen( $line ) - 1;
|
|
|
|
}
|
|
|
|
|
2013-11-05 19:02:06 +00:00
|
|
|
// Avoid PHP warning: Offset is greater than the length of haystack string
|
|
|
|
if ( abs( $offset ) > strlen( $line ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-27 22:16:19 +00:00
|
|
|
$linkPos = strripos( $line, $linkPrefix, $offset );
|
|
|
|
|
|
|
|
if ( $linkPos === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$linkUser = self::extractUserFromLink( $line, $linkPrefix, $linkPos );
|
|
|
|
|
|
|
|
if ( $linkUser === false ) {
|
|
|
|
// Look for another place.
|
|
|
|
return self::getLinkFromLine( $line, $linkPrefix, $linkPos );
|
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ $linkPos, $linkUser ];
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given text including a link, gives the user that that link refers to
|
|
|
|
*
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text The text to extract from.
|
|
|
|
* @param string $prefix The link prefix that was used to find the link.
|
|
|
|
* @param int $offset Optionally, the offset of the start of the link.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return bool|string Type description
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
private static function extractUserFromLink( $text, $prefix, $offset = 0 ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
$userPart = substr( $text, strlen( $prefix ) + $offset );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$userMatches = [];
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !preg_match(
|
2012-07-27 22:16:19 +00:00
|
|
|
'/^[^\|\]\#]+/u',
|
|
|
|
$userPart,
|
2012-08-01 19:53:05 +00:00
|
|
|
$userMatches
|
2012-07-27 22:16:19 +00:00
|
|
|
) ) {
|
|
|
|
// user link is invalid
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $userMatches[0];
|
2021-02-24 13:31:50 +00:00
|
|
|
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils();
|
2012-07-27 22:16:19 +00:00
|
|
|
if (
|
2021-02-24 13:31:50 +00:00
|
|
|
!$userNameUtils->isIP( $user ) &&
|
2021-02-21 23:01:37 +00:00
|
|
|
$userNameUtils->getCanonical( $user ) === false
|
2012-07-27 22:16:19 +00:00
|
|
|
) {
|
|
|
|
// Not a real username
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:01:37 +00:00
|
|
|
return $userNameUtils->getCanonical( $userMatches[0], UserNameUtils::RIGOR_NONE );
|
2012-07-27 22:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a regular expression that will match this wiki's
|
|
|
|
* timestamps as given by ~~~~.
|
2012-08-31 21:50:46 +00:00
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2016-05-27 15:38:06 +00:00
|
|
|
* @return string regular expression fragment.
|
2012-07-27 22:16:19 +00:00
|
|
|
*/
|
2018-11-02 17:47:39 +00:00
|
|
|
public static function getTimestampRegex() {
|
2012-07-27 22:16:19 +00:00
|
|
|
if ( self::$timestampRegex !== null ) {
|
|
|
|
return self::$timestampRegex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 1: Get an exemplar timestamp
|
|
|
|
$title = Title::newMainPage();
|
2012-08-30 16:04:39 +00:00
|
|
|
$user = User::newFromName( 'Test' );
|
2020-03-04 18:06:30 +00:00
|
|
|
$options = new ParserOptions( $user );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2019-07-01 17:49:31 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
2012-07-27 22:16:19 +00:00
|
|
|
$exemplarTimestamp =
|
2019-07-01 17:49:31 +00:00
|
|
|
$parser->preSaveTransform( '~~~~~', $title, $user, $options );
|
2012-07-27 22:16:19 +00:00
|
|
|
|
|
|
|
// Step 2: Generalise it
|
|
|
|
// Trim off the timezone to replace at the end
|
|
|
|
$output = $exemplarTimestamp;
|
2020-10-27 18:59:26 +00:00
|
|
|
$tzRegex = '/\h*\(\w+\)\h*$/u';
|
2016-12-05 18:51:07 +00:00
|
|
|
$tzMatches = [];
|
2018-10-11 08:59:04 +00:00
|
|
|
if ( preg_match( $tzRegex, $output, $tzMatches, PREG_OFFSET_CAPTURE ) ) {
|
|
|
|
$output = substr( $output, 0, $tzMatches[0][1] );
|
2014-12-03 17:07:50 +00:00
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
$output = preg_quote( $output, '/' );
|
|
|
|
$output = preg_replace( '/[^\d\W]+/u', '[^\d\W]+', $output );
|
|
|
|
$output = preg_replace( '/\d+/u', '\d+', $output );
|
|
|
|
|
2014-12-03 17:07:50 +00:00
|
|
|
if ( $tzMatches ) {
|
2018-10-11 08:59:04 +00:00
|
|
|
$output .= preg_quote( $tzMatches[0][0] );
|
2014-12-03 17:07:50 +00:00
|
|
|
}
|
2012-07-27 22:16:19 +00:00
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !preg_match( "/$output/u", $exemplarTimestamp ) ) {
|
2012-07-27 22:16:19 +00:00
|
|
|
throw new MWException( "Timestamp regex does not match exemplar" );
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$timestampRegex = $output;
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2012-11-07 01:41:06 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-30 02:16:31 +00:00
|
|
|
* Parse wikitext into truncated plain text.
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text
|
2015-09-02 18:09:11 +00:00
|
|
|
* @param Language $lang
|
2020-07-22 22:03:44 +00:00
|
|
|
* @param int $length Length in characters (not bytes); default DEFAULT_SNIPPET_LENGTH
|
2016-09-01 20:48:56 +00:00
|
|
|
* @param Title|null $title Page from which the text snippet is being extracted
|
2012-11-07 01:41:06 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-22 22:03:44 +00:00
|
|
|
public static function getTextSnippet(
|
|
|
|
$text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH, $title = null
|
|
|
|
) {
|
Unconvolute DiscussionParser::getTextSnippet()
The previous implementation did the following weird things:
* Stripped tags before parsing
* Stripped templates before parsing using a hacky while loop
that bails after ten attempts
* Decoded entities using htmlspecialchars_decode(), while
html_entity_decode() makes more sense
* ...which meant it had to manually convert   back
to spaces, which is not necessary if you use html_entity_decode()
* Removed any single braces ('{' and '}') from the output
* Rejected the entire output if there were any entities left,
which is fairly likely since htmlspecialchars_decode()
only decodes a few of them
Instead of all this, just parse, strip tags, decode entities
(all of them, not just a few), trim and truncate. In particular,
don't strip templates, because we use getTextSnippet() in mention
notifications, which look weird when {{ping}} templates are stripped.
Bug: T129531
Change-Id: I956b2f6badc40d2f5bf90a0458ccab8b8fc6fefb
2016-03-11 02:20:08 +00:00
|
|
|
// Parse wikitext
|
2020-03-14 13:04:32 +00:00
|
|
|
$html = MediaWikiServices::getInstance()->getMessageCache()->parse( $text, $title )->getText( [
|
2017-11-22 20:54:02 +00:00
|
|
|
'enableSectionEditLinks' => false
|
|
|
|
] );
|
2017-07-07 23:55:51 +00:00
|
|
|
$plaintext = trim( Sanitizer::stripAllTags( $html ) );
|
2018-06-15 20:01:16 +00:00
|
|
|
return $lang->truncateForVisual( $plaintext, $length );
|
2012-11-07 01:41:06 +00:00
|
|
|
}
|
2016-03-30 02:16:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse an edit summary into truncated plain text.
|
2016-05-27 15:38:06 +00:00
|
|
|
* @param string $text
|
2016-03-30 02:16:31 +00:00
|
|
|
* @param Language $lang
|
2020-07-22 22:03:44 +00:00
|
|
|
* @param int $length Length in characters (not bytes); default DEFAULT_SNIPPET_LENGTH
|
2016-03-30 02:16:31 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-22 22:03:44 +00:00
|
|
|
public static function getTextSnippetFromSummary( $text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH ) {
|
2016-03-30 02:16:31 +00:00
|
|
|
// Parse wikitext with summary parser
|
|
|
|
$html = Linker::formatLinksInComment( Sanitizer::escapeHtmlAllowEntities( $text ) );
|
2017-07-07 23:55:51 +00:00
|
|
|
$plaintext = trim( Sanitizer::stripAllTags( $html ) );
|
2018-06-15 20:01:16 +00:00
|
|
|
return $lang->truncateForVisual( $plaintext, $length );
|
2016-03-30 02:16:31 +00:00
|
|
|
}
|
2015-10-29 21:14:35 +00:00
|
|
|
|
2016-06-17 15:19:34 +00:00
|
|
|
/**
|
|
|
|
* Extract an edit excerpt from a revision
|
|
|
|
*
|
2019-04-19 20:57:42 +00:00
|
|
|
* @param RevisionRecord $revision
|
2016-06-17 15:19:34 +00:00
|
|
|
* @param Language $lang
|
2020-07-22 22:03:44 +00:00
|
|
|
* @param int $length Length in characters (not bytes); default DEFAULT_SNIPPET_LENGTH
|
2016-06-17 15:19:34 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-22 22:03:44 +00:00
|
|
|
public static function getEditExcerpt(
|
|
|
|
RevisionRecord $revision, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH
|
|
|
|
) {
|
2016-06-17 15:19:34 +00:00
|
|
|
$interpretation = self::getChangeInterpretationForRevision( $revision );
|
|
|
|
$section = self::detectSectionTitleAndText( $interpretation );
|
2018-06-15 20:01:16 +00:00
|
|
|
return $lang->truncateForVisual( $section['section-title'] . ' ' . $section['section-text'], $length );
|
2016-06-17 15:19:34 +00:00
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|