└── wp2e2.php /wp2e2.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | 11 | Class Parse { 12 | public $wpHost; 13 | public $wpDb; 14 | public $wpUser; 15 | public $wpPassword; 16 | public $wpPrefix='wp_'; 17 | 18 | public $e2Host; 19 | public $e2Db; 20 | public $e2User; 21 | public $e2Password; 22 | public $e2Prefix='e2_'; 23 | 24 | // Название поля URL тега 25 | public $e2KeywordsUrlName='URLName'; 26 | 27 | 28 | /** 29 | * Собственно сама работа 30 | * @return void 31 | */ 32 | function go() 33 | { 34 | try { 35 | $dbWp = new PDO('mysql:dbname=' . $this->wpDb . ';host=' . $this->wpHost, $this->wpUser, $this->wpPassword); 36 | $dbE2 = new PDO('mysql:dbname=' . $this->e2Db . ';host=' . $this->e2Host, $this->e2User, $this->e2Password); 37 | 38 | $dbWp->query('SET NAMES utf8'); 39 | $dbE2->query('SET NAMES utf8'); 40 | 41 | // Посты 42 | $sSql = 'SELECT id, post_date, post_date_gmt, post_content, post_title, post_name, post_type, post_modified, post_status, comment_status 43 | FROM `' . $this->wpPrefix . 'posts` 44 | WHERE post_type = "post" 45 | AND post_status IN ("publish", "draft") 46 | ORDER BY id ASC'; 47 | $aPosts = array(); 48 | foreach ($dbWp->query($sSql) as $aPost) 49 | { 50 | $aPosts[$aPost['id']] = $aPost; 51 | } 52 | unset($aPostsSql); 53 | echo 'Found ' . count($aPosts) . ' posts'; 54 | 55 | // Тэги 56 | $sSql = 'SELECT r.object_id as post_id, t.name 57 | FROM `' . $this->wpPrefix . 'term_relationships` AS r 58 | JOIN `' . $this->wpPrefix . 'term_taxonomy` AS tt ON r.term_taxonomy_id = tt.term_id AND tt.taxonomy="post_tag" 59 | JOIN `' . $this->wpPrefix . 'terms` AS t ON t.term_id = tt.term_id 60 | ORDER BY post_id'; 61 | 62 | foreach ($dbWp->query($sSql) as $aTag) 63 | { 64 | // Пропускаем тэг, если нет поста 65 | if ( ! isset($aPosts[$aTag['post_id']])) 66 | { 67 | continue; 68 | } 69 | if ( ! isset($aPosts[$aTag['post_id']]['tags'])) 70 | { 71 | $aPosts[$aTag['post_id']]['tags'] = array(); 72 | } 73 | $aPosts[$aTag['post_id']]['tags'][] = $aTag['name']; 74 | } 75 | unset($aTagsSql); 76 | 77 | // Комментарии 78 | $sSql = 'SELECT comment_post_ID AS post_id, 79 | comment_author AS author, 80 | comment_author_email AS email, 81 | comment_author_url AS url, 82 | comment_author_IP AS ip, 83 | comment_date AS create_time, 84 | comment_content AS content 85 | FROM `' . $this->wpPrefix . 'comments` 86 | WHERE comment_approved = 1 87 | AND comment_author_IP != "127.0.0.1"'; 88 | foreach ($dbWp->query($sSql) as $aComment) 89 | { 90 | // Пропускаем коммент, если нет поста 91 | if ( ! isset($aPosts[$aComment['post_id']])) 92 | { 93 | continue; 94 | } 95 | if ( ! isset($aPosts[$aComment['post_id']]['comments'])) 96 | { 97 | $aPosts[$aComment['post_id']]['comments'] = array(); 98 | } 99 | 100 | $aPosts[$aComment['post_id']]['comments'][] = $aComment; 101 | } 102 | 103 | foreach($aPosts as $aPost) 104 | { 105 | $aSql = array( 106 | 'Title' => $aPost['post_title'], 107 | 'OriginalAlias' => $aPost['post_name'], 108 | 'Text' => $aPost['post_content'], 109 | 'IsPublished' => $aPost['post_status'] == 'publish', 110 | 'IsCommentable' => $aPost['comment_status'] == 'open', 111 | 'Stamp' => strtotime($aPost['post_date']), 112 | 'LastModified' => strtotime($aPost['post_modified']), 113 | 'FormatterID' => 'raw', 114 | 'Offset' => (strtotime($aPost['post_date']) - strtotime($aPost['post_date_gmt'])), 115 | ); 116 | $sSql = 'INSERT INTO `' . $this->e2Prefix . 'Notes` (' . implode(',', array_keys($aSql)) . ') 117 | VALUES (' . implode(',', array_map(array($dbE2, 'quote'), $aSql)) . ')'; 118 | 119 | $dbE2->exec($sSql); 120 | $iPostId = $dbE2->lastInsertId(); 121 | echo 'post_id: ' . $iPostId; 122 | 123 | if (isset($aPost['tags'])) 124 | { 125 | foreach ($aPost['tags'] as $sTag) 126 | { 127 | $sth = $dbE2->prepare('SELECT id FROM `' . $this->e2Prefix . 'Keywords` WHERE LOWER(keyword) = LOWER(' . $dbE2->quote($sTag) . ')'); 128 | $sth->execute(); 129 | $iTagId = $sth->fetchColumn(); 130 | if ( ! $iTagId) 131 | { 132 | $sSql = 'INSERT INTO `' . $this->e2Prefix . 'Keywords`(Keyword, ' . $this->e2KeywordsUrlName . ') VALUES (' . $dbE2->quote($sTag) . ',' . $dbE2->quote($sTag) . ')'; 133 | $dbE2->exec($sSql); 134 | $iTagId = $dbE2->lastInsertId(); 135 | } 136 | $sSql = 'INSERT INTO `' . $this->e2Prefix . 'NotesKeywords`(NoteID, KeywordID) VALUES (' . $dbE2->quote($iPostId) . ',' . $dbE2->quote($iTagId) . ')'; 137 | $dbE2->exec($sSql); 138 | } 139 | } 140 | 141 | if (isset($aPost['comments']) && is_array($aPost['comments'])) 142 | { 143 | foreach ($aPost['comments'] as $aComment) 144 | { 145 | $aSql = array( 146 | 'NoteID' => $iPostId, 147 | 'AuthorName' => $aComment['author'], 148 | 'AuthorEmail' => $aComment['email'], 149 | 'Text' => $aComment['content'], 150 | 'Stamp' => strtotime($aComment['create_time']), 151 | 'LastModified' => strtotime($aComment['create_time']), 152 | 'IP' => $aComment['ip'], 153 | ); 154 | 155 | $sSql = 'INSERT INTO `' . $this->e2Prefix . 'Comments` (' . implode(',', array_map(array($this, 'quoteTable'), array_keys($aSql))) . ') 156 | VALUES (' . implode(',', array_map(array($dbE2, 'quote'), $aSql)) . ')'; 157 | $dbE2->query($sSql); 158 | } 159 | } 160 | } 161 | } catch (PDOException $e) { 162 | echo 'Connection failed: ' . $e->getMessage(); 163 | } 164 | } 165 | 166 | function quoteTable($s) 167 | { 168 | return '`' . $s . '`'; 169 | } 170 | } 171 | 172 | 173 | $parse = new Parse; 174 | 175 | $parse->wpHost = 'localhost'; 176 | $parse->wpDb = ''; 177 | $parse->wpUser = ''; 178 | $parse->wpPassword = ''; 179 | 180 | $parse->e2Host = 'localhost'; 181 | $parse->e2Db = ''; 182 | $parse->e2User = ''; 183 | $parse->e2Password = ''; 184 | // Если Эгея >= 2.5 версии, то раскоментируйте эту строку 185 | // $parse->e2Prefix = 'e2Blog'; 186 | 187 | // Если Эгея >= 2.7 версии, то раскоментируйте эту строку 188 | // $parse->e2KeywordsUrlName = 'OriginalAlias'; 189 | 190 | $parse->go(); 191 | --------------------------------------------------------------------------------