Search Replace DBのキモイ仕様

移行で、WordPressのDBデータを持って行っても
移行先ではそのままでは使えない
DBに入っているURLが違うから

そこで、役立つのが「Search Replace DB」だ
って、某ソフトウェア紹介サイトのセリフの真似だけど

こいつはそんなDBの中のデータを全置換してくれるんだけど
データベースにアクセスするには、DB名やDBユーザー名、パスワードを設定する必要があるんだけど
これは、そんな設定の必要がない
どっからとってきてるんかと言うと、wp-config.phpから
でも単にrequireしようとすると、余計なものまで読み込もうとするので

[fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”]

require_once(ABSPATH . 'wp-settings.php');

これね
エラーが出てしまう

でも、これはエラーが出ずに取ってきてるのでどうやってるのかと思ってソースを見てみた(374行目付近)

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”]

/**
* Search through the file name passed for a set of defines used to set up
* WordPress db access.
*
* @param string $filename The file name we need to scan for the defines.
*
* @return array List of db connection details.
*/
function icit_srdb_define_find( $filename = 'wp-config.php' ) {

$filename = dirname( __FILE__ ) . '/' . basename( $filename );

if ( file_exists( $filename ) && is_file( $filename ) && is_readable( $filename ) ) {
$file = @fopen( $filename, 'r' );
$file_content = fread( $file, filesize( $filename ) );
@fclose( $file );
}

preg_match_all( '/define\s*?\(\s*?([\'"])(DB_NAME|DB_USER|DB_PASSWORD|DB_HOST|DB_CHARSET)\1\s*?,\s*?([\'"])([^\3]*?)\3\s*?\)\s*?;/si', $file_content, $defines );

if ( ( isset( $defines[ 2 ] ) && ! empty( $defines[ 2 ] ) ) && ( isset( $defines[ 4 ] ) && ! empty( $defines[ 4 ] ) ) ) {
foreach( $defines[ 2 ] as $key => $define ) {

switch( $define ) {
case 'DB_NAME':
$name = $defines[ 4 ][ $key ];
break;
case 'DB_USER':
$user = $defines[ 4 ][ $key ];
break;
case 'DB_PASSWORD':
$pass = $defines[ 4 ][ $key ];
break;
case 'DB_HOST':
$host = $defines[ 4 ][ $key ];
break;
case 'DB_CHARSET':
$char = $defines[ 4 ][ $key ];
break;
}
}
}

return array( $host, $name, $user, $pass, $char );
}

噴いた
なるほど、fopen fread fcloseでそのまま読んでるわけかw
file_get_contentsを使えばいいのに・・・
そして正規表現を使ってパースしているわけか
エラーハンドリングしているのにエラー制御したりしてるしw

どうやってるのかなと、調べてみたら結構な力業だった[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.