Skip to content

Commit

Permalink
convert to use new caching backend
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnFLewis committed Apr 3, 2020
1 parent af5864e commit f914eb5
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 411 deletions.
14 changes: 0 additions & 14 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"AutoloadClasses": {
"ApiQueryWikiConfig": "includes/api/ApiQueryWikiConfig.php",
"ManageWiki": "includes/ManageWiki.php",
"ManageWikiCDB": "includes/helpers/ManageWikiCDB.php",
"ManageWikiDeletedWikiPager": "includes/helpers/ManageWikiDeletedWikiPager.php",
"ManageWikiFormFactory": "includes/formFactory/ManageWikiFormFactory.php",
"ManageWikiFormFactoryBuilder": "includes/formFactory/ManageWikiFormFactoryBuilder.php",
Expand Down Expand Up @@ -122,26 +121,13 @@
"description": "Array of Booleans. Which modules of ManageWiki should be enabled on the wiki.",
"public": true,
"value": {
"cdb": false,
"core": false,
"extensions": false,
"namespaces": false,
"permissions": false,
"settings": false
}
},
"ManageWikiBackendModules": {
"description": "Array. List of backend ManageWiki modules. Shouldn't be changed.",
"public": true,
"value": [
"cdb"
]
},
"ManageWikiCDBDirectory": {
"description": "String. Directory to store CDB files in. This will enable CDB capabilities in ManageWiki.",
"public": true,
"value": false
},
"ManageWikiExtensions": {
"description": "Array. An array of extensions enabled within the wiki farm.",
"public": true,
Expand Down
24 changes: 7 additions & 17 deletions includes/ManageWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@

class ManageWiki {
public static function checkSetup( string $module, bool $verbose = false, $out = false ) {
global $wgManageWiki, $wgManageWikiCDBDirectory;
global $wgManageWiki;

// Checks ManageWiki module is enabled before doing anything
// $verbose means output an error. Otherwise return true/false.

if ( $wgManageWiki[$module] ) {
if ( $module == 'cdb' ) {
return (bool)$wgManageWikiCDBDirectory;
}

return true;
} else {
if ( !$wgManageWiki[$module] ) {
if ( $verbose && $out ) {
$out->addWikiMsg( 'managewiki-disabled', $module );
}

return false;
}
}

public static function listModules( bool $public = true ) {
global $wgManageWiki, $wgManageWikiBackendModules;

$enabledModules = array_keys( $wgManageWiki, true );
return true;
}

if ( $public ) {
return array_diff( $enabledModules, $wgManageWikiBackendModules );
}
public static function listModules() {
global $wgManageWiki;

return $enabledModules;
return array_keys( $wgManageWiki, true );
}

public static function checkPermission( RemoteWiki $rm, User $user, string $perm = "" ) {
Expand Down
188 changes: 43 additions & 145 deletions includes/ManageWikiHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function onRegistration() {
}

public static function onCreateWikiJsonBuilder( string $wiki, Database $dbr, array &$jsonArray ) {
global $wgManageWikiExtensions;
global $wgManageWikiExtensions, $wgManageWikiPermissionsAdditionalRights, $wgManageWikiPermissionsAdditionalAddGroups, $wgManageWikiPermissionsAdditionalRemoveGroups;

$setObject = $dbr->selectRow(
'mw_settings',
Expand Down Expand Up @@ -78,7 +78,20 @@ public static function onCreateWikiJsonBuilder( string $wiki, Database $dbr, arr
'aliases' => json_decode( $ns->ns_aliases, true ),
'additional' => json_decode( $ns->ns_additional, true )
];

$nsAdditional = json_decode( $ns->ns_additional, true );

foreach ( $nsAdditional as $var => $val ) {
if ( $val ) {
if ( $wgManageWikiNamespacesAdditional[$var]['vestyle'] ) {
$jsonArray['settings'][$var][$ns->ns_namespace_id] = true;
} else {
$jsonArray['settings'][$var][] = $ns->ns_namespace_id;
}
}
}
}

}

// Same as NS above but for permissions
Expand All @@ -92,124 +105,43 @@ public static function onCreateWikiJsonBuilder( string $wiki, Database $dbr, arr
);

foreach ( $permObjects as $perm ) {
$addPerms =[];

foreach ( ( $wgManageWikiPermissionsAdditionalRights ?? [] ) as $right => $bool ) {
if ( $bool ) {
$addPerms[] = $right;
}
}

$jsonArray['permissions'][$perm->perm_group] = [
'permissions' => json_decode( $perm->perm_permissions, true ),
'addgroups' => json_decode( $perm->perm_addgroups, true ),
'removegroups' => json_decode( $perm->perm_removegroups, true ),
'permissions' => array_merge( json_decode( $perm->perm_permissions, true ), $addPerms ),
'addgroups' => array_merge( json_decode( $perm->perm_addgroups, true ), $wgManageWikiPermissionsAdditionalAddGroups[$perm->perm_group] ?? [] ),
'removegroups' => array_merge( json_decode( $perm->perm_removegroups, true ), $wgManageWikiPermissionsAdditionalRemoveGroups[$perm->perm_group] ?? [] ),
'addself' => json_decode( $perm->perm_addgroupstoself, true ),
'removeself' => json_decode( $perm->perm_removegroupsfromself, true ),
'autopromote' => json_decode( $perm->perm_autopromote, true )
];
}
}
}

public static function onSetupAfterCache() {
global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgCreateWikiDatabase, $wgDBname, $wgManageWikiPermissionsAdditionalRights, $wgManageWikiPermissionsAdditionalAddGroups, $wgManageWikiPermissionsAdditionalRemoveGroups, $wgManageWikiCDBDirectory,
$wgManageWikiNamespacesCore, $wgContentNamespaces, $wgExtraNamespaces, $wgNamespaceProtection, $wgNamespacesToBeSearchedDefault, $wgNamespaceAliases, $wgNamespacesWithSubpages, $wgManageWikiPermissionsAdditionalAddGroupsSelf,
$wgManageWikiPermissionsAdditionalRemoveGroupsSelf, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf, $wgAutopromote, $wgNamespaceContentModels, $wgManageWikiNamespacesAdditional;

// Safe guard if - should not remove all existing settigs if we're not managing permissions with in.
if ( ManageWiki::checkSetup( 'permissions' ) ) {
$wgGroupPermissions = [];
$wgAddGroups = [];
$wgRemoveGroups = [];
$wgGroupsAddToSelf = [];
$wgGroupsRemoveFromSelf = [];
$wgAutopromote = [];

if ( !ManageWikiCDB::latest( 'permissions' ) ) {
ManageWikiCDB::upsert( 'permissions' );
}

$permsArray = ManageWikiCDB::get( 'permissions', [ 'wgGroupPermissions', 'wgAddGroups', 'wgRemoveGroups', 'wgGroupsAddToSelf', 'wgGroupsRemoveFromSelf', 'wgAutopromote' ] );
$diffKeys = array_keys( array_diff_key( $wgManageWikiPermissionsAdditionalRights, $jsonArray['permissions'] ) );

foreach( $permsArray as $key => $json ) {
$permsArray[$key] = json_decode( $json, true );
}
foreach ( $diffKeys as $missingKey ) {
$missingPermissions = [];

foreach ( $permsArray as $key => $array ) {
if ( $key == 'wgGroupPermissions' ) {
foreach ( $array as $group => $perms ) {
foreach ( $perms as $i => $perm ) {
$$key[$group][$perm] = true;
}
}
} elseif ( $key == 'wgAutopromote' ) {
if ( !is_null( $array ) ) {
$$key = $array;
}
} else {
foreach ( $array as $i => $groups ) {
if ( is_array( $groups ) && count( $groups ) >= 1 ) {
foreach ( $groups as $id => $group ) {
$$key[$i][] = $group;
}
}
foreach ( $wgManageWikiPermissionsAdditionalRights as $right => $bool ) {
if ( $bool ) {
$missingPermissions[] = $right;
}
}
}

if ( $wgManageWikiPermissionsAdditionalRights ) {
$wgGroupPermissions = array_merge_recursive( $wgGroupPermissions, $wgManageWikiPermissionsAdditionalRights );
}

if ( $wgManageWikiPermissionsAdditionalAddGroups ) {
$wgAddGroups = array_merge_recursive( $wgAddGroups, $wgManageWikiPermissionsAdditionalAddGroups );
}

if ( $wgManageWikiPermissionsAdditionalRemoveGroups ) {
$wgRemoveGroups = array_merge_recursive( $wgRemoveGroups, $wgManageWikiPermissionsAdditionalRemoveGroups );
}

if ( $wgManageWikiPermissionsAdditionalAddGroupsSelf ) {
$wgGroupsAddToSelf = array_merge_recursive( $wgGroupsAddToSelf, $wgManageWikiPermissionsAdditionalAddGroupsSelf );
}

if ( $wgManageWikiPermissionsAdditionalRemoveGroupsSelf ) {
$wgGroupsRemoveFromSelf = array_merge_recursive( $wgGroupsRemoveFromSelf, $wgManageWikiPermissionsAdditionalRemoveGroupsSelf );
}
}

// Safe guard if - should not remove existing namespaces if we're not going to manage them
if ( ManageWiki::checkSetup( 'namespaces' ) ) {
$wgContentNamespaces = [];
$wgExtraNamespaces = [];
$wgNamespaceProtection = [];
$wgNamespacesToBeSearchedDefault = [];
$wgNamespacesWithSubpages = [];
$wgNamespaceAliases = [];

if ( !ManageWikiCDB::latest( 'namespaces' ) ) {
ManageWikiCDB::upsert( 'namespaces' );
}

$nsArray = ManageWikiCDB::get( 'namespaces', [ 'wgContentNamespaces', 'wgNamespaceContentModels', 'wgExtraNamespaces', 'wgNamespaceProtection', 'wgNamespacesToBeSearchedDefault', 'wgNamespacesWithSubpages', 'wgNamespaceAliases', 'wgManageWikiNamespacesCore', 'mwAdditional' ] );

foreach ( $nsArray as $key => $json ) {
$nsArray[$key] = json_decode( $json, true );
}

foreach ( $nsArray as $key => $array ) {
if ( !is_array( $array ) ) {
continue;
}

if ( $key == 'mwAdditional' ) {
foreach ( $array as $key => $id ) {
global $$key;

if ( !empty( $id ) && isset( $wgManageWikiNamespacesAdditional[$key] ) ) {
foreach ( $id as $nsID ) {
$$key[$nsID] = ( $wgManageWikiNamespacesAdditional[$key]['vestyle'] ) ? true : $nsID;
}
}
}
} else {
foreach ( $array as $id => $val ) {
$$key[$id] = $val;
}
}
$jsonArray['permissions'][$missingKey] = [
'permissions' => $missingPermissions,
'addgroups' => $wgManageWikiPermissionsAdditionalAddGroups[$missingKey] ?? [],
'removegroups' => $wgManageWikiPermissionsAdditionalRemoveGroups[$missingKey] ?? [],
'addself' => [],
'removeself' => [],
'autopromote' => []
];
}
}
}
Expand Down Expand Up @@ -246,7 +178,6 @@ public static function onCreateWikiCreation( $dbname, $private ) {
ManageWikiHooks::onCreateWikiStatePrivate( $dbname );
}

ManageWikiCDB::changes( 'permissions' );
}

if ( $wgManageWikiExtensions && $wgManageWikiExtensionsDefault ) {
Expand Down Expand Up @@ -298,7 +229,6 @@ public static function onCreateWikiCreation( $dbname, $private ) {
);
}

ManageWikiCDB::changes( 'namespaces' );
}
}

Expand All @@ -312,40 +242,6 @@ public static function onCreateWikiTables( &$tables ) {
}
}

public static function onCreateWikiDeletion( $dbw, $wiki ) {
global $wgManageWikiCDBDirectory;

if ( ManageWiki::checkSetup( 'cdb' ) ) {
if ( ManageWiki::checkSetup( 'permissions' ) ) {
unlink( $wgManageWikiCDBDirectory . '/' . $wiki . '-permissions.cdb' );
}

if ( ManageWiki::checkSetup( 'namespaces' ) ) {
unlink( $wgManageWikiCDBDirectory . '/' . $wiki . '-namespaces.cdb' );
}
}
}

public static function onCreateWikiRename( $dbw, $old, $new ) {
global $wgManageWikiCDBDirectory;

if ( ManageWiki::checkSetup( 'cdb' ) ) {
if ( ManageWiki::checkSetup( 'permissions' ) ) {
$filePM = $wgManageWikiCDBDirectory . '/' . $old . '-permissions.cdb';
if ( file_exists( $filePM ) ) {
unlink( $filePM );
}
}

if ( ManageWiki::checkSetup( 'namespaces' ) ) {
$fileNS = $wgManageWikiCDBDirectory . '/' . $old . '-namespaces.cdb';
if ( file_exists( $fileNS ) ) {
unlink( $fileNS );
}
}
}
}

public static function onCreateWikiStatePrivate( $dbname ) {
global $wgManageWikiPermissionsDefaultPrivateGroup, $wgCreateWikiDatabase;

Expand Down Expand Up @@ -398,7 +294,8 @@ public static function onCreateWikiStatePrivate( $dbname ) {
);
}

ManageWikiCDB::changes( 'permissions' );
$cWJ = new CreateWikiJson( $dbname );
$cWJ->resetWiki();
}

