PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.17
Yatra – Travel Booking & Tour Operator Software v3.0.17
3.0.17 3.0.16 3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 All 85 releases
yatra / app / Repositories / ExportImportRepository.php

ExportImportRepository.php in Yatra – Travel Booking & Tour Operator Software 3.0.17, at app/Repositories/ExportImportRepository.php

204 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Find a row's id by its business-unique columns.
144 *
145 * Import used to insert every row unconditionally, so re-running it
146 * duplicated everything that had no unique constraint and rejected
147 * everything that had one. Looking the row up first lets the importer
148 * recognise what it has already brought in.
149 *
150 * @param array<string, scalar|null> $criteria column => value (all must match)
151 * @return int|null existing row id, or null when there is no match
152 */
153 public function findRowIdBy(string $tableName, array $criteria): ?int
154 {
155 global $wpdb;
156
157 if ($criteria === []) {
158 return null;
159 }
160
161 $where = [];
162 $values = [];
163 foreach ($criteria as $column => $value) {
164 if (!preg_match('/^[a-zA-Z0-9_]+$/', (string) $column)) {
165 return null;
166 }
167 if ($value === null) {
168 $where[] = "`{$column}` IS NULL";
169 continue;
170 }
171 $where[] = "`{$column}` = %s";
172 $values[] = (string) $value;
173 }
174
175 $sql = "SELECT id FROM `" . esc_sql($tableName) . "` WHERE " . implode(' AND ', $where) . " LIMIT 1";
176 if ($values !== []) {
177 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
178 $sql = $wpdb->prepare($sql, $values);
179 }
180
181 $id = $wpdb->get_var($sql);
182
183 return $id !== null ? (int) $id : null;
184 }
185
186 /**
187 * Insert and return new auto-increment id, or null on failure.
188 */
189 public function insertRecordReturningId(string $tableName, array $record): ?int
190 {
191 global $wpdb;
192
193 $result = $wpdb->insert($tableName, $record);
194
195 if ($result === false) {
196 return null;
197 }
198
199 $id = (int) $wpdb->insert_id;
200
201 return $id > 0 ? $id : null;
202 }
203 }
204