Skip to content

Instantly share code, notes, and snippets.

@athoune
Created February 24, 2015 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athoune/a39ae465c86d92e3e01b to your computer and use it in GitHub Desktop.
Save athoune/a39ae465c86d92e3e01b to your computer and use it in GitHub Desktop.
Reading wp options from sql dump
#!/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