| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use MediaWiki\Content\Content; |
| 6 |
use MediaWiki\Content\TextContent; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner; |
| 8 |
use MediaWiki\Permissions\Authority; |
| 9 |
use MediaWiki\Revision\RevisionRecord; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class TextExtractor { |
| 18 |
public const SERVICE_NAME = 'AbuseFilterTextExtractor'; |
| 19 |
|
| 20 |
|
| 21 |
private $hookRunner; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( AbuseFilterHookRunner $hookRunner ) { |
| 27 |
$this->hookRunner = $hookRunner; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function revisionToString( ?RevisionRecord $revision, Authority $performer ): string { |
| 51 |
if ( !$revision ) { |
| 52 |
return ''; |
| 53 |
} |
| 54 |
|
| 55 |
$strings = []; |
| 56 |
|
| 57 |
foreach ( $revision->getSlotRoles() as $role ) { |
| 58 |
$content = $revision->getContent( $role, RevisionRecord::FOR_THIS_USER, $performer ); |
| 59 |
if ( $content === null ) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
$strings[$role] = $this->contentToString( $content ); |
| 63 |
} |
| 64 |
|
| 65 |
return implode( "\n\n", $strings ); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function contentToString( Content $content ): string { |
| 82 |
$text = null; |
| 83 |
|
| 84 |
if ( $this->hookRunner->onAbuseFilter_contentToString( |
| 85 |
$content, |
| 86 |
$text |
| 87 |
) ) { |
| 88 |
$text = $content instanceof TextContent |
| 89 |
? $content->getText() |
| 90 |
: $content->getTextForSearchIndex(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
return TextContent::normalizeLineEndings( (string)$text ); |
| 95 |
} |
| 96 |
} |