Skip to content

Commit

Permalink
Use namespaced classes and other cleanup (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Universal-Omega authored Nov 18, 2024
1 parent 7870458 commit 4c57c07
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 194 deletions.
1 change: 0 additions & 1 deletion .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
$cfg['suppress_issue_types'] = [
'PhanAccessMethodInternal',
'SecurityCheck-LikelyFalsePositive',
'UnusedPluginSuppression'
];

return $cfg;
23 changes: 20 additions & 3 deletions .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@
<ruleset>
<file>.</file>
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki" />
<arg name="bootstrap" value="./vendor/mediawiki/mediawiki-codesniffer/utils/bootstrap-ci.php"/>
<arg name="extensions" value="php"/>
<arg name="encoding" value="UTF-8"/>
<rule ref="Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma">
<severity>5</severity>
</rule>
<rule ref="Squiz.WhiteSpace.FunctionSpacing.BeforeFirst">
<severity>5</severity>
</rule>
<rule ref="Squiz.WhiteSpace.MemberVarSpacing.FirstIncorrect">
<severity>5</severity>
</rule>
<rule ref="MediaWiki.Classes.FullQualifiedClassName">
<severity>5</severity>
<properties>
<property name="allowMainNamespace" value="false" />
<property name="allowInheritance" value="false" />
<property name="allowFunctions" value="false" />
</properties>
</rule>
<arg name="bootstrap" value="./vendor/mediawiki/mediawiki-codesniffer/utils/bootstrap-ci.php" />
<arg name="extensions" value="php" />
<arg name="encoding" value="UTF-8" />
</ruleset>
45 changes: 22 additions & 23 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
"Miraheze\\RottenLinks\\": "includes/"
},
"JobClasses": {
"RottenLinksJob": "Miraheze\\RottenLinks\\RottenLinksJob"
"RottenLinksJob": "Miraheze\\RottenLinks\\Jobs\\RottenLinksJob"
},
"SpecialPages": {
"RottenLinks": {
"class": "Miraheze\\RottenLinks\\SpecialRottenLinks",
"class": "Miraheze\\RottenLinks\\Specials\\SpecialRottenLinks",
"services": [
"ConfigFactory",
"DBLoadBalancer"
"ConnectionProvider"
]
}
},
Expand All @@ -62,8 +61,8 @@
"Main": {
"class": "Miraheze\\RottenLinks\\HookHandlers\\Main",
"services": [
"JobQueueGroup",
"ConnectionProvider"
"ConnectionProvider",
"JobQueueGroupFactory"
]
},
"Scribunto": {
Expand All @@ -72,36 +71,36 @@
},
"config": {
"RottenLinksBadCodes": {
"value": [ "0", "400", "401", "403", "404", "405", "410", "502", "503", "504" ],
"description": "Holds a list of HTTP codes that are considered bad. (array)"
"description": "Array. Holds a list of HTTP codes that are considered bad.",
"value": [ "0", "400", "401", "403", "404", "405", "410", "502", "503", "504" ]
},
"RottenLinksCurlTimeout": {
"value": 30,
"description": "Sets the timeout for cURL in seconds. (integer)"
},
"RottenLinksHTTPProxy": {
"value": "",
"description": "Sets a proxy to use for requests. (string)"
"description": "Integer. Sets the timeout for cURL in seconds.",
"value": 30
},
"RottenLinksExcludeProtocols": {
"value": [ "tel", "mailto" ],
"description": "Holds a list of protocols that should not be checked for validity. (array)"
"description": "Array. Holds a list of protocols that should not be checked for validity.",
"value": [ "tel", "mailto" ]
},
"RottenLinksExcludeWebsites": {
"value": false,
"description": "List of websites to exclude checking of response codes for. (array)"
"description": "Array. List of websites to exclude checking of response codes for.",
"value": []
},
"RottenLinksExternalLinkTarget": {
"value": "_self",
"description": "Sets the external link target (_self for the current tab or _blank for a new tab). (string)"
"description": "String. Sets the external link target (_self for the current tab or _blank for a new tab).",
"value": "_self"
},
"RottenLinksHTTPProxy": {
"description": "String. Sets a proxy to use for requests.",
"value": ""
},
"RottenLinksUserAgent": {
"value": "",
"description": "Overrides the user-agent to use for requests. Defaults to 'RottenLinks, MediaWiki extension (https://github.com/miraheze/RottenLinks), running on <Wiki base URL>'. (string)"
"description": "String. Overrides the user-agent to use for requests. Defaults to 'RottenLinks, MediaWiki extension (https://github.com/miraheze/RottenLinks), running on <Wiki base URL>'.",
"value": ""
}
},
"ConfigRegistry": {
"RottenLinks": "GlobalVarConfig::newInstance"
"RottenLinks": "MediaWiki\\Config\\GlobalVarConfig::newInstance"
},
"manifest_version": 2
}
5 changes: 1 addition & 4 deletions includes/HookHandlers/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

