importbookmarks.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010 StatusNet, Inc.
  5. *
  6. * Import a bookmarks file as notices
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Bookmark
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
  31. $shortoptions = 'i:n:f:';
  32. $longoptions = array('id=', 'nickname=', 'file=');
  33. $helptext = <<<END_OF_IMPORTBOOKMARKS_HELP
  34. importbookmarks.php [options]
  35. Restore a backed-up Delicious.com bookmark file
  36. -i --id ID of user to import bookmarks for
  37. -n --nickname nickname of the user to import for
  38. -f --file file to read from (STDIN by default)
  39. END_OF_IMPORTBOOKMARKS_HELP;
  40. require_once INSTALLDIR.'/scripts/commandline.inc';
  41. /**
  42. * Get the bookmarks file as a string
  43. *
  44. * Uses the -f or --file parameter to open and read a
  45. * a bookmarks file
  46. *
  47. * @return string Contents of the file
  48. */
  49. function getBookmarksFile()
  50. {
  51. $filename = get_option_value('f', 'file');
  52. if (empty($filename)) {
  53. show_help();
  54. exit(1);
  55. }
  56. if (!file_exists($filename)) {
  57. // TRANS: Exception thrown when a file upload cannot be found.
  58. // TRANS: %s is the file that could not be found.
  59. throw new Exception(sprintf(_m('No such file "%s".'),$filename));
  60. }
  61. if (!is_file($filename)) {
  62. // TRANS: Exception thrown when a file upload is incorrect.
  63. // TRANS: %s is the irregular file.
  64. throw new Exception(sprintf(_m('Not a regular file: "%s".'),$filename));
  65. }
  66. if (!is_readable($filename)) {
  67. // TRANS: Exception thrown when a file upload is not readable.
  68. // TRANS: %s is the file that could not be read.
  69. throw new Exception(sprintf(_m('File "%s" not readable.'),$filename));
  70. }
  71. // TRANS: %s is the filename that contains a backup for a user.
  72. printfv(_m('Getting backup from file "%s".')."\n", $filename);
  73. $html = file_get_contents($filename);
  74. return $html;
  75. }
  76. try {
  77. $user = getUser();
  78. $html = getBookmarksFile();
  79. $qm = QueueManager::get();
  80. $qm->enqueue(array($user, $html), 'dlcsback');
  81. } catch (Exception $e) {
  82. print $e->getMessage()."\n";
  83. exit(1);
  84. }