private function pushToHgRepository(PhabricatorRepository $repository, PhabricatorRepositoryURI $mirror_uri)
 {
     $argv = array('push --verbose --rev tip -- %P', $mirror_uri->getURIEnvelope());
     $future = $mirror_uri->newCommandEngine()->setArgv($argv)->newFuture();
     try {
         $future->setCWD($repository->getLocalPath())->resolvex();
     } catch (CommandException $ex) {
         if (preg_match('/no changes found/', $ex->getStdOut())) {
             // mercurial says nothing changed, but that's good
         } else {
             throw $ex;
         }
     }
 }
 public function buildManagementPanelContent()
 {
     $repository = $this->getRepository();
     $viewer = $this->getViewer();
     $uris = $repository->getURIs();
     Javelin::initBehavior('phabricator-tooltips');
     $rows = array();
     foreach ($uris as $uri) {
         $uri_name = $uri->getDisplayURI();
         $uri_name = phutil_tag('a', array('href' => $uri->getViewURI()), $uri_name);
         if ($uri->getIsDisabled()) {
             $status_icon = 'fa-times grey';
         } else {
             $status_icon = 'fa-check green';
         }
         $uri_status = id(new PHUIIconView())->setIcon($status_icon);
         $io_type = $uri->getEffectiveIOType();
         $io_map = PhabricatorRepositoryURI::getIOTypeMap();
         $io_spec = idx($io_map, $io_type, array());
         $io_icon = idx($io_spec, 'icon');
         $io_color = idx($io_spec, 'color');
         $io_label = idx($io_spec, 'label', $io_type);
         $uri_io = array(id(new PHUIIconView())->setIcon("{$io_icon} {$io_color}"), ' ', $io_label);
         $display_type = $uri->getEffectiveDisplayType();
         $display_map = PhabricatorRepositoryURI::getDisplayTypeMap();
         $display_spec = idx($display_map, $display_type, array());
         $display_icon = idx($display_spec, 'icon');
         $display_color = idx($display_spec, 'color');
         $display_label = idx($display_spec, 'label', $display_type);
         $uri_display = array(id(new PHUIIconView())->setIcon("{$display_icon} {$display_color}"), ' ', $display_label);
         $rows[] = array($uri_status, $uri_name, $uri_io, $uri_display);
     }
     $table = id(new AphrontTableView($rows))->setNoDataString(pht('This repository has no URIs.'))->setHeaders(array(null, pht('URI'), pht('I/O'), pht('Display')))->setColumnClasses(array(null, 'pri wide', null, null));
     $doc_href = PhabricatorEnv::getDoclink('Diffusion User Guide: URIs');
     $add_href = $repository->getPathURI('uri/edit/');
     $header = id(new PHUIHeaderView())->setHeader(pht('Repository URIs'))->addActionLink(id(new PHUIButtonView())->setIcon('fa-plus')->setHref($add_href)->setTag('a')->setText(pht('Add New URI')))->addActionLink(id(new PHUIButtonView())->setIcon('fa-book')->setHref($doc_href)->setTag('a')->setText(pht('Documentation')));
     $is_new = $repository->isNewlyInitialized();
     $messages = array();
     if ($repository->isHosted()) {
         if ($is_new) {
             $host_message = pht('Phabricator will host this repository.');
         } else {
             $host_message = pht('Phabricator is hosting this repository.');
         }
         $messages[] = array(id(new PHUIIconView())->setIcon('fa-folder'), ' ', $host_message);
     } else {
         if ($is_new) {
             $observe_message = pht('Phabricator will observe a remote repository.');
         } else {
             $observe_message = pht('This repository is hosted remotely. Phabricator is observing it.');
         }
         $messages[] = array(id(new PHUIIconView())->setIcon('fa-download'), ' ', $observe_message);
     }
     $info_view = id(new PHUIInfoView())->setSeverity(PHUIInfoView::SEVERITY_NOTICE)->setErrors($messages);
     return id(new PHUIObjectBoxView())->setHeader($header)->setInfoView($info_view)->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setTable($table);
 }
 protected function newEditableObject()
 {
     $uri = PhabricatorRepositoryURI::initializeNewURI();
     $repository = $this->getRepository();
     if ($repository) {
         $uri->setRepositoryPHID($repository->getPHID());
         $uri->attachRepository($repository);
     }
     return $uri;
 }
 public function getTitle()
 {
     $author_phid = $this->getAuthorPHID();
     $old = $this->getOldValue();
     $new = $this->getNewValue();
     switch ($this->getTransactionType()) {
         case self::TYPE_URI:
             return pht('%s changed this URI from "%s" to "%s".', $this->renderHandleLink($author_phid), $old, $new);
         case self::TYPE_IO:
             $map = PhabricatorRepositoryURI::getIOTypeMap();
             $old_label = idx(idx($map, $old, array()), 'label', $old);
             $new_label = idx(idx($map, $new, array()), 'label', $new);
             return pht('%s changed the display type for this URI from "%s" to "%s".', $this->renderHandleLink($author_phid), $old_label, $new_label);
         case self::TYPE_DISPLAY:
             $map = PhabricatorRepositoryURI::getDisplayTypeMap();
             $old_label = idx(idx($map, $old, array()), 'label', $old);
             $new_label = idx(idx($map, $new, array()), 'label', $new);
             return pht('%s changed the display type for this URI from "%s" to "%s".', $this->renderHandleLink($author_phid), $old_label, $new_label);
         case self::TYPE_DISABLE:
             if ($new) {
                 return pht('%s disabled this URI.', $this->renderHandleLink($author_phid));
             } else {
                 return pht('%s enabled this URI.', $this->renderHandleLink($author_phid));
             }
         case self::TYPE_CREDENTIAL:
             if ($old && $new) {
                 return pht('%s changed the credential for this URI from %s to %s.', $this->renderHandleLink($author_phid), $this->renderHandleLink($old), $this->renderHandleLink($new));
             } else {
                 if ($old) {
                     return pht('%s removed %s as the credential for this URI.', $this->renderHandleLink($author_phid), $this->renderHandleLink($old));
                 } else {
                     if ($new) {
                         return pht('%s set the credential for this URI to %s.', $this->renderHandleLink($author_phid), $this->renderHandleLink($new));
                     }
                 }
             }
     }
     return parent::getTitle();
 }
 public function newBuiltinURIs()
 {
     $has_callsign = $this->getCallsign() !== null;
     $has_shortname = $this->getRepositorySlug() !== null;
     // TODO: For now, never enable these because they don't work yet.
     $has_shortname = false;
     $identifier_map = array(PhabricatorRepositoryURI::BUILTIN_IDENTIFIER_CALLSIGN => $has_callsign, PhabricatorRepositoryURI::BUILTIN_IDENTIFIER_SHORTNAME => $has_shortname, PhabricatorRepositoryURI::BUILTIN_IDENTIFIER_ID => true);
     // If the view policy of the repository is public, support anonymous HTTP
     // even if authenticated HTTP is not supported.
     if ($this->getViewPolicy() === PhabricatorPolicies::POLICY_PUBLIC) {
         $allow_http = true;
     } else {
         $allow_http = PhabricatorEnv::getEnvConfig('diffusion.allow-http-auth');
     }
     $base_uri = PhabricatorEnv::getURI('/');
     $base_uri = new PhutilURI($base_uri);
     $has_https = $base_uri->getProtocol() == 'https';
     $has_https = $has_https && $allow_http;
     $has_http = !PhabricatorEnv::getEnvConfig('security.require-https');
     $has_http = $has_http && $allow_http;
     // HTTP is not supported for Subversion.
     if ($this->isSVN()) {
         $has_http = false;
         $has_https = false;
     }
     $has_ssh = (bool) strlen(PhabricatorEnv::getEnvConfig('phd.user'));
     $protocol_map = array(PhabricatorRepositoryURI::BUILTIN_PROTOCOL_SSH => $has_ssh, PhabricatorRepositoryURI::BUILTIN_PROTOCOL_HTTPS => $has_https, PhabricatorRepositoryURI::BUILTIN_PROTOCOL_HTTP => $has_http);
     $uris = array();
     foreach ($protocol_map as $protocol => $proto_supported) {
         foreach ($identifier_map as $identifier => $id_supported) {
             // This is just a dummy value because it can't be empty; we'll force
             // it to a proper value when using it in the UI.
             $builtin_uri = "{$protocol}://{$identifier}";
             $uris[] = PhabricatorRepositoryURI::initializeNewURI()->setRepositoryPHID($this->getPHID())->attachRepository($this)->setBuiltinProtocol($protocol)->setBuiltinIdentifier($identifier)->setURI($builtin_uri)->setIsDisabled((int) (!$proto_supported || !$id_supported));
         }
     }
     return $uris;
 }
 private function renderCloneURI(PhabricatorRepository $repository, PhabricatorRepositoryURI $uri)
 {
     if ($repository->isSVN()) {
         $display = csprintf('svn checkout %R %R', (string) $uri->getDisplayURI(), $repository->getCloneName());
     } else {
         $display = csprintf('%R', (string) $uri->getDisplayURI());
     }
     $display = (string) $display;
     $viewer = $this->getViewer();
     return id(new DiffusionCloneURIView())->setViewer($viewer)->setRepository($repository)->setRepositoryURI($uri)->setDisplayURI($display);
 }
 private function buildPropertySection(PhabricatorRepositoryURI $uri)
 {
     $viewer = $this->getViewer();
     $properties = id(new PHUIPropertyListView())->setUser($viewer);
     $properties->addProperty(pht('URI'), $uri->getDisplayURI());
     $credential_phid = $uri->getCredentialPHID();
     $command_engine = $uri->newCommandEngine();
     $is_optional = $command_engine->isCredentialOptional();
     $is_supported = $command_engine->isCredentialSupported();
     $is_builtin = $uri->isBuiltin();
     if ($is_builtin) {
         $credential_icon = 'fa-circle-o';
         $credential_color = 'grey';
         $credential_label = pht('Builtin');
         $credential_note = pht('Builtin URIs do not use credentials.');
     } else {
         if (!$is_supported) {
             $credential_icon = 'fa-circle-o';
             $credential_color = 'grey';
             $credential_label = pht('Not Supported');
             $credential_note = pht('This protocol does not support authentication.');
         } else {
             if (!$credential_phid) {
                 if ($is_optional) {
                     $credential_icon = 'fa-circle-o';
                     $credential_color = 'green';
                     $credential_label = pht('No Credential');
                     $credential_note = pht('Configured for anonymous access.');
                 } else {
                     $credential_icon = 'fa-times';
                     $credential_color = 'red';
                     $credential_label = pht('Required');
                     $credential_note = pht('Credential required but not configured.');
                 }
             } else {
                 // Don't raise a policy exception if we can't see the credential.
                 $credentials = id(new PassphraseCredentialQuery())->setViewer($viewer)->withPHIDs(array($credential_phid))->execute();
                 $credential = head($credentials);
                 if (!$credential) {
                     $handles = $viewer->loadHandles(array($credential_phid));
                     $handle = $handles[$credential_phid];
                     if ($handle->getPolicyFiltered()) {
                         $credential_icon = 'fa-lock';
                         $credential_color = 'grey';
                         $credential_label = pht('Restricted');
                         $credential_note = pht('You do not have permission to view the configured ' . 'credential.');
                     } else {
                         $credential_icon = 'fa-times';
                         $credential_color = 'red';
                         $credential_label = pht('Invalid');
                         $credential_note = pht('Configured credential is invalid.');
                     }
                 } else {
                     $provides = $credential->getProvidesType();
                     $needs = $command_engine->getPassphraseProvidesCredentialType();
                     if ($provides != $needs) {
                         $credential_icon = 'fa-times';
                         $credential_color = 'red';
                         $credential_label = pht('Wrong Type');
                     } else {
                         $credential_icon = 'fa-check';
                         $credential_color = 'green';
                         $credential_label = $command_engine->getPassphraseCredentialLabel();
                     }
                     $credential_note = $viewer->renderHandle($credential_phid);
                 }
             }
         }
     }
     $credential_item = id(new PHUIStatusItemView())->setIcon($credential_icon, $credential_color)->setTarget(phutil_tag('strong', array(), $credential_label))->setNote($credential_note);
     $credential_view = id(new PHUIStatusListView())->addItem($credential_item);
     $properties->addProperty(pht('Credential'), $credential_view);
     $io_type = $uri->getEffectiveIOType();
     $io_map = PhabricatorRepositoryURI::getIOTypeMap();
     $io_spec = idx($io_map, $io_type, array());
     $io_icon = idx($io_spec, 'icon');
     $io_color = idx($io_spec, 'color');
     $io_label = idx($io_spec, 'label', $io_type);
     $io_note = idx($io_spec, 'note');
     $io_item = id(new PHUIStatusItemView())->setIcon($io_icon, $io_color)->setTarget(phutil_tag('strong', array(), $io_label))->setNote($io_note);
     $io_view = id(new PHUIStatusListView())->addItem($io_item);
     $properties->addProperty(pht('I/O'), $io_view);
     $display_type = $uri->getEffectiveDisplayType();
     $display_map = PhabricatorRepositoryURI::getDisplayTypeMap();
     $display_spec = idx($display_map, $display_type, array());
     $display_icon = idx($display_spec, 'icon');
     $display_color = idx($display_spec, 'color');
     $display_label = idx($display_spec, 'label', $display_type);
     $display_note = idx($display_spec, 'note');
     $display_item = id(new PHUIStatusItemView())->setIcon($display_icon, $display_color)->setTarget(phutil_tag('strong', array(), $display_label))->setNote($display_note);
     $display_view = id(new PHUIStatusListView())->addItem($display_item);
     $properties->addProperty(pht('Display'), $display_view);
     return id(new PHUIObjectBoxView())->setHeaderText(pht('Details'))->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->appendChild($properties);
 }
