-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extractor.php
479 lines (449 loc) · 18.2 KB
/
Extractor.php
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
header('Content-type: text/xml');
require_once './logoot_lib/utils.php';
require_once './logootComponent/DiffEngine.php';
require_once './WikipediaReader.php';
function __autoload($classe) {
require_once './logootComponent/' . $classe . '.php';
}
//$debug = true;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Extractor
*
* @author ed
*/
class Extractor extends WikipediaReader {
protected $writer, $uri, $rep;
protected $liste_pages, $tab_pages, $tab_rang, $tab_ns, $nb_loaded;
protected $debut, $next, $fin;
protected $page_title, $page_id, $user_id, $username, $user_ip, $revision_id,
$revision_timestamp, $comment, $isMinor, $text;
protected $inPage, $inRevision, $inContributor, $toSave;
//protected $logoot;
protected $page, $namespaces;
public function __construct($rep, $wiki, $liste, $p) {
parent::__construct($wiki);
$this->rep = $rep;
$this->liste_pages = simplexml_load_file($liste);
$this->fin = false;
$this->tab_pages = array();
$this->nb_loaded = 0;
$this->page = $p;
if (!is_dir($rep)) {
mkdir($rep);
}
//collecte des pages à récupérer
foreach ($this->liste_pages->children() as $ns) {
echo "Récup de " . $ns['nom'] . "\n";
$this->namespaces[] = $ns['nom'];
$dir_ns = ($ns['nom'] == '' ? $this->rep . '/default' : $this->rep . '/' . utils::toFileName($ns['nom']));
if (!is_dir($dir_ns)) {
mkdir($dir_ns);
}
foreach ($ns->liste_patchs as $patch) {
foreach ($patch->max as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
}
foreach ($patch->random as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
}
}
foreach ($ns->liste_tailles as $patch) {
foreach ($patch->max as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
}
foreach ($patch->random as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
}
}
foreach ($ns->liste_robots as $patch) {
foreach ($patch->max as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
} foreach ($patch->random as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
}
}
foreach ($ns->liste_users as $patch) {
foreach ($patch->max as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
} foreach ($patch->random as $p) {
$this->tab_pages[] = $p['titre'];
$this->tab_rang[] = $p['rang'];
}
}
}
$this->tab_pages = array_unique($this->tab_pages);
$this->tab_rang = array_unique($this->tab_rang);
natsort($this->tab_rang);
echo "Récupération de " . count($this->tab_pages) . " page(s)\n";
}
public function __destruct() {
parent::__destruct();
}
protected function openElement($element) {
$ok = false;
switch ($element) {
case 'mediawiki' :
$this->next = array_shift($this->tab_rang);
$this->inPage = false;
$this->inRevision = false;
$this->inContributor = false;
$ok = $this->read();
break;
case 'siteinfo' : case 'namespaces' :
$ok = $this->next();
break;
case 'page' :
$this->inPage = true;
$this->nb_pages += 1;
if ($this->nb_pages == $this->next) {
$this->debut = microtime(true);
$this->writer = new XMLWriter();
$this->toSave = true;
$ok = $this->read();
} else {
$this->toSave = false;
$ok = $this->next();
}
$this->text = '';
break;
case 'title' :
if ($this->toSave) {
$this->page_title = $this->readString();
echo "==> Récupération de : $this->page_title\n";
$t = explode(':', $this->page_title);
if (count($t) > 1)
if (in_array($t[0], $this->namespaces)) {
$ns = $t[0];
} else
$ns = NULL;
else
$ns = NULL;
$dir_ns = $this->rep . (isset($ns) ? '/' . utils::toFileName($ns) : '/default');
if (isset($ns)) unset($t[0]);
$file_name = utils::toFileName(implode(':', $t));
$this->uri = $dir_ns . '/' . $file_name . '.xml';
if (strlen($this->uri) > 255) {
echo "** replace (too long) ". $this->uri;
$file_name = substr($file_name, 0, 250);
$this->uri = $dir_ns . '/' . $file_name . '.xml';
echo " by " . $this->uri . "\n" ;
}
if (file_exists($this->uri)) {
echo "** replace (exists) ". $this->uri;
$this->uri = $dir_ns . '/page_' . $this->nb_pages . '.xml';
echo " by " . $this->uri . "\n" ;
}
//$this->writer->openMemory();
$this->writer->openURI($this->uri);
$this->writer->setIndent(true);
$this->writer->startDocument('1.0', 'UTF-8');
$this->writer->writeDTD("page", null, "http://www.desmontils.net/ns/WA/page_mediawiki.dtd");
$this->writer->startElement('page');
$titre = utils::toAttribute($this->page_title);
$this->writer->writeAttribute('title', $titre);
}
$ok = $this->next();
break;
case 'id' :
if ($this->toSave) {
$id = $this->readString();
if ($this->inPage) {
if ($this->inRevision)
if ($this->inContributor) {
$this->user_id = $id;
$this->writer->writeAttribute('id', 'u' . $id);
} else {
$this->revision_id = $id;
$this->writer->writeAttribute('id', 'r' . $id);
} else {
$this->page_id = $id;
$this->writer->writeAttribute('id', 'p' . $id);
}
}
}
$ok = $this->next();
break;
case 'revision' :
$this->inRevision = true;
if ($this->toSave) {
$this->writer->startElement('revision');
}
$ok = $this->read();
break;
case 'timestamp' :
if ($this->toSave) {
$this->writer->writeAttribute('timestamp', $this->readString());
}
$ok = $this->next();
break;
case 'contributor' :
$this->inContributor = true;
if ($this->toSave) {
$this->writer->startElement('contributor');
}
$ok = $this->read();
break;
case 'username' :
if ($this->toSave) {
$this->writer->writeAttribute('name', $this->readString());
}
$ok = $this->next();
break;
case 'ip' :
if ($this->toSave) {
$ip = $this->readString();
if ($this->isIP($ip))
$this->writer->writeAttribute('ip', $ip);
else
$this->writer->writeAttribute('name', $ip);
}
$ok = $this->next();
break;
case 'comment' :
if ($this->toSave) {
$this->writer->writeElement('comment', $this->readString());
}
$ok = $this->next();
break;
case 'text' :
if ($this->toSave) {
//$this->writer->startElement('text');
//$this->writer->writeAttribute('xml:space', 'preserve');
$old = $this->text;
$this->text = $this->readString();
//$this->writer->text($this->text);
//$this->writer->endElement();
$this->writer->startElement('change-list');
$this->generate($old, $this->text);
$this->writer->endElement();
}
$ok = $this->next();
break;
default : $ok = $this->next();
}
return $ok && (!$this->fin);
}
protected function closeElement($element) {
$ok = false;
switch ($element) {
case 'page' :
if ($this->toSave) {
$this->writer->endElement(); //page
$this->writer->endDocument();
//echo "\n=====\n" . $this->writer->outputMemory();
$this->writer->flush();
$this->toSave = false;
if (count($this->tab_rang) > 0)
$this->next = array_shift($this->tab_rang);
else
$this->fin = true;
$duree = round(microtime(true) - $this->debut, 2);
echo $this->nb_pages . " " . $this->uri . " mémorisée\n";
echo $duree . ' s. - ' . memory_get_peak_usage() .
":" . memory_get_usage(true) .
'(' . memory_get_usage(false) . ")\n";
}
$this->inPage = false;
$ok = $this->next();
$this->logoot = NULL;
break;
case 'revision' :
$this->inRevision = false;
if ($this->toSave) {
$this->writer->endElement();
}
$ok = $this->next();
break;
case 'contributor' :
$this->inContributor = false;
if ($this->toSave) {
$this->writer->endElement();
}
$ok = $this->next();
break;
default : $ok = $this->next();
}
return $ok;
}
public static function main($param) {
//frwiki-head.xml frwiki-20110409-pages-meta-history.xml
if (isset($param['w']) && isset($param['l']) && isset($param['d'])) {
$ex = new Extractor($param['d'], $param['w'], $param['l'], (isset($param['p']) ? $param['p'] : NULL));
$ex->run();
}
else
echo "php Extractor.php -d rep -w wiki_dump.xml [-p page_name] -l WA_list.xml\n";
}
/**
* Calculate the diff between two texts
* Returns a list of operations applied on this blobinfo(document model)
* For each operation (insert or delete), an operation object is created
* an applied via the 'integrateBlob' function call. These objects are
* stored in an array and returned for further implementations.
*
* NB: the direct implementation is necessary because the generation of
* a new position (LogootPosition) is based on the positions of the model
* (BlobInfo) and so we have to update (immediat integration) this model after
* each operation (that we get from the difference engine)
* @global <Object> $wgContLang
* @param <String> $oldtext
* @param <String> $newtext
* @return <array> list of logootOperation
*/
protected function locate($ota, $nta) {//recherche si $ota est inclus dans $nta
$ok = false;
$in = false;
$debut = -1;
$fin = -1;
$lota = count($ota);
$lnta = count($nta);
if ($lota < $lnta) {
$oi = 0;
$ni = 0;
while ($ni + $lota - $oi <= count($nta) && (!$ok)) {
//echo $ota[$oi] . '/' . $nta[$ni] . '/' . ($in ? 'in' : 'out') . "\n";
if ($ota[$oi] == $nta[$ni]) {
if (!$in) {
$debut = $ni;
$in = true;
$fin = $ni;
} else {
$fin = $ni;
}
$oi +=1;
$ok = ($oi == $lota);
} elseif ($ota[$oi] != $nta[$ni]) {
if ($in) {
$debut = -1;
$in = false;
$fin = -1;
$oi = 0;
}
}
$ni +=1;
//echo $ni . "::" . count($nta) . "::" . ($ok ? "ok" : "!ok") . '/' . ($in ? 'in' : 'out') . "\n";
}
}
return array($ok, $debut, $fin);
}
/*
* Méthode de création des opérations
*/
protected function generate_del_line($line_nb, $line_txt) {
$this->writer->startElement('delete');
$this->writer->writeAttribute('line', $line_nb);
$this->writer->writeAttribute('txt', $line_txt);
$this->writer->endElement();
}
protected function generate_ins_line($line_nb, $line_txt) {
$this->writer->startElement('insert');
$this->writer->writeAttribute('line', $line_nb);
$this->writer->writeAttribute('txt', $line_txt);
$this->writer->endElement();
}
protected function generate_ins_text($line_nb, $txt) {
$nb = count($txt);
for ($i = 0; $i < $nb; $i++) {
$this->generate_ins_line($line_nb + $i, $txt[$i]);
}
return $nb;
}
/*
* génère les opérations effectuées entre $oldText et $newText..
*/
public function generate($oldText, $newText) {
/* explode into lines */
$ota = explode("\n", $oldText);
$nta = explode("\n", $newText);
if ((count($ota) == 1) && ($ota[0] == "")) {// c'est un nouveau document
unset($ota[0]);
$this->generate_ins_text(1, $nta);
} else {
list($trouve, $deb, $fin) = $this->locate($ota, $nta);
if ($trouve) {//il y a eu un ajout de texte au début et/ou à la fin uniquement
if ($deb > 0) {
$delta = $this->generate_ins_text(1, array_slice($nta, 0, $deb));
} else
$delta = 0;
if ($fin + 1 < count($nta)) {
$this->generate_ins_text(count($ota) + 1 + $delta, array_slice($nta, $fin + 1, count($nta) - ($fin + 1)));
}
} else {
$counter = 0;
if ((count($ota) == 1) && ($ota[0] == "")) // c'est un nouveau document
unset($ota[0]);
$diffs = new Diff1($ota, $nta);
/* convert 4 operations into 2 operations */
foreach ($diffs->edits as $operation) {
switch ($operation->type) {
case "add":
$adds = $operation->closing;
ksort($adds, SORT_NUMERIC);
foreach ($adds as $lineNb => $linetxt) {
$this->generate_ins_line($lineNb, $linetxt);
$counter += 1;
}
break;
case "delete":
foreach ($operation->orig as $lineNb => $linetxt) {
$this->generate_del_line($lineNb + $counter, $linetxt);
$counter -= 1;
}
break;
case "change":
$this->writer->startElement('change');
foreach ($operation->orig as $lineNb => $linetxt) {
$this->generate_del_line($lineNb + $counter, $linetxt);
$counter -= 1;
}
$adds1 = $operation->closing;
ksort($adds1, SORT_NUMERIC);
foreach ($adds1 as $lineNb => $linetxt) {
$this->generate_ins_line($lineNb, $linetxt);
$counter += 1;
}
$this->writer->endElement();
break;
case "copy": break;
default :;
}
}
}
}
}
}
Extractor::main(getopt("w:l:d:"));
/* $env = logootEnv::getInstance();
var_dump($env);
var_dump(LogootId::IdMax()); */
/* $this->debut = microtime(true);
$sxml = new SimpleXMLElement($this->readOuterXml());
$duree = round(microtime(true) - $this->debut, 2);
echo $duree . ' s. - ' . memory_get_peak_usage() .
":" . memory_get_usage(true) .
'(' . memory_get_usage(false) . ")\n";
//$dom = $this->expand();
$titre = (String) $sxml->title;
if (in_array($titre, $this->tab_pages)) {
$filename = 'files/' . $this->toFileName($titre) . '.xml';
echo $this->nb_pages . " " . $filename . " mémorisée\n";
$sxml->asXML($filename);
} else
echo "$this->nb_pages $titre passée\n";
if (count($this->tab_rang) > 0)
$this->next = array_shift($this->tab_rang);
else
$this->fin = true; */
?>