[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/resources/sql/patches/ -> 090.forceuniqueprojectnames.php (source)

   1  <?php
   2  
   3  echo "Ensuring project names are unique enough...\n";
   4  $table = new PhabricatorProject();
   5  $table->openTransaction();
   6  $table->beginReadLocking();
   7  
   8  $projects = $table->loadAll();
   9  
  10  $slug_map = array();
  11  
  12  foreach ($projects as $project) {
  13    $project->setPhrictionSlug($project->getName());
  14    $slug = $project->getPhrictionSlug();
  15    if ($slug == '/') {
  16      $project_id = $project->getID();
  17      echo "Project #{$project_id} doesn't have a meaningful name...\n";
  18      $project->setName(trim('Unnamed Project '.$project->getName()));
  19    }
  20    $slug_map[$slug][] = $project->getID();
  21  }
  22  
  23  
  24  foreach ($slug_map as $slug => $similar) {
  25    if (count($similar) <= 1) {
  26      continue;
  27    }
  28    echo "Too many projects are similar to '{$slug}'...\n";
  29  
  30    foreach (array_slice($similar, 1, null, true) as $key => $project_id) {
  31      $project = $projects[$project_id];
  32      $old_name = $project->getName();
  33      $new_name = rename_project($project, $projects);
  34  
  35      echo "Renaming project #{$project_id} ".
  36           "from '{$old_name}' to '{$new_name}'.\n";
  37      $project->setName($new_name);
  38    }
  39  }
  40  
  41  $update = $projects;
  42  while ($update) {
  43    $size = count($update);
  44    foreach ($update as $key => $project) {
  45      $id = $project->getID();
  46      $name = $project->getName();
  47      $project->setPhrictionSlug($name);
  48      $slug = $project->getPhrictionSlug();
  49  
  50      echo "Updating project #{$id} '{$name}' ({$slug})...";
  51      try {
  52        queryfx(
  53          $project->establishConnection('w'),
  54          'UPDATE %T SET name = %s, phrictionSlug = %s WHERE id = %d',
  55          $project->getTableName(),
  56          $name,
  57          $slug,
  58          $project->getID());
  59        unset($update[$key]);
  60        echo "okay.\n";
  61      } catch (AphrontDuplicateKeyQueryException $ex) {
  62        echo "failed, will retry.\n";
  63      }
  64    }
  65    if (count($update) == $size) {
  66      throw new Exception(
  67        'Failed to make any progress while updating projects. Schema upgrade '.
  68        'has failed. Go manually fix your project names to be unique (they are '.
  69        'probably ridiculous?) and then try again.');
  70    }
  71  }
  72  
  73  $table->endReadLocking();
  74  $table->saveTransaction();
  75  echo "Done.\n";
  76  
  77  
  78  /**
  79   * Rename the project so that it has a unique slug, by appending (2), (3), etc.
  80   * to its name.
  81   */
  82  function rename_project($project, $projects) {
  83    $suffix = 2;
  84    while (true) {
  85      $new_name = $project->getName().' ('.$suffix.')';
  86      $project->setPhrictionSlug($new_name);
  87      $new_slug = $project->getPhrictionSlug();
  88  
  89      $okay = true;
  90      foreach ($projects as $other) {
  91        if ($other->getID() == $project->getID()) {
  92          continue;
  93        }
  94        if ($other->getPhrictionSlug() == $new_slug) {
  95          $okay = false;
  96          break;
  97        }
  98      }
  99      if ($okay) {
 100        break;
 101      } else {
 102        $suffix++;
 103      }
 104    }
 105  
 106    return $new_name;
 107  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1