| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications; |
| 4 |
|
| 5 |
use Article; |
| 6 |
use MediaWiki\Content\TextContent; |
| 7 |
use MediaWiki\Context\RequestContext; |
| 8 |
use MediaWiki\Extension\Notifications\Hooks\HookRunner; |
| 9 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 10 |
use MediaWiki\Language\Language; |
| 11 |
use MediaWiki\MediaWikiServices; |
| 12 |
use MediaWiki\Parser\ParserOptions; |
| 13 |
use MediaWiki\Parser\ParserOutput; |
| 14 |
use MediaWiki\Parser\Sanitizer; |
| 15 |
use MediaWiki\Revision\RevisionRecord; |
| 16 |
use MediaWiki\Revision\SlotRecord; |
| 17 |
use MediaWiki\Title\Title; |
| 18 |
use MediaWiki\User\User; |
| 19 |
use MediaWiki\User\UserNameUtils; |
| 20 |
use RuntimeException; |
| 21 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 22 |
|
| 23 |
abstract class DiscussionParser { |
| 24 |
private const HEADER_REGEX = '^(==+)\h*([^=].*)\h*\1$'; |
| 25 |
|
| 26 |
public const DEFAULT_SNIPPET_LENGTH = 150; |
| 27 |
|
| 28 |
|
| 29 |
protected static $timestampRegex; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
protected static $revisionInterpretationCache = []; |
| 37 |
|
| 38 |
|
| 39 |
protected static $diffParser = null; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static function generateEventsForRevision( RevisionRecord $revision, $isRevert ) { |
| 49 |
global $wgEchoMentionsOnMultipleSectionEdits; |
| 50 |
global $wgEchoMentionOnChanges; |
| 51 |
$services = MediaWikiServices::getInstance(); |
| 52 |
$store = $services->getRevisionStore(); |
| 53 |
|
| 54 |
|
| 55 |
if ( $store->getPreviousRevision( $revision ) ) { |
| 56 |
$title = Title::newFromID( $revision->getPageId() ); |
| 57 |
|
| 58 |
} else { |
| 59 |
$title = Title::newFromID( $revision->getPageId(), IDBAccessObject::READ_LATEST ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
if ( !$title ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$events = []; |
| 68 |
|
| 69 |
$interpretation = self::getChangeInterpretationForRevision( $revision ); |
| 70 |
|
| 71 |
$userID = $revision->getUser()->getId(); |
| 72 |
$userName = $revision->getUser()->getName(); |
| 73 |
$user = $userID !== 0 ? User::newFromId( $userID ) : User::newFromName( $userName, false ); |
| 74 |
|
| 75 |
foreach ( $interpretation as $action ) { |
| 76 |
if ( $action['type'] === 'add-comment' ) { |
| 77 |
$fullSection = $action['full-section']; |
| 78 |
$header = self::extractHeader( $fullSection ); |
| 79 |
$userLinks = self::getUserLinks( $action['content'], $title ); |
| 80 |
$events = array_merge( |
| 81 |
$events, |
| 82 |
self::collectMentionEvents( $header, $userLinks, $action['content'], $revision, $user ) |
| 83 |
); |
| 84 |
} elseif ( $action['type'] === 'new-section-with-comment' ) { |
| 85 |
$content = $action['content']; |
| 86 |
$header = self::extractHeader( $content ); |
| 87 |
$userLinks = self::getUserLinks( $content, $title ); |
| 88 |
$events = array_merge( |
| 89 |
$events, |
| 90 |
self::collectMentionEvents( $header, $userLinks, $content, $revision, $user ) |
| 91 |
); |
| 92 |
} elseif ( $action['type'] === 'add-section-multiple' && $wgEchoMentionsOnMultipleSectionEdits ) { |
| 93 |
$content = self::stripHeader( $action['content'] ); |
| 94 |
$content = self::stripSignature( $content ); |
| 95 |
$userLinks = self::getUserLinks( $content, $title ); |
| 96 |
$events = array_merge( |
| 97 |
$events, |
| 98 |
self::collectMentionEvents( $action['header'], $userLinks, $content, $revision, $user ) |
| 99 |
); |
| 100 |
} elseif ( $action['type'] === 'unknown-signed-change' ) { |
| 101 |
$userLinks = array_diff_key( |
| 102 |
self::getUserLinks( $action['new_content'], $title ), |
| 103 |
self::getUserLinks( $action['old_content'], $title ) |
| 104 |
); |
| 105 |
$header = self::extractHeader( $action['full-section'] ); |
| 106 |
|
| 107 |
if ( $wgEchoMentionOnChanges ) { |
| 108 |
$events = array_merge( |
| 109 |
$events, |
| 110 |
self::collectMentionEvents( $header, $userLinks, $action['new_content'], $revision, $user ) |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if ( $title->getNamespace() === NS_USER_TALK ) { |
| 117 |
$notifyUser = User::newFromName( $title->getText() ); |
| 118 |
|
| 119 |
if ( $notifyUser && $notifyUser->getId() ) { |
| 120 |
$permManager = MediaWikiServices::getInstance()->getPermissionManager(); |
| 121 |
|
| 122 |
|
| 123 |
if ( !$revision->isMinor() || !$permManager->userHasRight( $user, 'nominornewtalk' ) ) { |
| 124 |
$section = self::detectSectionTitleAndText( $interpretation, $title ); |
| 125 |
if ( $section['section-text'] === '' ) { |
| 126 |
$comment = $revision->getComment( RevisionRecord::FOR_PUBLIC, $notifyUser ); |
| 127 |
if ( $comment ) { |
| 128 |
$section['section-text'] = $comment->text; |
| 129 |
} |
| 130 |
} |
| 131 |
$events[] = [ |
| 132 |
'type' => 'edit-user-talk', |
| 133 |
'title' => $title, |
| 134 |
'extra' => [ |
| 135 |
'revid' => $revision->getId(), |
| 136 |
'minoredit' => $revision->isMinor(), |
| 137 |
'section-title' => $section['section-title'], |
| 138 |
'section-text' => $section['section-text'], |
| 139 |
'target-page' => $title->getArticleID(), |
| 140 |
], |
| 141 |
'agent' => $user, |
| 142 |
]; |
| 143 |
} |
| 144 |
} |
| 145 |
} elseif ( $title->inNamespace( NS_USER ) ) { |
| 146 |
$notifyUser = User::newFromName( $title->getText() ); |
| 147 |
|
| 148 |
|
| 149 |
if ( $notifyUser && $notifyUser->getId() ) { |
| 150 |
$events[] = [ |
| 151 |
'type' => 'edit-user-page', |
| 152 |
'title' => $title, |
| 153 |
'extra' => [ |
| 154 |
'revid' => $revision->getId(), |
| 155 |
], |
| 156 |
'agent' => $user, |
| 157 |
]; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
global $wgEchoMaxMentionsInEditSummary; |
| 163 |
|
| 164 |
if ( $wgEchoMaxMentionsInEditSummary > 0 && !$user->isBot() && !$isRevert ) { |
| 165 |
$summaryParser = new SummaryParser(); |
| 166 |
$usersInSummary = $summaryParser->parse( $revision->getComment()->text ); |
| 167 |
|
| 168 |
|
| 169 |
unset( $usersInSummary[$userName] ); |
| 170 |
|
| 171 |
$count = 0; |
| 172 |
$mentionedUsers = []; |
| 173 |
foreach ( $usersInSummary as $summaryUser ) { |
| 174 |
if ( $summaryUser->getTalkPage()->equals( $title ) ) { |
| 175 |
|
| 176 |
continue; |
| 177 |
} |
| 178 |
if ( $count >= $wgEchoMaxMentionsInEditSummary ) { |
| 179 |
break; |
| 180 |
} |
| 181 |
$mentionedUsers[$summaryUser->getId()] = $summaryUser->getId(); |
| 182 |
$count++; |
| 183 |
} |
| 184 |
|
| 185 |
if ( $mentionedUsers ) { |
| 186 |
$events[] = [ |
| 187 |
'type' => 'mention-summary', |
| 188 |
'title' => $title, |
| 189 |
'extra' => [ |
| 190 |
'revid' => $revision->getId(), |
| 191 |
'mentioned-users' => $mentionedUsers, |
| 192 |
], |
| 193 |
'agent' => $user, |
| 194 |
]; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
( new HookRunner( $services->getHookContainer() ) ) |
| 201 |
->onEchoGetEventsForRevision( $events, $revision, $isRevert ); |
| 202 |
|
| 203 |
|
| 204 |
foreach ( $events as $event ) { |
| 205 |
Event::create( $event ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static function detectSectionTitleAndText( array $interpretation, ?Title $title = null ) { |
| 217 |
$header = $snippet = ''; |
| 218 |
$found = false; |
| 219 |
|
| 220 |
foreach ( $interpretation as $action ) { |
| 221 |
switch ( $action['type'] ) { |
| 222 |
case 'add-comment': |
| 223 |
$header = self::extractHeader( $action['full-section'] ); |
| 224 |
$snippet = self::getTextSnippet( |
| 225 |
self::stripSignature( self::stripHeader( $action['content'] ), $title ), |
| 226 |
RequestContext::getMain()->getLanguage(), |
| 227 |
self::DEFAULT_SNIPPET_LENGTH, |
| 228 |
$title ); |
| 229 |
break; |
| 230 |
case 'new-section-with-comment': |
| 231 |
$header = self::extractHeader( $action['content'] ); |
| 232 |
$snippet = self::getTextSnippet( |
| 233 |
self::stripSignature( self::stripHeader( $action['content'] ), $title ), |
| 234 |
RequestContext::getMain()->getLanguage(), |
| 235 |
self::DEFAULT_SNIPPET_LENGTH, |
| 236 |
$title ); |
| 237 |
break; |
| 238 |
} |
| 239 |
if ( $header ) { |
| 240 |
|
| 241 |
|
| 242 |
if ( $found ) { |
| 243 |
$found = false; |
| 244 |
break; |
| 245 |
} |
| 246 |
$found = true; |
| 247 |
} |
| 248 |
} |
| 249 |
if ( !$found ) { |
| 250 |
return [ 'section-title' => '', 'section-text' => '' ]; |
| 251 |
} |
| 252 |
|
| 253 |
return [ 'section-title' => $header, 'section-text' => $snippet ]; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public static function generateMentionEvents( |
| 266 |
$header, |
| 267 |
array $userLinks, |
| 268 |
$content, |
| 269 |
RevisionRecord $revision, |
| 270 |
User $agent |
| 271 |
) { |
| 272 |
$events = self::collectMentionEvents( $header, $userLinks, $content, $revision, $agent ); |
| 273 |
foreach ( $events as $event ) { |
| 274 |
Event::create( $event ); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
protected static function collectMentionEvents( |
| 288 |
$header, |
| 289 |
array $userLinks, |
| 290 |
$content, |
| 291 |
RevisionRecord $revision, |
| 292 |
User $agent |
| 293 |
) { |
| 294 |
global $wgEchoMaxMentionsCount, $wgEchoMentionStatusNotifications; |
| 295 |
|
| 296 |
$title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() ); |
| 297 |
if ( !$title ) { |
| 298 |
return []; |
| 299 |
} |
| 300 |
$content = self::stripHeader( $content ); |
| 301 |
$content = self::stripSignature( $content, $title ); |
| 302 |
|
| 303 |
if ( !$userLinks ) { |
| 304 |
return []; |
| 305 |
} |
| 306 |
|
| 307 |
$userMentions = self::getUserMentions( |
| 308 |
$title, $revision->getUser( RevisionRecord::RAW )->getId(), $userLinks |
| 309 |
); |
| 310 |
$overallMentionsCount = self::getOverallUserMentionsCount( $userMentions ); |
| 311 |
if ( $overallMentionsCount === 0 ) { |
| 312 |
return []; |
| 313 |
} |
| 314 |
|
| 315 |
$events = []; |
| 316 |
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); |
| 317 |
|
| 318 |
if ( $overallMentionsCount > $wgEchoMaxMentionsCount ) { |
| 319 |
if ( $wgEchoMentionStatusNotifications ) { |
| 320 |
$events[] = [ |
| 321 |
'type' => 'mention-failure-too-many', |
| 322 |
'title' => $title, |
| 323 |
'extra' => [ |
| 324 |
'max-mentions' => $wgEchoMaxMentionsCount, |
| 325 |
'section-title' => $header, |
| 326 |
], |
| 327 |
'agent' => $agent, |
| 328 |
]; |
| 329 |
$stats->increment( 'echo.event.mention.notification.failure-too-many' ); |
| 330 |
} |
| 331 |
return $events; |
| 332 |
} |
| 333 |
|
| 334 |
if ( $userMentions['validMentions'] ) { |
| 335 |
$events[] = [ |
| 336 |
'type' => 'mention', |
| 337 |
'title' => $title, |
| 338 |
'extra' => [ |
| 339 |
'content' => $content, |
| 340 |
'section-title' => $header, |
| 341 |
'revid' => $revision->getId(), |
| 342 |
'mentioned-users' => $userMentions['validMentions'], |
| 343 |
], |
| 344 |
'agent' => $agent, |
| 345 |
]; |
| 346 |
} |
| 347 |
|
| 348 |
if ( $wgEchoMentionStatusNotifications ) { |
| 349 |
|
| 350 |
foreach ( $userMentions['validMentions'] as $mentionedUserId ) { |
| 351 |
$events[] = [ |
| 352 |
'type' => 'mention-success', |
| 353 |
'title' => $title, |
| 354 |
'extra' => [ |
| 355 |
'subject-name' => User::newFromId( $mentionedUserId )->getName(), |
| 356 |
'section-title' => $header, |
| 357 |
'revid' => $revision->getId(), |
| 358 |
], |
| 359 |
'agent' => $agent, |
| 360 |
]; |
| 361 |
$stats->increment( 'echo.event.mention.notification.success' ); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
foreach ( $userMentions['anonymousUsers'] as $anonymousUser ) { |
| 366 |
$events[] = [ |
| 367 |
'type' => 'mention-failure', |
| 368 |
'title' => $title, |
| 369 |
'extra' => [ |
| 370 |
'failure-type' => 'user-anonymous', |
| 371 |
'subject-name' => $anonymousUser, |
| 372 |
'section-title' => $header, |
| 373 |
'revid' => $revision->getId(), |
| 374 |
], |
| 375 |
'agent' => $agent, |
| 376 |
]; |
| 377 |
$stats->increment( 'echo.event.mention.notification.failure-user-anonymous' ); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
foreach ( $userMentions['unknownUsers'] as $unknownUser ) { |
| 382 |
$events[] = [ |
| 383 |
'type' => 'mention-failure', |
| 384 |
'title' => $title, |
| 385 |
'extra' => [ |
| 386 |
'failure-type' => 'user-unknown', |
| 387 |
'subject-name' => $unknownUser, |
| 388 |
'section-title' => $header, |
| 389 |
'revid' => $revision->getId(), |
| 390 |
], |
| 391 |
'agent' => $agent, |
| 392 |
]; |
| 393 |
$stats->increment( 'echo.event.mention.notification.failure-user-unknown' ); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
return $events; |
| 398 |
} |
| 399 |
|
| 400 |
private static function getOverallUserMentionsCount( array $userMentions ) { |
| 401 |
return count( $userMentions, COUNT_RECURSIVE ) - count( $userMentions ); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public static function getUserMentions( Title $title, $revisionUserId, array $userLinks ) { |
| 415 |
global $wgEchoMaxMentionsCount; |
| 416 |
|
| 417 |
$userMentions = [ |
| 418 |
'validMentions' => [], |
| 419 |
'unknownUsers' => [], |
| 420 |
'anonymousUsers' => [], |
| 421 |
]; |
| 422 |
|
| 423 |
$count = 0; |
| 424 |
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory(); |
| 425 |
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils(); |
| 426 |
|
| 427 |
foreach ( $userLinks as $dbk => $page_id ) { |
| 428 |
|
| 429 |
|
| 430 |
if ( $count > $wgEchoMaxMentionsCount ) { |
| 431 |
$stats->increment( 'echo.event.mention.error.tooMany' ); |
| 432 |
break; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
if ( self::hasSubpage( $dbk ) ) { |
| 438 |
continue; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
if ( $userNameUtils->isIP( $dbk ) ) { |
| 443 |
$userMentions['anonymousUsers'][] = $dbk; |
| 444 |
$count++; |
| 445 |
$stats->increment( 'echo.event.mention.error.anonUser' ); |
| 446 |
continue; |
| 447 |
} |
| 448 |
|
| 449 |
$user = User::newFromName( $dbk ); |
| 450 |
|
| 451 |
if ( !$user ) { |
| 452 |
$userMentions['unknownUsers'][] = str_replace( '_', ' ', $dbk ); |
| 453 |
$count++; |
| 454 |
$stats->increment( 'echo.event.mention.error.invalidUser' ); |
| 455 |
continue; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
if ( $user->getId() === $revisionUserId ) { |
| 460 |
$stats->increment( 'echo.event.mention.error.sameUser' ); |
| 461 |
continue; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
if ( $title->getNamespace() === NS_USER_TALK && $title->getDBkey() === $dbk ) { |
| 466 |
$stats->increment( 'echo.event.mention.error.ownPage' ); |
| 467 |
continue; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
if ( $user->getId() === 0 ) { |
| 472 |
$userMentions['unknownUsers'][] = str_replace( '_', ' ', $dbk ); |
| 473 |
$count++; |
| 474 |
$stats->increment( 'echo.event.mention.error.unknownUser' ); |
| 475 |
continue; |
| 476 |
} |
| 477 |
|
| 478 |
$userMentions['validMentions'][$user->getId()] = $user->getId(); |
| 479 |
$count++; |
| 480 |
} |
| 481 |
|
| 482 |
return $userMentions; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
public static function getUserLinks( $content, Title $title ) { |
| 492 |
$output = self::parseNonEditWikitext( $content, new Article( $title ) ); |
| 493 |
$links = $output->getLinks(); |
| 494 |
|
| 495 |
if ( !isset( $links[NS_USER] ) || !is_array( $links[NS_USER] ) ) { |
| 496 |
return []; |
| 497 |
} |
| 498 |
|
| 499 |
return $links[NS_USER]; |
| 500 |
} |
| 501 |
|
| 502 |
private static function hasSubpage( $dbk ) { |
| 503 |
return strpos( $dbk, '/' ) !== false; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public static function parseNonEditWikitext( $wikitext, Article $article ) { |
| 517 |
static $cache = []; |
| 518 |
|
| 519 |
$cacheKey = md5( $wikitext ) . ':' . $article->getTitle()->getPrefixedText(); |
| 520 |
|
| 521 |
if ( isset( $cache[$cacheKey] ) ) { |
| 522 |
return $cache[$cacheKey]; |
| 523 |
} |
| 524 |
|
| 525 |
$parser = MediaWikiServices::getInstance()->getParser(); |
| 526 |
|
| 527 |
$options = new ParserOptions( $article->getContext()->getUser() ); |
| 528 |
$output = $parser->parse( $wikitext, $article->getTitle(), $options ); |
| 529 |
$cache[$cacheKey] = $output; |
| 530 |
|
| 531 |
return $output; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
private static function getChangeInterpretationForRevision( RevisionRecord $revision ) { |
| 543 |
if ( $revision->getId() ) { |
| 544 |
$page = $revision->getPage(); |
| 545 |
$cacheKey = $revision->getId() . '|' . $page->getNamespace() . '|' . $page->getDBkey(); |
| 546 |
if ( isset( self::$revisionInterpretationCache[$cacheKey] ) ) { |
| 547 |
return self::$revisionInterpretationCache[$cacheKey]; |
| 548 |
} |
| 549 |
} else { |
| 550 |
$cacheKey = null; |
| 551 |
} |
| 552 |
|
| 553 |
$userIdentity = $revision->getUser(); |
| 554 |
|
| 555 |
$prevText = ''; |
| 556 |
if ( $revision->getParentId() ) { |
| 557 |
$store = MediaWikiServices::getInstance()->getRevisionStore(); |
| 558 |
$prevRevision = $store->getRevisionById( $revision->getParentId() ); |
| 559 |
if ( $prevRevision ) { |
| 560 |
$prevContent = $prevRevision->getContent( SlotRecord::MAIN ); |
| 561 |
$prevText = ( $prevContent instanceof TextContent ) ? $prevContent->getText() : ''; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
$content = $revision->getContent( SlotRecord::MAIN ); |
| 566 |
$changes = self::getMachineReadableDiff( |
| 567 |
$prevText, |
| 568 |
( $content instanceof TextContent ) ? $content->getText() : '' |
| 569 |
); |
| 570 |
$output = self::interpretDiff( |
| 571 |
$changes, |
| 572 |
$userIdentity ? $userIdentity->getName() : '', |
| 573 |
Title::newFromLinkTarget( $revision->getPageAsLinkTarget() ) |
| 574 |
); |
| 575 |
|
| 576 |
if ( $cacheKey ) { |
| 577 |
self::$revisionInterpretationCache[$cacheKey] = $output; |
| 578 |
} |
| 579 |
|
| 580 |
return $output; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public static function interpretDiff( array $changes, $username, ?Title $title = null ) { |
| 618 |
|
| 619 |
$actions = []; |
| 620 |
$signedSections = []; |
| 621 |
|
| 622 |
foreach ( $changes as $index => $change ) { |
| 623 |
if ( !is_numeric( $index ) ) { |
| 624 |
continue; |
| 625 |
} |
| 626 |
|
| 627 |
if ( !$change['action'] ) { |
| 628 |
|
| 629 |
continue; |
| 630 |
} |
| 631 |
|
| 632 |
if ( $change['action'] === 'add' ) { |
| 633 |
$content = trim( $change['content'] ); |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
$startSection = preg_match( '/\A' . self::HEADER_REGEX . '/um', $content ); |
| 638 |
$sectionCount = self::getSectionCount( $content ); |
| 639 |
$signedUsers = self::extractSignatures( $content, $title ); |
| 640 |
|
| 641 |
if ( |
| 642 |
count( $signedUsers ) === 1 && |
| 643 |
isset( $signedUsers[$username] ) |
| 644 |
) { |
| 645 |
if ( $sectionCount === 0 ) { |
| 646 |
$signedSections[] = self::getSectionSpan( $change['right-pos'], $changes['_info']['rhs'] ); |
| 647 |
$fullSection = self::getFullSection( $changes['_info']['rhs'], $change['right-pos'] ); |
| 648 |
$actions[] = [ |
| 649 |
'type' => 'add-comment', |
| 650 |
'content' => $content, |
| 651 |
'full-section' => $fullSection, |
| 652 |
]; |
| 653 |
} elseif ( $startSection && $sectionCount === 1 ) { |
| 654 |
$signedSections[] = self::getSectionSpan( $change['right-pos'], $changes['_info']['rhs'] ); |
| 655 |
$actions[] = [ |
| 656 |
'type' => 'new-section-with-comment', |
| 657 |
'content' => $content, |
| 658 |
]; |
| 659 |
} else { |
| 660 |
$nextSectionStart = $change['right-pos']; |
| 661 |
$sectionData = self::extractSections( $content ); |
| 662 |
foreach ( $sectionData as $section ) { |
| 663 |
$sectionSpan = self::getSectionSpan( $nextSectionStart, $changes['_info']['rhs'] ); |
| 664 |
$nextSectionStart = $sectionSpan[1] + 1; |
| 665 |
$sectionSignedUsers = self::extractSignatures( $section['content'], $title ); |
| 666 |
if ( $sectionSignedUsers ) { |
| 667 |
$signedSections[] = $sectionSpan; |
| 668 |
if ( !$section['header'] ) { |
| 669 |
$fullSection = self::getFullSection( |
| 670 |
$changes['_info']['rhs'], |
| 671 |
$change['right-pos'] |
| 672 |
); |
| 673 |
$section['header'] = self::extractHeader( $fullSection ); |
| 674 |
} |
| 675 |
$actions[] = [ |
| 676 |
'type' => 'add-section-multiple', |
| 677 |
'content' => $section['content'], |
| 678 |
'header' => $section['header'], |
| 679 |
]; |
| 680 |
} else { |
| 681 |
$actions[] = [ |
| 682 |
'type' => 'unknown-unsigned-addition', |
| 683 |
'content' => $section['content'], |
| 684 |
]; |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
} elseif ( $signedUsers !== [] ) { |
| 689 |
$actions[] = [ |
| 690 |
'type' => 'unknown-multi-signed-addition', |
| 691 |
'content' => $content, |
| 692 |
]; |
| 693 |
} else { |
| 694 |
$actions[] = [ |
| 695 |
'type' => 'unknown-unsigned-addition', |
| 696 |
'content' => $content, |
| 697 |
]; |
| 698 |
} |
| 699 |
} elseif ( $change['action'] === 'subtract' ) { |
| 700 |
$actions[] = [ |
| 701 |
'type' => 'unknown-subtraction', |
| 702 |
'content' => $change['content'], |
| 703 |
]; |
| 704 |
} elseif ( $change['action'] === 'change' ) { |
| 705 |
$actions[] = [ |
| 706 |
'type' => 'unknown-change', |
| 707 |
'old_content' => $change['old_content'], |
| 708 |
'new_content' => $change['new_content'], |
| 709 |
'right-pos' => $change['right-pos'], |
| 710 |
'full-section' => self::getFullSection( $changes['_info']['rhs'], $change['right-pos'] ), |
| 711 |
]; |
| 712 |
|
| 713 |
if ( self::hasNewSignature( |
| 714 |
$change['old_content'], |
| 715 |
$change['new_content'], |
| 716 |
$username, |
| 717 |
$title |
| 718 |
) ) { |
| 719 |
$signedSections[] = self::getSectionSpan( $change['right-pos'], $changes['_info']['rhs'] ); |
| 720 |
} |
| 721 |
} else { |
| 722 |
$actions[] = [ |
| 723 |
'type' => 'unknown', |
| 724 |
'details' => $change, |
| 725 |
]; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
if ( $signedSections ) { |
| 730 |
$actions = self::convertToUnknownSignedChanges( $signedSections, $actions ); |
| 731 |
} |
| 732 |
|
| 733 |
return $actions; |
| 734 |
} |
| 735 |
|
| 736 |
private static function hasNewSignature( $oldContent, $newContent, $username, $title ) { |
| 737 |
$oldSignedUsers = self::extractSignatures( $oldContent, $title ); |
| 738 |
$newSignedUsers = self::extractSignatures( $newContent, $title ); |
| 739 |
|
| 740 |
return !isset( $oldSignedUsers[$username] ) && isset( $newSignedUsers[$username] ); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
private static function convertToUnknownSignedChanges( array $signedSections, array $actions ) { |
| 751 |
return array_map( function ( $action ) use( $signedSections ) { |
| 752 |
if ( |
| 753 |
$action['type'] === 'unknown-change' && |
| 754 |
self::isInSignedSection( $action['right-pos'], $signedSections ) |
| 755 |
) { |
| 756 |
$signedUsers = self::extractSignatures( $action['new_content'], null ); |
| 757 |
if ( count( $signedUsers ) === 1 ) { |
| 758 |
$action['type'] = 'unknown-signed-change'; |
| 759 |
} else { |
| 760 |
$action['type'] = 'unknown-multi-signed-change'; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
return $action; |
| 765 |
}, $actions ); |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
private static function isInSignedSection( $line, array $signedSections ) { |
| 774 |
foreach ( $signedSections as $section ) { |
| 775 |
if ( $line > $section[0] && $line <= $section[1] ) { |
| 776 |
return true; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
return false; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
public static function getFullSection( array $lines, $offset ) { |
| 791 |
$start = self::getSectionStartIndex( $offset, $lines ); |
| 792 |
$end = self::getSectionEndIndex( $offset, $lines ); |
| 793 |
$content = implode( "\n", array_slice( $lines, $start, $end - $start ) ); |
| 794 |
|
| 795 |
return trim( $content, "\n" ); |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
private static function getSectionSpan( $offset, array $lines ) { |
| 806 |
return [ |
| 807 |
self::getSectionStartIndex( $offset, $lines ), |
| 808 |
self::getSectionEndIndex( $offset, $lines ) |
| 809 |
]; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
private static function getSectionStartIndex( $offset, array $lines ) { |
| 819 |
for ( $i = $offset - 1; $i >= 0; $i-- ) { |
| 820 |
if ( self::getSectionCount( $lines[$i] ) ) { |
| 821 |
break; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
return $i; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
private static function getSectionEndIndex( $offset, array $lines ) { |
| 835 |
$lastLine = count( $lines ); |
| 836 |
for ( $i = $offset; $i < $lastLine; $i++ ) { |
| 837 |
if ( self::getSectionCount( $lines[$i] ) ) { |
| 838 |
break; |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
return $i; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
public static function getSectionCount( $text ) { |
| 852 |
$text = trim( $text ); |
| 853 |
|
| 854 |
return (int)preg_match_all( '/' . self::HEADER_REGEX . '/um', $text ); |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
public static function extractHeader( $text ) { |
| 864 |
$text = trim( $text ); |
| 865 |
|
| 866 |
$matches = []; |
| 867 |
|
| 868 |
if ( !preg_match_all( '/' . self::HEADER_REGEX . '/um', $text, $matches ) ) { |
| 869 |
return false; |
| 870 |
} |
| 871 |
|
| 872 |
return trim( end( $matches[2] ) ); |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
private static function extractSections( $text ) { |
| 885 |
$matches = []; |
| 886 |
|
| 887 |
if ( !preg_match_all( '/' . self::HEADER_REGEX . '/um', $text, $matches, PREG_OFFSET_CAPTURE ) ) { |
| 888 |
return [ [ |
| 889 |
'header' => false, |
| 890 |
'content' => $text |
| 891 |
] ]; |
| 892 |
} |
| 893 |
|
| 894 |
$sectionNum = count( $matches[0] ); |
| 895 |
$sections = []; |
| 896 |
|
| 897 |
|
| 898 |
if ( $matches[0][0][1] > 1 ) { |
| 899 |
$sections[] = [ |
| 900 |
'header' => false, |
| 901 |
'content' => substr( $text, 0, $matches[0][0][1] - 1 ) |
| 902 |
]; |
| 903 |
} |
| 904 |
for ( $i = 0; $i < $sectionNum; $i++ ) { |
| 905 |
if ( $i + 1 < $sectionNum ) { |
| 906 |
$content = substr( $text, $matches[0][$i][1], $matches[0][$i + 1][1] - $matches[0][$i][1] ); |
| 907 |
} else { |
| 908 |
$content = substr( $text, $matches[0][$i][1] ); |
| 909 |
} |
| 910 |
$sections[] = [ |
| 911 |
'header' => self::extractHeader( $matches[0][$i][0] ), |
| 912 |
'content' => trim( $content ) |
| 913 |
]; |
| 914 |
} |
| 915 |
|
| 916 |
return $sections; |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
private static function stripSignature( $text, ?Title $title = null ) { |
| 927 |
$output = self::getUserFromLine( $text, $title ); |
| 928 |
if ( $output === false ) { |
| 929 |
$timestampPos = self::getTimestampPosition( $text ); |
| 930 |
|
| 931 |
return substr( $text, 0, $timestampPos ); |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
return MediaWikiServices::getInstance()->getContentLanguage() |
| 940 |
->truncateForDatabase( $text, $output[0], '' ); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
private static function stripHeader( $text ) { |
| 949 |
return preg_replace( '/' . self::HEADER_REGEX . '/um', '', $text ); |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
public static function isSignedComment( $text, $user = false, ?Title $title = null ) { |
| 962 |
$userData = self::getUserFromLine( $text, $title ); |
| 963 |
|
| 964 |
if ( $userData === false ) { |
| 965 |
return false; |
| 966 |
} elseif ( $user === false ) { |
| 967 |
return true; |
| 968 |
} |
| 969 |
|
| 970 |
[ , $foundUser ] = $userData; |
| 971 |
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils(); |
| 972 |
|
| 973 |
return $userNameUtils->getCanonical( $foundUser, UserNameUtils::RIGOR_NONE ) === |
| 974 |
$userNameUtils->getCanonical( $user, UserNameUtils::RIGOR_NONE ); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
public static function getTimestampPosition( $line ) { |
| 984 |
$timestampRegex = self::getTimestampRegex(); |
| 985 |
$tsMatches = []; |
| 986 |
if ( !preg_match( |
| 987 |
"/$timestampRegex/mu", |
| 988 |
$line, |
| 989 |
$tsMatches, |
| 990 |
PREG_OFFSET_CAPTURE |
| 991 |
) ) { |
| 992 |
return false; |
| 993 |
} |
| 994 |
|
| 995 |
return $tsMatches[0][1]; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
public static function getMachineReadableDiff( $oldText, $newText ) { |
| 1015 |
if ( self::$diffParser === null ) { |
| 1016 |
self::$diffParser = new DiffParser; |
| 1017 |
} |
| 1018 |
|
| 1019 |
return self::$diffParser->getChangeSet( $oldText, $newText ); |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
private static function extractSignatures( $text, ?Title $title = null ) { |
| 1031 |
$lines = explode( "\n", $text ); |
| 1032 |
|
| 1033 |
$output = []; |
| 1034 |
|
| 1035 |
$lineNumber = 0; |
| 1036 |
|
| 1037 |
foreach ( $lines as $line ) { |
| 1038 |
++$lineNumber; |
| 1039 |
|
| 1040 |
|
| 1041 |
$userData = self::getUserFromLine( $line, $title ); |
| 1042 |
if ( $userData === false ) { |
| 1043 |
continue; |
| 1044 |
} |
| 1045 |
|
| 1046 |
[ $signaturePos, $user ] = $userData; |
| 1047 |
|
| 1048 |
$signature = substr( $line, $signaturePos ); |
| 1049 |
$output[$user] = $signature; |
| 1050 |
} |
| 1051 |
|
| 1052 |
return $output; |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
public static function extractUsersFromLine( $line ) { |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
if ( !preg_match_all( '/\[\[([^\[]+)\]\]/', $line, $matches ) ) { |
| 1070 |
return []; |
| 1071 |
} |
| 1072 |
|
| 1073 |
$matches = $matches[1]; |
| 1074 |
|
| 1075 |
$usernames = []; |
| 1076 |
|
| 1077 |
foreach ( $matches as $match ) { |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
$match = explode( '|', $match, 2 ); |
| 1087 |
$title = Title::newFromText( $match[0] ); |
| 1088 |
|
| 1089 |
|
| 1090 |
if ( |
| 1091 |
$title |
| 1092 |
&& ( $title->getNamespace() === NS_USER || $title->getNamespace() === NS_USER_TALK ) |
| 1093 |
&& !$title->isSubpage() |
| 1094 |
) { |
| 1095 |
$usernames[] = $title->getText(); |
| 1096 |
} elseif ( $title && $title->isSpecial( 'Contributions' ) ) { |
| 1097 |
$parts = explode( '/', $title->getText(), 2 ); |
| 1098 |
$usernames[] = end( $parts ); |
| 1099 |
} else { |
| 1100 |
|
| 1101 |
continue; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
return $usernames; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
public static function getUserFromLine( $line, ?Title $title = null ) { |
| 1119 |
$parser = MediaWikiServices::getInstance()->getParser(); |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
$usernames = self::extractUsersFromLine( $line ); |
| 1128 |
$usernames = array_reverse( $usernames ); |
| 1129 |
foreach ( $usernames as $username ) { |
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
$user = User::newFromName( $username, false ); |
| 1134 |
$sig = $parser->preSaveTransform( |
| 1135 |
'~~~', |
| 1136 |
$title ?: Title::newMainPage(), |
| 1137 |
$user, |
| 1138 |
new ParserOptions( $user ) |
| 1139 |
); |
| 1140 |
|
| 1141 |
|
| 1142 |
$pos = strrpos( $line, $sig ); |
| 1143 |
if ( $pos !== false ) { |
| 1144 |
return [ $pos, $username ]; |
| 1145 |
} |
| 1146 |
|
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
return false; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
private static function getLinkFromLine( $line, $linkPrefix, $failureOffset = false ) { |
| 1164 |
$offset = 0; |
| 1165 |
|
| 1166 |
|
| 1167 |
if ( $failureOffset !== false ) { |
| 1168 |
$offset = $failureOffset - strlen( $line ) - 1; |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
if ( abs( $offset ) > strlen( $line ) ) { |
| 1173 |
return false; |
| 1174 |
} |
| 1175 |
|
| 1176 |
$linkPos = strripos( $line, $linkPrefix, $offset ); |
| 1177 |
|
| 1178 |
if ( $linkPos === false ) { |
| 1179 |
return false; |
| 1180 |
} |
| 1181 |
|
| 1182 |
$linkUser = self::extractUserFromLink( $line, $linkPrefix, $linkPos ); |
| 1183 |
|
| 1184 |
if ( $linkUser === false ) { |
| 1185 |
|
| 1186 |
return self::getLinkFromLine( $line, $linkPrefix, $linkPos ); |
| 1187 |
} else { |
| 1188 |
return [ $linkPos, $linkUser ]; |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
private static function extractUserFromLink( $text, $prefix, $offset = 0 ) { |
| 1201 |
$userPart = substr( $text, strlen( $prefix ) + $offset ); |
| 1202 |
|
| 1203 |
$userMatches = []; |
| 1204 |
if ( !preg_match( |
| 1205 |
'/^[^\|\]\#]+/u', |
| 1206 |
$userPart, |
| 1207 |
$userMatches |
| 1208 |
) ) { |
| 1209 |
|
| 1210 |
return false; |
| 1211 |
} |
| 1212 |
|
| 1213 |
$user = $userMatches[0]; |
| 1214 |
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils(); |
| 1215 |
if ( |
| 1216 |
!$userNameUtils->isIP( $user ) && |
| 1217 |
$userNameUtils->getCanonical( $user ) === false |
| 1218 |
) { |
| 1219 |
|
| 1220 |
return false; |
| 1221 |
} |
| 1222 |
|
| 1223 |
return $userNameUtils->getCanonical( $userMatches[0], UserNameUtils::RIGOR_NONE ); |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
public static function getTimestampRegex() { |
| 1233 |
if ( self::$timestampRegex !== null ) { |
| 1234 |
return self::$timestampRegex; |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
$title = Title::newMainPage(); |
| 1239 |
$user = User::newFromName( 'Test' ); |
| 1240 |
$options = new ParserOptions( $user ); |
| 1241 |
|
| 1242 |
$parser = MediaWikiServices::getInstance()->getParser(); |
| 1243 |
$exemplarTimestamp = |
| 1244 |
$parser->preSaveTransform( '~~~~~', $title, $user, $options ); |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
$output = $exemplarTimestamp; |
| 1249 |
$tzRegex = '/\h*\(\w+\)\h*$/u'; |
| 1250 |
$tzMatches = []; |
| 1251 |
if ( preg_match( $tzRegex, $output, $tzMatches, PREG_OFFSET_CAPTURE ) ) { |
| 1252 |
$output = substr( $output, 0, $tzMatches[0][1] ); |
| 1253 |
} |
| 1254 |
$output = preg_quote( $output, '/' ); |
| 1255 |
$output = preg_replace( '/[^\d\W]+/u', '[^\d\W]+', $output ); |
| 1256 |
$output = preg_replace( '/\d+/u', '\d+', $output ); |
| 1257 |
|
| 1258 |
if ( $tzMatches ) { |
| 1259 |
$output .= preg_quote( $tzMatches[0][0] ); |
| 1260 |
} |
| 1261 |
|
| 1262 |
if ( !preg_match( "/$output/u", $exemplarTimestamp ) ) { |
| 1263 |
throw new RuntimeException( "Timestamp regex does not match exemplar" ); |
| 1264 |
} |
| 1265 |
|
| 1266 |
self::$timestampRegex = $output; |
| 1267 |
|
| 1268 |
return $output; |
| 1269 |
} |
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
public static function getTextSnippet( |
| 1281 |
$text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH, $title = null, $linestart = true |
| 1282 |
) { |
| 1283 |
|
| 1284 |
$html = MediaWikiServices::getInstance()->getMessageCache()->parse( $text, $title, $linestart )->getText( [ |
| 1285 |
'enableSectionEditLinks' => false |
| 1286 |
] ); |
| 1287 |
$plaintext = trim( Sanitizer::stripAllTags( $html ) ); |
| 1288 |
return $lang->truncateForVisual( $plaintext, $length ); |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
public static function getTextSnippetFromSummary( $text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH ) { |
| 1299 |
|
| 1300 |
$html = MediaWikiServices::getInstance()->getCommentFormatter() |
| 1301 |
->formatLinks( Sanitizer::escapeHtmlAllowEntities( $text ) ); |
| 1302 |
$plaintext = trim( Sanitizer::stripAllTags( $html ) ); |
| 1303 |
return $lang->truncateForVisual( $plaintext, $length ); |
| 1304 |
} |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
public static function getEditExcerpt( |
| 1315 |
RevisionRecord $revision, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH |
| 1316 |
) { |
| 1317 |
$interpretation = self::getChangeInterpretationForRevision( $revision ); |
| 1318 |
$section = self::detectSectionTitleAndText( $interpretation ); |
| 1319 |
return $lang->truncateForVisual( $section['section-title'] . ' ' . $section['section-text'], $length ); |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
class_alias( DiscussionParser::class, 'EchoDiscussionParser' ); |