예제 #1
0
/**
 * Recursively scan directories and make a list of the deepest folders
 *
 * @global type $TMP_FOLDERS
 * @param type $base_folder
 * @return type
 */
function unc_tools_recurse_folders($base_folder)
{
    global $TMP_FOLDERS, $UNC_GALLERY;
    // if ($UNC_GALLERY['debug']) {XMPP_ERROR_trace(__FUNCTION__);}
    if (strpos($base_folder, './')) {
        die("Error, recursive path! {$base_folder}");
    }
    $has_subfolder = false;
    if (!file_exists($base_folder)) {
        if ($UNC_GALLERY['debug']) {
            XMPP_ERROR_trace("Base folder does not exist: ", $base_folder);
        }
        return false;
    }
    foreach (glob($base_folder . "/*") as $folder) {
        // found a sub-folder, go deeper
        if (is_dir($folder)) {
            unc_tools_recurse_folders($folder);
            $has_subfolder = true;
        }
    }
    if (!$has_subfolder) {
        $path_arr = explode("/", $base_folder);
        $date_elements = array_slice($path_arr, -3, 3);
        $date_string = implode("/", $date_elements);
        $TMP_FOLDERS[$date_string] = $base_folder;
    }
    return $TMP_FOLDERS;
}
예제 #2
0
/**
 * build the missing data
 *
 * @global type $UNC_GALLERY
 */
function unc_gallery_admin_rebuild_data()
{
    global $UNC_GALLERY, $wpdb;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    ob_clean();
    $max_time = ini_get('max_execution_time');
    if (!current_user_can('manage_options')) {
        echo "Cannot rebuild data, you are not admin!";
        wp_die();
    }
    $dirPath = $UNC_GALLERY['upload_path'];
    // let's count the number of files in the database so
    // we get an image how much work is to do
    $count_files_sql = "SELECT count(id) AS counter FROM " . $wpdb->prefix . "unc_gallery_img;";
    $file_counter = $wpdb->get_results($count_files_sql, 'ARRAY_A');
    $count = $file_counter[0]['counter'];
    // get the Process ID for the Ajax live update
    $process_id = filter_input(INPUT_POST, 'process_id');
    // send the first update
    $process_step_id = unc_tools_progress_update($process_id, "Cleared existing data");
    // TODO Check where do we delete empty folders?
    // unc_tools_folder_delete_empty($data_folder);
    // iterate all image folders
    $photo_folder = $dirPath . "/" . $UNC_GALLERY['photos'];
    $target_folders = unc_tools_recurse_folders($photo_folder);
    // calculate progress update percentages
    $overall_one_percent = 100 / $count;
    $overall_percentage = 0;
    $text = '';
    foreach ($target_folders as $date => $folder) {
        $process_step_id++;
        $text = "Processing {$date}: <span class=\"file_progress\" style=\"width:0%\">0 %</span>";
        $process_step_id = unc_tools_progress_update($process_id, $text, $overall_percentage);
        // iterate all files in a folder, write file info to DB
        $folder_files = glob($folder . "/*");
        $folder_file_count = count($folder_files);
        $file_one_percent = 100 / $folder_file_count;
        $folder_percentage = 0;
        foreach ($folder_files as $image_file) {
            if (!is_dir($image_file)) {
                // TODO: ERror in case the info cannot be written
                unc_image_info_write($image_file);
                $folder_percentage += $file_one_percent;
                $overall_percentage += $overall_one_percent;
            }
            $folder_percentage_text = intval($folder_percentage);
            $text = "Processing {$date}: <span class=\"file_progress\" style=\"width:{$folder_percentage_text}%\">{$folder_percentage_text} %</span>";
            unc_tools_progress_update($process_id, $text, $overall_percentage, $process_step_id);
        }
        $text = "Processing {$date}: <span class=\"file_progress\" style=\"width:100%\">100 %</span>";
        unc_tools_progress_update($process_id, $text, $overall_percentage, $process_step_id);
    }
    unc_tools_progress_update($process_id, "Done!", 100);
    // this signals to the JS function that we can terminate the process_get loop
    unc_tools_progress_update($process_id, false);
    wp_die();
}