Skip to content

Commit

Permalink
Cleanup and modernize (#74)
Browse files Browse the repository at this point in the history
* Cleanup and modernize

* Fix

* Use namespace

* Use namespace

* Update CHANGELOG

* Suppress UnusedPluginSuppression
  • Loading branch information
Universal-Omega authored Jan 2, 2024
1 parent a005bff commit f251554
Show file tree
Hide file tree
Showing 16 changed files with 393 additions and 177 deletions.
4 changes: 2 additions & 2 deletions .phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

$cfg['suppress_issue_types'] = [
'PhanAccessMethodInternal',
'PhanUndeclaredStaticMethod',
'SecurityCheck-LikelyFalsePositive'
'SecurityCheck-LikelyFalsePositive',
'UnusedPluginSuppression'
];

return $cfg;
5 changes: 1 addition & 4 deletions .phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?xml version="1.0"?>
<ruleset>
<file>.</file>
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki">
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="MediaWiki.Commenting.FunctionComment" />
</rule>
<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"/>
Expand Down
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
## ChangeLog for RottenLinks

### 2.0.0 (DD-MM-2023)

### 2.0.1 (02-01-2024)
* Add `requireExtension` to updateExternalLinks.
* Modernize extension:
* Use class namespacing
* Use dependency injection
* Use HookHandlers
* Convert DB select queries to use SelectQueryBuilder
* Enable and fix remaining PHPCS checks

### 2.0.0 (17-12-2023)
* Redesign RottenLinks to not depend on a maintenance script
* Changes how we count page usage on RottenLinks special page.
We directly gather this from externallinks table rather then
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{
"name": "John Lewis"
},
{
"name": "Paladox"
},
{
"name": "Universal Omega"
}
Expand Down
97 changes: 52 additions & 45 deletions extension.json
Original file line number Diff line number Diff line change
@@ -1,86 +1,93 @@
{
"name": "RottenLinks",
"version": "2.0.0",
"version": "2.0.1",
"author": [
"John Lewis",
"Universal Omega",
"Paladox"
"Paladox",
"Universal Omega"
],
"url": "https://github.com/miraheze/RottenLinks",
"descriptionmsg": "rottenlinks-desc",
"namemsg": "rottenlinks-extensionname",
"license-name": "GPL-3.0-or-later",
"url": "https://github.com/miraheze/RottenLinks",
"type": "specialpage",
"requires": {
"MediaWiki": ">= 1.40.0"
},
"SpecialPages": {
"RottenLinks": "SpecialRottenLinks"
},
"JobClasses": {
"RottenLinksJob": "RottenLinksJob"
},
"MessagesDirs": {
"RottenLinks": [
"i18n"
]
},
"ExtensionMessagesFiles": {
"RottenLinksAliases": "includes/RottenLinksAliases.php"
"RottenLinksAliases": "RottenLinksAliases.php"
},
"AutoloadNamespaces": {
"Miraheze\\RottenLinks\\": "includes/"
},
"AutoloadClasses": {
"RottenLinks": "includes/RottenLinks.php",
"RottenLinksHooks": "includes/RottenLinksHooks.php",
"RottenLinksJob": "includes/RottenLinksJob.php",
"RottenLinksPager": "includes/RottenLinksPager.php",
"SpecialRottenLinks": "includes/SpecialRottenLinks.php"
"JobClasses": {
"RottenLinksJob": "Miraheze\\RottenLinks\\RottenLinksJob"
},
"SpecialPages": {
"RottenLinks": {
"class": "Miraheze\\RottenLinks\\SpecialRottenLinks",
"services": [
"ConfigFactory",
"DBLoadBalancer"
]
}
},
"Hooks": {
"LoadExtensionSchemaUpdates": [
"RottenLinksHooks::onRottenLinksSchemaUpdates"
],
"LinksUpdateComplete": [
"RottenLinksHooks::onLinksUpdateComplete"
]
"LinksUpdateComplete": {
"handler": "Main"
},
"LoadExtensionSchemaUpdates": {
"handler": "Installer"
}
},
"HookHandlers": {
"Installer": {
"class": "Miraheze\\RottenLinks\\HookHandlers\\Installer"
},
"Main": {
"class": "Miraheze\\RottenLinks\\HookHandlers\\Main",
"services": [
"JobQueueGroup"
]
}
},
"config": {
"RottenLinksBadCodes": {
"description": "Holds a list of HTTP codes that are considered bad. (array)",
"public": true,
"value": [ "0", "400", "401", "403", "404", "405", "502", "503", "504" ]
"value": [ "0", "400", "401", "403", "404", "405", "502", "503", "504" ],
"description": "Holds a list of HTTP codes that are considered bad. (array)"
},
"RottenLinksCurlTimeout": {
"description": "Sets the timeout for cURL in seconds. (integer)",
"public": true,
"value": 30
"value": 30,
"description": "Sets the timeout for cURL in seconds. (integer)"
},
"RottenLinksHTTPProxy": {
"description": "Sets a proxy to use for requests. (string)",
"public": true,
"value": ""
"value": "",
"description": "Sets a proxy to use for requests. (string)"
},
"RottenLinksExcludeProtocols": {
"description": "Holds a list of protocols that should not be checked for validity. (array)",
"public": true,
"value": [ "tel", "mailto" ]
"value": [ "tel", "mailto" ],
"description": "Holds a list of protocols that should not be checked for validity. (array)"
},
"RottenLinksExcludeWebsites": {
"description": "List of websites to exclude checking of response codes for. (array)",
"public": true,
"value": false
"value": false,
"description": "List of websites to exclude checking of response codes for. (array)"
},
"RottenLinksExternalLinkTarget": {
"description": "Sets the external link target (_self for the current tab or _blank for a new tab). (string)",
"public": true,
"value": "_self"
"value": "_self",
"description": "Sets the external link target (_self for the current tab or _blank for a new tab). (string)"
},
"RottenLinksUserAgent": {
"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)",
"public": true,
"value": ""
"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)"
}
},
"ConfigRegistry": {
"rottenlinks": "GlobalVarConfig::newInstance"
"RottenLinks": "GlobalVarConfig::newInstance"
},
"manifest_version": 2
}
5 changes: 3 additions & 2 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"@metadata": {
"authors": [
"John Lewis",
"Kghbln"
"Kghbln",
"Universal Omega"
]
},
"rottenlinks": "Status of external links",
"rottenlinks-desc": "Shows the [[Special:RottenLinks|state of all external links]] on the wiki",
"rottenlinks-metadata": "Script Data",
"rottenlinks-extensionname": "RottenLinks",
"rottenlinks-table-external": "External Link",
"rottenlinks-table-response": "HTTP Response",
"rottenlinks-table-usage": "Page Usage",
Expand Down
1 change: 1 addition & 0 deletions i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"rottenlinks": "{{doc-special}}",
"rottenlinks-desc": "{{desc|name=RottenLinks|url=https://github.com/miraheze/RottenLinks}}",
"rottenlinks-extensionname": "{{name}}",
"rottenlinks-table-external": "This is the label of a tabel header on Special:RottenLinks",
"rottenlinks-table-response": "This is the label of a tabel header on Special:RottenLinks",
"rottenlinks-table-usage": "This is the label of a tabel header on Special:RottenLinks",
Expand Down
24 changes: 24 additions & 0 deletions includes/HookHandlers/Installer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Miraheze\RottenLinks\HookHandlers;

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

class Installer implements LoadExtensionSchemaUpdatesHook {

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

$updater->addExtensionTable( 'rottenlinks', "$dir/rottenlinks.sql" );

$updater->addExtensionField( 'rottenlinks', 'rl_id', "$dir/patches/patch-add-rl_id.sql" );

$updater->addExtensionIndex( 'rottenlinks', 'rl_externallink', "$dir/patches/20210215.sql" );

$updater->dropExtensionField( 'rottenlinks', 'rl_pageusage', "$dir/patches/patch-drop-rl_pageusage.sql" );
}
}
41 changes: 41 additions & 0 deletions includes/HookHandlers/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Miraheze\RottenLinks\HookHandlers;

use JobQueueGroup;
use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
use MediaWiki\Hook\LinksUpdateCompleteHook;
use Miraheze\RottenLinks\RottenLinksJob;

class Main implements LinksUpdateCompleteHook {

/** @var JobQueueGroup */
private $jobQueueGroup;

/**
* @param JobQueueGroup $jobQueueGroup
*/
public function __construct( JobQueueGroup $jobQueueGroup ) {
$this->jobQueueGroup = $jobQueueGroup;
}

/**
* Handler for LinksUpdateComplete hook.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/LinksUpdateComplete
* @param LinksUpdate $linksUpdate
* @param mixed $ticket
*/
public function onLinksUpdateComplete( $linksUpdate, $ticket ) {
$addedExternalLinks = $linksUpdate->getAddedExternalLinks();
$removedExternalLinks = $linksUpdate->getRemovedExternalLinks();

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

$this->jobQueueGroup->push( new RottenLinksJob( $params ) );
}
}
}
34 changes: 30 additions & 4 deletions includes/RottenLinks.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
<?php

