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/
48 class ImportdeliciousAction extends Action
50 protected $success = false;
51 private $inprogress = false;
54 * Return the title of the page
56 * @return string page title
61 return _("Import del.icio.us bookmarks");
65 * For initializing members of the class.
67 * @param array $argarray misc. arguments
69 * @return boolean true
72 function prepare($argarray)
74 parent::prepare($argarray);
76 $cur = common_current_user();
79 throw new ClientException(_('Only logged-in users can '.
80 'import del.icio.us backups.'),
84 if (!$cur->hasRight(BookmarkPlugin::IMPORTDELICIOUS)) {
85 throw new ClientException(_('You may not restore your account.'), 403);
94 * @param array $argarray is ignored since it's now passed in in prepare()
99 function handle($argarray=null)
101 parent::handle($argarray);
103 if ($this->isPost()) {
104 $this->importDelicious();
112 * Queue a file for importation
114 * Uses the DeliciousBackupImporter class; may take a long time!
119 function importDelicious()
121 $this->checkSessionToken();
123 if (!isset($_FILES[ImportDeliciousForm::FILEINPUT]['error'])) {
124 throw new ClientException(_('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(_('The uploaded file exceeds the ' .
133 'upload_max_filesize directive in php.ini.'));
135 case UPLOAD_ERR_FORM_SIZE:
136 throw new ClientException(
137 // TRANS: Client exception.
138 _('The uploaded file exceeds the MAX_FILE_SIZE directive' .
139 ' that was specified in the HTML form.'));
141 case UPLOAD_ERR_PARTIAL:
142 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
143 // TRANS: Client exception.
144 throw new ClientException(_('The uploaded file was only' .
145 ' partially uploaded.'));
147 case UPLOAD_ERR_NO_FILE:
148 // No file; probably just a non-AJAX submission.
149 throw new ClientException(_('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(_('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(_('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(_('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(_('System error uploading file.'));
171 $filename = $_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name'];
174 if (!file_exists($filename)) {
175 throw new ServerException("No such file '$filename'.");
178 if (!is_file($filename)) {
179 throw new ServerException("Not a regular file: '$filename'.");
182 if (!is_readable($filename)) {
183 throw new ServerException("File '$filename' not readable.");
186 common_debug(sprintf(_("Getting backup from file '%s'."), $filename));
188 $html = file_get_contents($filename);
190 // Enqueue for processing.
192 $qm = QueueManager::get();
193 $qm->enqueue(array(common_current_user(), $html), 'dlcsback');
195 if ($qm instanceof UnQueueManager) {
196 // No active queuing means we've actually just completed the job!
197 $this->success = true;
199 // We've fed data into background queues, and it's probably still running.
200 $this->inprogress = true;
205 } catch (Exception $e) {
206 // Delete the file and re-throw
207 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
213 * Show the content of the page
218 function showContent()
220 if ($this->success) {
221 $this->element('p', null,
222 _('Bookmarks have been imported. Your bookmarks should now appear in search and your profile page.'));
223 } else if ($this->inprogress) {
224 $this->element('p', null,
225 _('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?
242 function isReadOnly($args)
244 return !$this->isPost();
249 * A form for backing up the account.
253 * @author Evan Prodromou <evan@status.net>
254 * @copyright 2010 StatusNet, Inc.
255 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
256 * @link http://status.net/
259 class ImportDeliciousForm extends Form
261 const FILEINPUT = 'deliciousbackupfile';
266 * Set the encoding type, since this is a file upload.
268 * @param HTMLOutputter $out output channel
270 * @return ImportDeliciousForm this
273 function __construct($out=null)
275 parent::__construct($out);
276 $this->enctype = 'multipart/form-data';
282 * @return string the form's class
287 return 'form_import_delicious';
291 * URL the form posts to
293 * @return string the form's action URL
298 return common_local_url('importdelicious');
304 * Really, just instructions for doing a backup.
311 $this->out->elementStart('p', 'instructions');
313 $this->out->raw(_('You can upload a backed-up '.
314 'delicious.com bookmarks file.'));
316 $this->out->elementEnd('p');
318 $this->out->elementStart('ul', 'form_data');
320 $this->out->elementStart('li', array ('id' => 'settings_attach'));
321 $this->out->element('input', array('name' => self::FILEINPUT,
323 'id' => self::FILEINPUT));
324 $this->out->elementEnd('li');
326 $this->out->elementEnd('ul');
330 * Buttons for the form
332 * In this case, a single submit button
337 function formActions()
339 $this->out->submit('submit',
340 _m('BUTTON', 'Upload'),
343 _('Upload the file'));