3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Import del.icio.us bookmarks backups
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * UI for importing del.icio.us bookmark backups
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2010 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class ImportdeliciousAction extends Action
49 protected $success = false;
50 private $inprogress = false;
53 * Return the title of the page
55 * @return string page title
59 // TRANS: Title for page to import del.icio.us bookmark backups on.
60 return _m("Import del.icio.us bookmarks");
64 * For initializing members of the class.
66 * @param array $argarray misc. arguments
68 * @return boolean true
70 function prepare($argarray)
72 parent::prepare($argarray);
74 $cur = common_current_user();
77 // TRANS: Client exception thrown when trying to import bookmarks without being logged in.
78 throw new ClientException(_m('Only logged-in users can '.
79 'import del.icio.us backups.'),
83 if (!$cur->hasRight(BookmarkPlugin::IMPORTDELICIOUS)) {
84 // TRANS: Client exception thrown when trying to import bookmarks without having the rights to do so.
85 throw new ClientException(_m('You may not restore your account.'), 403);
94 * @param array $argarray is ignored since it's now passed in in prepare()
98 function handle($argarray=null)
100 parent::handle($argarray);
102 if ($this->isPost()) {
103 $this->importDelicious();
111 * Queue a file for importation
113 * Uses the DeliciousBackupImporter class; may take a long time!
117 function importDelicious()
119 $this->checkSessionToken();
121 if (!isset($_FILES[ImportDeliciousForm::FILEINPUT]['error'])) {
122 // TRANS: Client exception thrown when trying to import bookmarks and upload fails.
123 throw new ClientException(_m('No uploaded file.'));
126 switch ($_FILES[ImportDeliciousForm::FILEINPUT]['error']) {
127 case UPLOAD_ERR_OK: // success, jump out
129 case UPLOAD_ERR_INI_SIZE:
130 // TRANS: Client exception thrown when an uploaded file is too large.
131 throw new ClientException(_m('The uploaded file exceeds the ' .
132 'upload_max_filesize directive in php.ini.'));
134 case UPLOAD_ERR_FORM_SIZE:
135 throw new ClientException(
136 // TRANS: Client exception thrown when an uploaded file is too large.
137 _m('The uploaded file exceeds the MAX_FILE_SIZE directive' .
138 ' that was specified in the HTML form.'));
140 case UPLOAD_ERR_PARTIAL:
141 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
142 // TRANS: Client exception thrown when a file was only partially uploaded.
143 throw new ClientException(_m('The uploaded file was only' .
144 ' partially uploaded.'));
146 case UPLOAD_ERR_NO_FILE:
147 // No file; probably just a non-AJAX submission.
148 // TRANS: Client exception thrown when a file upload has failed.
149 throw new ClientException(_m('No uploaded file.'));
151 case UPLOAD_ERR_NO_TMP_DIR:
152 // TRANS: Client exception thrown when a temporary folder is not present.
153 throw new ClientException(_m('Missing a temporary folder.'));
155 case UPLOAD_ERR_CANT_WRITE:
156 // TRANS: Client exception thrown when writing to disk is not possible.
157 throw new ClientException(_m('Failed to write file to disk.'));
159 case UPLOAD_ERR_EXTENSION:
160 // TRANS: Client exception thrown when a file upload has been stopped.
161 throw new ClientException(_m('File upload stopped by extension.'));
164 common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
165 $_FILES[ImportDeliciousForm::FILEINPUT]['error']);
166 // TRANS: Client exception thrown when a file upload operation has failed.
167 throw new ClientException(_m('System error uploading file.'));
171 $filename = $_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name'];
174 if (!file_exists($filename)) {
175 // TRANS: Server exception thrown when a file upload cannot be found.
176 // TRANS: %s is the file that could not be found.
177 throw new ServerException(sprintf(_m('No such file "%s".'),$filename));
180 if (!is_file($filename)) {
181 // TRANS: Server exception thrown when a file upload is incorrect.
182 // TRANS: %s is the irregular file.
183 throw new ServerException(sprintf(_m('Not a regular file: "%s".'),$filename));
186 if (!is_readable($filename)) {
187 // TRANS: Server exception thrown when a file upload is not readable.
188 // TRANS: %s is the file that could not be read.
189 throw new ServerException(sprintf(_m('File "%s" not readable.'),$filename));
192 common_debug(sprintf("Getting backup from file '%s'.", $filename));
194 $html = file_get_contents($filename);
196 // Enqueue for processing.
198 $qm = QueueManager::get();
199 $qm->enqueue(array(common_current_user(), $html), 'dlcsback');
201 if ($qm instanceof UnQueueManager) {
202 // No active queuing means we've actually just completed the job!
203 $this->success = true;
205 // We've fed data into background queues, and it's probably still running.
206 $this->inprogress = true;
211 } catch (Exception $e) {
212 // Delete the file and re-throw
213 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
219 * Show the content of the page
223 function showContent()
225 if ($this->success) {
226 $this->element('p', null,
227 // TRANS: Success message after importing bookmarks.
228 _m('Bookmarks have been imported. Your bookmarks should now appear in search and your profile page.'));
229 } else if ($this->inprogress) {
230 $this->element('p', null,
231 // TRANS: Busy message for importing bookmarks.
232 _m('Bookmarks are being imported. Please wait a few minutes for results.'));
234 $form = new ImportDeliciousForm($this);
240 * Return true if read only.
244 * @param array $args other arguments
246 * @return boolean is read only action?
248 function isReadOnly($args)
250 return !$this->isPost();
255 * A form for backing up the account.
259 * @author Evan Prodromou <evan@status.net>
260 * @copyright 2010 StatusNet, Inc.
261 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
262 * @link http://status.net/
264 class ImportDeliciousForm extends Form
266 const FILEINPUT = 'deliciousbackupfile';
271 * Set the encoding type, since this is a file upload.
273 * @param HTMLOutputter $out output channel
275 * @return ImportDeliciousForm this
277 function __construct($out=null)
279 parent::__construct($out);
280 $this->enctype = 'multipart/form-data';
286 * @return string the form's class
290 return 'form_import_delicious';
294 * URL the form posts to
296 * @return string the form's action URL
300 return common_local_url('importdelicious');
306 * Really, just instructions for doing a backup.
312 $this->out->elementStart('p', 'instructions');
314 // TRANS: Form instructions for importing bookmarks.
315 $this->out->raw(_m('You can upload a backed-up '.
316 'delicious.com bookmarks file.'));
318 $this->out->elementEnd('p');
320 $this->out->elementStart('ul', 'form_data');
322 $this->out->elementStart('li', array ('id' => 'settings_attach'));
323 $this->out->element('input', array('name' => self::FILEINPUT,
325 'id' => self::FILEINPUT));
326 $this->out->elementEnd('li');
328 $this->out->elementEnd('ul');
332 * Buttons for the form
334 * In this case, a single submit button
339 function formActions()
341 $this->out->submit('submit',
342 // TRANS: Button text on form to import bookmarks.
343 _m('BUTTON', 'Upload'),
346 // TRANS: Button title on form to import bookmarks.
347 _m('Upload the file.'));