2023-01-24 19:00:39 +01:00
< ? php
/*
* To change this license header , choose License Headers in Project Properties .
* To change this template file , choose Tools | Templates
* and open the template in the editor .
*/
class _import {
protected $pubTypes = array ();
protected $items = array ();
protected $PDO ;
protected $current_record = array (
'id_manual' => '' ,
'year' => '' ,
'cited_vars' => array (),
'type' => '' ,
'year_pub' => '' ,
'language' => '2' ,
'citation' => '' ,
'citation_data' => '' ,
'author_data' => '' ,
'book_author_data' => ''
);
public static $brina_fields = array (
'Affil_1' , 'Affil_2' , 'Affil_3' , 'Affil_4' , 'Affil_5' , // countries of first five authors
'JField' , // journal scientific field
'R1_2002' , 'R2_2004' , 'R3_2006' , 'R4_2008' , 'R5_2010' , 'R6_2012' , 'R7_2014' ,
'R8_2016' , 'R9_2018' , // rounds of data article is refferring to
'ABCF_CoreYn' , 'R1_R7_ImmigrYn' , 'R1_CitizYn' , 'R2_R5_W_FamiYn' , 'R2_HealthYn' ,
'R2_EconMYn' , 'R3_R6_Pwell_bYn' , 'R3_TimingYn' , 'R4_AgeismYn' , 'R4_WelfareYn' ,
'R5_CrimiYn' , 'R6_DemocrYn' , 'R7_HealthYn' , 'R8_ClimateYn' , 'R9_JusticeYn' , // modules publication is refferring to
'TOPIC11' , 'TOPIC22' , // main two topics (dict_pub_topic weight 1 and 1)
'T_Politics' , 'T_Citizen' , 'T_Welfare' , 'T_Transition' , 'T_Inequality' ,
'T_Immigration' , 'T_Nation' , 'T_Crime' , 'T_Economy' , 'T_SWB' , 'T_Health' ,
'T_Culture' , 'T_SCapital' , 'T_Family' , 'T_Work' , 'T_Media' , 'T_Religion' ,
'T_Educ' , 'T_Age' , 'T_Gender' , 'T_Environ' , 'T_Europe' , 'T_Methods' ); // all topic article is refferring to
public static $round_ids = array ();
public static $round_fields = array ( 'R1_2002' , 'R2_2004' , 'R3_2006' , 'R4_2008' , 'R5_2010' , 'R6_2012' , 'R7_2014' ,
'R8_2016' , 'R9_2018' , 'R10_2020' );
// those are module names as written in TXT import.
// IDs are from DATABASE!!! PLEASE ADD THEM MANUALLY IF NEEDED AND UPDATE THIS
public static $ess_modules = array (
1 => 'ABCF_CoreYn' ,
2 => 'R1_R7_ImmigrYn' ,
3 => 'R1_CitizYn' ,
4 => 'R2_R5_W_FamiYn' ,
5 => 'R2_HealthYn' ,
6 => 'R2_EconMYn' ,
7 => 'R3_R6_Pwell_bYn' ,
8 => 'R3_TimingYn' ,
9 => 'R4_AgeismYn' ,
10 => 'R4_WelfareYn' ,
11 => 'R5_CrimiYn' ,
12 => 'R6_DemocrYn' ,
13 => 'R7_HealthYn' ,
14 => 'R8_ClimateYn' ,
15 => 'R9_JusticeYn'
);
// this array stores name of field (that we use) and its offset
// e.g. "T_Immigration = 7 means that input file stores t_immigration at offset 7
// we get this with _isolateBrinaColumns
public static $brina_offsets = array ();
// fetch from database: item[name] = id
protected $ess_items = array ();
// number of success and failed imports PER TYPE of citation
private $success = array ( 1 => 0 , 2 => 0 , 3 => 0 , 4 => 0 , 5 => 0 , 6 => 0 , 7 => 0 , 8 => 0 , 9 => 0 );
private $fail = array ( 1 => 0 , 2 => 0 , 3 => 0 , 4 => 0 , 5 => 0 , 6 => 0 , 7 => 0 , 8 => 0 , 9 => 0 );
private $already_in = 0 ;
// how many did we pass because we don't know the type
private $unknown_type = 0 ;
// failed to import, you can export/print it should you want to improve.
private $failed_citations = array ();
// this is array of ALL OK citations
private $parsed_citations = array ();
private $pub_id = - 1 ;
public function __construct () {
global $PDO ;
$this -> PDO = $PDO ;
// prep ESS item list
$this -> _prep_ess_items ();
}
protected function _prep_ess_items () {
try {
$stmt = $this -> PDO -> prepare ( " SELECT id, name FROM l_ess_item " );
$stmt -> execute ();
while ( $r = $stmt -> fetch ( PDO :: FETCH_ASSOC )) {
$this -> ess_items [ $r [ 'name' ]] = $r [ 'id' ];
}
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
return ;
}
// please note TXT must be pipe (|) separated file.
// reads TXT, year, manual id, item fields and -most important- the citation
// then, adds publication (and everything related to it - journal, authors)
// after the thing is added, it links items, topics,...
public function readTXT ( $filename , $block ) {
$line = 0 ;
if ( $file = fopen ( $filename , " r " )) {
while ( ! feof ( $file ) && $line < FILE_LINES_LIMIT ) {
$record = explode ( " | " , fgets ( $file ));
// first line contains field names. get vars
if ( $line == 0 ) {
$this -> items = _import :: _isolateItemColumns ( $record );
}
else if ( $line > FILE_LINES_SKIP ) {
$this -> cleanRecord ();
$this -> parseRecord ( $record );
$this -> current_record [ 'citation_data' ] = citation :: parse ( $this -> current_record [ 'citation' ], $this -> current_record [ 'type' ]);
$this -> counters ();
// DOI already in the DB
// check citation.php
if ( ! is_array ( $this -> current_record [ 'citation_data' ]) &&
$this -> current_record [ 'citation_data' ] > 0 ) {
$this -> pub_id = $this -> current_record [ 'citation_data' ];
}
// missing citation data
else if ( ! is_array ( $this -> current_record [ 'citation_data' ]) &&
$this -> current_record [ 'citation_data' ] == '-1' ) {
$this -> _MissingCitationData ();
}
// it's new.
else {
$this -> saveRecord ();
}
// now, if we have the record...
if ( $this -> pub_id > 0 ) {
$this -> linkItemsBrina ();
}
}
$line ++ ;
}
fclose ( $file );
}
echo " <br>Finished parsing TXT.<br> "
. " Successfully parsed: " . array_sum ( $this -> success ) . " <br> "
. " " . $this -> already_in . " of whom are already in the database <br> "
. " Couldn't parse: " . array_sum ( $this -> fail ) . " <br> "
. " Unknown type: " . $this -> unknown_type ;
echo " \n Fail stats: " ;
print_r ( $this -> fail );
// echo "\r\n<br>OK good:";
// print_r ($this->parsed_citations);
return ;
}
// same as _readTXT, but WITHOUT inserting the publication & base data (authors, publications,...)
// this is because metadata (items, topics,...) are split into more files in old scholar excels.
public function linkTXT ( $filename , $block ) {
$line = 0 ;
if ( $file = fopen ( $filename , " r " )) {
while ( ! feof ( $file ) && $line < FILE_LINES_LIMIT ) {
$record = explode ( " | " , fgets ( $file ));
// first line contains field names. get vars
if ( $line == 0 ) {
$this -> items = _import :: _isolateItemColumns ( $record );
}
else {
$this -> cleanRecord ();
$this -> parseRecord ( $record );
// now, if we have the record...
if ( $this -> current_record [ 'id_manual' ] != '' ) {
print_r ( $this -> current_record );
$this -> linkItemsBrina ();
}
}
$line ++ ;
}
fclose ( $file );
}
echo " \n Finished parsing TXT. Success: " . array_sum ( $this -> success ) . " ; fail: " . array_sum ( $this -> fail ) . " ; unknown type: " . $this -> unknown_type ;
echo " \n Fail stats: " ;
print_r ( $this -> fail );
// echo "\r\n<br>OK good:";
// print_r ($this->parsed_citations);
return ;
}
// this links Brina's google scholar data
public function linkScholar ( $filename ) {
echo " Parsing Scholar metadata... \n " ;
$line = 0 ;
if ( $file = fopen ( $filename , " r " )) {
while ( ! feof ( $file ) && $line < FILE_LINES_LIMIT ) {
$record = explode ( " | " , fgets ( $file ));
// first line contains field names. get vars
if ( $line == 0 ) {
$this -> items = _import :: _isolateBrinaColumns ( $record );
}
else if ( $line > FILE_LINES_SKIP ) {
// $this->_parse_B_Affiliations($record);
$this -> _parse_B_Topics ( $record );
// $this->_top_B_topics($record);
// $this->_round_fields2ids();
// $this->_parse_B_Rounds($record);
// $this->_parse_B_Modules($record);
// $this->_parse_B_jFields($record);
// $this->_parse_special_topics($record);
}
$line ++ ;
}
fclose ( $file );
}
else {
die ( 'E: Cannot open file' );
}
echo " Done linking. last. " ;
return ;
}
private function _MissingCitationData () {
echo " \n **ERR: Couldn't parse citation for this: \n " ;
print_r ( $this -> current_record );
return ;
}
// Saves current record to the database
// if the record is already there, update citations if needed.
protected function saveRecord () {
unset ( $authors_ids );
$authors_ids = array ();
// first check if it's already in (based on the manual id!)
if ( ! $this -> _isRecord ()) {
// explode authors and check if we already have them in the system.
$this -> prep_authors ();
//book chapter needs another layer (editors)
if ( $this -> current_record [ 'type' ] == '8' ) {
$this -> prep_authors ( 1 );
}
// add publication (check for it)
if ( ! $this -> pub_id = $this -> add_publication ()) {
common :: except ( 'Error getting publication ID: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
return ;
}
}
// record is in the DB, let's check if it needs to be updated
else {
//! TODO, currently it leaves them.
echo " It is already in the DB? \n " ;
}
}
protected function linkTopics () {
if ( intval ( $this -> publication_id ) == 0 ) {
return ;
}
foreach ( $this -> current_record [ 'topic' ] as $topic ) {
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_pub_topic (id_publication, id_topic) VALUES (:id_publication, :id_topic) " );
$stmt -> execute ( array (
'id_publication' => $this -> publication_id ,
'id_topic' => $topic ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
return false ;
}
}
}
protected function linkItems () {
if ( intval ( $this -> publication_id ) == 0 ) {
return ;
}
foreach ( $this -> current_record [ 'topic' ] as $item ) {
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_pub_item (id_publication, id_item) VALUES (:id_publication, :id_item) " );
$stmt -> execute ( array (
'id_publication' => $this -> publication_id ,
'id_item' => $item ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
return false ;
}
}
}
protected function linkModules () {
if ( intval ( $this -> publication_id ) == 0 ) {
return ;
}
foreach ( $this -> current_record [ 'module' ] as $module ) {
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_pub_module (id_publication, id_module) VALUES (:id_publication, :id_module) " );
$stmt -> execute ( array (
'id_publication' => $this -> publication_id ,
'id_module' => $module ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
return false ;
}
}
}
protected function linkRounds () {
if ( intval ( $this -> publication_id ) == 0 ) {
return ;
}
foreach ( $this -> current_record [ 'round' ] as $round ) {
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_pub_round (id_publication, id_round) VALUES (:id_publication, :id_round) " );
$stmt -> execute ( array (
'id_publication' => $this -> publication_id ,
'id_round' => $round ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
return false ;
}
}
}
protected function linkItemsBrina () {
foreach ( $this -> current_record [ 'cited_var' ] as $name => $cited ) {
if ( $cited == 1 ) {
// B and C blocks are refferred to as "B itemname" or "B itemname"
// a) merge spaces
// remove everything up to space
if ( strpos ( $name , " " ) !== false ) {
$name = str_replace ( " " , " " , str_replace ( " " , " " , $name ));
$n = explode ( " " , $name );
$name = $n [ 1 ];
}
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_pub_item (id_item, id_publication) (SELECT :item, id FROM publication WHERE id_manual=:id_manual) " );
$stmt -> execute ( array (
'item' => $this -> ess_items [ $name ],
'id_manual' => $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
return false ;
}
}
}
}
// go through publication data and add it
protected function add_publication ( $for_consideration = false ) {
$pub_id = false ;
$already_in = false ;
$is_doi = false ;
if ( isset ( $this -> current_record [ 'citation_data' ][ 'doi' ]) &&
$this -> current_record [ 'citation_data' ][ 'doi' ] != '' ) {
$is_doi = true ;
2023-03-08 21:07:47 +01:00
// don't print those that are present.
if ( ! $for_consideration ) {
//print_r ($this->current_record);
2023-01-24 19:00:39 +01:00
}
}
switch ( $this -> current_record [ 'type' ]) {
case 1 : // journal article
$pub = new journalArticle ();
$pub -> setName ( $this -> current_record [ 'citation_data' ][ 'title' ]);
$pub -> setLanguage ( $this -> current_record [ 'language' ] ? ? 2 );
$pub -> setJournal ( $this -> current_record [ 'citation_data' ][ 'journal' ]);
$pub -> setAuthors ( $this -> current_record [ 'author_data' ]);
$pub -> setAuthorscountries ( $this -> current_record [ 'author_countries' ]);
if ( $is_doi == true ) {
$pub -> setVolume ( $this -> current_record [ 'citation_data' ][ 'volume' ]);
$pub -> setIssue ( $this -> current_record [ 'citation_data' ][ 'number' ]);
$pub -> setDOI ( $this -> current_record [ 'citation_data' ][ 'doi' ]);
}
else {
$pub -> setVolumeIssue ( $this -> current_record [ 'citation_data' ][ 'issue' ]);
}
$pub -> setPages ( $this -> current_record [ 'citation_data' ][ 'pages' ]);
$pub -> setCitation ( $this -> current_record [ 'citation' ]);
$pub -> setYear ( $this -> current_record [ 'year_pub' ]);
$pub -> setId_manual ( $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]);
$pub -> setAbstractEng ( $this -> current_record [ 'citation_data' ][ 'abstract_eng' ]);
$pub -> setAbstractOrig ( $this -> current_record [ 'citation_data' ][ 'abstract_orig' ]);
$pub_id = $pub -> add ( $for_consideration , $already_in );
error_log ( $pub_id );
break ;
case 2 : // book
$pub = new book ();
$pub -> setName ( $this -> current_record [ 'citation_data' ][ 'title' ]);
$pub -> setLanguage ( $this -> current_record [ 'language' ] ? ? 2 );
$pub -> setCity ( $this -> current_record [ 'citation_data' ][ 'city' ]);
$pub -> setPublisher ( $this -> current_record [ 'citation_data' ][ 'publisher' ]);
$pub -> setYear ( $this -> current_record [ 'year' ]);
$pub -> setAuthors ( $this -> current_record [ 'author_data' ]);
if ( $is_doi == true ) {
$pub -> setDOI ( $this -> current_record [ 'citation_data' ][ 'doi' ]);
}
$pub -> setCitation ( $this -> current_record [ 'citation' ]);
$pub -> setId_manual ( $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]);
$pub_id = $pub -> add ( $for_consideration , $already_in );
// in case of DOI, ADD EXTRA FIELDS!!!!
break ;
case 3 :
$pub = new workpaper ();
$pub -> setName ( $this -> current_record [ 'citation_data' ][ 'title' ]);
$pub -> setLanguage ( $this -> current_record [ 'language' ] ? ? 2 );
$pub -> setYear ( $this -> current_record [ 'year' ]);
$pub -> setAuthors ( $this -> current_record [ 'author_data' ]);
if ( $is_doi == true ) {
$pub -> setPublisher ( $this -> current_record [ 'citation_data' ][ 'publisher' ]);
$pub -> setDOI ( $this -> current_record [ 'citation_data' ][ 'doi' ]);
}
$pub -> setCitation ( $this -> current_record [ 'citation' ]);
$pub -> setId_manual ( $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]);
$pub_id = $pub -> add ( $for_consideration , $already_in );
break ;
case 4 :
$pub = new conferencePP ();
$pub -> setName ( $this -> current_record [ 'citation_data' ][ 'title' ]);
$pub -> setLanguage ( $this -> current_record [ 'language' ] ? ? 2 );
$pub -> setYear ( $this -> current_record [ 'year' ]);
$pub -> setAuthors ( $this -> current_record [ 'author_data' ]);
if ( $is_doi == true ) {
$pub -> setDOI ( $this -> current_record [ 'citation_data' ][ 'doi' ]);
}
$pub -> setConferenceName ( $this -> current_record [ 'citation_data' ][ 'conference' ]);
$pub -> setConferenceCity ( $this -> current_record [ 'citation_data' ][ 'city' ]);
$pub -> setCitation ( $this -> current_record [ 'citation' ]);
$pub -> setId_manual ( $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]);
$pub_id = $pub -> add ( $for_consideration , $already_in );
break ;
case 5 : // report
$pub = new report ();
$pub -> setName ( $this -> current_record [ 'citation_data' ][ 'title' ]);
$pub -> setLanguage ( $this -> current_record [ 'language' ] ? ? 2 );
$pub -> setPublisher ( $this -> current_record [ 'citation_data' ][ 'publisher' ]);
$pub -> setYear ( $this -> current_record [ 'year' ]);
$pub -> setAuthors ( $this -> current_record [ 'author_data' ]);
if ( $is_doi == true ) {
$pub -> setDOI ( $this -> current_record [ 'citation_data' ][ 'doi' ]);
}
$pub -> setCitation ( $this -> current_record [ 'citation' ]);
$pub -> setId_manual ( $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]);
$pub_id = $pub -> add ( $for_consideration , $already_in );
// in case of DOI, ADD EXTRA FIELDS!!!!
break ;
case 6 :
$pub = new thesis ();
$pub -> setName ( $this -> current_record [ 'citation_data' ][ 'title' ]);
$pub -> setLanguage ( $this -> current_record [ 'language' ] ? ? 2 );
$pub -> setPublisher ( $this -> current_record [ 'citation_data' ][ 'publisher' ]);
$pub -> setYear ( $this -> current_record [ 'year' ]);
$pub -> setAuthors ( $this -> current_record [ 'author_data' ]);
if ( $is_doi == true ) {
$pub -> setDOI ( $this -> current_record [ 'citation_data' ][ 'doi' ]);
}
$pub -> setThesisType ( $this -> current_record [ 'thesis_type' ]);
$pub -> setCitation ( $this -> current_record [ 'citation' ]);
$pub -> setId_manual ( $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]);
$pub_id = $pub -> add ( $for_consideration , $already_in );
break ;
case 8 : // book chapter
$pub = new chapter ();
$pub -> setName ( $this -> current_record [ 'citation_data' ][ 'title' ]);
$pub -> setLanguage ( $this -> current_record [ 'language' ] ? ? 2 );
$pub -> setCity ( $this -> current_record [ 'citation_data' ][ 'location' ]);
$pub -> setAuthors ( $this -> current_record [ 'author_data' ]);
$pub -> setPublisher ( $this -> current_record [ 'citation_data' ][ 'publisher' ]);
$pub -> setYear ( $this -> current_record [ 'year' ]);
if ( $is_doi == true ) {
$pub -> setDOI ( $this -> current_record [ 'citation_data' ][ 'doi' ]);
}
if ( isset ( $this -> current_record [ 'citation_data' ][ 'pages' ])) {
$pub -> setPages ( $this -> current_record [ 'citation_data' ][ 'pages' ]);
}
$pub -> setBookTitle ( $this -> current_record [ 'citation_data' ][ 'booktitle' ]);
$pub -> setBookAuthors ( $this -> current_record [ 'book_author_data' ]);
$pub -> setCitation ( $this -> current_record [ 'citation' ]);
$pub -> setId_manual ( $this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]);
$pub_id = $pub -> add ( $for_consideration , $already_in );
break ;
default :
echo " PUB TYPE: " . $this -> current_record [ 'type' ] . " \n " ;
break ;
}
if ( is_object ( $pub )) {
unset ( $pub );
}
if ( $already_in == true ) {
$this -> already_in ++ ;
}
return $pub_id ;
}
// parse through all cites authors and check if they're in the db.
// for absent, add them.
// return array with IDs.
protected function prep_authors ( $parent = 0 ) {
// do we have &?
if ( $parent == 0 ) {
2023-03-08 21:07:47 +01:00
$this -> current_record [ 'citation_data' ][ 'author' ] = str_replace ( array ( '{}\\' ), array ( '' , '' , '' ), $this -> current_record [ 'citation_data' ][ 'author' ]);
2023-01-24 19:00:39 +01:00
$last = explode ( " & " , $this -> current_record [ 'citation_data' ][ 'author' ]);
}
else {
2023-03-08 21:07:47 +01:00
$this -> current_record [ 'citation_data' ][ 'book_author' ] = str_replace ( array ( '{}\\' ), array ( '' , '' , '' ), $this -> current_record [ 'citation_data' ][ 'book_author' ]);
2023-01-24 19:00:39 +01:00
$last = explode ( " & " , $this -> current_record [ 'citation_data' ][ 'book_author' ]);
}
2023-03-08 21:07:47 +01:00
// foo format (doi bibtex): separated by " AND "
if ( substr_count ( $last [ 0 ], ' and ' ) > 0 ) {
$authors = explode ( ' and ' , $last [ 0 ]);
// first name
/*
foreach ( $authors as $offset => $author ) {
$authors [ $offset ] = str_replace ( ' ' , ',' , $author , 1 );
} */
}
// proper format: we explode on every second comma (Lynn, P., Martin, P. & Fidzgerald, R.)
else {
$authors = array_filter ( array_map ( function ( $value ) {
2023-01-24 19:00:39 +01:00
return trim ( implode ( ',' , $value ));
},
array_chunk ( explode ( ',' , $last [ 0 ]), 2 )));
2023-03-08 21:07:47 +01:00
// add last author
if ( count ( $last ) > 1 ) {
array_push ( $authors , trim ( array_pop ( $last )));
}
}
2023-01-24 19:00:39 +01:00
// array_filter is needed to remove last empty element in case there is trailing comma (before &).
array_filter ( $authors , function ( $value ) { return trim ( $value ) !== " " ;});
// avoid looping this
$num = count ( $authors );
foreach ( $authors as $offset => $author ) {
$first = false ;
$last = false ;
if ( $offset == 0 ) {
$first = true ;
}
elseif ( $offset + 1 == $num ) {
$last = true ;
}
if ( ! author :: isAuthor ( $author )) {
$author_id = author :: insertAuthor ( $author );
}
}
if ( $parent == 1 ) {
$this -> current_record [ 'book_author_data' ] = $authors ;
}
else {
$this -> current_record [ 'author_data' ] = $authors ;
}
return ;
}
protected function _isRecord () {
try {
$stmt = $this -> PDO -> prepare ( " SELECT id FROM publication where id_manual=:id_manual " );
$stmt -> execute ( array (
$this -> current_record [ 'year' ] . $this -> current_record [ 'id_manual' ]) // id_manual is always YYYYxx (2008xx)
);
// this instead of rowCount to make in compatible with non-mysql DB
if ( $r = $stmt -> fetch ( PDO :: FETCH_NUM )) {
if ( $r [ 0 ] == 1 ) {
return true ;
}
}
return false ;
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
return false ;
}
protected function newLanguage ( $language ) {
try {
$stmt = $this -> PDO -> prepare ( " INSERT INTO l_language (name) VALUES (:language) " );
$stmt -> execute ( array (
'language' => $language ));
return $this -> PDO -> lastInsertId ();
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
// counts which records are good and which are not
private function counters () {
if ( ! is_array ( $this -> current_record [ 'citation_data' ]) && $this -> current_record [ 'citation_data' ] == - 2 ) {
$this -> unknown_type ++ ;
}
else if ( ! is_array ( $this -> current_record [ 'citation_data' ]) &&
$this -> current_record [ 'citation_data' ] > 0 ) {
$this -> success [ $this -> current_record [ 'type' ]] ++ ;
}
else if ( ! is_array ( $this -> current_record [ 'citation_data' ]) &&
$this -> current_record [ 'citation_data' ] == '-1' ) {
$this -> fail [ $this -> current_record [ 'type' ]] ++ ;
array_push ( $this -> failed_citations , " Type: " . $this -> current_record [ 'type' ]
. " ; Cit: " . $this -> current_record [ 'citation' ]); }
else {
$this -> success [ $this -> current_record [ 'type' ]] ++ ;
}
}
// Parses on which items the citation is referring. (and save it)
protected function parseReferences ( $block ) {
// '1' means given var was cited
foreach ( $this -> items as $offset => $item ) {
if ( intval ( $block [ $offset ]) == 1 ) {
$this -> current_record [ 'cited_var' ][ $item ] = 1 ;
}
}
}
protected function cleanRecord () {
unset ( $this -> current_record );
$this -> current_record = array (
'id_manual' => '' ,
'year' => '' ,
'cited_vars' => array (),
'type' => '' ,
'year_pub' => '' ,
'language' => 2 ,
'citation' => '' ,
'citation_data' => '' ,
'author_data' => ''
);
}
protected function parseRecord ( $record ) {
$this -> current_record [ 'year' ] = $record [ 0 ];
$this -> current_record [ 'id_manual' ] = $record [ 1 ];
$this -> current_record [ 'type' ] = $record [ count ( $record ) - 3 ];
$this -> current_record [ 'year_pub' ] = $record [ count ( $record ) - 2 ];
$this -> current_record [ 'citation' ] = $record [ count ( $record ) - 1 ];
$this -> current_record [ 'cited_vars' ] = $this -> parseReferences ( _import :: _isolateItemColumns ( $record ));
}
protected static function _isolateItemColumns ( $record ) {
// columns are:
// 0 - YEAR
// 1 - ID_MANUAL
// *** 2 ... n-3 item names ***
// n-2 pubtype
// n-1 pubyear
// n citation
// 2x right, remove last 3
for ( $n = 1 ; $n <= 2 ; $n ++ ) {
array_shift ( $record );
}
for ( $n = 1 ; $n <= 3 ; $n ++ ) {
array_pop ( $record );
}
return $record ;
}
// parse first line of TXT and detect "brina" columns defined in brina_fiels
protected static function _isolateBrinaColumns ( $record ) {
foreach ( _import :: $brina_fields as $field_name ) {
_import :: $brina_offsets [ $field_name ] = array_search ( $field_name , $record );
}
return ;
}
protected function _parse_B_Affiliations ( $record ) {
// affiliation fields are
$fields = array ( 'Affil_1' , 'Affil_2' , 'Affil_3' , 'Affil_4' , 'Affil_5' );
foreach ( $fields as $ord => $field ) {
$id_brina_country = $record [ _import :: $brina_offsets [ $field ]];
if ( $id_brina_country > 0 ) {
// check if we already have affiliation for this author
// please note author order in DB starts with 1, not 0 as here!!!
$author_order = $ord + 1 ;
try {
$stmt = $this -> PDO -> prepare ( " UPDATE dict_pub_author, l_country, publication "
. " SET dict_pub_author.id_country = l_country.id "
. " WHERE "
. " publication.id_manual = :id_manual AND publication.id = dict_pub_author.id_publication AND " // id_manual into ID
. " dict_pub_author.`ord`=:author_order AND dict_pub_author.id_country IS NULL AND " // link to author order and update only if no country info yet
. " l_country.brina_code = :brina_country_code " ); // finally link to Brina country code.
$stmt -> execute ( array (
'id_manual' => $record [ 0 ] . $record [ 1 ], // 0 - year, 1 - id_manual
'author_order' => $author_order ,
'brina_country_code' => $id_brina_country ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
}
/**
* Only for Brina file !
* @ param type $record
*/
protected function _parse_B_Topics ( $record ) {
echo " <br>RECORD: " . print_r ( $record , true );
// first, remove all topics for this publication
try {
$stmt = $this -> PDO -> prepare ( " DELETE FROM dict_pub_topic WHERE id_publication IN (SELECT id FROM publication WHERE id_manual=:id_manual) " ); // finally link to Brina country code.
$stmt -> execute ( array ( 'id_manual' => $record [ 0 ] . $record [ 1 ]));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
// we need to include IDs from the table (don't have that in the table)
$fields = array (
1 => 'T_Politics' , 2 => 'T_Citizen' , 3 => 'T_Welfare' , 4 => 'T_Transition' , 5 => 'T_Inequality' ,
6 => 'T_Immigration' , 7 => 'T_Nation' , 8 => 'T_Crime' , 9 => 'T_Economy' , 10 => 'T_SWB' ,
11 => 'T_Health' , 12 => 'T_Culture' , 13 => 'T_SCapital' , 14 => 'T_Family' , 15 => 'T_Work' ,
16 => 'T_Media' , 17 => 'T_Religion' , 18 => 'T_Educ' , 19 => 'T_Age' , 20 => 'T_Gender' ,
21 => 'T_Environ' , 22 => 'T_Europe' , 23 => 'T_Methods' ); // all topic article is refferring to
foreach ( $fields as $id_topic => $field ) {
// those are Y/N fields (1 means it works on that topic, 0 that it doesn't)
if ( intval ( $record [ _import :: $brina_offsets [ $field ]]) == 1 ) {
// special case
if ( $id_topic == 23 ) {
$id_topic = 99 ;
echo " Delam 99 " ;
}
try {
$stmt = $this -> PDO -> prepare ( " INSERT INTO dict_pub_topic (id_publication, id_topic) (SELECT id, :id_topic FROM publication where id_manual=:id_manual) " );
$stmt -> execute ( array (
'id_manual' => $record [ 0 ] . $record [ 1 ], // 0 - year, 1 - id_manual
'id_topic' => $id_topic ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
}
/**
* Only for Brina file !
* @ param type $record
*/
protected function _parse_special_topics ( $record ) {
// first, we are looking only for special topics
$topics = array ( 49 => 'Covid19' );
// this is slow, but OK for 1-2 topics
foreach ( $topics as $topic_id => $topic_name ) {
try {
$stmt = $this -> PDO -> prepare ( " DELETE FROM dict_pub_topic WHERE id_topic=:id_topic " ); // finally link to Brina country code.
$stmt -> execute ( array ( 'id_topic' => $id_topic ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
// please make sure it only contains topic1, topic2
if ( isset ( $topics [ trim ( $record [ 2 ])])) {
echo " Našel sem " . $record [ 2 ] . " v topic 1 <br> " ;
try {
$stmt = $this -> PDO -> prepare ( " INSERT INTO dict_pub_topic (id_publication, id_topic) (SELECT id, :id_topic FROM publication where id_manual=:id_manual) " );
$stmt -> execute ( array (
'id_manual' => $record [ 0 ] . $record [ 1 ], // 0 - year, 1 - id_manual
'id_topic' => trim ( $record [ 2 ])));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
// please make sure it only contains topic1, topic2
if ( isset ( $topics [ trim ( $record [ 3 ])])) {
echo " Našel sem " . $record [ 3 ] . " v topic 2 <br> " ;
try {
$stmt = $this -> PDO -> prepare ( " INSERT INTO dict_pub_topic (id_publication, id_topic) (SELECT id, :id_topic FROM publication where id_manual=:id_manual) " );
$stmt -> execute ( array (
'id_manual' => $record [ 0 ] . $record [ 1 ], // 0 - year, 1 - id_manual
'id_topic' => trim ( $record [ 3 ])));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
// this function marks topic1 and topic2 as the most important (weight 1 and 2)
protected function _top_B_topics ( $record ) {
// we need to include IDs from the table (don't have that in the table)
$fields = array ( 1 => " TOPIC11 " , 2 => " TOPIC22 " );
foreach ( $fields as $weight => $field ) {
$id_topic = $record [ _import :: $brina_offsets [ $field ]];
if ( $id_topic > 0 ) {
try {
$stmt = $this -> PDO -> prepare ( " UPDATE publication, dict_pub_topic SET dict_pub_topic.weight=:weight WHERE dict_pub_topic.id_publication=publication.id AND publication.id_manual=:id_manual AND dict_pub_topic.id_topic=:id_topic " );
$stmt -> execute ( array (
'weight' => $weight , // 0 - year, 1 - id_manual
'id_manual' => $record [ 0 ] . $record [ 1 ], // 0 - year, 1 - id_manual
'id_topic' => $id_topic ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
}
// create list of ids for fields (from the table). This is here to speed up the process
// (generate list only once instead of N times where N= num recs
public function _round_fields2ids () {
foreach ( _import :: $round_fields as $round ) {
$field = explode ( " _ " , $field );
try {
$stmt = $this -> PDO -> prepare ( " SELECT id FROM l_ess_round WHERE name=:name " );
$stmt -> execute ( array ( substr ( $field [ 0 ], 1 )));
// this instead of rowCount to make in compatible with non-mysql DB
if ( $r = $stmt -> fetch ( PDO :: FETCH_NUM )) {
_import :: $round_ids [ $round ] = $r [ 0 ];
}
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
return ;
}
protected function _parse_B_Rounds ( $record ) {
foreach ( _import :: $round_ids as $round => $id_round ) {
// those are Y/N fields (1 means it works on that topic, 0 that it doesn't)
if ( intval ( $record [ _import :: $brina_offsets [ $round ]]) == 1 ) {
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_pub_round (id_publication, id_round) (SELECT id, :id_round FROM publication where id_manual=:id_manual) " );
$stmt -> execute ( array (
'id_manual' => $record [ 0 ] . $record [ 1 ], // 0 - year, 1 - id_manual
'id_round' => $id_round ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
}
protected function _parse_B_Modules ( $record ) {
foreach ( _import :: $ess_modules as $id_module => $module ) {
// those are Y/N fields (1 means it works on that topic, 0 that it doesn't)
if ( intval ( $record [ _import :: $brina_offsets [ $module ]]) == 1 ) {
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_pub_module (id_publication, id_module) (SELECT id, :id_module FROM publication where id_manual=:id_manual) " );
$stmt -> execute ( array (
'id_manual' => $record [ 0 ] . $record [ 1 ], // 0 - year, 1 - id_manual
'id_module' => $id_module ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
}
protected function _parse_B_jFields ( $record ) {
// this is always JField
// those are Y/N fields (1 means it works on that topic, 0 that it doesn't)
$id_field = intval ( $record [ _import :: $brina_offsets [ 'JField' ]]);
if ( $id_field > 0 ) {
$id_journal = 0 ;
// is it a jornal, do we have it?
try {
$stmt = $this -> PDO -> prepare ( " select p_journalArticle.id_journal FROM p_journalArticle, publication WHERE publication.id_parent=p_journalArticle.id AND publication.id_manual=:id_manual AND publication.type=1 " );
$stmt -> execute ( array ( $record [ 0 ] . $record [ 1 ]));
if ( $r = $stmt -> fetch ( PDO :: FETCH_NUM )) {
$id_journal = $r [ 0 ];
try {
$stmt = $this -> PDO -> prepare ( " REPLACE INTO dict_journal_field (id_journal, id_field) VALUES (:id_journal, :id_field) " );
$stmt -> execute ( array (
'id_journal' => $id_journal ,
'id_field' => $id_field ));
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
catch ( exception $ex ) {
common :: except ( 'Err: ' . $ex . ' in ' . __DIR__ . '/' . __FILE__ . ':' . __LINE__ );
}
}
}
}
?>