XML_ID.pm

  1. package XML_ID;
  2. use strict;
  3. use File::Path;
  4. use File::Basename;
  5. sub getIDFilefromID
  6. {
  7. my ($id, $tmp, $idFile, $DATABASE_DIRECTORY);
  8. $DATABASE_DIRECTORY = '/Zvon/FileCentral';
  9. $id = $_[0];
  10. $idFile = $id;
  11. $idFile =~s/\D*(\d{4})(\d{2})(\d{2}).*/$1\/$2\/$3.x/;
  12. $idFile =~ s/(\d{4})(\d{2}).*/$DATABASE_DIRECTORY\/$1\/$2\/$3.x/;
  13. $idFile = "$DATABASE_DIRECTORY/$idFile";
  14. return $idFile;
  15. }
  16. #returns true, if the database contains a id, which leads to other URL then
  17. #the one given in the database
  18. sub existFileWithSameID
  19. {
  20. my ($id, $tmp, $idFile, $href,$first, $last);
  21. $id = $_[0];
  22. $href = $_[1];
  23. $idFile = getIDFilefromID($id);
  24. if(!(open IN, $idFile)){return 0;}
  25. read IN, $tmp, 1000000;
  26. close IN;
  27. $first = index($tmp,$id);
  28. if($first == -1) {return 0;}
  29. $last = rindex($tmp,$id);
  30. if ($first != $last) {print "\n\n$idFile \ncontains several occurences of $id.\nProgram was terminated\n\n";exit;}
  31. $first = index($tmp,"=",$first)+1;
  32. $last = index($tmp,"\n",$first);
  33. $tmp = substr($tmp,$first,$last-$first);
  34. if (uc($tmp)eq uc($href))
  35. {
  36. if ($tmp ne $href)
  37. {
  38. print "\nFor $id \nDifference in URL only in case\nin database:$tmp\nin file: $href. \n\nFailed.";exit;
  39. }
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. sub insertInDatabase
  45. {
  46. my ($id, $dir, $idFile, $href, $tmp);
  47. $id = $_[0];
  48. $href = $_[1];
  49. $idFile = getIDFilefromID($id);
  50. $dir = dirname($idFile);
  51. if(!(-d $dir)) {mkpath($dir,"true");};
  52. if(open IN, $idFile)
  53. {
  54. read IN, $tmp, 1000000;
  55. close IN;
  56. if ($tmp=~/$id/) {return;}
  57. }
  58. open OUT,">>$idFile";
  59. print OUT "$id=$href\n";
  60. close OUT;
  61. }
  62. sub getHREFfromID
  63. {
  64. my ($id, $idFile, $href, $tmp);
  65. $id = $_[0];
  66. $idFile = getIDFilefromID($id);
  67. if(!(open IN, $idFile)) {"print $id not in database";return}
  68. read IN, $tmp, 1000000;
  69. close IN;
  70. $tmp =~ /$id.*/;
  71. $href = $&;
  72. $href =~ s/$id=(.*)/$1/;
  73. return $href;
  74. }
  75. 1