Changeset View
Changeset View
Standalone View
Standalone View
resources/sql/patches/20130611.migrateoauth.php
| <?php | <?php | ||||
| // NOTE: We aren't using PhabricatorUserOAuthInfo anywhere here because it is | // NOTE: We aren't using PhabricatorUserOAuthInfo anywhere here because it is | ||||
| // getting nuked in a future diff. | // getting nuked in a future diff. | ||||
| $table = new PhabricatorUser(); | $table = new PhabricatorUser(); | ||||
| $table_name = 'user_oauthinfo'; | $table_name = 'user_oauthinfo'; | ||||
| $conn_w = $table->establishConnection('w'); | $conn_w = $table->establishConnection('w'); | ||||
| $xaccount = new PhabricatorExternalAccount(); | $xaccount = new PhabricatorExternalAccount(); | ||||
| echo "Migrating OAuth to ExternalAccount...\n"; | echo pht('Migrating OAuth to %s...', 'ExternalAccount')."\n"; | ||||
| $domain_map = array( | $domain_map = array( | ||||
| 'disqus' => 'disqus.com', | 'disqus' => 'disqus.com', | ||||
| 'facebook' => 'facebook.com', | 'facebook' => 'facebook.com', | ||||
| 'github' => 'github.com', | 'github' => 'github.com', | ||||
| 'google' => 'google.com', | 'google' => 'google.com', | ||||
| ); | ); | ||||
| try { | try { | ||||
| $phabricator_oauth_uri = new PhutilURI( | $phabricator_oauth_uri = new PhutilURI( | ||||
| PhabricatorEnv::getEnvConfig('phabricator.oauth-uri')); | PhabricatorEnv::getEnvConfig('phabricator.oauth-uri')); | ||||
| $domain_map['phabricator'] = $phabricator_oauth_uri->getDomain(); | $domain_map['phabricator'] = $phabricator_oauth_uri->getDomain(); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| // Ignore; this likely indicates that we have removed `phabricator.oauth-uri` | // Ignore; this likely indicates that we have removed `phabricator.oauth-uri` | ||||
| // in some future diff. | // in some future diff. | ||||
| } | } | ||||
| $rows = queryfx_all( | $rows = queryfx_all( | ||||
| $conn_w, | $conn_w, | ||||
| 'SELECT * FROM user_oauthinfo'); | 'SELECT * FROM user_oauthinfo'); | ||||
| foreach ($rows as $row) { | foreach ($rows as $row) { | ||||
| echo "Migrating row ID #".$row['id'].".\n"; | echo pht('Migrating row ID #%d.', $row['id'])."\n"; | ||||
| $user = id(new PhabricatorUser())->loadOneWhere( | $user = id(new PhabricatorUser())->loadOneWhere( | ||||
| 'id = %d', | 'id = %d', | ||||
| $row['userID']); | $row['userID']); | ||||
| if (!$user) { | if (!$user) { | ||||
| echo "Bad user ID!\n"; | echo pht('Bad user ID!')."\n"; | ||||
| continue; | continue; | ||||
| } | } | ||||
| $domain = idx($domain_map, $row['oauthProvider']); | $domain = idx($domain_map, $row['oauthProvider']); | ||||
| if (empty($domain)) { | if (empty($domain)) { | ||||
| echo "Unknown OAuth provider!\n"; | echo pht('Unknown OAuth provider!')."\n"; | ||||
| continue; | continue; | ||||
| } | } | ||||
| $xaccount = id(new PhabricatorExternalAccount()) | $xaccount = id(new PhabricatorExternalAccount()) | ||||
| ->setUserPHID($user->getPHID()) | ->setUserPHID($user->getPHID()) | ||||
| ->setAccountType($row['oauthProvider']) | ->setAccountType($row['oauthProvider']) | ||||
| ->setAccountDomain($domain) | ->setAccountDomain($domain) | ||||
| ->setAccountID($row['oauthUID']) | ->setAccountID($row['oauthUID']) | ||||
| ->setAccountURI($row['accountURI']) | ->setAccountURI($row['accountURI']) | ||||
| ->setUsername($row['accountName']) | ->setUsername($row['accountName']) | ||||
| ->setDateCreated($row['dateCreated']); | ->setDateCreated($row['dateCreated']); | ||||
| try { | try { | ||||
| $xaccount->save(); | $xaccount->save(); | ||||
| } catch (Exception $ex) { | } catch (Exception $ex) { | ||||
| phlog($ex); | phlog($ex); | ||||
| } | } | ||||
| } | } | ||||
| echo "Done.\n"; | echo pht('Done.')."\n"; | ||||