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;
53 * Return the title of the page
55 * @return string page title
60 return _("Import del.icio.us bookmarks");
64 * For initializing members of the class.
66 * @param array $argarray misc. arguments
68 * @return boolean true
71 function prepare($argarray)
73 parent::prepare($argarray);
75 $cur = common_current_user();
78 throw new ClientException(_('Only logged-in users can '.
79 'import del.icio.us backups.'),
83 if (!$cur->hasRight(BookmarkPlugin::IMPORTDELICIOUS)) {
84 throw new ClientException(_('You may not restore your account.'), 403);
93 * @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!
118 function importDelicious()
120 $this->checkSessionToken();
122 if (!isset($_FILES[ImportDeliciousForm::FILEINPUT]['error'])) {
123 throw new ClientException(_('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(_('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.
137 _('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.
143 throw new ClientException(_('The uploaded file was only' .
144 ' partially uploaded.'));
146 case UPLOAD_ERR_NO_FILE:
147 // No file; probably just a non-AJAX submission.
148 throw new ClientException(_('No uploaded file.'));
150 case UPLOAD_ERR_NO_TMP_DIR:
151 // TRANS: Client exception thrown when a temporary folder is not present
152 throw new ClientException(_('Missing a temporary folder.'));
154 case UPLOAD_ERR_CANT_WRITE:
155 // TRANS: Client exception thrown when writing to disk is not possible
156 throw new ClientException(_('Failed to write file to disk.'));
158 case UPLOAD_ERR_EXTENSION:
159 // TRANS: Client exception thrown when a file upload has been stopped
160 throw new ClientException(_('File upload stopped by extension.'));
163 common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
164 $_FILES[ImportDeliciousForm::FILEINPUT]['error']);
165 // TRANS: Client exception thrown when a file upload operation has failed
166 throw new ClientException(_('System error uploading file.'));
170 $filename = $_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name'];
173 if (!file_exists($filename)) {
174 throw new ServerException("No such file '$filename'.");
177 if (!is_file($filename)) {
178 throw new ServerException("Not a regular file: '$filename'.");
181 if (!is_readable($filename)) {
182 throw new ServerException("File '$filename' not readable.");
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(array(common_current_user(), $html), 'dlcsback');
194 $this->success = true;
198 } catch (Exception $e) {
199 // Delete the file and re-throw
200 @unlink($_FILES[ImportDeliciousForm::FILEINPUT]['tmp_name']);
206 * Show the content of the page
211 function showContent()
213 if ($this->success) {
214 $this->element('p', null,
215 _('Feed will be restored. '.
216 'Please wait a few minutes for results.'));
218 $form = new ImportDeliciousForm($this);
224 * Return true if read only.
228 * @param array $args other arguments
230 * @return boolean is read only action?
233 function isReadOnly($args)
235 return !$this->isPost();
240 * A form for backing up the account.
244 * @author Evan Prodromou <evan@status.net>
245 * @copyright 2010 StatusNet, Inc.
246 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
247 * @link http://status.net/
250 class ImportDeliciousForm extends Form
252 const FILEINPUT = 'deliciousbackupfile';
257 * Set the encoding type, since this is a file upload.
259 * @param HTMLOutputter $out output channel
261 * @return ImportDeliciousForm this
264 function __construct($out=null)
266 parent::__construct($out);
267 $this->enctype = 'multipart/form-data';
273 * @return string the form's class
278 return 'form_import_delicious';
282 * URL the form posts to
284 * @return string the form's action URL
289 return common_local_url('importdelicious');
295 * Really, just instructions for doing a backup.
302 $this->out->elementStart('p', 'instructions');
304 $this->out->raw(_('You can upload a backed-up '.
305 'delicious.com bookmarks file.'));
307 $this->out->elementEnd('p');
309 $this->out->elementStart('ul', 'form_data');
311 $this->out->elementStart('li', array ('id' => 'settings_attach'));
312 $this->out->element('input', array('name' => self::FILEINPUT,
314 'id' => self::FILEINPUT));
315 $this->out->elementEnd('li');
317 $this->out->elementEnd('ul');
321 * Buttons for the form
323 * In this case, a single submit button
328 function formActions()
330 $this->out->submit('submit',
331 _m('BUTTON', 'Upload'),
334 _('Upload the file'));