/**
     * 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
        */
        if (isset($_SESSION['reason_thumbnail_standardize_script']['last_end_index'])) {
            $start_index = $_SESSION['reason_thumbnail_standardize_script']['last_end_index'];
        } else {
            $start_index = 0;
        }
        if ($start_index == -1) {
            return 'This script has already processed all images during this session!' . '<br />';
        } else {
            $start_time = time();
            $php_max_time = ini_get('max_execution_time');
            if ($php_max_time == 0) {
                $time_limit = 15;
            } else {
                $time_limit = min($php_max_time / 2, 15);
            }
            $es = new entity_selector();
            $es->add_type(id_of('image'));
            $all_image_ids = $es->get_ids();
            $ids_with_only_tb = array();
            $total_num_images = sizeof($all_image_ids);
            /* Start processing images where last time left off, or start at the beginning */
            /* Runs for $time_limit seconds at a time, continues process when script is run again */
            for ($i = $start_index; $i < $total_num_images; $i++) {
                $main_image_path = reason_get_image_path($all_image_ids[$i]);
                /* If no main image exists, copy the thumbnail to be the main image */
                if (!file_exists($main_image_path)) {
                    $thumbnail_path = reason_get_image_path($all_image_ids[$i], 'thumbnail');
                    if (file_exists($thumbnail_path)) {
                        $ids_with_only_tb[] = $all_image_ids[$i];
                        if (!copy($thumbnail_path, $main_image_path)) {
                            echo 'failed to copy image of id ' . $all_image_ids[$i];
                        }
                    }
                }
                if (time() - $start_time > $time_limit) {
                    break;
                }
            }
            /* If all images have been processed, reset the last index*/
            if ($i >= $total_num_images) {
                $_SESSION['reason_thumbnail_standardize_script']['last_end_index'] = -1;
                $_SESSION['reason_thumbnail_standardize_script']['run_again'] = false;
            } else {
                $_SESSION['reason_thumbnail_standardize_script']['last_end_index'] = $i;
                $_SESSION['reason_thumbnail_standardize_script']['run_again'] = true;
            }
            /* Give user some info on how many more times script should be run */
            $percent_remaining = substr(($total_num_images - $i) / $total_num_images * 100, 0, 4);
            echo '<br /> There are ' . $total_num_images . ' total images to process';
            echo '<br /> This execution of the script started at the ' . $start_index . 'th photo';
            echo '<br /> It processed a total of ' . ($i - $start_index) . ' images, and created new main
			images from ' . sizeof($ids_with_only_tb) . ' thumbnails.';
            echo '<br /> A total of ' . ($total_num_images - $i) . ', (' . $percent_remaining . '%)
			images still need to be processed <br />';
        }
    }
 protected function get_existing_section_ids($org_id = null)
 {
     $es = new entity_selector();
     $es->add_type(id_of('course_section_type'));
     if ($org_id) {
         $es->add_relation('org_id = "' . mysql_real_escape_string($org_id) . '"');
     }
     if ($result = $es->get_ids()) {
         return $result;
     } else {
         return array();
     }
 }
 /**
  * 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>';
     }
 }