Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1425841
TextExtractor.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
TextExtractor.php
View Options
<?php
namespace
MediaWiki\Extension\AbuseFilter
;
use
MediaWiki\Content\Content
;
use
MediaWiki\Content\TextContent
;
use
MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner
;
use
MediaWiki\Permissions\Authority
;
use
MediaWiki\Revision\RevisionRecord
;
/**
* This service provides an interface to convert RevisionRecord and Content objects to some text
* suitable for running abuse filters.
*
* @internal No external code should rely on this representation
*/
class
TextExtractor
{
public
const
SERVICE_NAME
=
'AbuseFilterTextExtractor'
;
/** @var AbuseFilterHookRunner */
private
$hookRunner
;
/**
* @param AbuseFilterHookRunner $hookRunner
*/
public
function
__construct
(
AbuseFilterHookRunner
$hookRunner
)
{
$this
->
hookRunner
=
$hookRunner
;
}
/**
* Look up some text of a revision from its revision id
*
* Note that this is really *some* text, we do not make *any* guarantee
* that this text will be even close to what the user actually sees, or
* that the form is fit for any intended purpose.
*
* Note also that if the revision for any reason is not an Revision
* the function returns with an empty string.
*
* For now, this returns all the revision's slots, concatenated together.
* In future, this will be replaced by a better solution. See T208769 for
* discussion.
*
* @param RevisionRecord|null $revision a valid revision
* @param Authority $performer to check for privileged access
* @return string the content of the revision as some kind of string,
* or an empty string if it can not be found
* @return-taint none
*/
public
function
revisionToString
(
?
RevisionRecord
$revision
,
Authority
$performer
):
string
{
if
(
!
$revision
)
{
return
''
;
}
$strings
=
[];
foreach
(
$revision
->
getSlotRoles
()
as
$role
)
{
$content
=
$revision
->
getContent
(
$role
,
RevisionRecord
::
FOR_THIS_USER
,
$performer
);
if
(
$content
===
null
)
{
continue
;
}
$strings
[
$role
]
=
$this
->
contentToString
(
$content
);
}
return
implode
(
"
\n\n
"
,
$strings
);
}
/**
* Converts the given Content object to a string.
*
* This uses TextContent::getText() if $content is an instance of TextContent,
* or Content::getTextForSearchIndex() otherwise.
*
* The hook AbuseFilterContentToString can be used to override this
* behavior.
*
* @param Content $content
*
* @return string a suitable string representation of the content.
*/
public
function
contentToString
(
Content
$content
):
string
{
$text
=
null
;
if
(
$this
->
hookRunner
->
onAbuseFilter_contentToString
(
$content
,
$text
)
)
{
$text
=
$content
instanceof
TextContent
?
$content
->
getText
()
:
$content
->
getTextForSearchIndex
();
}
// T22310
return
TextContent
::
normalizeLineEndings
(
(
string
)
$text
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 12:05 (20 h, 37 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
73/8e/7c8eced99fbb6e43e39af598a9fd
Default Alt Text
TextExtractor.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment