updatelocation.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. $shortoptions = 'i:n:af';
  23. $longoptions = array('id=', 'nickname=', 'all', 'force');
  24. $helptext = <<<END_OF_UPDATELOCATION_HELP
  25. updatelocation.php [options]
  26. set the location for a profile
  27. -i --id ID of user to update
  28. -n --nickname nickname of the user to update
  29. -f --force force update even if user already has a location
  30. -a --all update all
  31. END_OF_UPDATELOCATION_HELP;
  32. require_once INSTALLDIR.'/scripts/commandline.inc';
  33. try {
  34. $user = null;
  35. if (have_option('i', 'id')) {
  36. $id = get_option_value('i', 'id');
  37. $user = User::getKV('id', $id);
  38. if (empty($user)) {
  39. throw new Exception("Can't find user with id '$id'.");
  40. }
  41. updateLocation($user);
  42. } else if (have_option('n', 'nickname')) {
  43. $nickname = get_option_value('n', 'nickname');
  44. $user = User::getKV('nickname', $nickname);
  45. if (empty($user)) {
  46. throw new Exception("Can't find user with nickname '$nickname'");
  47. }
  48. updateLocation($user);
  49. } else if (have_option('a', 'all')) {
  50. $user = new User();
  51. if ($user->find()) {
  52. while ($user->fetch()) {
  53. updateLocation($user);
  54. }
  55. }
  56. } else {
  57. show_help();
  58. exit(1);
  59. }
  60. } catch (Exception $e) {
  61. print $e->getMessage()."\n";
  62. exit(1);
  63. }
  64. function updateLocation($user)
  65. {
  66. $profile = $user->getProfile();
  67. if (empty($profile)) {
  68. throw new Exception("User has no profile: " . $user->nickname);
  69. }
  70. if (empty($profile->location)) {
  71. if (have_option('v', 'verbose')) {
  72. print "No location string for '".$user->nickname."'\n";
  73. }
  74. return;
  75. }
  76. if (!empty($profile->location_id) && !have_option('f', 'force')) {
  77. if (have_option('v', 'verbose')) {
  78. print "Location ID already set for '".$user->nickname."'\n";
  79. }
  80. return;
  81. }
  82. $loc = Location::fromName($profile->location);
  83. if (empty($loc)) {
  84. if (have_option('v', 'verbose')) {
  85. print "No structured location for string '".$profile->location."' for user '".$user->nickname."'\n";
  86. }
  87. return;
  88. } else {
  89. $orig = clone($profile);
  90. $profile->lat = $loc->lat;
  91. $profile->lon = $loc->lon;
  92. $profile->location_id = $loc->location_id;
  93. $profile->location_ns = $loc->location_ns;
  94. $result = $profile->update($orig);
  95. if (!$result) {
  96. common_log_db_error($profile, 'UPDATE', __FILE__);
  97. }
  98. if (!have_option('q', 'quiet')) {
  99. print "Location ID " . $profile->location_id . " set for user " . $user->nickname . "\n";
  100. }
  101. }
  102. $profile->free();
  103. unset($loc);
  104. unset($profile);
  105. return;
  106. }