Since 2013, I have set up my personal blog with wordpress. In four years, I have written nearly 50 articles, recorded some problems and solutions I encountered in daily development, and also made several series of technical sharing. Although the number of blog posts is not very many, but every article spent a lot of thought in writing, and recently I have been thinking about the problem of keeping writing blog and keeping blog updated frequently.
WordPress is phper’s favorite blog system, and also a very popular CMS management system around the world. Its advantages are frequent updates and iterations, powerful functions, and quite a number of themes and plug-ins available. But the disadvantage is that the front-end rendering is slow, and the background references some CDN and AVATAR libraries that are walled. If you don’t configure them well, the experience of writing articles in the background will be quite bad. In recent years, Hexo has been on the rise, becoming a favorite of most bloggers due to its Markdown writing style and static publishing nature. With these considerations in mind, I came up with the idea of moving my blog from wordpress to Hexo. The following documents the migration process. For those of you who have the same needs, quit wordpress and join Hexo
Upload pictures to Seven Cows
In wordpress before writing articles, containing images are using wordpress multimedia add, so pictures to access the links are basically like http://idoubi.cc/wp-content/uploads/2017/08/01/abc.jpg, Therefore, in order to enable the normal display of images after the blog migration, it is necessary to write scripts to upload the images in the wordpress media space to Qiniu, and then replace the image links contained in the articles in batches.
-
Introduce the Seven Cow SDK
composer require qiniu/php-sdkCopy the code
-
Write scripts to upload local pictures to Seven Cows
require_once './vendor/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
$accessKey = 'RKassuWW4TB4_xxxxxyyyyyQj2iLEIBq9GSGs8E';
$secretKey = '6iVWfPayYPhosxxxxxyyyyyO909Wp6GsV0oOt';
$bucket = 'idoublog';
$auth = new Auth($accessKey, $secretKey);
$token = $auth->uploadToken($bucket);
$uploadManager = new UploadManager();
// Upload images in batches
$basePath = 'D:/phpStudy/WWW/project/idoubiblog/wp-content/uploads/';
$fileDir = 'D:/phpStudy/WWW/project/idoubiblog/wp-content/uploads';
$files = getUploadFiles($fileDir);
$successCount = 0;
$failCount = 0;
foreach ($files as $k => $v) {
$key = str_replace($basePath, ' ', $k);
$file = mb_convert_encoding($v, 'gbk'.'utf8'); // Transcode the file path to prevent garbled characters when the file name is Chinese
list($res, $err) = $uploadManager->putFile($token, $key, $file);
if (empty($err)) { // Upload succeeded
$successCount++;
} else { // Upload failed
$failCount++;
var_export('Upload failed:' . $v);
echo "\r\n"; }}echo "Upload success: {$successCount}, upload failure: {$failCount}";
// Get all files that need to be uploaded
function getUploadFiles($dirPath) {
$files = array(a); $scanFiles = myScanDir($dirPath);foreach ($scanFiles as $ak => $av) {
if (is_array($av)) {
foreach ($av as $bk => $bv) {
if (is_array($bv)) {
foreach ($bv as$ck => $cv) { $files[$cv] = $cv; }}else{ $files[$bv] = $bv; }}}else{ $files[$av] = $av; }}return $files;
}
// Walk through the files under the folder
function myScanDir($dirPath) {
$files = array(a);if (is_dir($dirPath)) {
if ($fp = opendir($dirPath)) {
while(($file = readdir($fp)) ! = =false) {
if($file ! ='. '&& $file ! ='.. ') {
$filePath = $dirPath . '/' . $file;
if (is_dir($filePath)) {
$files[] = myScanDir($filePath);
} else {
$files[] = iconv('gbk'.'utf-8', $filePath); // Transcode the file path to prevent garbled characters when the file name is Chinese
}
}
}
}
}
return $files;
}Copy the code
Exporting wordpress Data
-
Batch replace image addresses contained in database articles
update wp_posts set post_content = replace(post_content, 'http://idoubi.cc/wp-content/uploads/'.'http://qiniu.idoubi.cc/');Copy the code
-
Export XML files in the wordpress background
Install hexo
npm i -g hexo-cli
hexo init idoublogCopy the code
Migrate data to Hexo
-
Installing the Migration plug-in
cd idoublog npm install hexo-migrator-wordpress --saveCopy the code
-
Modify plug-ins (to prevent garbled article titles after importing data)
# add node_modules/hexo-migrator-wordpress/index.js on line 63 if (slug) slug = decodeURI(slug);Copy the code
-
Import data
hexo migrate wordpress /path/to/wordpress.xmlCopy the code
Install the theme
-
The fork theme
https://github.com/yscoder/hexo-theme-indigoCopy the code
-
Install the theme
cd idoublog/ git submodule add [email protected]:mikemintang/hexo-theme-indigo.git themes/indigoCopy the code
-
Modify the theme configuration and submit it to fork’s theme repository
Generated content
hexo generateCopy the code
Local preview
-
Install the browser sync plugin to sync your preview while writing your article
npm i hexo-browsersync --saveCopy the code
-
Preview with port
hexo s -p8031Copy the code
Publish content
-
Configuring the Publishing Environment
deploy: type: git repo: https://github.com/mikemintang/idoubi.cc branch: masterCopy the code
-
Publish content
# after executing this command, the blog static content will be released to https://github.com/mikemintang/idoubi.cc hexo deployCopy the code
Cloud server synchronizes blog content
-
Initialize the project folder used to synchronize blog content
mkdir /idoublog cd /idoublog git init git remote add origin https://github.com/mikemintang/idoubi.cc.gitCopy the code
-
Write automatic synchronization shell script vi /shell/deploy_idoublog.sh
#! /bin/sh
cd /idoublog
git pull origin masterCopy the code
- Execute shell scripts periodically
crontab -e
*/1 * * * * /bin/sh /shell/deploy_idoublog.shCopy the code
Through the above steps, my blog was successfully migrated from wordpress to Hexo. I can now write a blog locally with Sublime, then execute hexo deploy –generate and the blog content will be automatically synchronized to idoubi.cc/