Guest User

Retrieve.php

a guest
Apr 1st, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.36 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL & ~8192 & ~E_USER_WARNING);   # 8192 == E_DEPRECATED maar PHP < 5.3 heeft die niet
  3.  
  4. # Risky warning, might trip up some stuff
  5. if (@!file_exists(getcwd() . '/' . basename($argv[0]))) {
  6.     echo "It appears you are not running retrieve.php from its own directory. Please CHDIR to my directoy and run again!" . PHP_EOL;
  7.     die();
  8. } # if
  9.  
  10. require_once "settings.php";
  11. require_once "lib/exceptions/ParseSpotXmlException.php";
  12. require_once "lib/SpotDb.php";
  13. require_once "lib/SpotReq.php";
  14. require_once "lib/SpotParser.php";
  15. require_once "lib/SpotNntp.php";
  16. require_once "lib/retriever/SpotRetriever_Spots.php";
  17. require_once "lib/retriever/SpotRetriever_Comments.php";
  18. require_once "lib/imexport/Spot_SpotMapping.php";
  19.  
  20. # in safe mode, max execution time cannot be set, warn the user
  21. if (ini_get('safe_mode') ) {
  22.     echo "WARNING: PHP safemode is enabled, maximum execution cannot be reset! Turn off safemode if this causes problems" . PHP_EOL . PHP_EOL;
  23. } # if
  24.  
  25. if ((isset($argc)) && ($argc > 1) && ($argv[1] == '--export')) {
  26.     if (!$settings['retrieve_full']) {
  27.         die("Databases zonder retrieve_full kunnen we niet importeren dus exporteren is disabled.");
  28.     } # if
  29.    
  30.     try {
  31.         $db = new SpotDb($settings['db']);
  32.         $db->connect();
  33.    
  34.         $fp = fopen('export-db.csv', 'w');
  35.         $spotCount = $db->getSpotCount('');
  36.         for ($i = 0; $i < $spotCount; $i = $i + 5000) {    
  37.             $spots = $db->getSpots($i / 5000, 5000, '', array('field' => 'id', 'direction' => 'asc'), true);
  38.            
  39.             foreach($spots as $spot) {
  40.                 $mappedSpot = array();
  41.  
  42.                 # We exporteren alleen als we alle velden hebben
  43.                 if (count($spot) == count(Spot_SpotMapping::$fieldMapping)) {
  44.                     foreach(Spot_SpotMapping::$fieldMapping as $key => $value) {
  45.                         $mappedSpot[$value] = $spot[$key];
  46.                     } # foreach
  47.  
  48.                     fputcsv($fp, $mappedSpot);
  49.                 } # if
  50.             } # foreach
  51.         } # for
  52.         fclose($fp);
  53.     }
  54.     catch(Exception $x) {
  55.         die("Error exporting data: " . $x->getMessage() . PHP_EOL);
  56.     } # catch
  57.    
  58.     exit;
  59. } # export
  60.  
  61.  
  62. if ((isset($argc)) && ($argc > 1) && ($argv[1] == '--import')) {
  63.     try {
  64.         $db = new SpotDb($settings['db']);
  65.         $db->connect();
  66.    
  67.         $fp = fopen('export-db.csv', 'r');
  68.         $db->beginTransaction();
  69.         while (($line = fgetcsv($fp)) !== FALSE) {
  70.             $mappedSpot = array();
  71.             foreach($line as $key => $value) {
  72.                 $mappedSpot[Spot_SpotMapping::$valueMapping[$key]] = $value;
  73.             } # foreach
  74.  
  75.             $db->addSpot($mappedSpot, $mappedSpot);
  76.         } # while
  77.         $db->commitTransaction();
  78.        
  79.         fclose($fp);
  80.     }
  81.     catch(Exception $x) {
  82.         die("Error importing data: " . $x->getMessage() . PHP_EOL);
  83.     } # catch
  84.    
  85.     exit;
  86. } # import
  87.  
  88. $req = new SpotReq();
  89. $req->initialize();
  90.  
  91. if ($req->getDef('output', '') == 'xml') {
  92.     echo "<xml>";
  93. } # if
  94.  
  95. try {
  96.     $db = new SpotDb($settings['db']);
  97.     $db->connect();
  98. }
  99. catch(Exception $x) {
  100.     die("Unable to connect to database: " . $x->getMessage() . PHP_EOL);
  101. } # catch
  102.  
  103. ## Als we forceren om de "already running" check te bypassen, doe dat dan
  104. if ((isset($argc)) && ($argc > 1) && ($argv[1] == '--force')) {
  105.     $db->setRetrieverRunning($settings['nntp_hdr']['host'], false);
  106. } # if
  107.  
  108. ## Spots
  109. try {
  110.  
  111.     $retriever = new SpotRetriever_Spots($settings['nntp_hdr'],
  112.                                          $db,
  113.                                          $settings,                                      
  114.                                          $settings['rsa_keys'],
  115.                                          $req->getDef('output', ''),
  116.                                          $settings['retrieve_full']);
  117.     $msgdata = $retriever->connect($settings['hdr_group']);
  118.     $retriever->displayStatus('dbcount', $db->getSpotCount(''));
  119.    
  120.     $curMsg = $db->getMaxArticleId($settings['nntp_hdr']['host']);
  121.     if ($curMsg != 0) {
  122.         $curMsg = $retriever->searchMessageId($db->getMaxMessageId('headers'));
  123.     } # if
  124.  
  125.     $retriever->loopTillEnd($curMsg, $settings['retrieve_increment']);
  126.     $retriever->quit();
  127.     $db->setLastUpdate($settings['nntp_hdr']['host']);
  128. }
  129. catch(RetrieverRunningException $x) {
  130.     echo PHP_EOL . PHP_EOL;
  131.     echo "retriever.php draait al, geef de parameter '--force' mee om te forceren." . PHP_EOL;
  132. }
  133. catch(Exception $x) {
  134.     echo PHP_EOL . PHP_EOL;
  135.     echo "Fatal error occured retrieving messages:" . PHP_EOL;
  136.     echo "  " . $x->getMessage() . PHP_EOL . PHP_EOL;
  137.     die();
  138. } # catch
  139.  
  140. ## Comments
  141. try {
  142.     if ($settings['retrieve_comments']) {
  143.         $retriever = new SpotRetriever_Comments($settings['nntp_hdr'],
  144.                                                 $db,
  145.                                                 $settings,
  146.                                                 $req->getDef('output', ''));
  147.         $msgdata = $retriever->connect($settings['comment_group']);
  148.  
  149.         $curMsg = $db->getMaxArticleId('comments');
  150.         if ($curMsg != 0) {
  151.             $curMsg = $retriever->searchMessageId($db->getMaxMessageId('comments'));
  152.         } # if
  153.  
  154.         $retriever->loopTillEnd($curMsg, $settings['retrieve_increment']);
  155.         $retriever->quit();
  156.     } # if
  157. }
  158. catch(RetrieverRunningException $x) {
  159.     echo PHP_EOL . PHP_EOL;
  160.     echo "retriever.php draait al, geef de parameter '--force' mee om te forceren." . PHP_EOL;
  161. } catch(Exception $x) {
  162.     echo PHP_EOL . PHP_EOL;
  163.     echo "Fatal error occured retrieving messages:" . PHP_EOL;
  164.     echo "  " . $x->getMessage() . PHP_EOL . PHP_EOL;
  165.     die();
  166. } # catch
  167.  
  168. ## Retention cleanup
  169. try {
  170.     if ($settings['retention'] > 0) {
  171.         $db->deleteSpotsRetention($settings['retention']);
  172.     } # if
  173. } catch(Exception $x) {
  174.     echo PHP_EOL . PHP_EOL;
  175.     echo "Fatal error occured retrieving messages:" . PHP_EOL;
  176.     echo "  " . $x->getMessage() . PHP_EOL . PHP_EOL;
  177.     die();
  178. } # catch
  179.  
  180. if ($req->getDef('output', '') == 'xml') {
  181.     echo "</xml>";
  182. } # if
Advertisement
Add Comment
Please, Sign In to add comment