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 $args misc. arguments
68 * @return boolean true
69 * @throws ClientException
71 function prepare(array $args = [])
73 parent::prepare($args);
75 $cur = common_current_user();
78 // TRANS: Client exception thrown when trying to import bookmarks without being logged in.
79 throw new ClientException(_m('Only logged-in users can ' .
80 'import del.icio.us backups.'),
84 if (!$cur->hasRight(BookmarkPlugin::IMPORTDELICIOUS)) {
85 // TRANS: Client exception thrown when trying to import bookmarks without having the rights to do so.
86 throw new ClientException(_m('You may not restore your account.'), 403);
96 * @throws ClientException
102 if ($this->isPost()) {
103 $this->importDelicious();
111 * Queue a file for importation
113 * Uses the DeliciousBackupImporter class; may take a long time!
116 * @throws ClientException
118 function importDelicious()
120 $this->checkSessionToken();
122 if (!isset($_FILES[ImportDeliciousForm::FILEINPUT]['error'])) {
123 // TRANS: Client exception thrown when trying to import bookmarks and upload fails.
124 throw new ClientException(_m('No uploaded file.'));
127 switch ($_FILES[ImportDeliciousForm::FILEINPUT]['error']) {
128 case UPLOAD_ERR_OK: // success, jump out
130 case UPLOAD_ERR_INI_SIZE:
131 // TRANS: Client exception thrown when an uploaded file is too large.
132 throw new ClientException(_m('The uploaded file exceeds the ' .
133 '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.'));
139 case UPLOAD_ERR_PARTIAL:
140 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
141 // TRANS: Client exception thrown when a file was only partially uploaded.
142 throw new ClientException(_m('The uploaded file was only' .
143 ' partially uploaded.'));
144 case UPLOAD_ERR_NO_FILE:
145 // No file; probably just a non-AJAX submission.
146 // TRANS: Client exception thrown when a file upload has failed.
147 throw new ClientException(_m('No uploaded file.'));
148 case UPLOAD_ERR_NO_TMP_DIR:
149 // TRANS: Client exception thrown when a temporary folder is not present.
150 throw new ClientException(_m('Missing a temporary folder.'));
151 case UPLOAD_ERR_CANT_WRITE:
152 // TRANS: Client exception thrown when writing to disk is not possible.
153 throw new ClientException(_m('Failed to write file to disk.'));
154 case UPLOAD_ERR_EXTENSION:
155 // TRANS: Client exception thrown when a file upload has been stopped.
156 throw new ClientException(_m('File upload stopped by extension.'));
158 common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
159 $_FILES[ImportDeliciousForm::FILEINPUT]['error']);
160 // TRANS: Client exception thrown when a file upload operation has failed.
161 throw new ClientException(_m('System error uploading file.'));
164 $filename = $_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name'];
167 if (!file_exists($filename)) {
168 // TRANS: Server exception thrown when a file upload cannot be found.
169 // TRANS: %s is the file that could not be found.
170 throw new ServerException(sprintf(_m('No such file "%s".'), $filename));
173 if (!is_file($filename)) {
174 // TRANS: Server exception thrown when a file upload is incorrect.
175 // TRANS: %s is the irregular file.
176 throw new ServerException(sprintf(_m('Not a regular file: "%s".'), $filename));
179 if (!is_readable($filename)) {
180 // TRANS: Server exception thrown when a file upload is not readable.
181 // TRANS: %s is the file that could not be read.
182 throw new ServerException(sprintf(_m('File "%s" not readable.'), $filename));
185 common_debug(sprintf("Getting backup from file '%s'.", $filename));
187 $html = file_get_contents($filename);
189 // Enqueue for processing.
191 $qm = QueueManager::get();
192 $qm->enqueue([common_current_user(), $html], 'dlcsback');
194 if ($qm instanceof UnQueueManager) {
195 // No active queuing means we've actually just completed the job!
196 $this->success = true;
198 // We've fed data into background queues, and it's probably still running.
199 $this->inprogress = true;
204 } catch (Exception $e) {
205 // Delete the file and re-throw
206 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
212 * Show the content of the page
216 function showContent()
218 if ($this->success) {
219 $this->element('p', null,
220 // TRANS: Success message after importing bookmarks.
221 _m('Bookmarks have been imported. Your bookmarks should now appear in search and your profile page.'));
222 } else if ($this->inprogress) {
223 $this->element('p', null,
224 // TRANS: Busy message for importing bookmarks.
225 _m('Bookmarks are being imported. Please wait a few minutes for results.'));
227 $form = new ImportDeliciousForm($this);
233 * Return true if read only.
237 * @param array $args other arguments
239 * @return boolean is read only action?
241 function isReadOnly($args)
243 return !$this->isPost();
248 * A form for backing up the account.
252 * @author Evan Prodromou <evan@status.net>
253 * @copyright 2010 StatusNet, Inc.
254 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
255 * @link http://status.net/
257 class ImportDeliciousForm extends Form
259 const FILEINPUT = 'deliciousbackupfile';
264 * Set the encoding type, since this is a file upload.
266 * @param HTMLOutputter $out output channel
269 function __construct($out = null)
271 parent::__construct($out);
272 $this->enctype = 'multipart/form-data';
278 * @return string the form's class
282 return 'form_import_delicious';
286 * URL the form posts to
288 * @return string the form's action URL
292 return common_local_url('importdelicious');
298 * Really, just instructions for doing a backup.
304 $this->out->elementStart('p', 'instructions');
306 // TRANS: Form instructions for importing bookmarks.
307 $this->out->raw(_m('You can upload a backed-up ' .
308 'delicious.com bookmarks file.'));
310 $this->out->elementEnd('p');
312 $this->out->elementStart('ul', 'form_data');
314 $this->out->elementStart('li', array('id' => 'settings_attach'));
315 $this->out->element('input', array('name' => self::FILEINPUT,
317 'id' => self::FILEINPUT));
318 $this->out->elementEnd('li');
320 $this->out->elementEnd('ul');
324 * Buttons for the form
326 * In this case, a single submit button
331 function formActions()
333 $this->out->submit('submit',
334 // TRANS: Button text on form to import bookmarks.
335 _m('BUTTON', 'Upload'),
338 // TRANS: Button title on form to import bookmarks.
339 _m('Upload the file.'));