2 * An XML parser for Wikipedia Data dumps.
3 * Converts XML files to tab-separated values files readable by spreadsheets
4 * and statistical packages.
16 // timestamp of the form 2003-11-07T00:43:23Z
17 #define DATE_LENGTH 10
19 #define TIMESTAMP_LENGTH 20
21 #define MEGABYTE 1048576
22 #define FIELD_BUFFER_SIZE 1024
23 // 2048 KB in bytes + 1
24 //#define TEXT_BUFFER_SIZE 2097153
25 //#define TEXT_BUFFER_SIZE 10485760
28 TITLE, ARTICLEID, REVISION, REVID, TIMESTAMP, CONTRIBUTOR,
29 EDITOR, EDITORID, MINOR, COMMENT, UNUSED, TEXT
32 enum block { TITLE_BLOCK, REVISION_BLOCK, CONTRIBUTOR_BLOCK, SKIP };
34 enum outtype { FULL, SIMPLE };
38 // pointers to once-allocated buffers
51 // track string size of the elements, to prevent O(N^2) processing in charhndl
52 // when we have to take strlen for every character which we append to the buffer
54 size_t articleid_size;
58 size_t timestamp_size;
67 enum elements element;
69 enum outtype output_type;
74 /* free_data and clean_data
75 * Takes a pointer to the data struct and an integer {0,1} indicating if the
76 * title data needs to be cleared as well.
77 * Also, frees memory dynamically allocated to store data.
80 clean_data(revisionData *data, int title)
82 // reset title (if we are switching articles)
84 data->title[0] = '\0';
85 data->articleid[0] = '\0';
87 data->articleid_size = 0;
91 data->revid[0] = '\0';
94 data->timestamp[0] = '\0';
96 data->editor[0] = '\0';
97 data->editorid[0] = '\0';
98 data->comment[0] = '\0';
101 // reset length tracking
102 data->revid_size = 0;
105 data->timestamp_size = 0;
107 data->editor_size = 0;
108 data->editorid_size = 0;
109 data->comment_size = 0;
112 // reset flags and element type info
114 data->element = UNUSED;
120 free_data(revisionData *data, int title)
123 //printf("freeing article\n");
125 free(data->articleid);
130 free(data->timestamp);
133 free(data->editorid);
138 void cleanup_revision(revisionData *data) {
142 void cleanup_article(revisionData *data) {
148 init_data(revisionData *data, outtype output_type)
150 data->text = (char*) malloc(4 * MEGABYTE); // 2MB is the article length limit, 4MB is 'safe'?
151 data->comment = (char*) malloc(FIELD_BUFFER_SIZE);
152 data->title = (char*) malloc(FIELD_BUFFER_SIZE);
153 data->articleid = (char*) malloc(FIELD_BUFFER_SIZE);
154 data->revid = (char*) malloc(FIELD_BUFFER_SIZE);
155 data->date = (char*) malloc(FIELD_BUFFER_SIZE);
156 data->time = (char*) malloc(FIELD_BUFFER_SIZE);
157 data->timestamp = (char*) malloc(FIELD_BUFFER_SIZE);
158 data->anon = (char*) malloc(FIELD_BUFFER_SIZE);
159 data->editor = (char*) malloc(FIELD_BUFFER_SIZE);
160 data->editorid = (char*) malloc(FIELD_BUFFER_SIZE);
163 // resets the data fields, null terminates strings, sets lengths
166 data->output_type = output_type;
169 /* for debugging only, prints out the state of the data struct
172 print_state(revisionData *data)
174 printf("element = %i\n", data->element);
175 printf("output_type = %i\n", data->output_type);
176 printf("title = %s\n", data->title);
177 printf("articleid = %s\n", data->articleid);
178 printf("revid = %s\n", data->revid);
179 printf("date = %s\n", data->date);
180 printf("time = %s\n", data->time);
181 printf("anon = %s\n", data->anon);
182 printf("editor = %s\n", data->editor);
183 printf("editorid = %s\n", data->editorid);
184 printf("minor = %s\n", (data->minor ? "1" : "0"));
185 printf("comment = %s\n", data->comment);
186 printf("text = %s\n", data->text);
191 /* Write a header for the comma-separated output
196 // printf("title, articleid, revid, date, time, anon, editor, editorid, minor, comment\n");
197 // printf("title\tarticleid\trevid\tdate time\tanon\teditor\teditorid\tminor\n");
203 * write a line of comma-separated value formatted data to standard out
205 * title,articleid,revid,date,time,anon,editor,editorid,minor,comment
206 * (str) (int) (int) (str)(str)(bin)(str) (int) (bin) (str)
208 * it is called right before cleanup_revision() and cleanup_article()
211 write_row(revisionData *data)
216 md5_byte_t digest[16];
217 char md5_hex_output[2 * 16 + 1];
219 md5_append(&state, (const md5_byte_t *)data->text, data->text_size);
220 md5_finish(&state, digest);
222 for (di = 0; di < 16; ++di) {
223 sprintf(md5_hex_output + di * 2, "%02x", digest[di]);
226 // print line of tsv output
227 printf("%s\t%s\t%s\t%s %s\t%s\t%s\t%s\t%s\t%i\t%f\t%s\n",
233 (data->editor[0] != '\0') ? "0" : "1", // anon?
236 (data->minor) ? "1" : "0",
237 (unsigned int) data->text_size,
238 shannon_H(data->text, data->text_size),
243 if (data->output_type == FULL) {
244 printf("comment:%s\ntext:\n%s\n", data->comment, data->text);
250 split_timestamp(revisionData *data)
252 char *t = data->timestamp;
253 strncpy(data->date, data->timestamp, DATE_LENGTH);
254 char *timeinstamp = &data->timestamp[DATE_LENGTH+1];
255 strncpy(data->time, timeinstamp, TIME_LENGTH);
258 // like strncat but with previously known length
260 strlcatn(char *dest, const char *src, size_t dest_len, size_t n)
262 //size_t dest_len = strlen(dest);
265 for (i = 0 ; i < n && src[i] != '\0' ; i++)
266 dest[dest_len + i] = src[i];
267 dest[dest_len + i] = '\0';
273 charhndl(void* vdata, const XML_Char* s, int len)
275 revisionData* data = (revisionData*) vdata;
276 if (data->element != UNUSED && data->position != SKIP) {
279 //t[len] = '\0'; // makes t a well-formed string
280 switch (data->element) {
282 // printf("buffer length = %i, text: %s\n", len, t);
283 strlcatn(data->text, s, data->text_size, len);
284 data->text_size += len;
287 strlcatn(data->comment, s, data->comment_size, len);
288 data->comment_size += len;
291 strlcatn(data->title, s, data->title_size, len);
292 data->title_size += len;
295 // printf("articleid = %s\n", t);
296 strlcatn(data->articleid, s, data->articleid_size, len);
297 data->articleid_size += len;
300 // printf("revid = %s\n", t);
301 strlcatn(data->revid, s, data->revid_size, len);
302 data->revid_size += len;
305 strlcatn(data->timestamp, s, data->timestamp_size, len);
306 data->timestamp_size += len;
307 if (strlen(data->timestamp) == TIMESTAMP_LENGTH)
308 split_timestamp(data);
311 strlcatn(data->editor, s, data->editor_size, len);
312 data->editor_size += len;
315 //printf("editorid = %s\n", t);
316 strlcatn(data->editorid, s, data->editorid_size, len);
317 data->editorid_size += len;
319 /* the following are implied or skipped:
321 printf("found minor element\n"); doesn't work
322 break; minor tag is just a tag
331 start(void* vdata, const XML_Char* name, const XML_Char** attr)
333 revisionData* data = (revisionData*) vdata;
335 if (strcmp(name,"title") == 0) {
336 cleanup_article(data); // cleans up data from last article
337 data->element = TITLE;
338 data->position = TITLE_BLOCK;
339 } else if (data->position != SKIP) {
340 if (strcmp(name,"revision") == 0) {
341 data->element = REVISION;
342 data->position = REVISION_BLOCK;
343 } else if (strcmp(name, "contributor") == 0) {
344 data->element = CONTRIBUTOR;
345 data->position = CONTRIBUTOR_BLOCK;
346 } else if (strcmp(name,"id") == 0)
347 switch (data->position) {
349 data->element = ARTICLEID;
352 data->element = REVID;
354 case CONTRIBUTOR_BLOCK:
355 data->element = EDITORID;
359 // minor tag has no character data, so we parse here
360 else if (strcmp(name,"minor") == 0) {
361 data->element = MINOR;
364 else if (strcmp(name,"timestamp") == 0)
365 data->element = TIMESTAMP;
367 else if (strcmp(name, "username") == 0)
368 data->element = EDITOR;
370 else if (strcmp(name,"ip") == 0)
371 data->element = EDITORID;
373 else if (strcmp(name,"comment") == 0)
374 data->element = COMMENT;
376 else if (strcmp(name,"text") == 0)
377 data->element = TEXT;
379 else if (strcmp(name,"page") == 0
380 || strcmp(name,"mediawiki") == 0
381 || strcmp(name,"restrictions") == 0
382 || strcmp(name,"siteinfo") == 0)
383 data->element = UNUSED;
390 end(void* vdata, const XML_Char* name)
392 revisionData* data = (revisionData*) vdata;
393 if (strcmp(name, "revision") == 0 && data->position != SKIP) {
394 write_row(data); // crucial... :)
395 cleanup_revision(data); // also crucial
397 data->element = UNUSED; // sets our state to "not-in-useful"
398 } // thus avoiding unpleasant character data
399 // b/w tags (newlines etc.)
402 void print_usage(char* argv[]) {
403 fprintf(stderr, "usage: <wikimedia dump xml> | %s [options]\n", argv[0]);
404 fprintf(stderr, "\n");
405 fprintf(stderr, "options:\n");
406 fprintf(stderr, " -t print text and comments after each line of tab separated data\n");
407 fprintf(stderr, "\n");
408 fprintf(stderr, "Takes a wikimedia data dump XML stream on standard in, and produces\n");
409 fprintf(stderr, "a tab-separated stream of revisions on standard out:\n");
410 fprintf(stderr, "\n");
411 fprintf(stderr, "title, articleid, revid, timestamp, anon, editor, editorid, minor, revlength, reventropy, revmd5\n");
412 fprintf(stderr, "\n");
413 fprintf(stderr, "author: Erik Garrison <erik@hypervolu.me>\n");
418 main(int argc, char *argv[])
421 enum outtype output_type;
423 // in "simple" output, we don't print text and comments
424 output_type = SIMPLE;
427 while ((c = getopt(argc, argv, "ht")) != -1)
442 if (dry_run) { // lets us print initialization options
443 printf("simple_output = %i\n", output_type);
447 // create a new instance of the expat parser
448 XML_Parser parser = XML_ParserCreate("UTF-8");
450 // initialize the user data struct which is passed to callback functions
452 // initialize the elements of the struct to default values
453 init_data(&data, output_type);
456 // makes the parser pass "data" as the first argument to every callback
457 XML_SetUserData(parser, &data);
458 void (*startFnPtr)(void*, const XML_Char*, const XML_Char**) = start;
459 void (*endFnPtr)(void*, const XML_Char*) = end;
460 void (*charHandlerFnPtr)(void*, const XML_Char*, int) = charhndl;
462 // sets start and end to be the element start and end handlers
463 XML_SetElementHandler(parser, startFnPtr, endFnPtr);
464 // sets charhndl to be the callback for character data
465 XML_SetCharacterDataHandler(parser, charHandlerFnPtr);
470 // shovel data into the parser
473 // read into buf a bufferfull of data from standard input
474 size_t len = fread(buf, 1, BUFSIZ, stdin);
475 done = len < BUFSIZ; // checks if we've got the last bufferfull
477 // passes the buffer of data to the parser and checks for error
478 // (this is where the callbacks are invoked)
479 if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
482 XML_ErrorString(XML_GetErrorCode(parser)),
483 (int) XML_GetCurrentLineNumber(parser));
489 XML_ParserFree(parser);