123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class DeliciousBackupImporter extends QueueHandler
- {
-
- function transport()
- {
- return 'dlcsback';
- }
-
- function handle($data) : bool
- {
- list($user, $body) = $data;
- try {
- $doc = $this->importHTML($body);
- } catch (ClientException $cex) {
-
- common_log(LOG_WARNING, $cex->getMessage());
- return true;
- }
-
- if (empty($doc)) {
- return true;
- }
- $dls = $doc->getElementsByTagName('dl');
- if ($dls->length != 1) {
-
- common_log(LOG_WARNING, 'Bad input file');
- return true;
- }
- $dl = $dls->item(0);
- $children = $dl->childNodes;
- $dt = null;
- for ($i = 0; $i < $children->length; $i++) {
- try {
- $child = $children->item($i);
- if ($child->nodeType != XML_ELEMENT_NODE) {
- continue;
- }
- switch (strtolower($child->tagName)) {
- case 'dt':
-
-
-
- if (!empty($dt)) {
-
- $this->importBookmark($user, $dt);
- $dt = null;
- }
- $dt = $child;
- break;
- case 'dd':
- $dd = $child;
- if (!empty($dt)) {
-
-
- $saved = $this->importBookmark($user, $dt, $dd);
- }
- $dt = null;
- $dd = null;
- break;
- case 'p':
- common_log(LOG_INFO, 'Skipping the <p> in the <dl>.');
- break;
- default:
- common_log(LOG_WARNING,
- "Unexpected element $child->tagName ".
- " found in import.");
- }
- } catch (Exception $e) {
- common_log(LOG_ERR, $e->getMessage());
- $dt = $dd = null;
- }
- }
- if (!empty($dt)) {
-
- try {
- $this->importBookmark($user, $dt);
- } catch (Exception $e) {
- common_log(LOG_ERR, $e->getMessage());
- }
- }
- return true;
- }
-
- function importBookmark($user, $dt, $dd = null)
- {
- $as = $dt->getElementsByTagName('a');
- if ($as->length == 0) {
-
- throw new ClientException(_m("No <A> tag in a <DT>."));
- }
- $a = $as->item(0);
- $private = $a->getAttribute('private');
- if ($private != 0) {
-
- throw new ClientException(_m('Skipping private bookmark.'));
- }
- if (!empty($dd)) {
- $description = $dd->nodeValue;
- } else {
- $description = null;
- }
- $addDate = $a->getAttribute('add_date');
- $data = array(
- 'profile_id' => $user->id,
- 'title' => $a->nodeValue,
- 'description' => $description,
- 'url' => $a->getAttribute('href'),
- 'tags' => preg_split('/[\s,]+/', $a->getAttribute('tags'), null, PREG_SPLIT_NO_EMPTY),
- 'created' => common_sql_date(intval($addDate))
- );
- $qm = QueueManager::get();
- $qm->enqueue($data, 'dlcsbkmk');
- }
-
- function importHTML($body)
- {
-
-
- $old = error_reporting(error_reporting() & ~(E_WARNING | E_NOTICE));
- $dom = new DOMDocument();
- $ok = $dom->loadHTML($body);
- error_reporting($old);
- if ($ok) {
- foreach ($dom->getElementsByTagName('body') as $node) {
- $this->fixListsIn($node);
- }
- return $dom;
- } else {
- return null;
- }
- }
- function fixListsIn(DOMNode $body) {
- $toFix = array();
- foreach ($body->childNodes as $node) {
- if ($node->nodeType == XML_ELEMENT_NODE) {
- $el = strtolower($node->nodeName);
- if ($el == 'dl') {
- $toFix[] = $node;
- }
- }
- }
- foreach ($toFix as $node) {
- $this->fixList($node);
- }
- }
- function fixList(DOMNode $list) {
- $toFix = array();
- foreach ($list->childNodes as $node) {
- if ($node->nodeType == XML_ELEMENT_NODE) {
- $el = strtolower($node->nodeName);
- if ($el == 'dt' || $el == 'dd') {
- $toFix[] = $node;
- }
- if ($el == 'dl') {
-
-
- $this->fixList($node);
- }
- }
- }
- foreach ($toFix as $node) {
- $this->fixListItem($node);
- }
- }
- function fixListItem(DOMNode $item) {
-
-
-
-
-
-
-
-
-
-
-
-
-
- $toMove = array();
- foreach ($item->childNodes as $node) {
- if ($node->nodeType == XML_ELEMENT_NODE) {
- $el = strtolower($node->nodeName);
- if ($el == 'dt' || $el == 'dd') {
-
-
- $toMove[] = $node;
- }
- if ($el == 'dl') {
-
-
- $this->fixList($node);
- }
- }
- }
- $parent = $item->parentNode;
- $next = $item->nextSibling;
- foreach ($toMove as $node) {
- $item->removeChild($node);
- $parent->insertBefore($node, $next);
- $this->fixListItem($node);
- }
- }
- }
|