<?php

$table = new PhabricatorRepository();
$conn_w = $table->establishConnection('w');
$mirrors = queryfx_all($conn_w, 'SELECT * FROM %T', 'repository_mirror');
foreach ($mirrors as $mirror) {
    $repository_phid = $mirror['repositoryPHID'];
    $uri = $mirror['remoteURI'];
    $already_exists = id(new PhabricatorRepositoryURI())->loadOneWhere('repositoryPHID = %s AND uri = %s', $repository_phid, $uri);
    if ($already_exists) {
        // Decline to migrate stuff that looks like it was already migrated.
        continue;
    }
    $new_uri = PhabricatorRepositoryURI::initializeNewURI()->setIOType(PhabricatorRepositoryURI::IO_MIRROR)->setRepositoryPHID($repository_phid)->setURI($uri)->setCredentialPHID($mirror['credentialPHID'])->setDateCreated($mirror['dateCreated'])->setDateModified($mirror['dateModified'])->save();
    echo tsprintf("%s\n", pht('Migrated mirror "%s".', $uri));
}
        case 'off':
        default:
            $disable_ssh = true;
            break;
    }
    $uris = $repository->newBuiltinURIs();
    foreach ($uris as $uri) {
        $builtin_protocol = $uri->getBuiltinProtocol();
        if ($builtin_protocol == PhabricatorRepositoryURI::BUILTIN_PROTOCOL_SSH) {
            $uri->setIsDisabled((int) $disable_ssh);
            $uri->setIoType($ssh_io);
        } else {
            $uri->setIsDisabled((int) $disable_http);
            $uri->setIoType($http_io);
        }
    }
    if (!$repository->isHosted()) {
        $remote_uri = $repository->getDetail('remote-uri');
        if (strlen($remote_uri)) {
            $uris[] = PhabricatorRepositoryURI::initializeNewURI()->setRepositoryPHID($repository->getPHID())->attachRepository($repository)->setURI($remote_uri)->setCredentialPHID($repository->getCredentialPHID())->setIOType(PhabricatorRepositoryURI::IO_OBSERVE);
        }
    }
    foreach ($uris as $uri) {
        $already_exists = id(new PhabricatorRepositoryURI())->loadOneWhere('repositoryPHID = %s AND uri = %s LIMIT 1', $repository->getPHID(), $uri->getURI());
        if ($already_exists) {
            continue;
        }
        $uri->save();
        echo tsprintf("%s\n", pht('Migrated URI "%s" for repository "%s".', $uri->getURI(), $repository->getDisplayName()));
    }
}