Skip to content

Commit

Permalink
Update for MediaWiki 1.42 and use virtual domain (#99)
Browse files Browse the repository at this point in the history
Also adds new tests for SpecialRequestImport and
ImportDumpRequestManager to ensure new DatabaseVirtualDomains handling.
It also adds a new ConfigNames class to improve code standards and
protect against typos.

This should not be merged until we want to drop MediaWiki 1.41 support.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Updated MediaWiki version requirement to 1.42.0 for better
compatibility and performance.
  - Enhanced database connection handling for improved stability.

- **Bug Fixes**
- Fixed import statements and dependencies to align with MediaWiki's
updated structure.

- **Refactor**
- Replaced `DBLoadBalancerFactory` with `ConnectionProvider` in various
classes for more efficient database management.
- Added `ConfigNames` to facilitate getting configuration options and to
protect against typos.

- **Tests**
- Added comprehensive test cases for `SpecialRequestImport` to ensure
robust functionality and reliability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
Universal-Omega committed Jul 17, 2024
1 parent 2693858 commit fd9ab0e
Show file tree
Hide file tree
Showing 20 changed files with 491 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mediawiki-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
matrix:
include:
# Latest MediaWiki stable - PHP 8.1
- mw: 'REL1_41'
- mw: 'REL1_42'
php: 8.1
php-docker: 81
composer-test: true
Expand Down
18 changes: 8 additions & 10 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license-name": "GPL-3.0-or-later",
"type": "specialpage",
"requires": {
"MediaWiki": ">= 1.41.0"
"MediaWiki": ">= 1.42.0"
},
"MessagesDirs": {
"ImportDump": [
Expand All @@ -21,15 +21,14 @@
"Miraheze\\ImportDump\\": "includes/"
},
"TestAutoloadNamespaces": {
"Miraheze\\ImportDump\\Tests\\": "tests/phpunit/",
"Miraheze\\ImportDump\\Tests\\Unit\\": "tests/phpunit/unit/"
"Miraheze\\ImportDump\\Tests\\": "tests/phpunit/"
},
"JobClasses": {
"ImportDumpJob": {
"class": "Miraheze\\ImportDump\\Jobs\\ImportDumpJob",
"services": [
"ConfigFactory",
"DBLoadBalancerFactory",
"ConnectionProvider",
"JobQueueGroupFactory",
"ImportDumpHookRunner",
"ImportDumpRequestManager",
Expand Down Expand Up @@ -111,7 +110,7 @@
"RequestImport": {
"class": "Miraheze\\ImportDump\\Specials\\SpecialRequestImport",
"services": [
"DBLoadBalancerFactory",
"ConnectionProvider",
"MimeAnalyzer",
"PermissionManager",
"RepoGroup",
Expand Down Expand Up @@ -155,7 +154,7 @@
"Main": {
"class": "Miraheze\\ImportDump\\Hooks\\Handlers\\Main",
"services": [
"ConfigFactory"
"ConnectionProvider"
]
}
},
Expand All @@ -176,10 +175,6 @@
"remoteExtPath": "ImportDump/modules"
},
"config": {
"ImportDumpCentralWiki": {
"value": "",
"description": "If set, only allow users to request import dumps on this wiki."
},
"ImportDumpEnableAutomatedJob": {
"value": false,
"description": "Whether to enable a job where a reviewer just has to 'approve' an import and job handles everything else. You can use the ImportDumpJobGetFile hook to manipulate how the job gets the XML file."
Expand Down Expand Up @@ -227,5 +222,8 @@
"ServiceWiringFiles": [
"includes/ServiceWiring.php"
],
"DatabaseVirtualDomains": [
"virtual-importdump"
],
"manifest_version": 2
}
23 changes: 23 additions & 0 deletions includes/ConfigNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

// phpcs:disable Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase
namespace Miraheze\ImportDump;

/**
* A class containing constants representing the names of configuration variables,
* to protect against typos.
*/
class ConfigNames {

public const EnableAutomatedJob = 'ImportDumpEnableAutomatedJob';

public const HelpUrl = 'ImportDumpHelpUrl';

public const InterwikiMap = 'ImportDumpInterwikiMap';

public const ScriptCommand = 'ImportDumpScriptCommand';

public const UsersNotifiedOnAllRequests = 'ImportDumpUsersNotifiedOnAllRequests';

public const UsersNotifiedOnFailedImports = 'ImportDumpUsersNotifiedOnFailedImports';
}
2 changes: 1 addition & 1 deletion includes/Hooks/Handlers/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Miraheze\ImportDump\Hooks\Handlers;

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

class Installer implements LoadExtensionSchemaUpdatesHook {
Expand Down
25 changes: 10 additions & 15 deletions includes/Hooks/Handlers/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Miraheze\ImportDump\Hooks\Handlers;

use MediaWiki\Block\Hook\GetAllBlockActionsHook;
use MediaWiki\Config\Config;
use MediaWiki\Config\ConfigFactory;
use MediaWiki\Extension\Notifications\AttributeManager;
use MediaWiki\Extension\Notifications\UserLocator;
use MediaWiki\Hook\LoginFormValidErrorMessagesHook;
Expand All @@ -14,21 +12,22 @@
use Miraheze\ImportDump\Notifications\EchoNewRequestPresentationModel;
use Miraheze\ImportDump\Notifications\EchoRequestCommentPresentationModel;
use Miraheze\ImportDump\Notifications\EchoRequestStatusUpdatePresentationModel;
use Wikimedia\Rdbms\IConnectionProvider;

class Main implements
GetAllBlockActionsHook,
LoginFormValidErrorMessagesHook,
UserGetReservedNamesHook
{

/** @var Config */
private $config;
/** @var IConnectionProvider */
private $connectionProvider;

/**
* @param ConfigFactory $configFactory
* @param IConnectionProvider $connectionProvider
*/
public function __construct( ConfigFactory $configFactory ) {
$this->config = $configFactory->makeConfig( 'ImportDump' );
public function __construct( IConnectionProvider $connectionProvider ) {
$this->connectionProvider = $connectionProvider;
}

/**
Expand All @@ -45,10 +44,8 @@ public function onUserGetReservedNames( &$reservedUsernames ) {
* @param array &$actions
*/
public function onGetAllBlockActions( &$actions ) {
if (
$this->config->get( 'ImportDumpCentralWiki' ) &&
!WikiMap::isCurrentWikiId( $this->config->get( 'ImportDumpCentralWiki' ) )
) {
$dbr = $this->connectionProvider->getReplicaDatabase( 'virtual-importdump' );
if ( !WikiMap::isCurrentWikiDbDomain( $dbr->getDomainID() ) ) {
return;
}

Expand All @@ -68,10 +65,8 @@ public function onLoginFormValidErrorMessages( array &$messages ) {
* @param array &$icons
*/
public function onBeforeCreateEchoEvent( &$notifications, &$notificationCategories, &$icons ) {
if (
$this->config->get( 'ImportDumpCentralWiki' ) &&
!WikiMap::isCurrentWikiId( $this->config->get( 'ImportDumpCentralWiki' ) )
) {
$dbr = $this->connectionProvider->getReplicaDatabase( 'virtual-importdump' );
if ( !WikiMap::isCurrentWikiDbDomain( $dbr->getDomainID() ) ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion includes/ImportDumpOOUIForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Miraheze\ImportDump;

use MediaWiki\HTMLForm\OOUIHTMLForm;
use OOUI\FieldsetLayout;
use OOUI\HtmlSnippet;
use OOUI\IndexLayout;
use OOUI\PanelLayout;
use OOUI\TabPanelLayout;
use OOUI\Widget;
use OOUIHTMLForm;
use Xml;

class ImportDumpOOUIForm extends OOUIHTMLForm {
Expand Down
29 changes: 10 additions & 19 deletions includes/ImportDumpRequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
use MediaWiki\Interwiki\InterwikiLookup;
use MediaWiki\JobQueue\JobQueueGroupFactory;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Message\Message;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\User\ActorStoreFactory;
use MediaWiki\User\User;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserGroupManagerFactory;
use Message;
use MessageLocalizer;
use Miraheze\CreateWiki\Hooks\CreateWikiHookRunner;
use Miraheze\CreateWiki\RemoteWiki;
Expand All @@ -38,9 +38,8 @@ class ImportDumpRequestManager {
];

public const CONSTRUCTOR_OPTIONS = [
'ImportDumpCentralWiki',
'ImportDumpInterwikiMap',
'ImportDumpScriptCommand',
ConfigNames::InterwikiMap,
ConfigNames::ScriptCommand,
];

/** @var Config */
Expand Down Expand Up @@ -136,13 +135,9 @@ public function __construct(
* @param int $requestID
*/
public function fromID( int $requestID ) {
$this->dbw = $this->connectionProvider->getPrimaryDatabase( 'virtual-importdump' );
$this->ID = $requestID;

$centralWiki = $this->options->get( 'ImportDumpCentralWiki' );
$this->dbw = $this->connectionProvider->getPrimaryDatabase(
$centralWiki ?: false
);

$this->row = $this->dbw->newSelectQueryBuilder()
->table( 'import_requests' )
->field( '*' )
Expand Down Expand Up @@ -312,9 +307,7 @@ public function getInvolvedUsers(): array {
* @return bool
*/
public function insertInterwikiPrefix( string $prefix, string $url, User $user ): bool {
$dbw = $this->connectionProvider->getPrimaryDatabase(
$this->getTarget()
);
$dbw = $this->connectionProvider->getPrimaryDatabase( $this->getTarget() );

$dbw->newInsertQueryBuilder()
->insertInto( 'interwiki' )
Expand Down Expand Up @@ -364,9 +357,7 @@ public function insertInterwikiPrefix( string $prefix, string $url, User $user )
* @return string
*/
public function getInterwikiPrefix(): string {
$dbr = $this->connectionProvider->getReplicaDatabase(
$this->getTarget()
);
$dbr = $this->connectionProvider->getReplicaDatabase( $this->getTarget() );

$sourceHost = parse_url( $this->getSource(), PHP_URL_HOST );
if ( !$sourceHost ) {
Expand Down Expand Up @@ -410,14 +401,14 @@ public function getInterwikiPrefix(): string {
}
}

if ( $this->options->get( 'ImportDumpInterwikiMap' ) ) {
if ( $this->options->get( ConfigNames::InterwikiMap ) ) {
$parsedSource = parse_url( $this->getSource(), PHP_URL_HOST ) ?: '';
$domain = explode( '.', $parsedSource )[1] ?? '';

if ( $domain ) {
$domain .= '.' . ( explode( '.', $parsedSource )[2] ?? '' );
if ( $this->options->get( 'ImportDumpInterwikiMap' )[$domain] ?? '' ) {
$domain = $this->options->get( 'ImportDumpInterwikiMap' )[$domain];
if ( $this->options->get( ConfigNames::InterwikiMap )[$domain] ?? '' ) {
$domain = $this->options->get( ConfigNames::InterwikiMap )[$domain];
$subdomain = explode( '.', $parsedSource )[0] ?? '';

if ( $subdomain ) {
Expand All @@ -434,7 +425,7 @@ public function getInterwikiPrefix(): string {
* @return string
*/
public function getCommand(): string {
$command = $this->options->get( 'ImportDumpScriptCommand' );
$command = $this->options->get( ConfigNames::ScriptCommand );

if ( !$this->getInterwikiPrefix() ) {
$command = preg_replace( '/--username-prefix=?/', '', $command );
Expand Down
10 changes: 2 additions & 8 deletions includes/ImportDumpRequestQueuePager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Miraheze\ImportDump;

use IContextSource;
use MediaWiki\Config\Config;
use MediaWiki\Context\IContextSource;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Pager\TablePager;
use MediaWiki\SpecialPage\SpecialPage;
Expand All @@ -29,7 +28,6 @@ class ImportDumpRequestQueuePager extends TablePager
private $target;

/**
* @param Config $config
* @param IContextSource $context
* @param IConnectionProvider $connectionProvider
* @param LinkRenderer $linkRenderer
Expand All @@ -39,7 +37,6 @@ class ImportDumpRequestQueuePager extends TablePager
* @param string $target
*/
public function __construct(
Config $config,
IContextSource $context,
IConnectionProvider $connectionProvider,
LinkRenderer $linkRenderer,
Expand All @@ -50,10 +47,7 @@ public function __construct(
) {
parent::__construct( $context, $linkRenderer );

$centralWiki = $config->get( 'ImportDumpCentralWiki' );
$this->mDb = $connectionProvider->getReplicaDatabase(
$centralWiki ?: false
);
$this->mDb = $connectionProvider->getReplicaDatabase( 'virtual-importdump' );

$this->linkRenderer = $linkRenderer;
$this->userFactory = $userFactory;
Expand Down
Loading

0 comments on commit fd9ab0e

Please sign in to comment.