namespace Miraheze\RottenLinks\HookHandlers;

use DatabaseUpdater;
use MediaWiki\Installer\Hook\LoadExtensionSchemaUpdatesHook;

class Installer implements LoadExtensionSchemaUpdatesHook {

/**
* @param DatabaseUpdater $updater
*/
/** @inheritDoc */
public function onLoadExtensionSchemaUpdates( $updater ) {
$dir = __DIR__ . '/../../sql';

Expand Down
24 changes: 12 additions & 12 deletions includes/HookHandlers/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

namespace Miraheze\RottenLinks\HookHandlers;

use JobQueueGroup;
use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
use MediaWiki\Hook\LinksUpdateCompleteHook;
use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\JobQueue\JobQueueGroupFactory;
use MediaWiki\Parser\Parser;
use Miraheze\RottenLinks\RottenLinksJob;
use Miraheze\RottenLinks\Jobs\RottenLinksJob;
use Miraheze\RottenLinks\RottenLinksParserFunctions;
use Wikimedia\Rdbms\IConnectionProvider;

class Main implements LinksUpdateCompleteHook, ParserFirstCallInitHook {

private JobQueueGroup $jobQueueGroup;
private JobQueueGroupFactory $jobQueueGroupFactory;
private RottenLinksParserFunctions $parserFunctions;

/**
* @param JobQueueGroup $jobQueueGroup
* @param IConnectionProvider $connectionProvider
*/
public function __construct( JobQueueGroup $jobQueueGroup, IConnectionProvider $connectionProvider ) {
$this->jobQueueGroup = $jobQueueGroup;
public function __construct(
IConnectionProvider $connectionProvider,
JobQueueGroupFactory $jobQueueGroupFactory
) {
$this->jobQueueGroupFactory = $jobQueueGroupFactory;
$this->parserFunctions = new RottenLinksParserFunctions( $connectionProvider );
}

Expand All @@ -37,11 +36,12 @@ public function onLinksUpdateComplete( $linksUpdate, $ticket ) {

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

$this->jobQueueGroup->push( new RottenLinksJob( $params ) );
$jobQueueGroup = $this->jobQueueGroupFactory->makeJobQueueGroup();
$jobQueueGroup->push( new RottenLinksJob( $params ) );
}
}

Expand Down
34 changes: 8 additions & 26 deletions includes/RottenLinksJob.php → includes/Jobs/RottenLinksJob.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
<?php

namespace Miraheze\RottenLinks;
namespace Miraheze\RottenLinks\Jobs;

use GenericParameterJob;
use Job;
use MediaWiki\ExternalLinks\LinkFilter;
use MediaWiki\MediaWikiServices;
use Miraheze\RottenLinks\RottenLinks;

class RottenLinksJob extends Job implements GenericParameterJob {

/** @var array */
private $addedExternalLinks;
private array $addedExternalLinks;
private array $removedExternalLinks;

/** @var array */
private $removedExternalLinks;

/**
* @param array $params Job parameters.
*/
public function __construct( array $params ) {
parent::__construct( 'RottenLinksJob', $params );

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

/**
* Execute the job, updating the 'rottenlinks' table based on added and removed external links.
*
* @return bool True on success.
*/
public function run() {
public function run(): bool {
$config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'RottenLinks' );
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();

if ( $this->addedExternalLinks ) {
$dbw = MediaWikiServices::getInstance()
->getDBLoadBalancer()
->getMaintenanceConnectionRef( DB_PRIMARY );

$excludeProtocols = (array)$config->get( 'RottenLinksExcludeProtocols' );
$excludeWebsites = (array)$config->get( 'RottenLinksExcludeWebsites' );

Expand Down Expand Up @@ -86,10 +73,6 @@ public function run() {
}

if ( $this->removedExternalLinks ) {
$dbw = MediaWikiServices::getInstance()
->getDBLoadBalancer()
->getMaintenanceConnectionRef( DB_PRIMARY );

foreach ( $this->removedExternalLinks as $url ) {
$url = $this->decodeDomainName( $url );

Expand Down Expand Up @@ -130,7 +113,6 @@ public function run() {
* URL-decoding the domain part turns these URLs back into valid syntax.
*
* @param string $url The URL to decode.
*
* @return string The URL with the decoded domain name.
*/
private function decodeDomainName( string $url ): string {
Expand Down
10 changes: 4 additions & 6 deletions includes/RottenLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Miraheze\RottenLinks;

use Config;
use MediaWiki\Config\Config;
use MediaWiki\MediaWikiServices;
use WikiMedia\Rdbms\IReadableDatabase;

class RottenLinks {

/**
* Get the HTTP response status code for a given URL.
*
* @param string $url The URL to check.
*
* @return int The HTTP status code.
*/
public static function getResponse( string $url ) {
public static function getResponse( string $url ): int {
$services = MediaWikiServices::getInstance();

$config = $services->getConfigFactory()->makeConfig( 'RottenLinks' );
Expand All @@ -41,15 +41,14 @@ public static function getResponse( string $url ) {
* @param string $method The HTTP method to use ('HEAD' or 'GET').
* @param MediaWikiServices $services MediaWiki service instance.
* @param Config $config Configuration instance.
*
* @return int The HTTP status code.
*/
private static function getHttpStatus(
string $url,
string $method,
MediaWikiServices $services,
Config $config
) {
): int {
$httpProxy = $config->get( 'RottenLinksHTTPProxy' );

$userAgent = $config->get( 'RottenLinksUserAgent' ) ?:
Expand All @@ -76,7 +75,6 @@ private static function getHttpStatus(
*
* @param IReadableDatabase $dbr
* @param string $url
*
* @return ?int null if the URL is not in the database, 0 if there was no response, or the response code
*/
public static function getResponseFromDatabase( IReadableDatabase $dbr, string $url ): ?int {
Expand Down
1 change: 1 addition & 0 deletions includes/RottenLinksLuaLibrary.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Miraheze\RottenLinks;

use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LibraryBase;
Expand Down
32 changes: 13 additions & 19 deletions includes/RottenLinksPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,31 @@

namespace Miraheze\RottenLinks;

use Config;
use Html;
use HttpStatus;
use IContextSource;
use Linker;
use MediaWiki\Config\Config;
use MediaWiki\Context\IContextSource;
use MediaWiki\ExternalLinks\LinkFilter;
use SpecialPage;
use TablePager;
use MediaWiki\Html\Html;
use MediaWiki\Linker\Linker;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Pager\TablePager;
use MediaWiki\SpecialPage\SpecialPage;

class RottenLinksPager extends TablePager {

/** @var Config */
private $config;
private Config $config;
private bool $showBad;

/** @var bool */
private $showBad;

/**
* @param IContextSource $context The context source.
* @param Config $config RottenLinks config factory instance.
* @param bool $showBad Whether to show only links with bad status.
*/
public function __construct(
IContextSource $context,
Config $config,
IContextSource $context,
LinkRenderer $linkRenderer,
bool $showBad
) {
parent::__construct( $context );
parent::__construct( $context, $linkRenderer );

$this->showBad = $showBad;
$this->config = $config;
$this->showBad = $showBad;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions includes/RottenLinksParserFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ class RottenLinksParserFunctions {

private IConnectionProvider $connectionProvider;

/**
* @param IConnectionProvider $connectionProvider
*/
public function __construct( IConnectionProvider $connectionProvider ) {
$this->connectionProvider = $connectionProvider;
}
Expand Down
Loading

0 comments on commit 4c57c07

Please sign in to comment.