"Fossies" - the Fresh Open Source Software Archive

Member "4.6.1/db/migrations/06_step_25_raumzeit_db_conversion.php" (8 Apr 2021, 10503 Bytes) of package /linux/www/studip-4.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 class Step25RaumzeitDbConversion extends Migration
    3 {
    4     public function description ()
    5     {
    6         return 'convert dates for StEP00025; see logfile in $TMP_PATH';
    7     }
    8 
    9     public function up ()
   10     {
   11         // open log file
   12         $logfile_handle = fopen( $GLOBALS["TMP_PATH"] ."/Stud.IP_date_conversion.log", "ab");
   13         if(!$logfile_handle) {
   14             throw new Exception ("Can't open logfile ".$GLOBALS["TMP_PATH"]."/Stud.IP_date_conversion.log");
   15         }
   16 
   17         $this->write( get_class($this).": Starting data conversion... - this may take a very long time");
   18 
   19         // create secret password for subroutine authentication
   20         $secret_password = md5(uniqid("ditnuc6532ktn"));
   21 
   22         // signal start of conversion and set 'secret'
   23         DBManager::get()->exec("
   24             REPLACE INTO `config` ( `config_id` , `parent_id` , `field` , `value` , `is_default` , `type` , `range` , `section` , `position` , `mkdate` , `chdate` , `description` , `comment` , `message_template` )
   25             VALUES (
   26             'migration5' , '', 'MIGRATION_5_TEMPORARY_SECRET', '".$secret_password."', '0', 'string', 'global', '', '0', '0', '0', 'Temporary secret string for migragtion 5', 'Temporary entry of migration 5', ''
   27             );
   28         ");
   29 
   30         $this->convert_data( $logfile_handle, $secret_password);
   31 
   32         // remove signal
   33         DBManager::get()->exec("DELETE FROM `config` WHERE config_id = 'migration5'");
   34 
   35         $this->write( get_class($this).": Finished with data conversion...");
   36 
   37         // close logfile
   38         fclose($logfile_handle);
   39     }
   40 
   41 
   42     public function convert_data($logfile_handle, $secret_password)
   43     {
   44         // data conversion code:
   45 
   46         // we need enough memory
   47         ini_set( "memory_limit", "256M");
   48 
   49         // set URL of subroutine file
   50         // (needed because of PHP memory problems, if the conversion would be done in one step)
   51         $CONVERSION_SUBROUTINE_URL = $GLOBALS["ABSOLUTE_URI_STUDIP"] ."raumzeit_conversion_subroutine.php";
   52 
   53         // define step size (number of rows) for subroutine proccessing
   54         $STEP_SIZE= 300;
   55 
   56 
   57         // include business logic
   58         require_once('lib/classes/Seminar.class.php');
   59         require_once('lib/resources/lib/VeranstaltungResourcesAssign.class.php');
   60 
   61 
   62 
   63         // lets go...
   64         fwrite($logfile_handle, "(". date("Y-m-d H:i:s T") .") Starting conversion of imported seminar dates.\n");
   65 
   66 
   67         // STEP 1:
   68         //      convert the title of dates (="content") to real themes
   69         //      converts all dates, that don't have content==''
   70         fwrite($logfile_handle, "(". date("Y-m-d H:i:s T") .") Step 1: Converting the title of dates to real themes:\n");
   71 
   72         // get all dates (=termine) with content!=''
   73         $query = "SELECT termine.*
   74                   FROM seminare
   75                   LEFT JOIN termine ON (seminare.Seminar_id = termine.range_id)
   76                   WHERE content != '' OR description != ''";
   77         $data = DBManager::get()->fetchAll($query);
   78 
   79         // create new theme for each date
   80         foreach ($data as $row) {
   81             $new_issue_id = md5(uniqid('Issue', true));
   82 
   83             $query = "INSERT INTO `themen_termine` (`issue_id`, `termin_id`)
   84                       VALUES (:issue_id, :termin_id)";
   85             $statement = DBManager::get()->prepare($query);
   86             $statement->bindValue(':issue_id', $new_issue_id);
   87             $statement->bindValue(':termin_id', $row['termin_id']);
   88             $statement->execute();
   89 
   90             $query = "INSERT INTO themen
   91                         (issue_id, seminar_id, author_id, title, description, mkdate, chdate)
   92                       VALUES
   93                         (:issue_id, :seminar_id, :author_id, :title, :description, :mkdate, :chdate)";
   94             $statement = DBManager::get()->prepare($query);
   95             $statement->bindValue(':issue_id', $new_issue_id);
   96             $statement->bindValue(':seminar_id', $row['range_id']);
   97             $statement->bindValue(':author_id', $row['author_id']);
   98             $statement->bindValue(':title', $row['content']);
   99             $statement->bindValue(':description', $row['description']);
  100             $statement->bindValue(':mkdate', $row['mkdate']);
  101             $statement->bindValue(':chdate', $row['chdate']);
  102             $statement->execute();
  103 
  104             $query = "UPDATE termine
  105                       SET content = '', description = ''
  106                       WHERE termin_id = :termin_id";
  107             $statement = DBManager::get()->prepare($query);
  108             $statement->bindValue(':termin_id', $row['termin_id']);
  109             $statement->execute();
  110 
  111             $query = "UPDATE folder
  112                       SET range_id = :range_id
  113                       WHERE range_id = :termin_id";
  114             $statement = DBManager::get()->prepare($query);
  115             $statement->bindValue(':range_id', $new_issue_id);
  116             $statement->bindValue(':termin_id', $row['termin_id']);
  117             $statement->execute();
  118 
  119             if ($row['topic_id']) {
  120                 $query = "UPDATE px_topics
  121                           SET topic_id = :issue_id
  122                           WHERE topic_id = :topic_id";
  123                 $statement = DBManager::get()->prepare($query);
  124                 $statement->bindValue(':issue_id', $new_issue_id);
  125                 $statement->bindValue(':topic_id', $row['topic_id']);
  126                 $statement->execute();
  127 
  128                 $query = "UPDATE px_topics
  129                           SET root_id = :issue_id
  130                           WHERE root_id = :topic_id";
  131                 $statement = DBManager::get()->prepare($query);
  132                 $statement->bindValue(':issue_id', $new_issue_id);
  133                 $statement->bindValue(':topic_id', $row['topic_id']);
  134                 $statement->execute();
  135 
  136                 $query = "UPDATE px_topics
  137                           SET parent_id = :issue_id
  138                           WHERE parent_id = :topic_id";
  139                 $statement = DBManager::get()->prepare($query);
  140                 $statement->bindValue(':issue_id', $new_issue_id);
  141                 $statement->bindValue(':topic_id', $row['topic_id']);
  142                 $statement->execute();
  143             }
  144 
  145             fwrite($logfile_handle, "(". date("Y-m-d H:i:s T") .") converting termin_id='".$row['termin_id']."', added theme_id='".$new_issue_id."'\n");
  146             flush();
  147         }
  148 
  149         fwrite($logfile_handle, "(". date("Y-m-d H:i:s T") .") Finished Step 1. Converted " . count($data) . " dates.\n");
  150 
  151         // END OF STEP 1
  152 
  153 
  154         // STEP 2:
  155         //      create single dates for all regular dates (turnus_data in metadata_dates)
  156         fwrite($logfile_handle, "(". date("Y-m-d H:i:s T") .") Step 2: Creating single dates for all regular dates:\n");
  157 
  158         // reset counter
  159         $counter = 0;
  160 
  161         // set number of record to start with
  162         $start_at = 0;
  163 
  164         do {
  165             // call the conversion subroutine with number of rows that should get processed
  166 
  167             $subroutine_url = $CONVERSION_SUBROUTINE_URL ."?step_size=".$STEP_SIZE."&start_at=".$start_at."&secret=".$secret_password;
  168 
  169 // curl:
  170             // create cURL-Handle
  171             $ch = curl_init();
  172 
  173             // set url and other option
  174             curl_setopt($ch, CURLOPT_URL, $subroutine_url);
  175             curl_setopt($ch, CURLOPT_HEADER, 0);
  176             curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
  177 
  178             // make the call to the url
  179             $response = curl_exec ($ch);
  180 
  181             // close cURL-Handle und gebe die Systemresourcen frei
  182             curl_close($ch);
  183 
  184 // file_get_contents (fopen wrappers)
  185 //   removed file_get_contents access, in favor of curl because file_get_contents could not access the desired URL with
  186 //   every data we had for testing; it was not possible to reproduce this error e.g. by calling file_get_contents
  187 //   directly from the shell
  188 //            // open URL via fopen = "call" subroutine
  189 //            $response = file_get_contents( $subroutine_url );
  190 
  191             // success ?
  192             if( $response == FALSE ){
  193                 $this->write( get_class($this)." - Error while executing subroutine. Can't open URL. Stopping.\n");
  194                 fwrite($logfile_handle, "Error while executing subroutine. Can't open URL '$subroutine_url'. Stopping.\n");
  195                 throw new Exception("Error while executing subroutine.");
  196             }
  197 
  198 
  199             // some not quite nice error handling:
  200             if( substr($response,0,5) == "ERROR" ){
  201                 // write output to logfile
  202                 $this->write( get_class($this)." - Error while executing subroutine. Please see logfile for details. Stopping.\n");
  203                 fwrite( $logfile_handle, $response);
  204                 fwrite($logfile_handle, "Error while executing subroutine. Stopping.\n");
  205                 throw new Exception("Error while executing subroutine.". $response);
  206             }
  207 
  208             // get last line (holds the number of converted rows)
  209             $begin_of_last_line = strrpos( $response, "\n")+1;
  210             $numberOfConvertedRows = substr($response, $begin_of_last_line, strlen($response)-$begin_of_last_line);
  211 
  212             // check, if $numberOfConvertedRows is really a number
  213             if( !is_numeric($numberOfConvertedRows) ){
  214                 $this->write( get_class($this)." - Error while executing subroutine. Please see logfile for details. Stopping.\n");
  215                 // write output to logfile
  216                 fwrite( $logfile_handle, $response."\n");
  217                 fwrite($logfile_handle, "Error while executing subroutine. Invalid number of converted lines found. Stopping.\n");
  218                 throw new Exception("Error while executing subroutine.\n ". $response);
  219             }
  220 
  221             // cutoff last line
  222             $response = substr($response, 0, $begin_of_last_line);
  223 
  224             // write output to logfile
  225             fwrite( $logfile_handle, $response);
  226 
  227             // count total amount of converted seminars
  228             $counter += $numberOfConvertedRows;
  229 
  230             // step to next record package
  231             $start_at += $STEP_SIZE;
  232 
  233         } while( $numberOfConvertedRows != 0);
  234 
  235         fwrite($logfile_handle, "(". date("Y-m-d H:i:s T") .") Finished Step 2. Converted $counter seminars.\n");
  236 
  237         fwrite($logfile_handle, "(". date("Y-m-d H:i:s T") .") Conversion finished.");
  238 
  239         $this->write( get_class($this).": Converted $counter seminars.");
  240     }
  241 }