-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
432 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
<?php | ||
require_once 'vendor/base.php'; | ||
|
||
class AdminTypesManager { | ||
private $typeLevelMap = [ | ||
// Level 1 (Primary/Top-level Divisions) | ||
'country' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'state' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'region' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'province' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'autonomous republic' => ['level' => 1, 'category' => 'AUTONOMOUS'], | ||
'republic' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'federal territory' => ['level' => 1, 'category' => 'FEDERAL'], | ||
'federal district' => ['level' => 1, 'category' => 'FEDERAL'], | ||
'federal capital territory' => ['level' => 1, 'category' => 'FEDERAL'], | ||
'union territory' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'autonomous region' => ['level' => 1, 'category' => 'AUTONOMOUS'], | ||
'Special region' => ['level' => 1, 'category' => 'SPECIAL'], | ||
'special administrative region' => ['level' => 1, 'category' => 'SPECIAL'], | ||
'autonomous community' => ['level' => 1, 'category' => 'AUTONOMOUS'], | ||
'capital territory' => ['level' => 1, 'category' => 'CAPITAL'], | ||
'oblast' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'voivodship' => ['level' => 1, 'category' => 'PRIMARY'], | ||
'emirate' => ['level' => 1, 'category' => 'PRIMARY'], | ||
|
||
// Level 2 (Secondary Divisions) | ||
'county' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'district' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'prefecture' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'department' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'governorate' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'division' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'capital district' => ['level' => 2, 'category' => 'CAPITAL'], | ||
'metropolitan district' => ['level' => 2, 'category' => 'METROPOLITAN'], | ||
'london borough' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'two-tier county' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'council area' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'unitary authority' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'city corporation' => ['level' => 2, 'category' => 'MUNICIPAL'], | ||
'administrative region' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'metropolitan region' => ['level' => 2, 'category' => 'METROPOLITAN'], | ||
'metropolitan administration' => ['level' => 2, 'category' => 'METROPOLITAN'], | ||
'metropolitan city' => ['level' => 2, 'category' => 'METROPOLITAN'], | ||
'metropolitan department' => ['level' => 2, 'category' => 'METROPOLITAN'], | ||
'autonomous district' => ['level' => 2, 'category' => 'AUTONOMOUS'], | ||
'regional unit' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'territorial unit' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'geographical region' => ['level' => 2, 'category' => 'GEOGRAPHIC'], | ||
'economic prefecture' => ['level' => 2, 'category' => 'SPECIAL'], | ||
|
||
// Level 3 (Local Divisions) | ||
'municipality' => ['level' => 3, 'category' => 'MUNICIPAL'], | ||
'municipalities' => ['level' => 3, 'category' => 'MUNICIPAL'], | ||
'city' => ['level' => 3, 'category' => 'MUNICIPAL'], | ||
'capital city' => ['level' => 3, 'category' => 'CAPITAL'], | ||
'special municipality' => ['level' => 3, 'category' => 'SPECIAL'], | ||
'district municipality' => ['level' => 3, 'category' => 'MUNICIPAL'], | ||
'urban municipality' => ['level' => 3, 'category' => 'MUNICIPAL'], | ||
'autonomous municipality' => ['level' => 3, 'category' => 'AUTONOMOUS'], | ||
'special city' => ['level' => 3, 'category' => 'SPECIAL'], | ||
'autonomous city' => ['level' => 3, 'category' => 'AUTONOMOUS'], | ||
'city with county rights' => ['level' => 3, 'category' => 'SPECIAL'], | ||
'state city' => ['level' => 3, 'category' => 'SPECIAL'], | ||
'district council' => ['level' => 3, 'category' => 'COUNCIL'], | ||
'borough council' => ['level' => 3, 'category' => 'COUNCIL'], | ||
'town council' => ['level' => 3, 'category' => 'COUNCIL'], | ||
'island council' => ['level' => 3, 'category' => 'COUNCIL'], | ||
'local council' => ['level' => 3, 'category' => 'COUNCIL'], | ||
'commune' => ['level' => 3, 'category' => 'MUNICIPAL'], | ||
'canton' => ['level' => 3, 'category' => 'ADMINISTRATIVE'], | ||
'borough' => ['level' => 3, 'category' => 'ADMINISTRATIVE'], | ||
'parish' => ['level' => 3, 'category' => 'ADMINISTRATIVE'], | ||
'quarter' => ['level' => 3, 'category' => 'ADMINISTRATIVE'], | ||
|
||
// Level 4 (Special/Geographic/Minor Divisions) | ||
'village' => ['level' => 4, 'category' => 'SETTLEMENT'], | ||
'island' => ['level' => 4, 'category' => 'GEOGRAPHIC'], | ||
'administrative atoll' => ['level' => 4, 'category' => 'GEOGRAPHIC'], | ||
'special island authority' => ['level' => 4, 'category' => 'SPECIAL'], | ||
'islands / groups of islands' => ['level' => 4, 'category' => 'GEOGRAPHIC'], | ||
'arctic region' => ['level' => 4, 'category' => 'GEOGRAPHIC'], | ||
'administered area' => ['level' => 4, 'category' => 'ADMINISTRATIVE'], | ||
|
||
// Special Categories | ||
'overseas territory' => ['level' => 2, 'category' => 'OVERSEAS'], | ||
'overseas region' => ['level' => 2, 'category' => 'OVERSEAS'], | ||
'overseas collectivity' => ['level' => 2, 'category' => 'OVERSEAS'], | ||
'European collectivity' => ['level' => 2, 'category' => 'OVERSEAS'], | ||
'metropolitan collectivity with special status' => ['level' => 2, 'category' => 'SPECIAL'], | ||
'autonomous city in North Africa' => ['level' => 2, 'category' => 'AUTONOMOUS'], | ||
'districts under republic administration' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'decentralized regional entity' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'free municipal consortium' => ['level' => 3, 'category' => 'MUNICIPAL'], | ||
'autonomous territorial unit' => ['level' => 2, 'category' => 'AUTONOMOUS'], | ||
'special self-governing province' => ['level' => 1, 'category' => 'SPECIAL'], | ||
'special self-governing city' => ['level' => 2, 'category' => 'SPECIAL'], | ||
'federal dependency' => ['level' => 2, 'category' => 'FEDERAL'], | ||
'administrative territory' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'indigenous region' => ['level' => 2, 'category' => 'SPECIAL'], | ||
|
||
// Edge Cases and Special Handling | ||
'administration' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'area' => ['level' => 2, 'category' => 'GEOGRAPHIC'], | ||
'territory' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'capital' => ['level' => 2, 'category' => 'CAPITAL'], | ||
'entity' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'dependency' => ['level' => 2, 'category' => 'SPECIAL'], | ||
'chain' => ['level' => 4, 'category' => 'GEOGRAPHIC'], | ||
'popularate' => ['level' => 2, 'category' => 'ADMINISTRATIVE'], | ||
'land' => ['level' => 2, 'category' => 'GEOGRAPHIC'], | ||
'sheadings' => ['level' => 3, 'category' => 'ADMINISTRATIVE'], | ||
'NULL' => ['level' => 2, 'category' => 'ADMINISTRATIVE'] | ||
]; | ||
|
||
private $categories = [ | ||
'PRIMARY' => 'Primary administrative divisions', | ||
'ADMINISTRATIVE' => 'General administrative divisions', | ||
'MUNICIPAL' => 'Municipal level divisions', | ||
'METROPOLITAN' => 'Metropolitan and urban divisions', | ||
'AUTONOMOUS' => 'Self-governing divisions', | ||
'SPECIAL' => 'Special status divisions', | ||
'CAPITAL' => 'Capital related divisions', | ||
'FEDERAL' => 'Federal level divisions', | ||
'GEOGRAPHIC' => 'Geographic based divisions', | ||
'COUNCIL' => 'Council based divisions', | ||
'SETTLEMENT' => 'Settlement based divisions', | ||
'OVERSEAS' => 'Overseas territories and divisions' | ||
]; | ||
|
||
public function migrateTypes($conn) { | ||
$conn->begin_transaction(); | ||
|
||
try { | ||
// Create types table | ||
$this->createTypesTable($conn); | ||
|
||
// Insert all types | ||
foreach ($this->typeLevelMap as $type => $info) { | ||
$this->insertType($conn, $type, $info); | ||
} | ||
|
||
$conn->commit(); | ||
echo "Successfully migrated all administrative types\n"; | ||
} catch (Exception $e) { | ||
$conn->rollback(); | ||
echo "Error during migration: " . $e->getMessage() . "\n"; | ||
} | ||
} | ||
|
||
private function createTypesTable($conn) { | ||
$sql = " | ||
CREATE TABLE IF NOT EXISTS division_types ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(100) NOT NULL, | ||
normalized_name VARCHAR(100) NOT NULL, | ||
level INT NOT NULL, | ||
category VARCHAR(50) NOT NULL, | ||
description TEXT, | ||
metadata JSON, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
UNIQUE KEY (normalized_name) | ||
) | ||
"; | ||
$conn->query($sql); | ||
} | ||
|
||
private function insertType($conn, $type, $info) { | ||
$stmt = $conn->prepare(" | ||
INSERT INTO division_types | ||
(name, normalized_name, level, category, description) | ||
VALUES (?, ?, ?, ?, ?) | ||
ON DUPLICATE KEY UPDATE | ||
level = VALUES(level), | ||
category = VALUES(category), | ||
description = VALUES(description) | ||
"); | ||
|
||
$normalizedName = $this->normalizeName($type); | ||
$description = $this->getDescription($type, $info['category']); | ||
|
||
$stmt->bind_param( | ||
"ssiss", | ||
$type, | ||
$normalizedName, | ||
$info['level'], | ||
$info['category'], | ||
$description | ||
); | ||
|
||
$stmt->execute(); | ||
} | ||
|
||
private function normalizeName($type) { | ||
return strtolower(trim(str_replace([' ', '/', '-'], '_', $type))); | ||
} | ||
|
||
private function getDescription($type, $category) { | ||
return sprintf( | ||
"%s - %s level division (%s)", | ||
$type, | ||
$this->categories[$category], | ||
$category | ||
); | ||
} | ||
} | ||
|
||
// Usage | ||
try { | ||
$manager = new AdminTypesManager(); | ||
$manager->migrateTypes($conn); | ||
} catch (Exception $e) { | ||
echo "Error: " . $e->getMessage() . "\n"; | ||
} |
Oops, something went wrong.