namespace Miraheze\RottenLinks;

use Config;
use MediaWiki\MediaWikiServices;

class RottenLinks {
public static function getResponse( $url ) {
/**
* 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 ) {
$services = MediaWikiServices::getInstance();

$config = $services->getConfigFactory()->makeConfig( 'rottenlinks' );
$config = $services->getConfigFactory()->makeConfig( 'RottenLinks' );

// Make the protocol lowercase
$urlexp = explode( '://', $url, 2 );
Expand All @@ -23,11 +33,27 @@ public static function getResponse( $url ) {
return $status;
}

private static function getHttpStatus( $url, $method, $services, $config ) {
/**
* Get the HTTP status code for a given URL using a specified method.
*
* @param string $url The URL to check.
* @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
) {
$httpProxy = $config->get( 'RottenLinksHTTPProxy' );

$userAgent = $config->get( 'RottenLinksUserAgent' ) ?:
'RottenLinks, MediaWiki extension (https://github.com/miraheze/RottenLinks), running on ' . $config->get( 'Server' );
'RottenLinks, MediaWiki extension (https://github.com/miraheze/RottenLinks), running on ' .
$config->get( 'Server' );

$request = $services->getHttpRequestFactory()->createMultiClient( [ 'proxy' => $httpProxy ] )
->run( [
Expand Down
43 changes: 0 additions & 43 deletions includes/RottenLinksHooks.php

This file was deleted.

Loading

0 comments on commit f251554

Please sign in to comment.