Skip to content

Commit

Permalink
ManageWikiExtensions: Log error if ext is not set in config && allow …
Browse files Browse the repository at this point in the history
…removing (#502)
  • Loading branch information
paladox authored Nov 11, 2024
1 parent 60103c5 commit e14e511
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
16 changes: 13 additions & 3 deletions includes/Helpers/ManageWikiExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Miraheze\ManageWiki\Helpers;

use MediaWiki\Config\Config;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\DBConnRef;

Expand Down Expand Up @@ -55,9 +56,17 @@ public function __construct( string $wiki ) {
]
)->s_extensions ?? '[]';

$logger = LoggerFactory::getInstance( 'ManageWiki' );

// To simplify clean up and to reduce the need to constantly refer back to many different variables, we now
// populate extension lists with config associated with them.
foreach ( json_decode( $exts, true ) as $ext ) {
if ( !isset( $this->extConfig[$ext] ) ) {
$logger->error( 'Extension/Skin {ext} not set in wgManageWikiExtensions', [
'ext' => $ext,
] );
continue;
}
$this->liveExts[$ext] = $this->extConfig[$ext];
}
}
Expand Down Expand Up @@ -90,16 +99,17 @@ public function add( $extensions ) {
/**
* Removes an extension from the 'enabled' list
* @param string|string[] $extensions Either an array or string of extensions to disable
* @param bool $forceRemove Force removing extension incase it is removed from config
*/
public function remove( $extensions ) {
public function remove( $extensions, $forceRemove = false ) {
// We allow remove either one extension (string) or many (array)
// We will handle all processing in final stages
foreach ( (array)$extensions as $ext ) {
if ( !isset( $this->liveExts[$ext] ) ) {
if ( !isset( $this->liveExts[$ext] ) && !$forceRemove ) {
continue;
}

$this->removedExts[$ext] = $this->liveExts[$ext];
$this->removedExts[$ext] = $this->liveExts[$ext] ?? [];
unset( $this->liveExts[$ext] );

$this->changes[$ext] = [
Expand Down
2 changes: 1 addition & 1 deletion includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function onCreateWikiDataFactoryBuilder( string $wiki, IReadableDa
$cacheArray['extensions'][] = $manageWikiExtensions[$ext]['var'] ??
$manageWikiExtensions[$ext]['name'];
} else {
$logger->error( 'Extension {ext} not set in wgManageWikiExtensions', [
$logger->error( 'Extension/Skin {ext} not set in wgManageWikiExtensions', [
'ext' => $ext,
] );
}
Expand Down
4 changes: 3 additions & 1 deletion maintenance/toggleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public function __construct() {
$this->addOption( 'all-wikis', 'Run on all wikis present in $wgLocalDatabases.' );
$this->addOption( 'confirm', 'Confirm execution. Required if using --all-wikis' );
$this->addOption( 'no-list', 'Don\'t list on which wikis this script has ran. This may speed up execution.' );
$this->addOption( 'force-remove', 'Force removal of extension when not in config.' );

$this->requireExtension( 'ManageWiki' );
}

public function execute() {
$forceRemove = $this->getOption( 'force-remove', false );
$noList = $this->getOption( 'no-list', false );
$allWikis = $this->getOption( 'all-wikis', false );
$wikis = $allWikis ?
Expand All @@ -46,7 +48,7 @@ public function execute() {
$mwExt = new ManageWikiExtensions( $wiki );
$extensionList = $mwExt->list();
if ( $disable && in_array( $ext, $extensionList ) ) {
$mwExt->remove( $ext );
$mwExt->remove( $ext, $forceRemove );
$mwExt->commit();
if ( !$noList ) {
$this->output( "Removed $ext from $wiki" );
Expand Down

0 comments on commit e14e511

Please sign in to comment.