/**
  * Run the upgrader
  *
  * @return string HTML report
  */
 public function run()
 {
     /* 
     	Check for progress in script -- a starting index of -1 implies that all photos have 
     	already been processed during this session
     */
     $current_user = check_authentication();
     if (!reason_user_has_privs(get_user_id($current_user), 'db_maintenance')) {
         die('don\'t have the right permissions?');
     }
     if (isset($_SESSION['reason_image_type_script']['last_end_index'])) {
         $start_index = $_SESSION['reason_image_type_script']['last_end_index'];
     } else {
         $start_index = 0;
     }
     if ($start_index == -1) {
         echo '<p>This script has already processed all images during this session!' . '</p>';
     } else {
         $start_time = time();
         $php_max_time = ini_get('max_execution_time');
         if ($php_max_time == 0) {
             $time_limit = 30;
         } else {
             $time_limit = min($php_max_time / 2, 50);
         }
         $es = new entity_selector();
         $es->add_type(id_of('image'));
         $all_image_ids = $es->get_ids('', 'All', 'get_ids_error');
         /* 
             3 cases for which FILES actually exist
             1. only standard -- no need to update the image Entity table
             2. standard and thumbnail -- give these entities a thumbnail_image_type field
             3. standard, thumbnail, original (full-size) -- give these entites a thumbnail_image_type and
             original_image_type field
         */
         $ids_with_tn = array();
         $ids_with_tn_full = array();
         chdir(PHOTOSTOCK);
         $total_num_image_ids = count($all_image_ids);
         // make sure fields we are going to write to actually exist
         $this->add_unexisting_image_fields();
         for ($index = $start_index; $index < $total_num_image_ids; $index++) {
             $image_id = $all_image_ids[$index];
             if (file_exists(reason_get_image_path($image_id, 'thumbnail'))) {
                 $image = new entity($image_id);
                 // Check if the entity already has a thumbnail_image_type to avoid clobbering an old value
                 // when the UPDATE SQL query is run
                 // Note: at this point, we have determined a thumbnail exists, so 'thumbnail_image_type'
                 // will not be null if the field already exists
                 if (!$image->has_value('thumbnail_image_type')) {
                     if (file_exists(reason_get_image_path($image_id, 'original'))) {
                         $ids_with_tn_full[] = $image_id;
                     } else {
                         $ids_with_tn[] = $image_id;
                     }
                 }
             }
             if (time() - $start_time > $time_limit) {
                 break;
             }
         }
         $this->run_sql_queries($ids_with_tn, $ids_with_tn_full);
         // Session stuff
         if ($index >= $total_num_image_ids) {
             $_SESSION['reason_image_type_script']['last_end_index'] = -1;
             $_SESSION['reason_image_type_script']['run_again'] = false;
         } else {
             $_SESSION['reason_image_type_script']['last_end_index'] = $index;
             $_SESSION['reason_image_type_script']['run_again'] = true;
         }
         echo '<p>Started at index ' . $start_index . ' out of ' . $total_num_image_ids . '<br />';
         echo 'Ended at index ' . $index . ' (' . round($index / $total_num_image_ids * 100, 2) . '%)<br />';
         echo 'Processed ' . ($index - $start_index) . ' total images<br />';
         echo 'There were a total of ' . count($ids_with_tn) . ' ids with a main image and thumbnail <br />';
         echo 'There were a total of ' . count($ids_with_tn_full) . ' ids with a main image, thumbnail, and full</p>';
     }
 }