updatelocation.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2009, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = 'i:n:af';
  22. $longoptions = array('id=', 'nickname=', 'all', 'force');
  23. $helptext = <<<END_OF_UPDATELOCATION_HELP
  24. updatelocation.php [options]
  25. set the location for a profile
  26. -i --id ID of user to update
  27. -n --nickname nickname of the user to update
  28. -f --force force update even if user already has a location
  29. -a --all update all
  30. END_OF_UPDATELOCATION_HELP;
  31. require_once INSTALLDIR.'/scripts/commandline.inc';
  32. try {
  33. $user = null;
  34. if (have_option('i', 'id')) {
  35. $id = get_option_value('i', 'id');
  36. $user = User::getKV('id', $id);
  37. if (empty($user)) {
  38. throw new Exception("Can't find user with id '$id'.");
  39. }
  40. updateLocation($user);
  41. } else if (have_option('n', 'nickname')) {
  42. $nickname = get_option_value('n', 'nickname');
  43. $user = User::getKV('nickname', $nickname);
  44. if (empty($user)) {
  45. throw new Exception("Can't find user with nickname '$nickname'");
  46. }
  47. updateLocation($user);
  48. } else if (have_option('a', 'all')) {
  49. $user = new User();
  50. if ($user->find()) {
  51. while ($user->fetch()) {
  52. updateLocation($user);
  53. }
  54. }
  55. } else {
  56. show_help();
  57. exit(1);
  58. }
  59. } catch (Exception $e) {
  60. print $e->getMessage()."\n";
  61. exit(1);
  62. }
  63. function updateLocation($user)
  64. {
  65. $profile = $user->getProfile();
  66. if (empty($profile)) {
  67. throw new Exception("User has no profile: " . $user->nickname);
  68. }
  69. if (empty($profile->location)) {
  70. if (have_option('v', 'verbose')) {
  71. print "No location string for '".$user->nickname."'\n";
  72. }
  73. return;
  74. }
  75. if (!empty($profile->location_id) && !have_option('f', 'force')) {
  76. if (have_option('v', 'verbose')) {
  77. print "Location ID already set for '".$user->nickname."'\n";
  78. }
  79. return;
  80. }
  81. $loc = Location::fromName($profile->location);
  82. if (empty($loc)) {
  83. if (have_option('v', 'verbose')) {
  84. print "No structured location for string '".$profile->location."' for user '".$user->nickname."'\n";
  85. }
  86. return;
  87. } else {
  88. $orig = clone($profile);
  89. $profile->lat = $loc->lat;
  90. $profile->lon = $loc->lon;
  91. $profile->location_id = $loc->location_id;
  92. $profile->location_ns = $loc->location_ns;
  93. $result = $profile->update($orig);
  94. if (!$result) {
  95. common_log_db_error($profile, 'UPDATE', __FILE__);
  96. }
  97. if (!have_option('q', 'quiet')) {
  98. print "Location ID " . $profile->location_id . " set for user " . $user->nickname . "\n";
  99. }
  100. }
  101. $profile->free();
  102. unset($loc);
  103. unset($profile);
  104. return;
  105. }