| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class for handling service to get status of file population |
| 5 |
* Call should already be verified by permissions callback |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPSynchro\API; |
| 9 |
|
| 10 |
use WPSynchro\Transport\TransferAccessKey; |
| 11 |
use WPSynchro\Files\PopulateFileListState; |
| 12 |
use WPSynchro\Transport\ReturnResult; |
| 13 |
use WPSynchro\Transport\Transfer; |
| 14 |
use WPSynchro\Transport\TransferFile; |
| 15 |
|
| 16 |
class PopulateFileListStatus extends WPSynchroService |
| 17 |
{ |
| 18 |
public $state = null; |
| 19 |
public $basepath = null; |
| 20 |
// Result to be send back |
| 21 |
public $result = null; |
| 22 |
// last downloaded id |
| 23 |
public $last_download_id = 0; |
| 24 |
|
| 25 |
public function __construct() |
| 26 |
{ |
| 27 |
// Initialize return data |
| 28 |
$this->result = new \stdClass(); |
| 29 |
} |
| 30 |
|
| 31 |
public function service() |
| 32 |
{ |
| 33 |
// Get transfer object, so we can get data |
| 34 |
$transfer = new Transfer(); |
| 35 |
$transfer->setEncryptionKey(TransferAccessKey::getAccessKey()); |
| 36 |
$transfer->populateFromString($this->getRequestBody()); |
| 37 |
$body = $transfer->getDataObject(); |
| 38 |
|
| 39 |
// Extract parameters |
| 40 |
$section = $body->section; |
| 41 |
$type = $body->type; |
| 42 |
$job_id = $body->requestid; |
| 43 |
|
| 44 |
// Do some setup |
| 45 |
if ($type == "source") { |
| 46 |
$this->basepath = $section->source_basepath; |
| 47 |
} else { |
| 48 |
$this->basepath = $section->target_basepath; |
| 49 |
} |
| 50 |
|
| 51 |
// Figure out where we are in the process |
| 52 |
$this->state = get_option('wpsynchro_filepopulation_current'); |
| 53 |
|
| 54 |
// Add debugs and errors to result |
| 55 |
$this->setErrorsAndDebugs(); |
| 56 |
|
| 57 |
// Make sure we are checking with the right job and section |
| 58 |
if (is_object($this->state) && $this->state->job_id == $job_id && $this->state->section_id == $section->id) { |
| 59 |
// We are the right place, so return the state |
| 60 |
$this->result->state = $this->state; |
| 61 |
// Get any errors |
| 62 |
|
| 63 |
// If it is completed, return file list |
| 64 |
$this->last_download_id = intval(get_option("wpsynchro_filepopulation_current_download_id")); |
| 65 |
$this->result->filelist = $this->getFileList(990, $this->last_download_id); |
| 66 |
update_option("wpsynchro_filepopulation_current_download_id", $this->last_download_id, false); |
| 67 |
} |
| 68 |
|
| 69 |
$returnresult = new ReturnResult(); |
| 70 |
$returnresult->init(); |
| 71 |
$returnresult->setDataObject($this->result); |
| 72 |
return $returnresult->echoDataFromServiceAndExit(); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get file list on completion, in parts |
| 77 |
*/ |
| 78 |
public function getFileList($max_count, $id_offset) |
| 79 |
{ |
| 80 |
global $wpdb; |
| 81 |
$filelist = []; |
| 82 |
$filelist_sqlresult = $wpdb->get_results($wpdb->prepare("select * from " . $wpdb->prefix . "wpsynchro_file_population_list where id > %d order by id limit %d", $id_offset, $max_count)); |
| 83 |
|
| 84 |
if ($filelist_sqlresult) { |
| 85 |
foreach ($filelist_sqlresult as $filedir) { |
| 86 |
$file = new TransferFile(); |
| 87 |
$file->source_file = str_replace($this->basepath, "", $filedir->source_file); |
| 88 |
$file->size = $filedir->size; |
| 89 |
$file->is_dir = $filedir->is_dir == 0 ? false : true; |
| 90 |
$file->hash = $filedir->hash; |
| 91 |
|
| 92 |
if ($filedir->id > $this->last_download_id) { |
| 93 |
$this->last_download_id = $filedir->id; |
| 94 |
} |
| 95 |
|
| 96 |
$filelist[] = $file; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return $filelist; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get errors and debug from file population and add to result |
| 105 |
*/ |
| 106 |
public function setErrorsAndDebugs() |
| 107 |
{ |
| 108 |
// Get any errors from db |
| 109 |
$logs = get_option("wpsynchro_filepopulation_problems"); |
| 110 |
if (!is_array($logs)) { |
| 111 |
$logs = []; |
| 112 |
} |
| 113 |
|
| 114 |
if (isset($logs["ERROR"]) && count($logs["ERROR"]) > 0) { |
| 115 |
$this->result->errors = $logs["ERROR"]; |
| 116 |
} |
| 117 |
|
| 118 |
if (isset($logs["DEBUG"]) && count($logs["DEBUG"]) > 0) { |
| 119 |
$this->result->debugs = $logs["DEBUG"]; |
| 120 |
} |
| 121 |
|
| 122 |
// Reset logs |
| 123 |
update_option("wpsynchro_filepopulation_problems", [], false); |
| 124 |
} |
| 125 |
} |
| 126 |
|