Ejemplo n.º 1
0
def guest_user_garbage_collector():
    """Session Garbage Collector

    program flow/tasks:
    1: delete expired sessions
    1b:delete guest users without session
    2: delete queries not attached to any user
    3: delete baskets not attached to any user
    4: delete alerts not attached to any user
    5: delete expired mailcookies
    5b: delete expired not confirmed email address
    6: delete expired roles memberships

    verbose - level of program output.
              0 - nothing
              1 - default
              9 - max, debug"""

    # dictionary used to keep track of number of deleted entries
    delcount = {
        'session': 0,
        'user': 0,
        'user_query': 0,
        'query': 0,
        'bskBASKET': 0,
        'user_bskBASKET': 0,
        'bskREC': 0,
        'bskRECORDCOMMENT': 0,
        'bskEXTREC': 0,
        'bskEXTFMT': 0,
        'user_query_basket': 0,
        'mail_cookie': 0,
        'email_addresses': 0,
        'role_membership': 0
    }

    write_message("CLEANING OF GUEST SESSIONS STARTED")

    # 1 - DELETE EXPIRED SESSIONS
    write_message("- deleting expired sessions")
    timelimit = convert_datestruct_to_datetext(time.gmtime())
    write_message("  DELETE FROM session WHERE"
                  " session_expiry < %s \n" % (timelimit, ),
                  verbose=9)
    delcount['session'] += run_sql(
        "DELETE FROM session WHERE"
        " session_expiry < %s "
        "", (timelimit, ))

    # 1b - DELETE GUEST USERS WITHOUT SESSION
    write_message("- deleting guest users without session")

    # get uids
    write_message(
        """  SELECT u.id\n  FROM user AS u LEFT JOIN session AS s\n  ON u.id = s.uid\n  WHERE s.uid IS NULL AND u.email = ''""",
        verbose=9)

    result = run_sql("""SELECT u.id
    FROM user AS u LEFT JOIN session AS s
    ON u.id = s.uid
    WHERE s.uid IS NULL AND u.email = ''""")
    write_message(result, verbose=9)

    if result:
        # work on slices of result list in case of big result
        for i in range(0, len(result), CFG_MYSQL_ARGUMENTLIST_SIZE):
            # create string of uids
            uidstr = ''
            for (id_user, ) in result[i:i + CFG_MYSQL_ARGUMENTLIST_SIZE]:
                if uidstr: uidstr += ','
                uidstr += "%s" % (id_user, )

            # delete users
            write_message(
                "  DELETE FROM user WHERE"
                " id IN (TRAVERSE LAST RESULT) AND email = '' \n",
                verbose=9)
            delcount['user'] += run_sql("DELETE FROM user WHERE"
                                        " id IN (%s) AND email = ''" %
                                        (uidstr, ))

    # 2 - DELETE QUERIES NOT ATTACHED TO ANY USER

    # first step, delete from user_query
    write_message("- deleting user_queries referencing" " non-existent users")

    # find user_queries referencing non-existent users
    write_message(
        "  SELECT DISTINCT uq.id_user\n"
        "  FROM user_query AS uq LEFT JOIN user AS u\n"
        "  ON uq.id_user = u.id\n  WHERE u.id IS NULL",
        verbose=9)
    result = run_sql("""SELECT DISTINCT uq.id_user
        FROM user_query AS uq LEFT JOIN user AS u
        ON uq.id_user = u.id
        WHERE u.id IS NULL""")
    write_message(result, verbose=9)

    # delete in user_query one by one
    write_message(
        "  DELETE FROM user_query WHERE"
        " id_user = '******' \n",
        verbose=9)
    for (id_user, ) in result:
        delcount['user_query'] += run_sql("""DELETE FROM user_query
            WHERE id_user = %s""" % (id_user, ))

    # delete the actual queries
    write_message("- deleting queries not attached to any user")

    # select queries that must be deleted
    write_message(
        """  SELECT DISTINCT q.id\n  FROM query AS q LEFT JOIN user_query AS uq\n  ON uq.id_query = q.id\n  WHERE uq.id_query IS NULL AND\n  q.type <> 'p' """,
        verbose=9)
    result = run_sql("""SELECT DISTINCT q.id
                        FROM query AS q LEFT JOIN user_query AS uq
                        ON uq.id_query = q.id
                        WHERE uq.id_query IS NULL AND
                              q.type <> 'p'""")
    write_message(result, verbose=9)

    # delete queries one by one
    write_message(
        """  DELETE FROM query WHERE id = 'TRAVERSE LAST RESULT \n""",
        verbose=9)
    for (id_user, ) in result:
        delcount['query'] += run_sql("""DELETE FROM query WHERE id = %s""",
                                     (id_user, ))

    # 3 - DELETE BASKETS NOT OWNED BY ANY USER
    write_message("- deleting baskets not owned by any user")

    # select basket ids
    write_message(
        """ SELECT ub.id_bskBASKET\n  FROM user_bskBASKET AS ub LEFT JOIN user AS u\n  ON u.id = ub.id_user\n  WHERE u.id IS NULL""",
        verbose=9)
    try:
        result = run_sql("""SELECT ub.id_bskBASKET
                              FROM user_bskBASKET AS ub LEFT JOIN user AS u
                                ON u.id = ub.id_user
                             WHERE u.id IS NULL""")
    except:
        result = []
    write_message(result, verbose=9)

    # delete from user_basket and basket one by one
    write_message(
        """  DELETE FROM user_bskBASKET WHERE id_bskBASKET = 'TRAVERSE LAST RESULT' """,
        verbose=9)
    write_message(
        """  DELETE FROM bskBASKET WHERE id = 'TRAVERSE LAST RESULT' """,
        verbose=9)
    write_message(
        """  DELETE FROM bskREC WHERE id_bskBASKET = 'TRAVERSE LAST RESULT'""",
        verbose=9)
    write_message(
        """  DELETE FROM bskRECORDCOMMENT WHERE id_bskBASKET = 'TRAVERSE LAST RESULT' \n""",
        verbose=9)
    for (id_basket, ) in result:
        delcount['user_bskBASKET'] += run_sql(
            """DELETE FROM user_bskBASKET WHERE id_bskBASKET = %s""",
            (id_basket, ))
        delcount['bskBASKET'] += run_sql(
            """DELETE FROM bskBASKET WHERE id = %s""", (id_basket, ))
        delcount['bskREC'] += run_sql(
            """DELETE FROM bskREC WHERE id_bskBASKET = %s""", (id_basket, ))
        delcount['bskRECORDCOMMENT'] += run_sql(
            """DELETE FROM bskRECORDCOMMENT WHERE id_bskBASKET = %s""",
            (id_basket, ))
    write_message(
        """ SELECT DISTINCT ext.id, rec.id_bibrec_or_bskEXTREC FROM bskEXTREC AS ext \nLEFT JOIN bskREC AS rec ON ext.id=-rec.id_bibrec_or_bskEXTREC WHERE id_bibrec_or_bskEXTREC is NULL""",
        verbose=9)
    try:
        result = run_sql("""SELECT DISTINCT ext.id FROM bskEXTREC AS ext
                            LEFT JOIN bskREC AS rec ON ext.id=-rec.id_bibrec_or_bskEXTREC
                            WHERE id_bibrec_or_bskEXTREC is NULL""")
    except:
        result = []
    write_message(result, verbose=9)
    write_message(
        """  DELETE FROM bskEXTREC WHERE id = 'TRAVERSE LAST RESULT' """,
        verbose=9)
    write_message(
        """  DELETE FROM bskEXTFMT WHERE id_bskEXTREC = 'TRAVERSE LAST RESULT' \n""",
        verbose=9)
    for (id_basket, ) in result:
        delcount['bskEXTREC'] += run_sql(
            """DELETE FROM bskEXTREC WHERE id=%s""", (id_basket, ))
        delcount['bskEXTFMT'] += run_sql(
            """DELETE FROM bskEXTFMT WHERE id_bskEXTREC=%s""", (id_basket, ))

    # 4 - DELETE ALERTS NOT OWNED BY ANY USER
    write_message('- deleting alerts not owned by any user')

    # select user ids in uqb that reference non-existent users
    write_message(
        """SELECT DISTINCT uqb.id_user FROM user_query_basket AS uqb LEFT JOIN user AS u ON uqb.id_user = u.id WHERE u.id IS NULL""",
        verbose=9)
    result = run_sql(
        """SELECT DISTINCT uqb.id_user FROM user_query_basket AS uqb LEFT JOIN user AS u ON uqb.id_user = u.id WHERE u.id IS NULL"""
    )
    write_message(result, verbose=9)

    # delete all these entries
    for (id_user, ) in result:
        write_message(
            """DELETE FROM user_query_basket WHERE id_user = '******'user_query_basket'] += run_sql(
            """DELETE FROM user_query_basket WHERE id_user = %s """,
            (id_user, ))

    # 5 - delete expired mailcookies
    write_message("""mail_cookie_gc()""", verbose=9)
    delcount['mail_cookie'] = mail_cookie_gc()

    ## 5b - delete expired not confirmed email address
    write_message(
        """DELETE FROM user WHERE note='2' AND NOW()>ADDTIME(last_login, '%s 0:0:0')"""
        % CFG_WEBSESSION_NOT_CONFIRMED_EMAIL_ADDRESS_EXPIRE_IN_DAYS,
        verbose=9)
    delcount['email_addresses'] = run_sql(
        """DELETE FROM user WHERE note='2' AND NOW()>ADDTIME(last_login, '%s 0:0:0')""",
        (CFG_WEBSESSION_NOT_CONFIRMED_EMAIL_ADDRESS_EXPIRE_IN_DAYS, ))

    # 6 - delete expired roles memberships
    write_message("""DELETE FROM user_accROLE WHERE expiration<NOW()""",
                  verbose=9)
    delcount['role_membership'] = run_sql(
        """DELETE FROM user_accROLE WHERE expiration<NOW()""")

    # print STATISTICS
    write_message("""- statistics about deleted data: """)
    write_message("""  %7s sessions.""" % (delcount['session'], ))
    write_message("""  %7s users.""" % (delcount['user'], ))
    write_message("""  %7s user_queries.""" % (delcount['user_query'], ))
    write_message("""  %7s queries.""" % (delcount['query'], ))
    write_message("""  %7s baskets.""" % (delcount['bskBASKET'], ))
    write_message("""  %7s user_baskets.""" % (delcount['user_bskBASKET'], ))
    write_message("""  %7s basket_records.""" % (delcount['bskREC'], ))
    write_message("""  %7s basket_external_records.""" %
                  (delcount['bskEXTREC'], ))
    write_message("""  %7s basket_external_formats.""" %
                  (delcount['bskEXTFMT'], ))
    write_message("""  %7s basket_comments.""" %
                  (delcount['bskRECORDCOMMENT'], ))
    write_message("""  %7s user_query_baskets.""" %
                  (delcount['user_query_basket'], ))
    write_message("""  %7s mail_cookies.""" % (delcount['mail_cookie'], ))
    write_message("""  %7s non confirmed email addresses.""" %
                  delcount['email_addresses'])
    write_message("""  %7s role_memberships.""" %
                  (delcount['role_membership'], ))
    write_message("""CLEANING OF GUEST SESSIONS FINISHED""")
Ejemplo n.º 2
0
def guest_user_garbage_collector():
    """Session Garbage Collector

    program flow/tasks:
    1: delete expired sessions
    1b:delete guest users without session
    2: delete queries not attached to any user
    3: delete baskets not attached to any user
    4: delete alerts not attached to any user
    5: delete expired mailcookies
    5b: delete expired not confirmed email address
    6: delete expired roles memberships

    verbose - level of program output.
              0 - nothing
              1 - default
              9 - max, debug"""

    # dictionary used to keep track of number of deleted entries
    delcount = {'session': 0,
                'user': 0,
                'user_query': 0,
                'query': 0,
                'bskBASKET': 0,
                'user_bskBASKET': 0,
                'bskREC': 0,
                'bskRECORDCOMMENT': 0,
                'bskEXTREC': 0,
                'bskEXTFMT': 0,
                'user_query_basket': 0,
                'mail_cookie': 0,
                'email_addresses': 0,
                'role_membership' : 0}

    write_message("CLEANING OF GUEST SESSIONS STARTED")

    # 1 - DELETE EXPIRED SESSIONS
    write_message("- deleting expired sessions")
    timelimit = convert_datestruct_to_datetext(time.gmtime())
    write_message("  DELETE FROM session WHERE"
        " session_expiry < %s \n" % (timelimit,), verbose=9)
    delcount['session'] += run_sql("DELETE FROM session WHERE"
        " session_expiry < %s """, (timelimit,))


    # 1b - DELETE GUEST USERS WITHOUT SESSION
    write_message("- deleting guest users without session")

    # get uids
    write_message("""  SELECT u.id\n  FROM user AS u LEFT JOIN session AS s\n  ON u.id = s.uid\n  WHERE s.uid IS NULL AND u.email = ''""", verbose=9)

    result = run_sql("""SELECT u.id
    FROM user AS u LEFT JOIN session AS s
    ON u.id = s.uid
    WHERE s.uid IS NULL AND u.email = ''""")
    write_message(result, verbose=9)

    if result:
        # work on slices of result list in case of big result
        for i in range(0, len(result), CFG_MYSQL_ARGUMENTLIST_SIZE):
            # create string of uids
            uidstr = ''
            for (id_user,) in result[i:i + CFG_MYSQL_ARGUMENTLIST_SIZE]:
                if uidstr: uidstr += ','
                uidstr += "%s" % (id_user,)

            # delete users
            write_message("  DELETE FROM user WHERE"
                " id IN (TRAVERSE LAST RESULT) AND email = '' \n", verbose=9)
            delcount['user'] += run_sql("DELETE FROM user WHERE"
                " id IN (%s) AND email = ''" % (uidstr,))


    # 2 - DELETE QUERIES NOT ATTACHED TO ANY USER

    # first step, delete from user_query
    write_message("- deleting user_queries referencing"
        " non-existent users")

    # find user_queries referencing non-existent users
    write_message("  SELECT DISTINCT uq.id_user\n"
        "  FROM user_query AS uq LEFT JOIN user AS u\n"
        "  ON uq.id_user = u.id\n  WHERE u.id IS NULL", verbose=9)
    result = run_sql("""SELECT DISTINCT uq.id_user
        FROM user_query AS uq LEFT JOIN user AS u
        ON uq.id_user = u.id
        WHERE u.id IS NULL""")
    write_message(result, verbose=9)


    # delete in user_query one by one
    write_message("  DELETE FROM user_query WHERE"
        " id_user = '******' \n", verbose=9)
    for (id_user,) in result:
        delcount['user_query'] += run_sql("""DELETE FROM user_query
            WHERE id_user = %s""" % (id_user,))

    # delete the actual queries
    write_message("- deleting queries not attached to any user")

    # select queries that must be deleted
    write_message("""  SELECT DISTINCT q.id\n  FROM query AS q LEFT JOIN user_query AS uq\n  ON uq.id_query = q.id\n  WHERE uq.id_query IS NULL AND\n  q.type <> 'p' """, verbose=9)
    result = run_sql("""SELECT DISTINCT q.id
                        FROM query AS q LEFT JOIN user_query AS uq
                        ON uq.id_query = q.id
                        WHERE uq.id_query IS NULL AND
                              q.type <> 'p'""")
    write_message(result, verbose=9)

    # delete queries one by one
    write_message("""  DELETE FROM query WHERE id = 'TRAVERSE LAST RESULT \n""", verbose=9)
    for (id_user,) in result:
        delcount['query'] += run_sql("""DELETE FROM query WHERE id = %s""", (id_user,))


    # 3 - DELETE BASKETS NOT OWNED BY ANY USER
    write_message("- deleting baskets not owned by any user")

    # select basket ids
    write_message(""" SELECT ub.id_bskBASKET\n  FROM user_bskBASKET AS ub LEFT JOIN user AS u\n  ON u.id = ub.id_user\n  WHERE u.id IS NULL""", verbose=9)
    try:
        result = run_sql("""SELECT ub.id_bskBASKET
                              FROM user_bskBASKET AS ub LEFT JOIN user AS u
                                ON u.id = ub.id_user
                             WHERE u.id IS NULL""")
    except:
        result = []
    write_message(result, verbose=9)

    # delete from user_basket and basket one by one
    write_message("""  DELETE FROM user_bskBASKET WHERE id_bskBASKET = 'TRAVERSE LAST RESULT' """, verbose=9)
    write_message("""  DELETE FROM bskBASKET WHERE id = 'TRAVERSE LAST RESULT' """, verbose=9)
    write_message("""  DELETE FROM bskREC WHERE id_bskBASKET = 'TRAVERSE LAST RESULT'""", verbose=9)
    write_message("""  DELETE FROM bskRECORDCOMMENT WHERE id_bskBASKET = 'TRAVERSE LAST RESULT' \n""", verbose=9)
    for (id_basket,) in result:
        delcount['user_bskBASKET'] += run_sql("""DELETE FROM user_bskBASKET WHERE id_bskBASKET = %s""", (id_basket,))
        delcount['bskBASKET'] += run_sql("""DELETE FROM bskBASKET WHERE id = %s""", (id_basket,))
        delcount['bskREC'] += run_sql("""DELETE FROM bskREC WHERE id_bskBASKET = %s""", (id_basket,))
        delcount['bskRECORDCOMMENT'] += run_sql("""DELETE FROM bskRECORDCOMMENT WHERE id_bskBASKET = %s""", (id_basket,))
    write_message(""" SELECT DISTINCT ext.id, rec.id_bibrec_or_bskEXTREC FROM bskEXTREC AS ext \nLEFT JOIN bskREC AS rec ON ext.id=-rec.id_bibrec_or_bskEXTREC WHERE id_bibrec_or_bskEXTREC is NULL""", verbose=9)
    try:
        result = run_sql("""SELECT DISTINCT ext.id FROM bskEXTREC AS ext
                            LEFT JOIN bskREC AS rec ON ext.id=-rec.id_bibrec_or_bskEXTREC
                            WHERE id_bibrec_or_bskEXTREC is NULL""")
    except:
        result = []
    write_message(result, verbose=9)
    write_message("""  DELETE FROM bskEXTREC WHERE id = 'TRAVERSE LAST RESULT' """, verbose=9)
    write_message("""  DELETE FROM bskEXTFMT WHERE id_bskEXTREC = 'TRAVERSE LAST RESULT' \n""", verbose=9)
    for (id_basket,) in result:
        delcount['bskEXTREC'] += run_sql("""DELETE FROM bskEXTREC WHERE id=%s""", (id_basket,))
        delcount['bskEXTFMT'] += run_sql("""DELETE FROM bskEXTFMT WHERE id_bskEXTREC=%s""", (id_basket,))

    # 4 - DELETE ALERTS NOT OWNED BY ANY USER
    write_message('- deleting alerts not owned by any user')

    # select user ids in uqb that reference non-existent users
    write_message("""SELECT DISTINCT uqb.id_user FROM user_query_basket AS uqb LEFT JOIN user AS u ON uqb.id_user = u.id WHERE u.id IS NULL""", verbose=9)
    result = run_sql("""SELECT DISTINCT uqb.id_user FROM user_query_basket AS uqb LEFT JOIN user AS u ON uqb.id_user = u.id WHERE u.id IS NULL""")
    write_message(result, verbose=9)

    # delete all these entries
    for (id_user,) in result:
        write_message("""DELETE FROM user_query_basket WHERE id_user = '******'user_query_basket'] += run_sql("""DELETE FROM user_query_basket WHERE id_user = %s """, (id_user,))

    # 5 - delete expired mailcookies
    write_message("""mail_cookie_gc()""", verbose=9)
    delcount['mail_cookie'] = mail_cookie_gc()

    ## 5b - delete expired not confirmed email address
    write_message("""DELETE FROM user WHERE note='2' AND NOW()>ADDTIME(last_login, '%s 0:0:0')""" % CFG_WEBSESSION_NOT_CONFIRMED_EMAIL_ADDRESS_EXPIRE_IN_DAYS, verbose=9)
    delcount['email_addresses'] = run_sql("""DELETE FROM user WHERE note='2' AND NOW()>ADDTIME(last_login, '%s 0:0:0')""", (CFG_WEBSESSION_NOT_CONFIRMED_EMAIL_ADDRESS_EXPIRE_IN_DAYS,))

    # 6 - delete expired roles memberships
    write_message("""DELETE FROM user_accROLE WHERE expiration<NOW()""", verbose=9)
    delcount['role_membership'] = run_sql("""DELETE FROM user_accROLE WHERE expiration<NOW()""")

    # print STATISTICS
    write_message("""- statistics about deleted data: """)
    write_message("""  %7s sessions.""" % (delcount['session'],))
    write_message("""  %7s users.""" % (delcount['user'],))
    write_message("""  %7s user_queries.""" % (delcount['user_query'],))
    write_message("""  %7s queries.""" % (delcount['query'],))
    write_message("""  %7s baskets.""" % (delcount['bskBASKET'],))
    write_message("""  %7s user_baskets.""" % (delcount['user_bskBASKET'],))
    write_message("""  %7s basket_records.""" % (delcount['bskREC'],))
    write_message("""  %7s basket_external_records.""" % (delcount['bskEXTREC'],))
    write_message("""  %7s basket_external_formats.""" % (delcount['bskEXTFMT'],))
    write_message("""  %7s basket_comments.""" % (delcount['bskRECORDCOMMENT'],))
    write_message("""  %7s user_query_baskets.""" % (delcount['user_query_basket'],))
    write_message("""  %7s mail_cookies.""" % (delcount['mail_cookie'],))
    write_message("""  %7s non confirmed email addresses.""" % delcount['email_addresses'])
    write_message("""  %7s role_memberships.""" % (delcount['role_membership'],))
    write_message("""CLEANING OF GUEST SESSIONS FINISHED""")