2009-02-27 00:03:30 +00:00
|
|
|
<?php
|
|
|
|
|
2020-06-03 00:43:22 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Hooks\AbuseFilterHookRunner;
|
2018-08-29 08:57:56 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2020-01-21 07:38:52 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-01-24 17:26:03 +00:00
|
|
|
use Wikimedia\IPUtils;
|
2020-01-21 07:38:52 +00:00
|
|
|
use Wikimedia\Rdbms\Database;
|
2018-02-15 21:55:51 +00:00
|
|
|
|
2009-02-27 00:03:30 +00:00
|
|
|
class AFComputedVariable {
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
|
|
|
* @var string The method used to compute the variable
|
|
|
|
*/
|
|
|
|
public $mMethod;
|
|
|
|
/**
|
|
|
|
* @var array Parameters to be used with the specified method
|
|
|
|
*/
|
|
|
|
public $mParameters;
|
|
|
|
/**
|
2019-08-26 13:01:09 +00:00
|
|
|
* @var User[] Cache containing User objects already constructed
|
2018-11-08 14:34:32 +00:00
|
|
|
*/
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $userCache = [];
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
2019-08-26 13:01:09 +00:00
|
|
|
* @var WikiPage[] Cache containing Page objects already constructed
|
2018-11-08 14:34:32 +00:00
|
|
|
*/
|
2017-06-15 14:23:34 +00:00
|
|
|
public static $articleCache = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2019-03-25 19:19:17 +00:00
|
|
|
/** @var float The amount of time to subtract from profiling */
|
|
|
|
public static $profilingExtraTime = 0;
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string $method
|
|
|
|
* @param array $parameters
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function __construct( $method, $parameters ) {
|
2009-02-27 00:03:30 +00:00
|
|
|
$this->mMethod = $method;
|
|
|
|
$this->mParameters = $parameters;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
2016-09-28 20:07:52 +00:00
|
|
|
* It's like Article::prepareContentForEdit, but not for editing (old wikitext usually)
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
|
|
|
*
|
2013-10-15 13:22:05 +00:00
|
|
|
* @param string $wikitext
|
2018-10-17 05:15:21 +00:00
|
|
|
* @param WikiPage $article
|
2020-03-04 18:09:00 +00:00
|
|
|
* @param User $user Context user
|
2011-08-24 22:11:52 +00:00
|
|
|
*
|
2020-10-29 00:44:48 +00:00
|
|
|
* @return stdClass
|
2011-08-24 22:11:52 +00:00
|
|
|
*/
|
2020-03-04 18:09:00 +00:00
|
|
|
public function parseNonEditWikitext( $wikitext, WikiPage $article, User $user ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
static $cache = [];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
$cacheKey = md5( $wikitext ) . ':' . $article->getTitle()->getPrefixedText();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-05-22 06:42:10 +00:00
|
|
|
if ( isset( $cache[$cacheKey] ) ) {
|
2009-02-27 00:03:30 +00:00
|
|
|
return $cache[$cacheKey];
|
2009-05-22 06:42:10 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$edit = (object)[];
|
2020-03-04 18:09:00 +00:00
|
|
|
$options = ParserOptions::newFromUser( $user );
|
2018-10-21 09:42:48 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
|
|
|
$edit->output = $parser->parse( $wikitext, $article->getTitle(), $options );
|
2009-02-27 00:03:30 +00:00
|
|
|
$cache[$cacheKey] = $edit;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 00:03:30 +00:00
|
|
|
return $edit;
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2013-01-07 00:02:41 +00:00
|
|
|
* For backwards compatibility: Get the user object belonging to a certain name
|
|
|
|
* in case a user name is given as argument. Nowadays user objects are passed
|
|
|
|
* directly but many old log entries rely on this.
|
|
|
|
*
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param string|User $user
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return User
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function getUserObject( $user ) {
|
2013-01-07 00:02:41 +00:00
|
|
|
if ( $user instanceof User ) {
|
|
|
|
$username = $user->getName();
|
|
|
|
} else {
|
|
|
|
$username = $user;
|
|
|
|
if ( isset( self::$userCache[$username] ) ) {
|
|
|
|
return self::$userCache[$username];
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
|
|
|
$logger->debug( "Couldn't find user $username in cache" );
|
2013-01-07 00:02:41 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-05-26 13:08:15 +00:00
|
|
|
if ( count( self::$userCache ) > 1000 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
self::$userCache = [];
|
2009-05-26 13:08:15 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
if ( $user instanceof User ) {
|
2018-11-19 14:58:53 +00:00
|
|
|
$ret = $user;
|
2020-01-24 17:26:03 +00:00
|
|
|
} elseif ( IPUtils::isIPAddress( $username ) ) {
|
2018-11-19 14:58:53 +00:00
|
|
|
$ret = new User;
|
|
|
|
$ret->setName( $username );
|
|
|
|
} else {
|
|
|
|
$ret = User::newFromName( $username );
|
|
|
|
$ret->load();
|
2009-02-28 01:10:45 +00:00
|
|
|
}
|
2018-11-19 14:58:53 +00:00
|
|
|
self::$userCache[$username] = $ret;
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-11-19 14:58:53 +00:00
|
|
|
return $ret;
|
2009-02-27 00:03:30 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param int $namespace
|
2018-10-03 12:02:00 +00:00
|
|
|
* @param string $title
|
2018-10-17 05:15:21 +00:00
|
|
|
* @return WikiPage
|
2012-03-11 20:40:04 +00:00
|
|
|
*/
|
2019-08-26 13:01:09 +00:00
|
|
|
public function pageFromTitle( $namespace, $title ) {
|
2009-05-22 06:42:10 +00:00
|
|
|
if ( isset( self::$articleCache["$namespace:$title"] ) ) {
|
2009-02-27 00:03:30 +00:00
|
|
|
return self::$articleCache["$namespace:$title"];
|
2009-05-22 06:42:10 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-05-26 13:08:15 +00:00
|
|
|
if ( count( self::$articleCache ) > 1000 ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
self::$articleCache = [];
|
2009-05-26 13:08:15 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
2018-10-17 05:15:21 +00:00
|
|
|
$logger->debug( "Creating wikipage object for $namespace:$title in cache" );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
$t = $this->buildTitle( $namespace, $title );
|
2018-10-17 05:15:21 +00:00
|
|
|
self::$articleCache["$namespace:$title"] = WikiPage::factory( $t );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 00:03:30 +00:00
|
|
|
return self::$articleCache["$namespace:$title"];
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2019-08-26 13:01:09 +00:00
|
|
|
/**
|
|
|
|
* Mockable wrapper
|
|
|
|
*
|
|
|
|
* @param int $namespace
|
|
|
|
* @param string $title
|
|
|
|
* @return Title
|
|
|
|
*/
|
|
|
|
protected function buildTitle( $namespace, $title ) : Title {
|
|
|
|
return Title::makeTitle( $namespace, $title );
|
|
|
|
}
|
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2018-10-17 05:15:21 +00:00
|
|
|
* @param WikiPage $article
|
2012-03-11 20:40:04 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-10-17 05:15:21 +00:00
|
|
|
public static function getLinksFromDB( WikiPage $article ) {
|
2018-10-03 12:02:00 +00:00
|
|
|
// Stolen from ConfirmEdit, SimpleCaptcha::getLinksFromTracker
|
2009-03-19 02:05:58 +00:00
|
|
|
$id = $article->getId();
|
2009-05-22 06:42:10 +00:00
|
|
|
if ( !$id ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
return [];
|
2009-05-22 06:42:10 +00:00
|
|
|
}
|
2010-02-13 14:10:36 +00:00
|
|
|
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2019-09-27 16:55:10 +00:00
|
|
|
return $dbr->selectFieldValues(
|
2010-08-19 21:12:09 +00:00
|
|
|
'externallinks',
|
2019-09-27 16:55:10 +00:00
|
|
|
'el_to',
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'el_from' => $id ],
|
2010-08-19 21:12:09 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2009-03-19 02:05:58 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2012-03-11 20:40:04 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param AbuseFilterVariableHolder $vars
|
2018-11-08 14:34:32 +00:00
|
|
|
* @return AFPData
|
2012-03-11 20:40:04 +00:00
|
|
|
* @throws MWException
|
|
|
|
* @throws AFPException
|
|
|
|
*/
|
2018-10-17 05:15:21 +00:00
|
|
|
public function compute( AbuseFilterVariableHolder $vars ) {
|
2020-10-11 21:35:13 +00:00
|
|
|
// phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
|
2018-11-12 16:04:11 +00:00
|
|
|
global $wgUser;
|
|
|
|
|
2020-09-13 22:47:57 +00:00
|
|
|
// Used for parsing wikitext from saved revisions and checking for
|
|
|
|
// whether to show fields. Do not use $wgUser below here, in preparation
|
|
|
|
// for eventually injecting. See T246733
|
|
|
|
$computeForUser = $wgUser;
|
|
|
|
|
2019-08-11 13:11:20 +00:00
|
|
|
$vars->setLogger( LoggerFactory::getInstance( 'AbuseFilter' ) );
|
2009-02-27 00:03:30 +00:00
|
|
|
$parameters = $this->mParameters;
|
|
|
|
$result = null;
|
2012-11-16 17:06:34 +00:00
|
|
|
|
2020-06-03 00:43:22 +00:00
|
|
|
$hookRunner = AbuseFilterHookRunner::getRunner();
|
|
|
|
|
|
|
|
if ( !$hookRunner->onAbuseFilterInterceptVariable(
|
|
|
|
$this->mMethod,
|
|
|
|
$vars,
|
|
|
|
$parameters,
|
|
|
|
$result
|
|
|
|
) ) {
|
2012-11-16 17:06:34 +00:00
|
|
|
return $result instanceof AFPData
|
|
|
|
? $result : AFPData::newFromPHPVar( $result );
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:46:24 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2015-09-28 18:03:35 +00:00
|
|
|
switch ( $this->mMethod ) {
|
2009-02-27 00:03:30 +00:00
|
|
|
case 'diff':
|
2018-04-08 15:38:39 +00:00
|
|
|
// Currently unused. Kept for backwards compatibility since it remains
|
|
|
|
// as mMethod for old variables. A fallthrough would instead change old results.
|
2009-02-27 00:03:30 +00:00
|
|
|
$text1Var = $parameters['oldtext-var'];
|
|
|
|
$text2Var = $parameters['newtext-var'];
|
2017-02-24 14:22:56 +00:00
|
|
|
$text1 = $vars->getVar( $text1Var )->toString();
|
|
|
|
$text2 = $vars->getVar( $text2Var )->toString();
|
|
|
|
$diffs = new Diff( explode( "\n", $text1 ), explode( "\n", $text2 ) );
|
|
|
|
$format = new UnifiedDiffFormatter();
|
|
|
|
$result = $format->format( $diffs );
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
2018-04-08 15:38:39 +00:00
|
|
|
case 'diff-array':
|
|
|
|
// Introduced with T74329 to uniform the diff to MW's standard one.
|
|
|
|
// The difference with 'diff' method is noticeable when one of the
|
|
|
|
// $text is empty: it'll be treated as **really** empty, instead of
|
|
|
|
// an empty string.
|
|
|
|
$text1Var = $parameters['oldtext-var'];
|
|
|
|
$text2Var = $parameters['newtext-var'];
|
|
|
|
$text1 = $vars->getVar( $text1Var )->toString();
|
|
|
|
$text2 = $vars->getVar( $text2Var )->toString();
|
|
|
|
$text1 = $text1 === '' ? [] : explode( "\n", $text1 );
|
|
|
|
$text2 = $text2 === '' ? [] : explode( "\n", $text2 );
|
|
|
|
$diffs = new Diff( $text1, $text2 );
|
|
|
|
$format = new UnifiedDiffFormatter();
|
|
|
|
$result = $format->format( $diffs );
|
|
|
|
break;
|
2009-02-27 00:03:30 +00:00
|
|
|
case 'diff-split':
|
|
|
|
$diff = $vars->getVar( $parameters['diff-var'] )->toString();
|
|
|
|
$line_prefix = $parameters['line-prefix'];
|
|
|
|
$diff_lines = explode( "\n", $diff );
|
2020-01-12 16:42:29 +00:00
|
|
|
$result = [];
|
2010-02-13 14:10:36 +00:00
|
|
|
foreach ( $diff_lines as $line ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( substr( $line, 0, 1 ) === $line_prefix ) {
|
2020-01-12 16:42:29 +00:00
|
|
|
$result[] = substr( $line, strlen( $line_prefix ) );
|
2009-02-27 00:03:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'links-from-wikitext':
|
2009-05-26 13:08:15 +00:00
|
|
|
// This should ONLY be used when sharing a parse operation with the edit.
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-22 16:02:21 +00:00
|
|
|
/* @var WikiPage $article */
|
2015-03-21 02:53:13 +00:00
|
|
|
if ( isset( $parameters['article'] ) ) {
|
|
|
|
$article = $parameters['article'];
|
|
|
|
} else {
|
2019-08-26 13:01:09 +00:00
|
|
|
$article = $this->pageFromTitle(
|
2015-03-21 02:53:13 +00:00
|
|
|
$parameters['namespace'],
|
|
|
|
$parameters['title']
|
|
|
|
);
|
|
|
|
}
|
2015-11-23 10:02:53 +00:00
|
|
|
if ( $article->getContentModel() === CONTENT_MODEL_WIKITEXT ) {
|
2019-03-25 19:19:17 +00:00
|
|
|
// Shared with the edit, don't count it in profiling
|
|
|
|
$startTime = microtime( true );
|
2009-05-26 13:08:15 +00:00
|
|
|
$textVar = $parameters['text-var'];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-05-26 13:08:15 +00:00
|
|
|
$new_text = $vars->getVar( $textVar )->toString();
|
2013-04-01 09:05:08 +00:00
|
|
|
$content = ContentHandler::makeContent( $new_text, $article->getTitle() );
|
2019-04-11 07:33:16 +00:00
|
|
|
try {
|
|
|
|
// @fixme TEMPORARY WORKAROUND FOR T187153
|
|
|
|
$editInfo = $article->prepareContentForEdit( $content );
|
|
|
|
$links = array_keys( $editInfo->output->getExternalLinks() );
|
2019-08-31 18:41:00 +00:00
|
|
|
} catch ( Error $e ) {
|
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
|
|
|
$logger->warning( 'Caught Error, case 1 - T187153' );
|
2019-04-11 07:33:16 +00:00
|
|
|
$links = [];
|
|
|
|
}
|
2009-05-26 13:08:15 +00:00
|
|
|
$result = $links;
|
2019-03-25 19:19:17 +00:00
|
|
|
self::$profilingExtraTime += ( microtime( true ) - $startTime );
|
2011-11-09 08:36:26 +00:00
|
|
|
break;
|
2009-05-26 13:08:15 +00:00
|
|
|
}
|
2011-11-09 08:36:26 +00:00
|
|
|
// Otherwise fall back to database
|
2009-03-19 03:10:18 +00:00
|
|
|
case 'links-from-wikitext-nonedit':
|
2009-03-19 02:05:58 +00:00
|
|
|
case 'links-from-wikitext-or-database':
|
2018-10-17 05:15:21 +00:00
|
|
|
// TODO: use Content object instead, if available!
|
2019-08-26 13:01:09 +00:00
|
|
|
$article = $this->pageFromTitle(
|
2010-08-19 21:12:09 +00:00
|
|
|
$parameters['namespace'],
|
|
|
|
$parameters['title']
|
|
|
|
);
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
2019-01-05 17:30:37 +00:00
|
|
|
if ( $vars->forFilter ) {
|
2009-03-19 02:05:58 +00:00
|
|
|
$links = $this->getLinksFromDB( $article );
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger->debug( 'Loading old links from DB' );
|
2015-11-23 10:02:53 +00:00
|
|
|
} elseif ( $article->getContentModel() === CONTENT_MODEL_WIKITEXT ) {
|
2018-08-29 08:57:56 +00:00
|
|
|
$logger->debug( 'Loading old links from Parser' );
|
2009-03-19 02:05:58 +00:00
|
|
|
$textVar = $parameters['text-var'];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-03-19 02:05:58 +00:00
|
|
|
$wikitext = $vars->getVar( $textVar )->toString();
|
2020-03-04 18:09:00 +00:00
|
|
|
$editInfo = $this->parseNonEditWikitext(
|
|
|
|
$wikitext,
|
|
|
|
$article,
|
2020-09-13 22:47:57 +00:00
|
|
|
$computeForUser
|
2020-03-04 18:09:00 +00:00
|
|
|
);
|
2009-03-19 02:05:58 +00:00
|
|
|
$links = array_keys( $editInfo->output->getExternalLinks() );
|
2013-01-22 16:02:21 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Get links from Content object. But we don't have the content object.
|
2017-07-08 18:49:13 +00:00
|
|
|
// And for non-text content, $wikitext is usually not going to be a valid
|
|
|
|
// serialization, but rather some dummy text for filtering.
|
2017-06-15 14:23:34 +00:00
|
|
|
$links = [];
|
2009-03-19 02:05:58 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-04-05 19:07:47 +00:00
|
|
|
$result = $links;
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'link-diff-added':
|
|
|
|
case 'link-diff-removed':
|
|
|
|
$oldLinkVar = $parameters['oldlink-var'];
|
|
|
|
$newLinkVar = $parameters['newlink-var'];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 00:03:30 +00:00
|
|
|
$oldLinks = $vars->getVar( $oldLinkVar )->toString();
|
|
|
|
$newLinks = $vars->getVar( $newLinkVar )->toString();
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 00:03:30 +00:00
|
|
|
$oldLinks = explode( "\n", $oldLinks );
|
|
|
|
$newLinks = explode( "\n", $newLinks );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $this->mMethod === 'link-diff-added' ) {
|
2009-05-26 13:08:15 +00:00
|
|
|
$result = array_diff( $newLinks, $oldLinks );
|
|
|
|
}
|
2018-08-26 08:34:42 +00:00
|
|
|
if ( $this->mMethod === 'link-diff-removed' ) {
|
2009-05-26 13:08:15 +00:00
|
|
|
$result = array_diff( $oldLinks, $newLinks );
|
|
|
|
}
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'parse-wikitext':
|
2009-05-26 13:08:15 +00:00
|
|
|
// Should ONLY be used when sharing a parse operation with the edit.
|
2015-03-21 02:53:13 +00:00
|
|
|
if ( isset( $parameters['article'] ) ) {
|
|
|
|
$article = $parameters['article'];
|
|
|
|
} else {
|
2019-08-26 13:01:09 +00:00
|
|
|
$article = $this->pageFromTitle(
|
2015-03-21 02:53:13 +00:00
|
|
|
$parameters['namespace'],
|
|
|
|
$parameters['title']
|
|
|
|
);
|
|
|
|
}
|
2015-11-23 10:02:53 +00:00
|
|
|
if ( $article->getContentModel() === CONTENT_MODEL_WIKITEXT ) {
|
2019-03-25 19:19:17 +00:00
|
|
|
// Shared with the edit, don't count it in profiling
|
|
|
|
$startTime = microtime( true );
|
2009-05-26 13:08:15 +00:00
|
|
|
$textVar = $parameters['wikitext-var'];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-05-26 13:08:15 +00:00
|
|
|
$new_text = $vars->getVar( $textVar )->toString();
|
2016-09-28 20:07:52 +00:00
|
|
|
$content = ContentHandler::makeContent( $new_text, $article->getTitle() );
|
2019-05-16 13:49:17 +00:00
|
|
|
try {
|
|
|
|
// @fixme TEMPORARY WORKAROUND FOR T187153
|
|
|
|
$editInfo = $article->prepareContentForEdit( $content );
|
2019-08-31 18:41:00 +00:00
|
|
|
} catch ( Error $e ) {
|
|
|
|
$logger = LoggerFactory::getInstance( 'AbuseFilter' );
|
|
|
|
$logger->warning( 'Caught Error, case 2 - T187153' );
|
2019-05-16 13:49:17 +00:00
|
|
|
$result = '';
|
|
|
|
break;
|
|
|
|
}
|
2013-04-24 14:53:12 +00:00
|
|
|
if ( isset( $parameters['pst'] ) && $parameters['pst'] ) {
|
|
|
|
$result = $editInfo->pstContent->serialize( $editInfo->format );
|
|
|
|
} else {
|
|
|
|
$newHTML = $editInfo->output->getText();
|
|
|
|
// Kill the PP limit comments. Ideally we'd just remove these by not setting the
|
|
|
|
// parser option, but then we can't share a parse operation with the edit, which is bad.
|
2020-01-12 16:42:29 +00:00
|
|
|
// @fixme No awfulness scale can measure how awful this hack is.
|
|
|
|
$re = '/<!--\s*NewPP limit [^>]*-->\s*(?:<!--\s*Transclusion [^>]+-->\s*)?(?:<\/div>\s*)?$/i';
|
|
|
|
$result = preg_replace( $re, '', $newHTML );
|
2013-04-24 14:53:12 +00:00
|
|
|
}
|
2019-03-25 19:19:17 +00:00
|
|
|
self::$profilingExtraTime += ( microtime( true ) - $startTime );
|
2011-11-09 08:36:26 +00:00
|
|
|
break;
|
2009-05-26 13:08:15 +00:00
|
|
|
}
|
2011-11-09 08:36:26 +00:00
|
|
|
// Otherwise fall back to database
|
2009-02-27 00:03:30 +00:00
|
|
|
case 'parse-wikitext-nonedit':
|
2018-10-17 05:15:21 +00:00
|
|
|
// TODO: use Content object instead, if available!
|
2019-08-26 13:01:09 +00:00
|
|
|
$article = $this->pageFromTitle( $parameters['namespace'], $parameters['title'] );
|
2009-02-27 00:03:30 +00:00
|
|
|
$textVar = $parameters['wikitext-var'];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2015-11-23 10:02:53 +00:00
|
|
|
if ( $article->getContentModel() === CONTENT_MODEL_WIKITEXT ) {
|
2013-04-24 14:53:12 +00:00
|
|
|
if ( isset( $parameters['pst'] ) && $parameters['pst'] ) {
|
|
|
|
// $textVar is already PSTed when it's not loaded from an ongoing edit.
|
|
|
|
$result = $vars->getVar( $textVar )->toString();
|
|
|
|
} else {
|
|
|
|
$text = $vars->getVar( $textVar )->toString();
|
2020-03-04 18:09:00 +00:00
|
|
|
$editInfo = $this->parseNonEditWikitext(
|
|
|
|
$text,
|
|
|
|
$article,
|
2020-09-13 22:47:57 +00:00
|
|
|
$computeForUser
|
2020-03-04 18:09:00 +00:00
|
|
|
);
|
2013-04-24 14:53:12 +00:00
|
|
|
$result = $editInfo->output->getText();
|
|
|
|
}
|
2013-01-22 16:02:21 +00:00
|
|
|
} else {
|
|
|
|
// TODO: Parser Output from Content object. But we don't have the content object.
|
2017-07-08 18:49:13 +00:00
|
|
|
// And for non-text content, $wikitext is usually not going to be a valid
|
|
|
|
// serialization, but rather some dummy text for filtering.
|
2013-01-22 16:02:21 +00:00
|
|
|
$result = '';
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'strip-html':
|
|
|
|
$htmlVar = $parameters['html-var'];
|
|
|
|
$html = $vars->getVar( $htmlVar )->toString();
|
2020-01-12 16:42:29 +00:00
|
|
|
$stripped = StringUtils::delimiterReplace( '<', '>', '', $html );
|
|
|
|
// We strip extra spaces to the right because the stripping above
|
|
|
|
// could leave a lot of whitespace.
|
|
|
|
// @fixme Find a better way to do this.
|
|
|
|
$result = TextContent::normalizeLineEndings( $stripped );
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'load-recent-authors':
|
2019-08-26 13:01:09 +00:00
|
|
|
$title = $this->buildTitle( $parameters['namespace'], $parameters['title'] );
|
2009-10-07 13:57:06 +00:00
|
|
|
if ( !$title->exists() ) {
|
2009-03-16 08:21:24 +00:00
|
|
|
$result = '';
|
|
|
|
break;
|
|
|
|
}
|
2015-12-22 20:37:35 +00:00
|
|
|
|
2016-01-27 01:37:58 +00:00
|
|
|
$result = self::getLastPageAuthors( $title );
|
2014-07-09 16:58:07 +00:00
|
|
|
break;
|
|
|
|
case 'load-first-author':
|
2019-08-26 13:01:09 +00:00
|
|
|
$title = $this->buildTitle( $parameters['namespace'], $parameters['title'] );
|
2014-07-09 16:58:07 +00:00
|
|
|
|
2020-04-14 21:54:02 +00:00
|
|
|
$revision = $services->getRevisionLookup()->getFirstRevision( $title );
|
2014-07-09 16:58:07 +00:00
|
|
|
if ( $revision ) {
|
2020-04-14 21:54:02 +00:00
|
|
|
$user = $revision->getUser();
|
|
|
|
$result = $user === null ? '' : $user->getName();
|
2014-07-09 16:58:07 +00:00
|
|
|
} else {
|
|
|
|
$result = '';
|
|
|
|
}
|
|
|
|
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'get-page-restrictions':
|
|
|
|
$action = $parameters['action'];
|
2019-08-26 13:01:09 +00:00
|
|
|
$title = $this->buildTitle( $parameters['namespace'], $parameters['title'] );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-10-03 14:38:41 +00:00
|
|
|
$result = $title->getRestrictions( $action );
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'simple-user-accessor':
|
|
|
|
$user = $parameters['user'];
|
|
|
|
$method = $parameters['method'];
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( !$user ) {
|
|
|
|
throw new MWException( 'No user parameter given.' );
|
2009-02-27 13:16:34 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2013-01-07 00:02:41 +00:00
|
|
|
$obj = self::getUserObject( $user );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
|
|
|
if ( !$obj ) {
|
2009-02-27 13:16:34 +00:00
|
|
|
throw new MWException( "Invalid username $user" );
|
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2017-06-15 14:23:34 +00:00
|
|
|
$result = call_user_func( [ $obj, $method ] );
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
2019-05-02 19:42:15 +00:00
|
|
|
case 'user-block':
|
|
|
|
// @todo Support partial blocks
|
|
|
|
$user = $parameters['user'];
|
|
|
|
$result = (bool)$user->getBlock();
|
|
|
|
break;
|
2009-02-27 00:03:30 +00:00
|
|
|
case 'user-age':
|
|
|
|
$user = $parameters['user'];
|
|
|
|
$asOf = $parameters['asof'];
|
2013-01-07 00:02:41 +00:00
|
|
|
$obj = self::getUserObject( $user );
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2019-12-07 17:20:10 +00:00
|
|
|
$registration = $obj->getRegistration();
|
|
|
|
|
2020-01-22 23:21:39 +00:00
|
|
|
if ( $obj->getId() === 0 ) {
|
2009-03-18 00:26:28 +00:00
|
|
|
$result = 0;
|
2019-12-07 17:20:10 +00:00
|
|
|
} else {
|
2020-01-22 23:21:39 +00:00
|
|
|
// HACK: If there's no registration date, assume 2008-01-15, Wikipedia Day
|
|
|
|
// in the year before the new user log was created. See T243469.
|
|
|
|
if ( $registration === null ) {
|
|
|
|
$registration = "20080115000000";
|
|
|
|
}
|
2019-12-07 17:20:10 +00:00
|
|
|
$result = (int)wfTimestamp( TS_UNIX, $asOf ) - (int)wfTimestamp( TS_UNIX, $registration );
|
2009-03-18 00:26:28 +00:00
|
|
|
}
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
2015-04-01 00:41:09 +00:00
|
|
|
case 'page-age':
|
2019-08-26 13:01:09 +00:00
|
|
|
$title = $this->buildTitle( $parameters['namespace'], $parameters['title'] );
|
2015-04-01 00:41:09 +00:00
|
|
|
|
|
|
|
$firstRevisionTime = $title->getEarliestRevTime();
|
|
|
|
if ( !$firstRevisionTime ) {
|
|
|
|
$result = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$asOf = $parameters['asof'];
|
2019-12-07 17:20:10 +00:00
|
|
|
$result = (int)wfTimestamp( TS_UNIX, $asOf ) - (int)wfTimestamp( TS_UNIX, $firstRevisionTime );
|
2015-04-01 00:41:09 +00:00
|
|
|
break;
|
2009-02-27 00:03:30 +00:00
|
|
|
case 'user-groups':
|
2013-01-07 00:02:41 +00:00
|
|
|
// Deprecated but needed by old log entries
|
2009-02-27 00:03:30 +00:00
|
|
|
$user = $parameters['user'];
|
2013-01-07 00:02:41 +00:00
|
|
|
$obj = self::getUserObject( $user );
|
2009-04-05 19:07:47 +00:00
|
|
|
$result = $obj->getEffectiveGroups();
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'length':
|
|
|
|
$s = $vars->getVar( $parameters['length-var'] )->toString();
|
|
|
|
$result = strlen( $s );
|
|
|
|
break;
|
2018-04-07 15:15:30 +00:00
|
|
|
case 'subtract':
|
|
|
|
// Currently unused, kept for backwards compatibility for old filters.
|
|
|
|
$v1 = $vars->getVar( $parameters['val1-var'] )->toFloat();
|
|
|
|
$v2 = $vars->getVar( $parameters['val2-var'] )->toFloat();
|
|
|
|
$result = $v1 - $v2;
|
|
|
|
break;
|
2018-03-25 17:19:10 +00:00
|
|
|
case 'subtract-int':
|
|
|
|
$v1 = $vars->getVar( $parameters['val1-var'] )->toInt();
|
|
|
|
$v2 = $vars->getVar( $parameters['val2-var'] )->toInt();
|
2009-02-27 00:03:30 +00:00
|
|
|
$result = $v1 - $v2;
|
|
|
|
break;
|
|
|
|
case 'revision-text-by-id':
|
2020-01-08 16:46:24 +00:00
|
|
|
$revRec = $services
|
|
|
|
->getRevisionLookup()
|
|
|
|
->getRevisionById( $parameters['revid'] );
|
2020-09-13 22:47:57 +00:00
|
|
|
$result = AbuseFilter::revisionToString( $revRec, $computeForUser );
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
|
|
|
case 'revision-text-by-timestamp':
|
|
|
|
$timestamp = $parameters['timestamp'];
|
2020-08-19 10:33:48 +00:00
|
|
|
if ( $timestamp === null ) {
|
|
|
|
// Temporary BC for T246539#6388362
|
|
|
|
$result = '[Revision text not available]';
|
|
|
|
break;
|
|
|
|
}
|
2019-08-26 13:01:09 +00:00
|
|
|
$title = $this->buildTitle( $parameters['namespace'], $parameters['title'] );
|
2020-01-08 16:46:24 +00:00
|
|
|
$revRec = $services
|
|
|
|
->getRevisionStore()
|
|
|
|
->getRevisionByTimestamp( $title, $timestamp );
|
2020-09-13 22:47:57 +00:00
|
|
|
$result = AbuseFilter::revisionToString( $revRec, $computeForUser );
|
2009-02-27 00:03:30 +00:00
|
|
|
break;
|
2019-12-17 15:56:36 +00:00
|
|
|
case 'get-wiki-name':
|
|
|
|
$result = WikiMap::getCurrentWikiDbDomain()->getId();
|
|
|
|
break;
|
|
|
|
case 'get-wiki-language':
|
2020-01-08 16:46:24 +00:00
|
|
|
$result = $services->getContentLanguage()->getCode();
|
2019-12-17 15:56:36 +00:00
|
|
|
break;
|
2009-02-27 00:03:30 +00:00
|
|
|
default:
|
2020-06-03 00:43:22 +00:00
|
|
|
if ( $hookRunner->onAbuseFilterComputeVariable(
|
|
|
|
$this->mMethod,
|
|
|
|
$vars,
|
|
|
|
$parameters,
|
|
|
|
$result
|
|
|
|
) ) {
|
2009-10-07 13:57:06 +00:00
|
|
|
throw new AFPException( 'Unknown variable compute type ' . $this->mMethod );
|
2009-03-25 05:18:27 +00:00
|
|
|
}
|
2009-02-27 00:03:30 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2009-05-26 13:08:15 +00:00
|
|
|
return $result instanceof AFPData
|
|
|
|
? $result : AFPData::newFromPHPVar( $result );
|
2009-02-27 00:03:30 +00:00
|
|
|
}
|
2016-01-27 01:37:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Title $title
|
2018-04-16 15:37:10 +00:00
|
|
|
* @return string[] Usernames of the last 10 (unique) authors from $title
|
2016-01-27 01:37:58 +00:00
|
|
|
*/
|
|
|
|
public static function getLastPageAuthors( Title $title ) {
|
|
|
|
if ( !$title->exists() ) {
|
2017-06-15 14:23:34 +00:00
|
|
|
return [];
|
2016-01-27 01:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 21:55:51 +00:00
|
|
|
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
|
2018-09-30 12:06:04 +00:00
|
|
|
$fname = __METHOD__;
|
2016-01-27 01:37:58 +00:00
|
|
|
|
|
|
|
return $cache->getWithSetCallback(
|
|
|
|
$cache->makeKey( 'last-10-authors', 'revision', $title->getLatestRevID() ),
|
|
|
|
$cache::TTL_MINUTE,
|
2018-09-30 12:06:04 +00:00
|
|
|
function ( $oldValue, &$ttl, array &$setOpts ) use ( $title, $fname ) {
|
2017-08-30 02:51:39 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2016-01-27 01:37:58 +00:00
|
|
|
$setOpts += Database::getCacheSetOptions( $dbr );
|
|
|
|
// Get the last 100 edit authors with a trivial query (avoid T116557)
|
2020-01-08 16:46:24 +00:00
|
|
|
$revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo();
|
2016-01-27 01:37:58 +00:00
|
|
|
$revAuthors = $dbr->selectFieldValues(
|
2018-03-09 21:23:38 +00:00
|
|
|
$revQuery['tables'],
|
|
|
|
$revQuery['fields']['rev_user_text'],
|
2017-06-15 14:23:34 +00:00
|
|
|
[ 'rev_page' => $title->getArticleID() ],
|
2018-09-30 12:06:04 +00:00
|
|
|
$fname,
|
2016-01-27 01:37:58 +00:00
|
|
|
// Some pages have < 10 authors but many revisions (e.g. bot pages)
|
2018-11-09 21:58:40 +00:00
|
|
|
[ 'ORDER BY' => 'rev_timestamp DESC, rev_id DESC',
|
2017-04-24 19:03:12 +00:00
|
|
|
'LIMIT' => 100,
|
|
|
|
// Force index per T116557
|
2018-03-09 21:23:38 +00:00
|
|
|
'USE INDEX' => [ 'revision' => 'page_timestamp' ],
|
|
|
|
],
|
|
|
|
$revQuery['joins']
|
2016-01-27 01:37:58 +00:00
|
|
|
);
|
|
|
|
// Get the last 10 distinct authors within this set of edits
|
2017-06-15 14:23:34 +00:00
|
|
|
$users = [];
|
2016-01-27 01:37:58 +00:00
|
|
|
foreach ( $revAuthors as $author ) {
|
|
|
|
$users[$author] = 1;
|
|
|
|
if ( count( $users ) >= 10 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_keys( $users );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2009-03-25 05:18:27 +00:00
|
|
|
}
|