1. <?php
  2. /*
  3.   يَستخدم ملف تحويل XSLT لإنتاج ملف Atom بناء على بيانات في صيغة XML تمثل نتائج بحث عن محتوى منشور في أرشيف الإنترنت archive.org
  4.   حقوق الطبع محفوظة: أحمد غربية، مارس 2009
  5.   منشور برخصة جنو العمومية، الإصدارة الثالثة
  6. Uses an XSLT to produce an Atom feed file from a sarch result in XML retrieved from archive.org
  7. Copyright Ahmad Gharbeia, March 2009
  8. Published under a GNU Pulic License version 3 (GPLv3)
  9. */
  10.  
  11. $xml_dataset_URL = 'http://www.archive.org/advancedsearch.php?q=subject%3A%22%D8%A7%D9%84%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D8%AC+%D8%A7%D9%84%D8%AB%D8%A7%D9%86%D9%8A%22+AND+mediatype%3Aaudio+AND+uploader%3Aagharbeia&fl[]=identifier&fl[]=title&fl[]=description&fl[]=creator&fl[]=oai_updatedate&fl[]=publicdate&fl[]=subject&sort[]=publicdate+desc&sort[]=&sort[]=&rows=50&save=yes&fmt=xml&xmlsearch=Search';
  12.  
  13. $xml_dataset_cache_path = 'search_result.xml';
  14. $atom_path = 'atom.xml';
  15. $xsl_path = 'atom.xsl';
  16.  
  17. $xml_date_xpath = '/response/result/doc[position()=1]/arr[@name="oai_updatedate"]/date[last()]';
  18. $atom_date_xpath = '/atom:feed/atom:entry[position()=1]/atom:updated';
  19.  
  20.  
  21. function latest_public_date(DOMDocument $xml, $xpath)
  22. {
  23. $domxpath = new DOMXPath($xml);
  24. $domxpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
  25. return strtotime($domxpath->evaluate($xpath)->item(0)->nodeValue);
  26. }
  27.  
  28.  
  29. try
  30. {
  31. $search = new DOMDocument();
  32. if ($search->load($xml_dataset_URL))
  33. {
  34. echo "Loaded search from result: {$xml_dataset_URL}<br />";
  35. $latest_in_xml = latest_public_date($search, $xml_date_xpath);
  36. echo "Latest date in search result: " . date('c', $latest_in_xml) . "<br />";
  37. $atom = new DOMDocument();
  38. if ((($atom_exists = $atom->load($atom_path)) === false) || ($latest_in_xml > ($latest_in_atom = latest_public_date($atom, $atom_date_xpath))))
  39. {
  40. echo (!$atom_exists?"No current feed":"search result has updated items") . "<br />";
  41. if (($cachedsize = $search->save($xml_dataset_cache_path)) !== false)
  42. {
  43. echo "Search result cached to: {$xml_dataset_cache_path}; {$cachedsize} bytes writ.<br />";
  44. }
  45. else
  46. {
  47. echo "Saving cache file failed!<br />";
  48. }
  49. $xslt = new XSLTProcessor();
  50. $xslt->importStyleSheet(DOMDocument::load($xsl_path));
  51. if (($atom = $xslt->transformToDoc($search)) !== false)
  52. {
  53. echo "XML transformed into Atom using: {$xsl_path}<br />";
  54. if (($atomsize = $atom->save($atom_path)) !== false)
  55. {
  56. echo "Atom saved to: {$atom_path}; {$atomsize} bytes writ.<br />";
  57. }
  58. else
  59. {
  60. echo "Saving Atom file failed!<br />";
  61. }
  62. }
  63. else
  64. {
  65. echo "Transformation faild!<br />";
  66. }
  67. }
  68. else
  69. {
  70. echo "There was a feed AND there were no newer items in search result<br />";
  71. }
  72. if ($atom_exists) echo "Latest date in current feed: " . date('c', $latest_in_atom) . "<br />";
  73. }
  74. else
  75. {
  76. echo "Couldn't retrieve search results<br />";
  77. }
  78. echo "Done.";
  79. }
  80. catch (Exception $e)
  81. {
  82. echo 'Caught exception: ', $e->getMessage(), "<br />";
  83. }
  84. ?>
  85.  

هذا الكود المصدري منشور في موقع أحمد غربية.

This source code listing is published in the web site of Ahmad Gharbeia.