| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Repositories; |
| 6 |
|
| 7 |
/** |
| 8 |
* Export/Import Repository |
| 9 |
* |
| 10 |
* Handles database operations for export/import functionality. |
| 11 |
*/ |
| 12 |
class ExportImportRepository |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Get MySQL version |
| 16 |
*/ |
| 17 |
public function getMySQLVersion(): string |
| 18 |
{ |
| 19 |
global $wpdb; |
| 20 |
return $wpdb->get_var("SELECT VERSION()") ?: 'Unknown'; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get all job options from database |
| 25 |
*/ |
| 26 |
public function getAllJobOptions(): array |
| 27 |
{ |
| 28 |
global $wpdb; |
| 29 |
|
| 30 |
return $wpdb->get_results( |
| 31 |
"SELECT option_name, option_value FROM {$wpdb->options} |
| 32 |
WHERE option_name LIKE 'yatra_job_%'" |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get job options for specific user |
| 38 |
*/ |
| 39 |
public function getJobOptionsForUser(int $userId): array |
| 40 |
{ |
| 41 |
$options = $this->getAllJobOptions(); |
| 42 |
|
| 43 |
$jobs = []; |
| 44 |
foreach ($options as $option) { |
| 45 |
$jobData = maybe_unserialize($option->option_value); |
| 46 |
if (is_array($jobData) && isset($jobData['user_id']) && (int) $jobData['user_id'] === $userId) { |
| 47 |
$jobs[] = $jobData; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $jobs; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Check if table exists |
| 56 |
*/ |
| 57 |
public function tableExists(string $tableName): bool |
| 58 |
{ |
| 59 |
global $wpdb; |
| 60 |
$tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'"); |
| 61 |
return !empty($tableExists); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get count of records in table |
| 66 |
*/ |
| 67 |
public function getRecordCount(string $tableName): int |
| 68 |
{ |
| 69 |
global $wpdb; |
| 70 |
return (int) $wpdb->get_var("SELECT COUNT(*) FROM $tableName"); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get batch of records from table |
| 75 |
*/ |
| 76 |
public function getBatchRecords(string $tableName, int $offset, int $limit): array |
| 77 |
{ |
| 78 |
global $wpdb; |
| 79 |
|
| 80 |
return $wpdb->get_results( |
| 81 |
$wpdb->prepare( |
| 82 |
"SELECT * FROM `{$tableName}` LIMIT %d, %d", |
| 83 |
$offset, |
| 84 |
$limit |
| 85 |
) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Count rows in classifications table for a single type (destinations, activities, etc.). |
| 91 |
*/ |
| 92 |
public function getClassificationCount(string $tableName, string $type): int |
| 93 |
{ |
| 94 |
global $wpdb; |
| 95 |
|
| 96 |
return (int) $wpdb->get_var( |
| 97 |
$wpdb->prepare( |
| 98 |
"SELECT COUNT(*) FROM `{$tableName}` WHERE `type` = %s", |
| 99 |
$type |
| 100 |
) |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Batch fetch classification rows for export. |
| 106 |
* |
| 107 |
* @return array<int, object|array> |
| 108 |
*/ |
| 109 |
public function getClassificationBatch(string $tableName, string $type, int $offset, int $limit): array |
| 110 |
{ |
| 111 |
global $wpdb; |
| 112 |
|
| 113 |
$results = $wpdb->get_results( |
| 114 |
$wpdb->prepare( |
| 115 |
"SELECT * FROM `{$tableName}` WHERE `type` = %s ORDER BY `id` ASC LIMIT %d, %d", |
| 116 |
$type, |
| 117 |
$offset, |
| 118 |
$limit |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
return is_array($results) ? $results : []; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get table columns |
| 127 |
*/ |
| 128 |
public function getTableColumns(string $tableName): array |
| 129 |
{ |
| 130 |
global $wpdb; |
| 131 |
return $wpdb->get_col("DESC {$tableName}", 0); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Insert record into table |
| 136 |
*/ |
| 137 |
public function insertRecord(string $tableName, array $record): bool |
| 138 |
{ |
| 139 |
return $this->insertRecordReturningId($tableName, $record) !== null; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Insert and return new auto-increment id, or null on failure. |
| 144 |
*/ |
| 145 |
public function insertRecordReturningId(string $tableName, array $record): ?int |
| 146 |
{ |
| 147 |
global $wpdb; |
| 148 |
|
| 149 |
$result = $wpdb->insert($tableName, $record); |
| 150 |
|
| 151 |
if ($result === false) { |
| 152 |
return null; |
| 153 |
} |
| 154 |
|
| 155 |
$id = (int) $wpdb->insert_id; |
| 156 |
|
| 157 |
return $id > 0 ? $id : null; |
| 158 |
} |
| 159 |
} |
| 160 |
|