-
Notifications
You must be signed in to change notification settings - Fork 33
/
scanLinks.py
174 lines (129 loc) · 4.15 KB
/
scanLinks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python
'''
Copyright (C) 2010 Cagatay Calli <[email protected]>
Scans XML output (gum.xml) from Wikiprep, creates 3 tables:
TABLE: pagelinks COLUMNS: source_id INT, target_id INT
TABLE: inlinks COLUMNS: target_id INT, inlink INT
TABLE: outlinks COLUMNS: source_id INT, outlink INT
TABLE: namespace COLUMNS: id INT
USAGE: scanData.py <hgw.xml file from Wikiprep>
IMPORTANT: If you use XML output from a recent version of Wikiprep
(e.g. Zemanta fork), then set FORMAT to 'Zemanta-legacy' or 'Zemanta-modern'.
'''
import sys
import re
import MySQLdb
import signal
import xmlwikiprep
LINK_LOAD_THRES = 10000
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='123456',db='wiki',charset = "utf8", use_unicode = True)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
try:
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS namespace")
cursor.execute("""
CREATE TABLE namespace
(
id INT(10),
KEY (id)
) DEFAULT CHARSET=binary
""")
cursor.execute("DROP TABLE IF EXISTS pagelinks")
cursor.execute("""
CREATE TABLE pagelinks
(
source_id INT(10),
target_id INT(10),
KEY (source_id),
KEY (target_id)
) DEFAULT CHARSET=binary
""")
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
## handler for SIGTERM ###
def signalHandler(signum, frame):
global conn, cursor
cursor.close()
conn.close()
sys.exit(1)
signal.signal(signal.SIGTERM, signalHandler)
#####
reOtherNamespace = re.compile("^(User|Wikipedia|File|MediaWiki|Template|Help|Category|Portal|Book|Talk|Special|Media|WP|User talk|Wikipedia talk|File talk|MediaWiki talk|Template talk|Help talk|Category talk|Portal talk):.+",re.DOTALL)
linkBuffer = []
linkBuflen = 0
nsBuffer = []
nsBuflen = 0
mainNS = []
# pageContent - <page>..content..</page>
# pageDict - stores page attribute dict
def recordArticle(pageDoc):
global linkBuffer, linkBuflen, nsBuffer, nsBuflen
# a simple check for content
if pageDoc['length'] < 10:
return
_id = pageDoc['_id']
# only keep articles of Main namespace
if reOtherNamespace.match(pageDoc['title']):
return
nsBuffer.append((_id))
nsBuflen += 1
if nsBuflen >= LINK_LOAD_THRES:
cursor.executemany("""
INSERT INTO namespace (id)
VALUES (%s)
""",nsBuffer)
nsBuffer = []
nsBuflen = 0
# write links
for l in pageDoc['links']:
linkBuffer.append((_id,l)) # source, target
linkBuflen += 1
if linkBuflen >= LINK_LOAD_THRES:
cursor.executemany("""
INSERT INTO pagelinks (source_id,target_id)
VALUES (%s,%s)
""",linkBuffer)
linkBuffer = []
linkBuflen = 0
return
args = sys.argv[1:]
# scanData.py <hgw_file>
if len(args) < 1:
sys.exit()
f = open(args[0],'r')
for doc in xmlwikiprep.read(f):
recordArticle(doc)
f.close()
if nsBuflen > 0:
cursor.executemany("""
INSERT INTO namespace (id)
VALUES (%s)
""",nsBuffer)
nsBuffer = []
nsBuflen = 0
if linkBuflen > 0:
cursor.executemany("""
INSERT INTO pagelinks (source_id,target_id)
VALUES (%s,%s)
""",linkBuffer)
linkBuffer = []
linkBuflen = 0
cursor.execute("DROP TABLE IF EXISTS tmppagelinks")
cursor.execute("CREATE TABLE tmppagelinks LIKE pagelinks")
cursor.execute("INSERT tmppagelinks SELECT p.* FROM pagelinks p WHERE EXISTS (SELECT * FROM namespace n WHERE p.target_id = n.id)")
cursor.execute("DROP TABLE pagelinks")
cursor.execute("RENAME TABLE tmppagelinks TO pagelinks")
# inlinks
cursor.execute("DROP TABLE IF EXISTS inlinks")
cursor.execute("CREATE TABLE inlinks AS SELECT p.target_id, COUNT(p.source_id) AS inlink FROM pagelinks p GROUP BY p.target_id")
cursor.execute("CREATE INDEX idx_target_id ON inlinks (target_id)")
# outlinks
cursor.execute("DROP TABLE IF EXISTS outlinks")
cursor.execute("CREATE TABLE outlinks AS SELECT p.source_id, COUNT(p.target_id) AS outlink FROM pagelinks p GROUP BY p.source_id")
cursor.execute("CREATE INDEX idx_source_id ON outlinks (source_id)")
cursor.close()
conn.close()