/index.php
PHP | 355 lines | 308 code | 41 blank | 6 comment | 59 complexity | bc9e85ef4662f38e358e0518075e83b8 MD5 | raw file
- <?php
- if(file_exists('wxr.xml')) {
- unlink('wxr.xml');
- }
- if(file_exists('md')) {
- deldir('md');
- }
- if(file_exists('md.zip')) {
- unlink('md.zip');
- }
- if ($_SERVER['REQUEST_METHOD'] == 'GET') {
- ?>
- <!doctype html>
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- <title>WXR 转 Markdown</title>
- </head>
- <body>
- <form action="<?=$_SERVER['PHP_SELF']?>" enctype="multipart/form-data" method="POST">
- <input type="file" name="wxr"><br />
- <input type="submit" value="上传 XML" />
- </form>
- </body>
- </html>
- <?php }
- elseif ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['wxr'])) {
- if ($_FILES['wxr']['size'] == 0) {
- header('Location: '.$_SERVER['PHP_SELF']);
- }
- class WXR_Parser_SimpleXML {
- function parse( $file ) {
- $authors = $posts = $categories = $tags = $terms = array();
- $internal_errors = libxml_use_internal_errors(true);
- $dom = new DOMDocument;
- $old_value = null;
- if ( function_exists( 'libxml_disable_entity_loader' ) ) {
- $old_value = libxml_disable_entity_loader( true );
- }
- $success = $dom->loadXML( file_get_contents( $file ) );
- if ( ! is_null( $old_value ) ) {
- libxml_disable_entity_loader( $old_value );
- }
- if ( ! $success || isset( $dom->doctype ) ) {
- return false;
- }
- $xml = simplexml_import_dom( $dom );
- unset( $dom );
- // halt if loading produces an error
- if ( ! $xml )
- return false;
- $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
- if ( ! $wxr_version )
- return false;
- $wxr_version = (string) trim( $wxr_version[0] );
- // confirm that we are dealing with the correct file format
- if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
- return false;
- $base_url = $xml->xpath('/rss/channel/wp:base_site_url');
- $base_url = (string) trim( $base_url[0] );
- $namespaces = $xml->getDocNamespaces();
- if ( ! isset( $namespaces['wp'] ) )
- $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
- if ( ! isset( $namespaces['excerpt'] ) )
- $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
- // grab authors
- foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
- $a = $author_arr->children( $namespaces['wp'] );
- $login = (string) $a->author_login;
- $authors[$login] = array(
- 'author_id' => (int) $a->author_id,
- 'author_login' => $login,
- 'author_email' => (string) $a->author_email,
- 'author_display_name' => (string) $a->author_display_name,
- 'author_first_name' => (string) $a->author_first_name,
- 'author_last_name' => (string) $a->author_last_name
- );
- }
- // grab cats, tags and terms
- foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
- $t = $term_arr->children( $namespaces['wp'] );
- $categories[] = array(
- 'term_id' => (int) $t->term_id,
- 'category_nicename' => (string) $t->category_nicename,
- 'category_parent' => (string) $t->category_parent,
- 'cat_name' => (string) $t->cat_name,
- 'category_description' => (string) $t->category_description
- );
- }
- foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
- $t = $term_arr->children( $namespaces['wp'] );
- $tags[] = array(
- 'term_id' => (int) $t->term_id,
- 'tag_slug' => (string) $t->tag_slug,
- 'tag_name' => (string) $t->tag_name,
- 'tag_description' => (string) $t->tag_description
- );
- }
- foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
- $t = $term_arr->children( $namespaces['wp'] );
- $terms[] = array(
- 'term_id' => (int) $t->term_id,
- 'term_taxonomy' => (string) $t->term_taxonomy,
- 'slug' => (string) $t->term_slug,
- 'term_parent' => (string) $t->term_parent,
- 'term_name' => (string) $t->term_name,
- 'term_description' => (string) $t->term_description
- );
- }
- // grab posts
- foreach ( $xml->channel->item as $item ) {
- $post = array(
- 'post_title' => (string) $item->title,
- 'guid' => (string) $item->guid,
- );
- $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
- $post['post_author'] = (string) $dc->creator;
- $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
- $excerpt = $item->children( $namespaces['excerpt'] );
- $post['post_content'] = (string) $content->encoded;
- $post['post_excerpt'] = (string) $excerpt->encoded;
- $wp = $item->children( $namespaces['wp'] );
- $post['post_id'] = (int) $wp->post_id;
- $post['post_date'] = (string) $wp->post_date;
- $post['post_date_gmt'] = (string) $wp->post_date_gmt;
- $post['comment_status'] = (string) $wp->comment_status;
- $post['ping_status'] = (string) $wp->ping_status;
- $post['post_name'] = (string) $wp->post_name;
- $post['status'] = (string) $wp->status;
- $post['post_parent'] = (int) $wp->post_parent;
- $post['menu_order'] = (int) $wp->menu_order;
- $post['post_type'] = (string) $wp->post_type;
- $post['post_password'] = (string) $wp->post_password;
- $post['is_sticky'] = (int) $wp->is_sticky;
- if ( isset($wp->attachment_url) )
- $post['attachment_url'] = (string) $wp->attachment_url;
- foreach ( $item->category as $c ) {
- $att = $c->attributes();
- if ( isset( $att['nicename'] ) )
- $post['terms'][] = array(
- 'name' => (string) $c,
- 'slug' => (string) $att['nicename'],
- 'domain' => (string) $att['domain']
- );
- }
- foreach ( $wp->postmeta as $meta ) {
- $post['postmeta'][] = array(
- 'key' => (string) $meta->meta_key,
- 'value' => (string) $meta->meta_value
- );
- }
- foreach ( $wp->comment as $comment ) {
- $meta = array();
- if ( isset( $comment->commentmeta ) ) {
- foreach ( $comment->commentmeta as $m ) {
- $meta[] = array(
- 'key' => (string) $m->meta_key,
- 'value' => (string) $m->meta_value
- );
- }
- }
- $post['comments'][] = array(
- 'comment_id' => (int) $comment->comment_id,
- 'comment_author' => (string) $comment->comment_author,
- 'comment_author_email' => (string) $comment->comment_author_email,
- 'comment_author_IP' => (string) $comment->comment_author_IP,
- 'comment_author_url' => (string) $comment->comment_author_url,
- 'comment_date' => (string) $comment->comment_date,
- 'comment_date_gmt' => (string) $comment->comment_date_gmt,
- 'comment_content' => (string) $comment->comment_content,
- 'comment_approved' => (string) $comment->comment_approved,
- 'comment_type' => (string) $comment->comment_type,
- 'comment_parent' => (string) $comment->comment_parent,
- 'comment_user_id' => (int) $comment->comment_user_id,
- 'commentmeta' => $meta,
- );
- }
- $posts[] = $post;
- }
- return array(
- 'authors' => $authors,
- 'posts' => $posts,
- 'categories' => $categories,
- 'tags' => $tags,
- 'terms' => $terms,
- 'base_url' => $base_url,
- 'version' => $wxr_version
- );
- }
- }
- function zip($source, $destination = null) {
- if (!extension_loaded('zip') || !file_exists($source)) {
- return false;
- }
- if (empty($destination)) {
- $destination = basename($source).'.zip';
- }
- $zip = new ZipArchive();
- if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
- return false;
- }
- $source = str_replace('\\', '/', realpath($source));
- if (is_dir($source) === true) {
- $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
- foreach ($files as $file) {
- $file = str_replace('\\', '/', $file);
- // Ignore "." and ".." folders
- if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ) {
- continue;
- }
- $file = realpath($file);
- $file = str_replace("\\","/",$file);
- if (is_dir($file) === true) {
- $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
- }
- else if (is_file($file) === true) {
- $contextFilePath = substr(str_replace($source, '', $file),1);
- $zip->addFromString($contextFilePath, file_get_contents($file));
- }
- }
- }
- else if (is_file($source) === true) {
- $zip->addFromString(basename($source), file_get_contents($source));
- }
- return $zip->close();
- }
- function deldir($dirPath) {
- if (! is_dir($dirPath)) {
- return 0;
- }
- if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
- $dirPath .= '/';
- }
- $files = glob($dirPath . '*', GLOB_MARK);
- foreach ($files as $file) {
- if (is_dir($file)) {
- deldir($file);
- } else {
- unlink($file);
- }
- }
- rmdir($dirPath);
- }
- function generate($layout, $name, $title, $author, $date, $content, $comments, $tags = '') {
- if($comments == 'open') {
- $comments = 'true';
- }
- else {
- $comments = 'false';
- }
- if($layout == 'post') {
- $content = trim(preg_replace('/<!--more-->/', '', $content));
- $file = "---\nlayout: post\ntitle: \"{$title}\"\nauthor: \"{$author}\"\ndate: ".date('Y-m-d H:i', $date)."\ncomments: {$comments}\ncategories: {$tags}\n---\n{$content}";
- $name = date('Y-m-d', $date).'-'.$name.'.markdown';
- if (!file_exists('md/_posts')) {
- mkdir('md/_posts');
- }
- if (!file_exists('md/_posts/'.$name)) {
- file_put_contents('md/_posts/'.$name, $file);
- }
- }
- elseif($layout == 'page') {
- $file = "---\nlayout: page\ntitle: \"{$title}\"\ndate: ".date('Y-m-d H:i', $date)."\ncomments: {$comments}\nfooter: true\n---\n{$content}";
- if (!file_exists('md/'.$name)) {
- mkdir('md/'.$name);
- }
- if (!file_exists('md/'.$name.'/index.markdown')) {
- file_put_contents('md/'.$name.'/index.markdown', $file);
- }
- }
- }
- move_uploaded_file($_FILES['wxr']['tmp_name'], './wxr.xml');
- $xmlparse = new WXR_Parser_SimpleXML();
- $xml = $xmlparse->parse('wxr.xml');
- unset($xmlparse);
- if (!isset($xml) || $xml == false) {
- if(file_exists('wxr.xml')) {
- unlink('wxr.xml');
- }
- exit('非法文件!');
- }
- mkdir('md');
- $count = count($xml['posts']);
- for($i=0; $i<$count; $i++) {
- if(isset($xml['posts'][$i]['terms'])) {
- $termscount = count($xml['posts'][$i]['terms']);
- $tags = '';
- for($j=0; $j<$termscount; $j++) {
- if($xml['posts'][$i]['terms'][$j]['domain'] == 'post_tag') {
- if($tags!='') {
- $tags .=', ';
- }
- $tags .= $xml['posts'][$i]['terms'][$j]['name'];
- }
- }
- $tags = "[{$tags}]";
- }
- else {
- $tags = '';
- }
- generate($xml['posts'][$i]['post_type'], $xml['posts'][$i]['post_name'], $xml['posts'][$i]['post_title'], $xml['authors'][$xml['posts'][$i]['post_author']]['author_display_name'], strtotime($xml['posts'][$i]['post_date']), $xml['posts'][$i]['post_content'], $xml['posts'][$i]['comment_status'], $tags);
- }
- zip('md');
- header('Location: md.zip');
- if(file_exists('md')) {
- deldir('md');
- }
- if(file_exists('wxr.xml')) {
- unlink('wxr.xml');
- }
- }
- ?>