Пример #1
0
 /**
  * Given the XML source of a WordPress export, imports each post into Known.
  * @param $xml
  */
 static function importWordPressXML($xml)
 {
     // XML will be imported as blog posts, so make sure we can import those ...
     if (!($text = \Idno\Core\Idno::site()->plugins()->get('Text'))) {
         return false;
     }
     if ($data = simplexml_load_string($xml, null, LIBXML_NOCDATA)) {
         $namespaces = $data->getDocNamespaces(false);
         $namespaces[] = null;
         unset($namespace_data);
         unset($xml);
         if (!empty($data->channel->item)) {
             foreach ($data->channel->item as $item_structure) {
                 $item = [];
                 foreach ($namespaces as $ns => $namespace) {
                     if ($properties = (array) $item_structure->children($namespace)) {
                         foreach ($properties as $name => $val) {
                             if (!empty($ns)) {
                                 $name = $ns . ':' . $name;
                             }
                             $item[$name] = $val;
                         }
                     }
                 }
                 if ($item['wp:post_type'] == 'post' && $item['wp:status'] == 'publish') {
                     $title = $item['title'];
                     if (!empty($item['content:encoded'])) {
                         $body = $item['content:encoded'];
                     } else {
                         if (!empty($item['description'])) {
                             $body = $item['description'];
                         } else {
                             $body = '';
                         }
                     }
                     if (!empty($item['wp:post_date'])) {
                         $published = strtotime($item['wp:post_date']);
                     } else {
                         if (!empty($item['pubDate'])) {
                             $published = strtotime($item['pubDate']);
                         } else {
                             $published = time();
                         }
                     }
                     if (!empty($item['category'])) {
                         $tags = [];
                         if (!is_array($item['category'])) {
                             $item['category'] = [$item['category']];
                         }
                         foreach ($item['category'] as $category) {
                             $category = strtolower(trim($category));
                             if ($category != 'general' && $category != 'uncategorized') {
                                 $tags[] = '#' . preg_replace('/\\W+/', '', $category);
                             }
                         }
                         if (!empty($tags)) {
                             $body .= '<p>' . implode(' ', $tags) . '</p>';
                         }
                     }
                     self::importImagesFromBodyHTML($body, parse_url($item['link'], PHP_URL_HOST));
                     if (empty($item['title']) && strlen($body) < 600) {
                         $object = new \IdnoPlugins\Status\Status();
                         $object->created = $published;
                         $object->body = $body;
                         $object->publish(true);
                     } else {
                         $object = new \IdnoPlugins\Text\Entry();
                         $object->setTitle(html_entity_decode($title));
                         $object->created = $published;
                         $object->body = $body;
                         $object->publish(true);
                     }
                     if (!empty($item['wp:comment'])) {
                         if (!is_array($item['wp:comment'])) {
                             $item['wp:comment'] = [$item['wp:comment']];
                         }
                         foreach ($item['wp:comment'] as $comment_obj) {
                             $comment = (array) $comment_obj;
                             if ($object->addAnnotation('reply', $comment['comment_author'], $comment['comment_author_url'], '', $comment['comment_content'], null, strtotime($comment['comment_date_gmt']), null, false)) {
                                 $object->save();
                             }
                         }
                     }
                 }
             }
         }
     }
 }