Created
February 24, 2015 09:45
-
-
Save athoune/a39ae465c86d92e3e01b to your computer and use it in GitHub Desktop.
Reading wp options from sql dump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sqlparse | |
from sqlparse.sql import Comment, Identifier, Parenthesis | |
from phpserialize import loads, phpobject | |
f = open('dump-no-contents.sql', 'r') | |
for line in sqlparse.split(f): | |
p = sqlparse.parse(line)[0] | |
if type(p.tokens[0]) == Comment: | |
continue | |
if p.get_type() == u'INSERT': | |
if type(p.tokens[4]) == Identifier: | |
if p.tokens[4].value[1:-1] == 'wp_options': | |
for t in p.tokens[4:]: | |
if type(t) != Parenthesis: | |
continue | |
triplet = [tt.value for tt in t.tokens[1].tokens | |
if tt.ttype[0] == "Literal"] | |
v = triplet[2][1:-1] | |
try: | |
v = loads(v.replace('\\"', '"'), object_hook=phpobject) | |
if type(v) == phpobject: | |
v = v._asdict() | |
except ValueError: | |
pass | |
print triplet[0], triplet[1] | |
print v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment