Skip to content

Commit

Permalink
RottenLinksJob: use service injection (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal-Omega authored Nov 18, 2024
1 parent 88e8693 commit cb1d737
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
9 changes: 8 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@
"Miraheze\\RottenLinks\\": "includes/"
},
"JobClasses": {
"RottenLinksJob": "Miraheze\\RottenLinks\\Jobs\\RottenLinksJob"
"RottenLinksJob": {
"class": "Miraheze\\RottenLinks\\Jobs\\RottenLinksJob",
"services": [
"ConfigFactory",
"ConnectionProvider"
],
"needsPage": false
}
},
"SpecialPages": {
"RottenLinks": {
Expand Down
21 changes: 14 additions & 7 deletions includes/HookHandlers/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Miraheze\RottenLinks\HookHandlers;

use JobSpecification;
use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
use MediaWiki\Hook\LinksUpdateCompleteHook;
use MediaWiki\Hook\ParserFirstCallInitHook;
Expand All @@ -11,7 +12,10 @@
use Miraheze\RottenLinks\RottenLinksParserFunctions;
use Wikimedia\Rdbms\IConnectionProvider;

class Main implements LinksUpdateCompleteHook, ParserFirstCallInitHook {
class Main implements
LinksUpdateCompleteHook,
ParserFirstCallInitHook
{

private JobQueueGroupFactory $jobQueueGroupFactory;
private RottenLinksParserFunctions $parserFunctions;
Expand All @@ -35,13 +39,16 @@ public function onLinksUpdateComplete( $linksUpdate, $ticket ) {
$removedExternalLinks = $linksUpdate->getRemovedExternalLinks();

if ( $addedExternalLinks || $removedExternalLinks ) {
$params = [
'addedExternalLinks' => $addedExternalLinks ?? [],
'removedExternalLinks' => $removedExternalLinks ?? [],
];

$jobQueueGroup = $this->jobQueueGroupFactory->makeJobQueueGroup();
$jobQueueGroup->push( new RottenLinksJob( $params ) );
$jobQueueGroup->push(
new JobSpecification(
RottenLinksJob::JOB_NAME,
[
'addedExternalLinks' => $addedExternalLinks ?? [],
'removedExternalLinks' => $removedExternalLinks ?? [],
]
)
);
}
}

Expand Down
30 changes: 21 additions & 9 deletions includes/Jobs/RottenLinksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,43 @@

namespace Miraheze\RottenLinks\Jobs;

use GenericParameterJob;
use Job;
use MediaWiki\Config\Config;
use MediaWiki\Config\ConfigFactory;
use MediaWiki\ExternalLinks\LinkFilter;
use MediaWiki\MediaWikiServices;
use Miraheze\RottenLinks\RottenLinks;
use Wikimedia\Rdbms\IConnectionProvider;

class RottenLinksJob extends Job implements GenericParameterJob {
class RottenLinksJob extends Job {

public const JOB_NAME = 'RottenLinksJob';

private Config $config;
private IConnectionProvider $connectionProvider;

private array $addedExternalLinks;
private array $removedExternalLinks;

public function __construct( array $params ) {
parent::__construct( 'RottenLinksJob', $params );
public function __construct(
array $params,
ConfigFactory $configFactory,
IConnectionProvider $connectionProvider
) {
parent::__construct( self::JOB_NAME, $params );

$this->addedExternalLinks = $params['addedExternalLinks'];
$this->removedExternalLinks = $params['removedExternalLinks'];

$this->config = $configFactory->makeConfig( 'RottenLinks' );
$this->connectionProvider = $connectionProvider;
}

public function run(): bool {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'RottenLinks' );
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
$dbw = $this->connectionProvider->getPrimaryDatabase();

if ( $this->addedExternalLinks ) {
$excludeProtocols = (array)$config->get( 'RottenLinksExcludeProtocols' );
$excludeWebsites = (array)$config->get( 'RottenLinksExcludeWebsites' );
$excludeProtocols = (array)$this->config->get( 'RottenLinksExcludeProtocols' );
$excludeWebsites = (array)$this->config->get( 'RottenLinksExcludeWebsites' );

foreach ( $this->addedExternalLinks as $url ) {
$url = $this->decodeDomainName( $url );
Expand Down

0 comments on commit cb1d737

Please sign in to comment.