Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-13-2011, 11:54 PM
fotografik fotografik is offline
 
Join Date: Jun 2006
Posts: 24
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default SQL query to delete secondary usergroup

Due to some recent changes in usergroups, some members inadvertently ended up with their PRIMARY and SECONDARY usergroups being duplicated.

This is causing issues when these members request for a new password and the systems throws them into a loop where they are unable to get their primary usergroup changed from Email Verification back to their normal usergroup as the system already has them tagged to the secondary usergroup.

Anyway, i am looking for a SQL query to manually delete the secondary usergroup of the affected members (about 10K of our 130K members) which will be too much to do manually.

Thanks!
Reply With Quote
  #2  
Old 11-17-2011, 10:03 PM
fotografik fotografik is offline
 
Join Date: Jun 2006
Posts: 24
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Anyone? Help? Please??
Reply With Quote
  #3  
Old 11-17-2011, 11:15 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't know how to do it in SQL, but you could use this script:

PHP Code:
<?php
    
    
require_once("global.php");
    
    
// remove this group from users who have this group as a membergroup (secondary group)
    
$usergroupid X// change to group id to remove
    
$casesqlm $casesqli '';
    
$updateusersm $updateusersi = array();

    echo (
"Removing users from secondary group id=" $usergroupid "<BR/><BR/>\r\n");
    
flush();
    
$users $db->query_read("
        SELECT userid, username, membergroupids, infractiongroupids
        FROM " 
TABLE_PREFIX "user
        WHERE FIND_IN_SET('" 
$usergroupid "', membergroupids)
    "
);
    
    if (
$db->num_rows($users))
    {
        while(
$user $db->fetch_array($users))
        {
            if (!empty(
$user['membergroupids']))
            {
                
$membergroups fetch_membergroupids_array($userfalse);
                foreach(
$membergroups AS $key => $val)
                {
                    if (
$val == $usergroupid)
                    {
                        unset(
$membergroups["$key"]);
                    }
                }
                
$user['membergroupids'] = implode(','$membergroups);
                
$casesqlm .= "WHEN $user[userid] THEN '$user[membergroupids]' ";
                
$updateusersm[] = $user['userid'];
            }
        }

        
// do big update to get rid of this usergroup from matched members' membergroupids
        
if (!empty($casesqlm))
        {
            
$db->query_write("
                UPDATE " 
TABLE_PREFIX "user SET
                membergroupids = CASE userid
                
$casesqlm
                ELSE '' END
                WHERE userid IN(" 
implode(','$updateusersm) . ")
            "
);
        }
}

die(
"Done.");
First you probably want to make a backup of your database, or at least the user table. And you might want to close your forum while you run this.

Next, create a new php file (like maybe removegroup.php) and put in the above code. Then change this line

PHP Code:
    $usergroupid X// change to group id to remove 

to change X to the group id your want to remove. Then run the script (go to it with your browser).

When you're done you'll probably want to remove it.

BTW, this code comes from admincp/usergroup.php where it's used to remove users from a group when the group is being deleted.
Reply With Quote
  #4  
Old 11-17-2011, 11:27 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I was just about to start a script to do this but kh99 beat me to it.

One thing I would suggest is place that entire script inside a conditional so it runs for the admin only and then it can be a plugin in showthread start

PHP Code:
if($vbulletin->userinfo['userid'] == 1)
{

   
SCRIPT


I do this whenever I have to do any DB changes. It saves having to create a new file with all the standard requires.
Reply With Quote
Благодарность от:
kh99
  #5  
Old 11-17-2011, 11:34 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by nerbert View Post
I was just about to start a script to do this but kh99 beat me to it.

One thing I would suggest is place that entire script inside a conditional so it runs for the admin only and then it can be a plugin in showthread start ...
lol...it seems to happen a lot, either no one's doing it or two of us are. Anyway, I used to use hook misc_start for these things and pick a value for "do", but then someone pointed out that you really only need to include global.php, so that seemed easy enough. But that's a good point, if someone prefers to do it via plugin, that has it's advantages.
Reply With Quote
  #6  
Old 11-21-2011, 05:11 AM
fotografik fotografik is offline
 
Join Date: Jun 2006
Posts: 24
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the replies - not exactly what I was looking for and will have to discuss with the other SysAdmin who is a little more well-versed than I am with MySQL.
Reply With Quote
  #7  
Old 11-21-2011, 05:42 AM
TheLastSuperman's Avatar
TheLastSuperman TheLastSuperman is offline
Senior Member
 
Join Date: Sep 2008
Location: North Carolina
Posts: 5,844
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I have not tested this but you can try:

Code:
UPDATE user SET membergroupids = REPLACE(membergroupids,'2','');
So based on that, it reads:

Update all users, set their member group id (secondary usergroup) to "Blank" or '' where it currently is 2 i.e. Registered User/Member.

Now go through and do this for all the secondary usergroups listed in the usergroup manager so if you have a custom usergroup none of us have lets call it Main VIP which is usergroupid #29 you simply use this now:

Code:
UPDATE user SET membergroupids = REPLACE(membergroupids,'29','');
^ This should clear out all the secondary usergroups yet leave the primary remaining. If your asking how to do this query be sure to run a backup beforehand just to be safe.

Edit: Ahh I had to install a staging site on my localhost tonight regardless so tested and it works .
Reply With Quote
  #8  
Old 11-21-2011, 12:46 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm not clear on something: Is "Email Verification" a custom usergroup or do you mean "Users Awaiting Email Confirmation" (group 3)? Are several usergroups duplicated in secondary usergroups?
Reply With Quote
  #9  
Old 11-21-2011, 01:01 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by TheLastSuperman View Post
I have not tested this but you can try:...
Good job - I thought about ways to do it but couldn't come up with any. Since that text field can be a comma-separated list if the user belongs to more than one secondary group, I was thinking of handling a possible comma. But the code that reads and explodes that field is pretty forgiving [S]so I think your solution works.[/S]. I changed my mind...see the later posts.

Also, I believe he only wanted to remove on secondary user group, where it duplicates the primary group.
Reply With Quote
  #10  
Old 11-21-2011, 01:07 PM
nerbert nerbert is offline
 
Join Date: May 2008
Posts: 784
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
Good job - I thought about ways to do it but couldn't come up with any. Since that text field can be a comma-separated list if the user belongs to more than one secondary group, I was thinking of handling a possible comma. But the code that reads and explodes that field is pretty forgiving so I think your solution works.
Wouldn't it leave a double comma where the number was excised?
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:38 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05691 seconds
  • Memory Usage 2,282KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (2)bbcode_code
  • (3)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (1)post_thanks_box_bit
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete