2013-09-06 13:33:53 +04:00
|
|
|
#!/usr/bin/php -Cq
|
|
|
|
<?php
|
|
|
|
|
2017-10-26 22:21:21 +03:00
|
|
|
$hFile = @fopen('wikidatawiki-20130623-pages-articles.xml', 'r');
|
2013-09-06 13:33:53 +04:00
|
|
|
|
2017-10-26 22:21:21 +03:00
|
|
|
$hFileEntity = fopen('entity.csv', 'w');
|
|
|
|
$hFileEntityLabel = fopen('entity_label.csv', 'w');
|
|
|
|
$hFileEntityDescription = fopen('entity_description.csv', 'w');
|
|
|
|
$hFileEntityAlias = fopen('entity_alias.csv', 'w');
|
|
|
|
$hFileEntityLink = fopen('entity_link.csv', 'w');
|
|
|
|
$hFileEntityProperty = fopen('entity_property.csv', 'w');
|
2013-09-06 13:33:53 +04:00
|
|
|
|
|
|
|
$iCount = 0;
|
|
|
|
|
|
|
|
$sTitle = '';
|
|
|
|
$iNS = false;
|
|
|
|
$iID = false;
|
|
|
|
|
|
|
|
if ($hFile) {
|
2016-09-04 04:19:48 +03:00
|
|
|
while (($sLine = fgets($hFile, 4000000)) !== false) {
|
|
|
|
if (substr($sLine, 0, 11) == ' <title>') {
|
|
|
|
$sTitle = substr($sLine, 11, -9);
|
2016-09-14 04:39:17 +03:00
|
|
|
} elseif (substr($sLine, 0, 8) == ' <ns>') {
|
2016-09-04 04:19:48 +03:00
|
|
|
$iNS = (int)substr($sLine, 8, -6);
|
2016-09-14 04:39:17 +03:00
|
|
|
} elseif (substr($sLine, 0, 8) == ' <id>') {
|
2016-09-04 04:19:48 +03:00
|
|
|
$iID = (int)substr($sLine, 8, -6);
|
2016-09-14 04:39:17 +03:00
|
|
|
} elseif (substr($sLine, 0, 33) == ' <text xml:space="preserve">') {
|
2016-09-04 04:19:48 +03:00
|
|
|
if ($iNS == -2) continue;
|
|
|
|
if ($iNS == -1) continue;
|
|
|
|
if ($iNS == 1) continue;
|
|
|
|
if ($iNS == 2) continue;
|
|
|
|
if ($iNS == 3) continue;
|
|
|
|
if ($iNS == 4) continue;
|
|
|
|
if ($iNS == 5) continue;
|
|
|
|
if ($iNS == 6) continue;
|
|
|
|
if ($iNS == 7) continue;
|
|
|
|
if ($iNS == 8) continue;
|
|
|
|
if ($iNS == 9) continue;
|
|
|
|
if ($iNS == 10) continue;
|
|
|
|
if ($iNS == 11) continue;
|
|
|
|
if ($iNS == 12) continue;
|
|
|
|
if ($iNS == 13) continue;
|
|
|
|
if ($iNS == 14) continue;
|
|
|
|
if ($iNS == 15) continue;
|
|
|
|
if ($iNS == 121) continue;
|
|
|
|
if ($iNS == 123) continue;
|
|
|
|
if ($iNS == 829) continue;
|
|
|
|
if ($iNS == 1198) continue;
|
|
|
|
if ($iNS == 1199) continue;
|
|
|
|
$sText = html_entity_decode(substr($sLine, 33, -8), ENT_COMPAT, 'UTF-8');
|
|
|
|
$aArticle = json_decode($sText, true);
|
|
|
|
|
2017-10-26 22:21:21 +03:00
|
|
|
if (array_diff(array_keys($aArticle), array('label', 'description', 'aliases', 'links', 'entity', 'claims', 'datatype')) != array()) {
|
2016-09-04 04:19:48 +03:00
|
|
|
// DEBUG
|
|
|
|
var_dump($sTitle);
|
|
|
|
var_dump(array_keys($aArticle));
|
|
|
|
var_dump($aArticle);
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iPID = $iQID = null;
|
|
|
|
if ($aArticle['entity'][0] == 'p') {
|
2016-09-14 04:39:17 +03:00
|
|
|
$iPID = (int) substr($aArticle['entity'], 1);
|
|
|
|
} elseif ($aArticle['entity'][0] == 'q') {
|
|
|
|
$iQID = (int) substr($aArticle['entity'], 1);
|
2016-09-04 04:19:48 +03:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-26 22:21:21 +03:00
|
|
|
echo '.';
|
2016-09-04 04:19:48 +03:00
|
|
|
|
2016-09-14 04:39:17 +03:00
|
|
|
fputcsv($hFileEntity, array($iID, $sTitle, $iPID, $iQID, @$aArticle['datatype']));
|
2016-09-04 04:19:48 +03:00
|
|
|
|
2016-09-14 04:39:17 +03:00
|
|
|
foreach ($aArticle['label'] as $sLang => $sLabel) {
|
|
|
|
fputcsv($hFileEntityLabel, array($iID, $sLang, $sLabel));
|
2016-09-04 04:19:48 +03:00
|
|
|
// echo "insert into entity_label values (".$iID.",'".pg_escape_string($sLang)."','".pg_escape_string($sLabel)."');\n";
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:39:17 +03:00
|
|
|
foreach ($aArticle['description'] as $sLang => $sLabel) {
|
|
|
|
fputcsv($hFileEntityDescription, array($iID, $sLang, $sLabel));
|
2016-09-04 04:19:48 +03:00
|
|
|
// echo "insert into entity_description values (".$iID.",'".pg_escape_string($sLang)."','".pg_escape_string($sLabel)."');\n";
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:39:17 +03:00
|
|
|
foreach ($aArticle['aliases'] as $sLang => $aLabels) {
|
2016-09-04 04:19:48 +03:00
|
|
|
$aUniqueAlias = array();
|
2016-09-14 04:39:17 +03:00
|
|
|
foreach ($aLabels as $sLabel) {
|
2016-09-04 04:19:48 +03:00
|
|
|
if (!isset($aUniqueAlias[$sLabel]) && $sLabel) {
|
2016-09-14 04:39:17 +03:00
|
|
|
fputcsv($hFileEntityAlias, array($iID, $sLang, $sLabel));
|
|
|
|
// echo "insert into entity_alias values (".$iID.",'".pg_escape_string($sLang)."','".pg_escape_string($sLabel)."');\n";
|
|
|
|
$aUniqueAlias[$sLabel] = true;
|
|
|
|
}
|
2016-09-04 04:19:48 +03:00
|
|
|
}
|
|
|
|
}
|
2013-09-06 13:33:53 +04:00
|
|
|
|
2016-09-14 04:39:17 +03:00
|
|
|
foreach ($aArticle['links'] as $sLang => $sLabel) {
|
|
|
|
fputcsv($hFileEntityLink, array($iID, $sLang, $sLabel));
|
|
|
|
// echo "insert into entity_link values (".$iID.",'".pg_escape_string($sLang)."','".pg_escape_string($sLabel)."');\n";
|
2016-09-04 04:19:48 +03:00
|
|
|
}
|
2013-09-06 13:33:53 +04:00
|
|
|
|
|
|
|
|
2016-09-14 04:39:17 +03:00
|
|
|
if (isset($aArticle['claims'])) {
|
|
|
|
//
|
|
|
|
foreach ($aArticle['claims'] as $iClaim => $aClaim) {
|
|
|
|
//
|
|
|
|
$bFail = false;
|
|
|
|
if ($aClaim['m'][0] == 'novalue') continue;
|
|
|
|
if ($aClaim['m'][0] == 'somevalue') continue;
|
|
|
|
$iPID = (int)$aClaim['m'][1];
|
|
|
|
if ($aClaim['m'][0] != 'value') $bFail = true;
|
|
|
|
if ($aClaim['m'][2]== 'wikibase-entityid') {
|
|
|
|
//
|
|
|
|
if ($aClaim['m'][3]['entity-type'] != 'item') $bFail = true;
|
|
|
|
fputcsv($hFileEntityProperty, array($iID, $iClaim, $iPID, null, $aClaim['m'][3]['numeric-id'], null, null));
|
|
|
|
// echo "insert into entity_property values (nextval('seq_entity_property'),".$iID.",".$iPID.",null,".$aClaim['m'][3]['numeric-id'].",null);\n";
|
|
|
|
} elseif ($aClaim['m'][2] == 'globecoordinate') {
|
|
|
|
//
|
|
|
|
if ($aClaim['m'][3]['globe'] != 'http://www.wikidata.org/entity/Q2') $bFail = true;
|
2016-10-14 01:01:16 +03:00
|
|
|
fputcsv(
|
|
|
|
$hFileEntityProperty,
|
|
|
|
array(
|
|
|
|
$iID,
|
|
|
|
$iClaim,
|
|
|
|
$iPID,
|
|
|
|
null,
|
|
|
|
null,
|
2017-10-26 22:21:21 +03:00
|
|
|
'SRID=4326;POINT('.((float) $aClaim['m'][3]['longitude']).' '.((float)$aClaim['m'][3]['latitude']).')', null
|
2016-10-14 01:01:16 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
/* echo "insert into entity_property values (nextval('seq_entity_property'),";
|
|
|
|
* echo $iID.",".$iPID.",null,null,ST_SetSRID(ST_MakePoint(".((float)$aClaim['m'][3]['longitude']);
|
|
|
|
* echo ", ".((float)$aClaim['m'][3]['latitude'])."),4326));\n";
|
|
|
|
*/
|
2016-09-14 04:39:17 +03:00
|
|
|
} elseif ($aClaim['m'][2] == 'time') {
|
|
|
|
// TODO!
|
|
|
|
/*
|
|
|
|
if ($aClaim['m'][3]['calendarmodel'] == 'http://www.wikidata.org/entity/Q1985727') {
|
|
|
|
// Gregorian
|
|
|
|
if (preg_match('#(\\+|-)0*([0-9]{4})-([0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})Z#', $aClaim['m'][3]['time'], $aMatch)) {
|
|
|
|
if ((int)$aMatch[2] < 4700 && ) {
|
|
|
|
$sDateString = $aMatch[2].'-'.$aMatch[3].($aClaim['m'][3]['timezone']>=0?'+':'').$aClaim['m'][3]['timezone'].($aMatch[1]=='-'?' bc':'');
|
|
|
|
fputcsv($hFileEntityProperty, array($iID,$iClaim,$iPID,null,null,null,$sDateString));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// $bFail = true;
|
|
|
|
}
|
|
|
|
} elseif ( $aClaim['m'][3]['calendarmodel'] != 'http://www.wikidata.org/entity/Q1985786') {
|
|
|
|
// Julian
|
|
|
|
if (preg_match('#(\\+|-)0*([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2})Z#', $aClaim['m'][3]['time'], $aMatch)) {
|
|
|
|
var_dump($aMatch);
|
|
|
|
exit;
|
|
|
|
$iDayCount = juliantojd(2, 11, 1732);
|
|
|
|
var_dump($iDayCount, jdtogregorian($iDayCount));
|
|
|
|
} else {
|
|
|
|
$bFail = true;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
exit;
|
|
|
|
} else {
|
|
|
|
// $bFail = true;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
} elseif ($aClaim['m'][2] == 'string') {
|
|
|
|
// echo "insert into entity_property values (nextval('seq_entity_property'),".$iID.",".$iPID.",'".pg_escape_string($aClaim['m'][3])."',null,null);\n";
|
|
|
|
fputcsv($hFileEntityProperty, array($iID, $iClaim, $iPID, $aClaim['m'][3], null, null, null));
|
|
|
|
} else {
|
|
|
|
$bFail = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't care about sources: if ($aClaim['refs'] != array()) $bFail = true;
|
|
|
|
|
|
|
|
if ($bFail) {
|
|
|
|
var_dump($sTitle);
|
|
|
|
var_dump($aClaim);
|
|
|
|
} else {
|
|
|
|
// process
|
|
|
|
}
|
|
|
|
}
|
2016-09-04 04:19:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($hFile);
|
|
|
|
fclose($hFileEntity);
|
|
|
|
fclose($hFileEntityLabel);
|
|
|
|
fclose($hFileEntityDescription);
|
|
|
|
fclose($hFileEntityAlias);
|
|
|
|
fclose($hFileEntityLink);
|
|
|
|
fclose($hFileEntityProperty);
|
2013-09-06 13:33:53 +04:00
|
|
|
}
|