public static function execute( $params ) { // Set content offset if ( isset( $params['content_offset'] ) ) { $content_offset = (int) $params['content_offset']; } else { $content_offset = 0; } // Set filemap offset if ( isset( $params['filemap_offset'] ) ) { $filemap_offset = (int) $params['filemap_offset']; } else { $filemap_offset = 0; } // Get total files if ( isset( $params['total_files'] ) ) { $total_files = (int) $params['total_files']; } else { $total_files = 1; } // Get total size if ( isset( $params['total_size'] ) ) { $total_size = (int) $params['total_size']; } else { $total_size = 1; } // Get processed files if ( isset( $params['processed'] ) ) { $processed = (int) $params['processed']; } else { $processed = 0; } // What percent of files have we processed? $progress = (int) ( ( $processed / $total_size ) * 100 ); // Set progress if ( empty( $content_offset ) ) { Ai1wm_Status::info( sprintf( __( 'Archiving %d files...<br />%d%% complete', AI1WM_PLUGIN_NAME ), $total_files, $progress ) ); } // Get map file $filemap = fopen( ai1wm_filemap_path( $params ), 'r' ); // Start time $start = microtime( true ); // Flag to hold if all files have been processed $completed = true; // Set filemap pointer at the current index if ( fseek( $filemap, $filemap_offset ) !== -1 ) { // Get archive $archive = new Ai1wm_Compressor( ai1wm_archive_path( $params ) ); while ( $path = trim( fgets( $filemap ) ) ) { try { // Add file to archive if ( ( $content_offset = $archive->add_file( WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $path, $path, $content_offset, 10 ) ) ) { // Set progress if ( ( $processed += $content_offset ) ) { $progress = (int) ( ( $processed / $total_size ) * 100 ); } // Set progress Ai1wm_Status::info( sprintf( __( 'Archiving %d files...<br />%d%% complete', AI1WM_PLUGIN_NAME ), $total_files, $progress ) ); // Set content offset $params['content_offset'] = $content_offset; // Set filemap offset $params['filemap_offset'] = $filemap_offset; // Set completed flag $params['completed'] = false; // Close the filemap file fclose( $filemap ); return $params; } // Set content offset $content_offset = 0; // Set filemap offset $filemap_offset = ftell( $filemap ); } catch ( Exception $e ) { // Skip bad file permissions } // Increment processed files $processed += $archive->get_current_filesize(); // More than 10 seconds have passed, break and do another request if ( ( microtime( true ) - $start ) > 10 ) { $completed = false; break; } } $archive->close(); } // Set content offset $params['content_offset'] = $content_offset; // Set filemap offset $params['filemap_offset'] = $filemap_offset; // Set processed files $params['processed'] = $processed; // Set completed flag $params['completed'] = $completed; // Close the filemap file fclose( $filemap ); return $params; }
/** * Add content files and directories * * @return void */ public function content() { // Set content offset if (isset($this->args['content_offset'])) { $content_offset = $this->args['content_offset']; } else { $content_offset = 0; } // Set filemap offset if (isset($this->args['filemap_offset'])) { $filemap_offset = $this->args['filemap_offset']; } else { $filemap_offset = 0; } // Set total files if (isset($this->args['total'])) { $total = $this->args['total']; } else { $total = 1; } // Set processed files if (isset($this->args['processed'])) { $processed = $this->args['processed']; } else { $processed = 0; } // What percent of files have we processed? $progress = (int) ($processed / $total * 100); // Set progress if (empty($content_offset)) { Ai1wm_Status::set(array('type' => 'info', 'message' => sprintf(__('Archiving %d files...<br />%.2f%% complete', AI1WM_PLUGIN_NAME), $total, $progress))); } // Get map file $filemap = fopen($this->storage()->filemap(), 'r'); // Start time $start = microtime(true); // Flag to hold if all files have been processed $completed = true; // Set file map pointer at the current index if (fseek($filemap, $filemap_offset) !== -1) { // Get archive $archive = new Ai1wm_Compressor($this->storage()->archive()); while ($path = trim(fgets($filemap))) { try { // Set absolute path $abs_path = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $path; // Add file to archive if ($content_offset = $archive->add_file($abs_path, $path, $content_offset, 3)) { // Set progress if (($sub_progress = $content_offset / $archive->get_current_filesize()) < 1) { $progress += $sub_progress; } // Set progress Ai1wm_Status::set(array('type' => 'info', 'message' => sprintf(__('Archiving %d files...<br />%.2f%% complete', AI1WM_PLUGIN_NAME), $total, $progress))); // Set content offset $this->args['content_offset'] = $content_offset; // Set filemap offset $this->args['filemap_offset'] = $filemap_offset; // Close the filemap file fclose($filemap); // Redirect return $this->route_to('content'); } // Set content offset $content_offset = 0; // Set filemap offset $filemap_offset = ftell($filemap); } catch (Exception $e) { // Skip bad file permissions } $processed++; // Time elapsed if (microtime(true) - $start > 3) { // More than 3 seconds have passed, break and do another request $completed = false; break; } } $archive->close(); } // Set content offset $this->args['content_offset'] = $content_offset; // Set filemap offset $this->args['filemap_offset'] = $filemap_offset; // Set processed files $this->args['processed'] = $processed; // Close the filemap file fclose($filemap); // Redirect if ($completed) { $this->route_to('database'); } else { $this->route_to('content'); } }