Skip to content

Commit

Permalink
内部コードの修正
Browse files Browse the repository at this point in the history
  • Loading branch information
tomk79 committed Apr 11, 2023
1 parent 16c0630 commit 49db43e
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions php/commands/publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class publish{
private $plugin_conf;

/** パス設定 */
private $path_tmp_publish, $path_publish_dir, $path_docroot;
private $path_tmp_publish, $path_publish_dir, $path_controot;

/** ドメイン設定 */
private $domain;
Expand Down Expand Up @@ -87,19 +87,19 @@ public static function register( $px = null, $options = null ){
/**
* constructor
* @param object $px Picklesオブジェクト
* @param object $json プラグイン設定
* @param object $options プラグイン設定
*/
public function __construct( $px, $json ){
public function __construct( $px, $options ){
$this->px = $px;
$this->plugin_conf = $json;
$this->plugin_conf = $options;

$this->path_tmp_publish = $px->fs()->get_realpath( $px->get_realpath_homedir().'_sys/ram/publish/' );
$this->path_lockfile = $this->path_tmp_publish.'applock.txt';
if( $this->get_path_publish_dir() !== false ){
$this->path_publish_dir = $this->get_path_publish_dir();
}
$this->domain = $px->conf()->domain;
$this->path_docroot = $px->conf()->path_controot;
$this->path_controot = $px->conf()->path_controot;

// Extensionをマッチさせる正規表現
$process = array_keys( get_object_vars( $this->px->conf()->funcs->processor ) );
Expand Down Expand Up @@ -185,7 +185,7 @@ private function cli_header(){
print 'lockfile: '.$this->path_lockfile."\n";
print 'publish directory: '.$this->path_publish_dir."\n";
print 'domain: '.$this->domain."\n";
print 'docroot directory: '.$this->path_docroot."\n";
print 'docroot directory: '.$this->path_controot."\n";
print 'ignore: '.join(', ', $this->plugin_conf->paths_ignore)."\n";
print 'region: '.join(', ', $this->paths_region)."\n";
print 'ignore (tmp): '.join(', ', $this->paths_ignore)."\n";
Expand Down Expand Up @@ -328,7 +328,7 @@ function cont_EditPublishTargetPathApply(formElm){
</tr>
<tr>
<th style="word-break:break-all;">docroot directory</th>
<td style="word-break:break-all;"><?= htmlspecialchars($this->path_docroot ?? "") ?></td>
<td style="word-break:break-all;"><?= htmlspecialchars($this->path_controot ?? "") ?></td>
</tr>
<tr>
<th style="word-break:break-all;">region</th>
Expand Down Expand Up @@ -435,7 +435,7 @@ private function exec_publish( $px ){

}elseif( $this->px->fs()->is_dir(dirname($_SERVER['SCRIPT_FILENAME']).$path) ){
// ディレクトリを処理
$this->px->fs()->mkdir( $this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path );
$this->px->fs()->mkdir( $this->path_tmp_publish.'/htdocs'.$this->path_controot.$path );
print ' -> A directory.'."\n";

}else{
Expand All @@ -450,12 +450,12 @@ private function exec_publish( $px ){
case 'pass':
// pass
print $ext.' -> '.$proc_type."\n";
if( !$this->px->fs()->mkdir_r( dirname( $this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path ) ) ){
if( !$this->px->fs()->mkdir_r( dirname( $this->path_tmp_publish.'/htdocs'.$this->path_controot.$path ) ) ){
$status_code = 500;
$this->alert_log(array( @date('Y-m-d H:i:s'), $path, 'FAILED to making parent directory.' ));
break;
}
if( !$this->px->fs()->copy( dirname($_SERVER['SCRIPT_FILENAME']).$path , $this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path ) ){
if( !$this->px->fs()->copy( dirname($_SERVER['SCRIPT_FILENAME']).$path , $this->path_tmp_publish.'/htdocs'.$this->path_controot.$path ) ){
$status_code = 500;
$this->alert_log(array( @date('Y-m-d H:i:s'), $path, 'FAILED to copying file.' ));
break;
Expand Down Expand Up @@ -502,10 +502,10 @@ private function exec_publish( $px ){

// コンテンツの書き出し処理
// エラーが含まれている場合でも、得られたコンテンツを出力する。
$this->px->fs()->mkdir_r( dirname( $this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path ) );
$this->px->fs()->save_file( $this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path, base64_decode( $bin->body_base64 ?? null ) );
$this->px->fs()->mkdir_r( dirname( $this->path_tmp_publish.'/htdocs'.$this->path_controot.$path ) );
$this->px->fs()->save_file( $this->path_tmp_publish.'/htdocs'.$this->path_controot.$path, base64_decode( $bin->body_base64 ?? null ) );
foreach( $bin->relatedlinks as $link ){
$link = $this->px->fs()->get_realpath( $link, dirname($this->path_docroot.$path).'/' );
$link = $this->px->fs()->get_realpath( $link, dirname($this->path_controot.$path).'/' );
$link = $this->px->fs()->normalize_path( $link );
$tmp_link = preg_replace( '/^'.preg_quote($this->px->get_path_controot(), '/').'/s', '/', $link );
if( $this->px->fs()->is_dir( $this->px->get_realpath_docroot().'/'.$link ) ){
Expand Down Expand Up @@ -537,19 +537,19 @@ private function exec_publish( $px ){
$status_code ,
$status_message ,
$str_errors,
(file_exists($this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path) ? filesize($this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path) : false),
(file_exists($this->path_tmp_publish.'/htdocs'.$this->path_controot.$path) ? filesize($this->path_tmp_publish.'/htdocs'.$this->path_controot.$path) : false),
microtime(true)-$microtime
));

}

if( !empty( $this->path_publish_dir ) ){
// パブリッシュ先ディレクトリに都度コピー
if( $this->px->fs()->is_file( $this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path ) ){
$this->px->fs()->mkdir_r( dirname( $this->path_publish_dir.$this->path_docroot.$path ) );
if( $this->px->fs()->is_file( $this->path_tmp_publish.'/htdocs'.$this->path_controot.$path ) ){
$this->px->fs()->mkdir_r( dirname( $this->path_publish_dir.$this->path_controot.$path ) );
$this->px->fs()->copy(
$this->path_tmp_publish.'/htdocs'.$this->path_docroot.$path ,
$this->path_publish_dir.$this->path_docroot.$path
$this->path_tmp_publish.'/htdocs'.$this->path_controot.$path ,
$this->path_publish_dir.$this->path_controot.$path
);
print ' -> copied to publish dir'."\n";
}
Expand All @@ -576,8 +576,8 @@ private function exec_publish( $px ){
set_time_limit(30*60);
foreach( $this->paths_region as $path_region ){
$this->sync_dir(
$this->path_tmp_publish.'/htdocs'.$this->path_docroot ,
$this->path_publish_dir.$this->path_docroot ,
$this->path_tmp_publish.'/htdocs'.$this->path_controot ,
$this->path_publish_dir.$this->path_controot ,
$path_region
);
}
Expand All @@ -589,7 +589,7 @@ private function exec_publish( $px ){

$path_logfile = $this->path_tmp_publish.'alert_log.csv';
clearstatcache();
if( is_file( $path_logfile ) ){
if( $this->px->fs()->is_file( $path_logfile ) ){
sleep(1);
$alert_log = $this->px->fs()->read_csv( $path_logfile );
array_shift( $alert_log );
Expand Down Expand Up @@ -646,7 +646,7 @@ private function sync_dir( $path_sync_from , $path_sync_to, $path_region ){
$this->sync_dir_compare_and_cleanup( $path_sync_to , $path_sync_from, $path_region );
print "\n";
return true;
}//sync_dir()
}

/**
* ディレクトリを複製する(下層ディレクトリも全てコピー)
Expand All @@ -670,7 +670,7 @@ private function sync_dir_copy_r( $from, $to, $path_region, $perm = null ){

$result = true;

if( is_file( $from.$path_region ) ){
if( $this->px->fs()->is_file( $from.$path_region ) ){
if( $this->px->fs()->mkdir_r( dirname( $to.$path_region ) ) ){
if( $this->px->is_ignore_path( $path_region ) ){
// ignore指定されているパスには、操作しない。
Expand All @@ -684,8 +684,8 @@ private function sync_dir_copy_r( $from, $to, $path_region, $perm = null ){
}else{
$result = false;
}
}elseif( is_dir( $from.$path_region ) ){
if( !is_dir( $to.$path_region ) ){
}elseif( $this->px->fs()->is_dir( $from.$path_region ) ){
if( !$this->px->fs()->is_dir( $to.$path_region ) ){
if( $this->px->is_ignore_path( $path_region ) ){
// ignore指定されているパスには、操作しない。
}elseif( !$this->is_region_path( $path_region ) ){
Expand All @@ -699,10 +699,10 @@ private function sync_dir_copy_r( $from, $to, $path_region, $perm = null ){
$itemlist = $this->px->fs()->ls( $from.$path_region );
foreach( $itemlist as $Line ){
if( $Line == '.' || $Line == '..' ){ continue; }
if( is_dir( $from.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
if( is_file( $to.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
if( $this->px->fs()->is_dir( $from.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
if( $this->px->fs()->is_file( $to.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
continue;
}elseif( !is_dir( $to.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
}elseif( !$this->px->fs()->is_dir( $to.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
if( $this->px->is_ignore_path( $path_region.DIRECTORY_SEPARATOR.$Line ) ){
// ignore指定されているパスには、操作しない。
}elseif( !$this->is_region_path( $path_region.DIRECTORY_SEPARATOR.$Line ) ){
Expand All @@ -717,7 +717,7 @@ private function sync_dir_copy_r( $from, $to, $path_region, $perm = null ){
$result = false;
}
continue;
}elseif( is_file( $from.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
}elseif( $this->px->fs()->is_file( $from.$path_region.DIRECTORY_SEPARATOR.$Line ) ){
if( !$this->sync_dir_copy_r( $from, $to, $path_region.DIRECTORY_SEPARATOR.$Line , $perm ) ){
$result = false;
}
Expand All @@ -727,7 +727,7 @@ private function sync_dir_copy_r( $from, $to, $path_region, $perm = null ){
}

return $result;
}//sync_dir_copy_r()
}

/**
* ディレクトリの内部を比較し、$comparisonに含まれない要素を$targetから削除する。
Expand All @@ -744,15 +744,17 @@ private function sync_dir_compare_and_cleanup( $target , $comparison, $path_regi
if( $count%100 == 0 ){print '.';}
$count ++;

if( is_null( $comparison ) || is_null( $target ) ){ return false; }
if( is_null( $comparison ) || is_null( $target ) ){
return false;
}

$target = $this->px->fs()->localize_path($target);
$comparison = $this->px->fs()->localize_path($comparison);
$path_region = $this->px->fs()->localize_path($path_region);
$flist = array();

// 先に、ディレクトリ内をスキャンする
if( is_dir( $target.$path_region ) ){
if( $this->px->fs()->is_dir( $target.$path_region ) ){
$flist = $this->px->fs()->ls( $target.$path_region );
foreach ( $flist as $Line ){
if( $Line == '.' || $Line == '..' ){ continue; }
Expand All @@ -770,7 +772,7 @@ private function sync_dir_compare_and_cleanup( $target , $comparison, $path_regi
// 範囲外のパスには、操作しない。
return true;
}
if( is_dir( $target.$path_region ) ){
if( $this->px->fs()->is_dir( $target.$path_region ) ){
if( !count($flist) ){
// ディレクトリの場合は、内容が空でなければ削除しない。
$this->px->fs()->rm( $target.$path_region );
Expand All @@ -783,7 +785,7 @@ private function sync_dir_compare_and_cleanup( $target , $comparison, $path_regi
}

return true;
}//sync_dir_compare_and_cleanup()
}


/**
Expand Down Expand Up @@ -915,6 +917,7 @@ private function cleanup_tmp_publish_dir( $path, $localpath = null ){

/**
* make list by sitemap
*
* @return bool 常に `true` を返します。
*/
private function make_list_by_sitemap(){
Expand Down Expand Up @@ -1035,7 +1038,7 @@ private function add_queue( $path ){
return false;
}

$path = $this->px->fs()->normalize_path( $this->px->fs()->get_realpath( $path, $this->path_docroot ) );
$path = $this->px->fs()->normalize_path( $this->px->fs()->get_realpath( $path, $this->path_controot ) );
$path = preg_replace('/\#.*$/', '', $path);
$path = preg_replace('/\?.*$/', '', $path);
if( preg_match( '/\/$/', $path ) ){
Expand Down Expand Up @@ -1119,7 +1122,7 @@ private function is_ignore_path( $path ){
}
$rtn[$path] = false;// <- default
return $rtn[$path];
}// is_ignore_path()
}

/**
* パブリッシュ範囲内か調べる
Expand Down Expand Up @@ -1154,7 +1157,7 @@ private function is_region_path( $path ){
}
}
return true;
}// is_region_path()
}


/**
Expand All @@ -1177,7 +1180,7 @@ private function get_region_root_path(){
array_push($rtn, $path);
}
return $rtn;
}// get_region_root_path()
}


/**
Expand All @@ -1195,7 +1198,7 @@ private function get_path_publish_dir(){
return false;
}
return $tmp_path;
}// get_path_publish_dir()
}

/**
* パブリッシュをロックする。
Expand All @@ -1206,7 +1209,7 @@ private function lock(){
$lockfilepath = $this->path_lockfile;
$timeout_limit = 5;

if( !is_dir( dirname( $lockfilepath ) ) ){
if( !$this->px->fs()->is_dir( dirname( $lockfilepath ) ) ){
$this->px->fs()->mkdir_r( dirname( $lockfilepath ) );
}

Expand Down Expand Up @@ -1271,7 +1274,6 @@ private function is_locked(){
private function unlock(){
$lockfilepath = $this->path_lockfile;

// PHPのFileStatusCacheをクリア
clearstatcache();
if( !$this->px->fs()->is_file( $lockfilepath ) ){
return true;
Expand All @@ -1288,7 +1290,6 @@ private function unlock(){
private function touch_lockfile(){
$lockfilepath = $this->path_lockfile;

// PHPのFileStatusCacheをクリア
clearstatcache();
if( !is_file( $lockfilepath ) ){
return false;
Expand Down

0 comments on commit 49db43e

Please sign in to comment.