public static function onCreateWikiStatePublic( $dbname ) {
Expand Down Expand Up @@ -443,7 +340,8 @@ public static function onCreateWikiStatePublic( $dbname ) {
}
}

ManageWikiCDB::changes( 'permissions' );
$cWJ = new CreateWikiJson( $dbname );
$cWJ->resetWiki();
}

public static function fnNewSidebarItem( $skin, &$bar ) {
Expand Down
10 changes: 2 additions & 8 deletions includes/formFactory/ManageWikiFormFactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,9 +840,8 @@ public static function submissionHandler(
return [ 'Error processing.' ];
}

if ( $mwReturn['cdb' ] ) {
ManageWikiCDB::changes( $mwReturn['cdb'] );
}
$cWJ = new CreateWikiJson( $dbName );
$cWJ->resetWiki();

$mwLogEntry = new ManualLogEntry( 'managewiki', $mwReturn['log'] );
$mwLogEntry->setPerformer( $context->getUser() );
Expand Down Expand Up @@ -964,7 +963,6 @@ private static function submissionCore(
];

return [
'cdb' => false,
'changes' => implode( ', ', $changedArray ),
'data' => $data,
'errors' => false,
Expand Down Expand Up @@ -1019,7 +1017,6 @@ private static function submissionExtensions(
$extensionsArray[] = 'zzzz';

return [
'cdb' => false,
'changes' => implode( ', ', $changedExtensions ),
'data' => implode( ',', $extensionsArray ),
'errors' => $errors,
Expand Down Expand Up @@ -1094,7 +1091,6 @@ private static function submissionSettings(
}

return [
'cdb' => false,
'changes' => implode( ', ', $changedSettings ),
'data' => json_encode( $settingsArray ),
'errors' => $errors,
Expand Down Expand Up @@ -1177,7 +1173,6 @@ private static function submissionNamespaces(
}

return [
'cdb' => 'namespaces',
'changes' => $existingNamespace,
'data' => $build,
'errors' => $errors,
Expand Down Expand Up @@ -1312,7 +1307,6 @@ private static function submissionPermissions(
}

return [
'cdb' => 'permissions',
'changes' => $logBuild,
'data' => $dataArray,
'errors' => false,
Expand Down
Loading

0 comments on commit f914eb5

Please sign in to comment.