首页 > 博客工具 > 用WLW发布WordPress的乱码解决方案

用WLW发布WordPress的乱码解决方案

2009年8月14日 honkin 发表评论 阅读评论

本文来自http://thinkland.twbbs.org/189.html,作为备份,以便查阅!

原本在WLW看起来美美的排版发布到WordPress的后却变了乱码。研究了一下,似乎是WLW经由xmlrpc.php发布档案时,HTML标签处理不当所惹的祸。(<>&等三个符号显示不出来),再认真谷歌了一下,才知道是一个处理的XML的函式库libxml2出了问题,目前了解是libxml2版本2.7.0 – 2.7.3都会有这种问题产生。可以透过一个简单的程式检察一下

View Code PHP

<?php echo phpinfo(); ?>

將此code另存成phpinfo.php上傳到主機,在連到對應網址,再拉到xml那一欄觀看。

(000webhost.com 目前是 libxml2 2.7.3 囧~~)

解決方法有兩種,其一是把libxml2版本降級,方法可以參考這篇

當然,一般虛擬主機的使用者看到這個方法一定不滿意,(叫我怎麼改啊~~~翻桌)

而第二個方法則是依據Samantha所言,修改worpress資料夾下的相關檔案,

這也是本文重點。(這可容易多了XD)

當然,若依照Samantha的方法能令WLW正常運作我也不用廢精神寫這篇了~

(就是因為我直接套用他的方法不能work啊啊啊啊~~)

還好我四五年前做過一點php語法的入門功夫,知道str_replace這隻函式怎麼用,

也碰巧懂一點html符號的code

便稍微修改了一下述三個檔案裡面的一個function (注意各人檔案內容不盡相同)

  • /wp-admin/import/blogger.php — function parse($xml)
  • /wp-includes/rss.php — function MagpieRSS ($source)
  • /wp-includes/class-IXR.php — function parse()
    依照下面的方式修改

    ?View Code PHP

    //-----------------------------------------------
    // /wp-admin/import/blogger.php 913-935
    //-----------------------------------------------   function parse($xml) {   global $app_logging;
    		array_unshift($this->ns_contexts, array());   $parser = xml_parser_create_ns();
    		xml_set_object($parser, $this);
    		xml_set_element_handler($parser, "start_element", "end_element");
    		xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
    		xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,0);
    		xml_set_character_data_handler($parser, "cdata");
    		xml_set_default_handler($parser, "_default");
    		xml_set_start_namespace_decl_handler($parser, "start_ns");
    		xml_set_end_namespace_decl_handler($parser, "end_ns");   $contents = "";   //xmllib 2.7.0 -2.7.3 stripping leading angle brackets bug patch
    		$xml =str_replace("&lt;","&#60;",$xml );
    		$xml =str_replace("&gt;","&#62;",$xml );
    		$xml =str_replace("&amp;","&#38;",$xml );
    		//end Fix   xml_parse($parser, $xml);   xml_parser_free($parser);   return true;
    	}

    ?View Code PHP

    //-----------------------------------------------
    // /wp-includes/rss.php 49-90
    //-----------------------------------------------   function MagpieRSS ($source) {   # if PHP xml isn't compiled in, die
    		#
    		if ( !function_exists('xml_parser_create') )
    			trigger_error( "Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php" );   $parser = @xml_parser_create();   if ( !is_resource($parser) )
    			trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");     $this->parser = $parser;   # pass in parser, and a reference to this object
    		# setup handlers
    		#
    		xml_set_object( $this->parser, $this );
    		xml_set_element_handler($this->parser,
    				'feed_start_element', 'feed_end_element' );   xml_set_character_data_handler( $this->parser, 'feed_cdata' );   //xmllib 2.7.0 -2.7.3 stripping leading angle brackets bug patch
    		$source =str_replace("&lt;","&#60;",$source );
    		$source =str_replace("&gt;","&#62;",$source );
    		$source =str_replace("&amp;","&#38;",$source );
    		//end fix   $status = xml_parse( $this->parser, $source );   if (! $status ) {
    			$errorcode = xml_get_error_code( $this->parser );
    			if ( $errorcode != XML_ERROR_NONE ) {
    				$xml_error = xml_error_string( $errorcode );
    				$error_line = xml_get_current_line_number($this->parser);
    				$error_col = xml_get_current_column_number($this->parser);
    				$errormsg = "$xml_error at line $error_line, column $error_col";   $this->error( $errormsg );
    			}
    		}   xml_parser_free( $this->parser );   $this->normalize();
    	}

    ?View Code PHP

    //-----------------------------------------------
    // /wp-includes/class-IXR.php 159-185
    //-----------------------------------------------   function parse() {
            // first remove the XML declaration
            $this->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $this->message);
            if (trim($this->message) == '') {
                return false;
            }
            $this->_parser = xml_parser_create();
            // Set XML parser to take the case of tags in to account
            xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, false);
            // Set XML parser callback functions
            xml_set_object($this->_parser, $this);
            xml_set_element_handler($this->_parser, 'tag_open', 'tag_close');
            xml_set_character_data_handler($this->_parser, 'cdata');   //xmllib 2.7.0 -2.7.3 stripping leading angle brackets bug patch
    		$this->message =str_replace("&lt;","&#60;",$this->message);
    		$this->message =str_replace("&gt;","&#62;",$this->message);
    		$this->message =str_replace("&amp;","&#38;",$this->message);
    		//end fix   if (!xml_parse($this->_parser, $this->message)) {
                /* die(sprintf('XML error: %s at line %d',
                    xml_error_string(xml_get_error_code($this->_parser)),
                    xml_get_current_line_number($this->_parser))); */
                return false;
            }
            xml_parser_free($this->_parser);
            // Grab the error messages, if any
            if ($this->messageType == 'fault') {
                $this->faultCode = $this->params[0]['faultCode'];
                $this->faultString = $this->params[0]['faultString'];
            }
            return true;
        }

    大功告成 enjoy it!

    分类: 博客工具 标签: ,
    1. 本文目前尚无任何评论.
    1. 本文目前尚无任何 trackbacks 和 pingbacks.