Changeset View
Changeset View
Standalone View
Standalone View
resources/sql/patches/090.forceuniqueprojectnames.php
| <?php | <?php | ||||
| echo "Ensuring project names are unique enough...\n"; | echo pht('Ensuring project names are unique enough...')."\n"; | ||||
| $table = new PhabricatorProject(); | $table = new PhabricatorProject(); | ||||
| $table->openTransaction(); | $table->openTransaction(); | ||||
| $table->beginReadLocking(); | $table->beginReadLocking(); | ||||
| $projects = $table->loadAll(); | $projects = $table->loadAll(); | ||||
| $slug_map = array(); | $slug_map = array(); | ||||
| foreach ($projects as $project) { | foreach ($projects as $project) { | ||||
| $project->setPhrictionSlug($project->getName()); | $project->setPhrictionSlug($project->getName()); | ||||
| $slug = $project->getPhrictionSlug(); | $slug = $project->getPhrictionSlug(); | ||||
| if ($slug == '/') { | if ($slug == '/') { | ||||
| $project_id = $project->getID(); | $project_id = $project->getID(); | ||||
| echo "Project #{$project_id} doesn't have a meaningful name...\n"; | echo pht("Project #%d doesn't have a meaningful name...", $project_id)."\n"; | ||||
| $project->setName(trim('Unnamed Project '.$project->getName())); | $project->setName(trim(pht('Unnamed Project %s', $project->getName()))); | ||||
| } | } | ||||
| $slug_map[$slug][] = $project->getID(); | $slug_map[$slug][] = $project->getID(); | ||||
| } | } | ||||
| foreach ($slug_map as $slug => $similar) { | foreach ($slug_map as $slug => $similar) { | ||||
| if (count($similar) <= 1) { | if (count($similar) <= 1) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| echo "Too many projects are similar to '{$slug}'...\n"; | echo pht("Too many projects are similar to '%s'...", $slug)."\n"; | ||||
| foreach (array_slice($similar, 1, null, true) as $key => $project_id) { | foreach (array_slice($similar, 1, null, true) as $key => $project_id) { | ||||
| $project = $projects[$project_id]; | $project = $projects[$project_id]; | ||||
| $old_name = $project->getName(); | $old_name = $project->getName(); | ||||
| $new_name = rename_project($project, $projects); | $new_name = rename_project($project, $projects); | ||||
| echo "Renaming project #{$project_id} ". | echo pht( | ||||
| "from '{$old_name}' to '{$new_name}'.\n"; | "Renaming project #%d from '%s' to '%s'.\n", | ||||
| $project_id, | |||||
| $old_name, | |||||
| $new_name); | |||||
| $project->setName($new_name); | $project->setName($new_name); | ||||
| } | } | ||||
| } | } | ||||
| $update = $projects; | $update = $projects; | ||||
| while ($update) { | while ($update) { | ||||
| $size = count($update); | $size = count($update); | ||||
| foreach ($update as $key => $project) { | foreach ($update as $key => $project) { | ||||
| $id = $project->getID(); | $id = $project->getID(); | ||||
| $name = $project->getName(); | $name = $project->getName(); | ||||
| $project->setPhrictionSlug($name); | $project->setPhrictionSlug($name); | ||||
| $slug = $project->getPhrictionSlug(); | $slug = $project->getPhrictionSlug(); | ||||
| echo "Updating project #{$id} '{$name}' ({$slug})..."; | echo pht("Updating project #%d '%s' (%s)... ", $id, $name, $slug); | ||||
| try { | try { | ||||
| queryfx( | queryfx( | ||||
| $project->establishConnection('w'), | $project->establishConnection('w'), | ||||
| 'UPDATE %T SET name = %s, phrictionSlug = %s WHERE id = %d', | 'UPDATE %T SET name = %s, phrictionSlug = %s WHERE id = %d', | ||||
| $project->getTableName(), | $project->getTableName(), | ||||
| $name, | $name, | ||||
| $slug, | $slug, | ||||
| $project->getID()); | $project->getID()); | ||||
| unset($update[$key]); | unset($update[$key]); | ||||
| echo "okay.\n"; | echo pht('OKAY')."\n"; | ||||
| } catch (AphrontDuplicateKeyQueryException $ex) { | } catch (AphrontDuplicateKeyQueryException $ex) { | ||||
| echo "failed, will retry.\n"; | echo pht('Failed, will retry.')."\n"; | ||||
| } | } | ||||
| } | } | ||||
| if (count($update) == $size) { | if (count($update) == $size) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | |||||
| 'Failed to make any progress while updating projects. Schema upgrade '. | 'Failed to make any progress while updating projects. Schema upgrade '. | ||||
| 'has failed. Go manually fix your project names to be unique (they are '. | 'has failed. Go manually fix your project names to be unique '. | ||||
| 'probably ridiculous?) and then try again.'); | '(they are probably ridiculous?) and then try again.')); | ||||
| } | } | ||||
| } | } | ||||
| $table->endReadLocking(); | $table->endReadLocking(); | ||||
| $table->saveTransaction(); | $table->saveTransaction(); | ||||
| echo "Done.\n"; | echo pht('Done.')."\n"; | ||||
| /** | /** | ||||
| * Rename the project so that it has a unique slug, by appending (2), (3), etc. | * Rename the project so that it has a unique slug, by appending (2), (3), etc. | ||||
| * to its name. | * to its name. | ||||
| */ | */ | ||||
| function rename_project($project, $projects) { | function rename_project($project, $projects) { | ||||
| $suffix = 2; | $suffix = 2; | ||||
| Show All 24 Lines | |||||