]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/deliciousbackupimporter.php
Code standards for deliciousbackupimporter.php
[quix0rs-gnu-social.git] / plugins / Bookmark / deliciousbackupimporter.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Importer class for Delicious.com backups
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
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/apiauth.php';
38
39 /**
40  * Importer class for Delicious bookmarks
41  *
42  * @category  Bookmark
43  * @package   StatusNet
44  * @author    Evan Prodromou <evan@status.net>
45  * @copyright 2010 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49
50 class DeliciousBackupImporter
51 {
52     /**
53      * Import an in-memory bookmark list to a user's account
54      *
55      * Take a delicious.com backup file (same as Netscape bookmarks.html)
56      * and import to StatusNet as Bookmark activities.
57      *
58      * The document format is terrible. It consists of a <dl> with
59      * a bunch of <dt>'s, occasionally with <dd>'s.
60      * There are sometimes <p>'s lost inside.
61      *
62      * @param User   $user User whose feed we're going to fill
63      * @param string $body Body of the file
64      *
65      * @return void
66      */
67
68     function importBookmarks($user, $body)
69     {
70         $doc = $this->importHTML($body);
71
72         $dls = $doc->getElementsByTagName('dl');
73
74         if ($dls->length != 1) {
75             throw new ClientException(_("Bad import file."));
76         }
77
78         $dl = $dls->item(0);
79
80         $children = $dl->childNodes;
81
82         $dt = null;
83
84         for ($i = 0; $i < $children->length; $i++) {
85             try {
86                 $child = $children->item($i);
87                 if ($child->nodeType != XML_ELEMENT_NODE) {
88                     continue;
89                 }
90                 common_log(LOG_INFO, $child->tagName);
91                 switch (strtolower($child->tagName)) {
92                 case 'dt':
93                     if (!empty($dt)) {
94                         // No DD provided
95                         $this->importBookmark($user, $dt);
96                         $dt = null;
97                     }
98                     $dt = $child;
99                     break;
100                 case 'dd':
101                     $dd = $child;
102
103                     $saved = $this->importBookmark($user, $dt, $dd);
104
105                     $dt = null;
106                     $dd = null;
107                 case 'p':
108                     common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
109                     break;
110                 default:
111                     common_log(LOG_WARNING, 
112                                "Unexpected element $child->tagName ".
113                                " found in import.");
114                 }
115             } catch (Exception $e) {
116                 common_log(LOG_ERR, $e->getMessage());
117                 $dt = $dd = null;
118             }
119         }
120     }
121
122     /**
123      * Import a single bookmark
124      * 
125      * Takes a <dt>/<dd> pair. The <dt> has a single
126      * <a> in it with some non-standard attributes.
127      * 
128      * A <dt><dt><dd> sequence will appear as a <dt> with
129      * anothe <dt> as a child. We handle this case recursively. 
130      *
131      * @param User       $user User to import data as
132      * @param DOMElement $dt   <dt> element
133      * @param DOMElement $dd   <dd> element
134      *
135      * @return Notice imported notice
136      */
137
138     function importBookmark($user, $dt, $dd = null)
139     {
140         // We have to go squirrelling around in the child nodes
141         // on the off chance that we've received another <dt>
142         // as a child.
143
144         for ($i = 0; $i < $dt->childNodes->length; $i++) {
145             $child = $dt->childNodes->item($i);
146             if ($child->nodeType == XML_ELEMENT_NODE) {
147                 if ($child->tagName == 'dt' && !is_null($dd)) {
148                     $this->importBookmark($user, $dt);
149                     $this->importBookmark($user, $child, $dd);
150                     return;
151                 }
152             }
153         }
154
155         $as = $dt->getElementsByTagName('a');
156
157         if ($as->length == 0) {
158             throw new ClientException(_("No <A> tag in a <DT>."));
159         }
160
161         $a = $as->item(0);
162                     
163         $private = $a->getAttribute('private');
164
165         if ($private != 0) {
166             throw new ClientException(_('Skipping private bookmark.'));
167         }
168
169         if (!empty($dd)) {
170             $description = $dd->nodeValue;
171         } else {
172             $description = null;
173         }
174
175         $title   = $a->nodeValue;
176         $url     = $a->getAttribute('href');
177         $tags    = $a->getAttribute('tags');
178         $addDate = $a->getAttribute('add_date');
179         $created = common_sql_date(intval($addDate));
180
181         $saved = Notice_bookmark::saveNew($user,
182                                           $title,
183                                           $url,
184                                           $tags,
185                                           $description,
186                                           array('created' => $created));
187
188         return $saved;
189     }
190
191     /**
192      * Parse some HTML
193      *
194      * Hides the errors that the dom parser returns
195      *
196      * @param string $body Data to import
197      *
198      * @return DOMDocument parsed document
199      */
200
201     function importHTML($body)
202     {
203         // DOMDocument::loadHTML may throw warnings on unrecognized elements,
204         // and notices on unrecognized namespaces.
205         $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
206         $dom = new DOMDocument();
207         $ok  = $dom->loadHTML($body);
208         error_reporting($old);
209
210         if ($ok) {
211             return $dom;
212         } else {
213             return null;
214         }
215     }
216 }