changeset 112:b3ba2658a228

merged with upstream
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Sat, 13 Mar 2010 18:27:33 +0900
parents 74a174a245d2 (current diff) 3bed74eca373 (diff)
children 9b9e22be8407
files getepg.php storeProgram.php
diffstat 7 files changed, 223 insertions(+), 171 deletions(-) [+]
line wrap: on
line diff
--- a/getepg.old.php	Fri Mar 12 15:32:27 2010 +0900
+++ b/getepg.old.php	Sat Mar 13 18:27:33 2010 +0900
@@ -1,6 +1,8 @@
 #!/usr/bin/php
 <?php
-  include_once('config.php');
+  $script_path = dirname( __FILE__ );
+  chdir( $script_path );
+  include_once( $script_path . '/config.php');
   include_once( INSTALL_PATH . '/DBRecord.class.php' );
   include_once( INSTALL_PATH . '/Reservation.class.php' );
   include_once( INSTALL_PATH . '/Keyword.class.php' );
--- a/getepg.php	Fri Mar 12 15:32:27 2010 +0900
+++ b/getepg.php	Sat Mar 13 18:27:33 2010 +0900
@@ -1,17 +1,19 @@
 #!/usr/bin/php
 <?php
-  include_once('config.php');
+  $script_path = dirname( __FILE__ );
+  chdir( $script_path );
+  include_once( $script_path . '/config.php');
   include_once( INSTALL_PATH . '/DBRecord.class.php' );
   include_once( INSTALL_PATH . '/Reservation.class.php' );
   include_once( INSTALL_PATH . '/Keyword.class.php' );
   include_once( INSTALL_PATH . '/Settings.class.php' );
+  include_once( INSTALL_PATH . '/storeProgram.inc.php' );
 
   // 後方互換性
   if( ! defined( "BS_EPG_CHANNEL" )  ) define( "BS_EPG_CHANNEL",  "211"  );
   if( ! defined( "CS1_EPG_CHANNEL" ) ) define( "CS1_EPG_CHANNEL", "CS8"  );
   if( ! defined( "CS2_EPG_CHANNEL" ) ) define( "CS2_EPG_CHANNEL", "CS24" );
 
-
   function check_file( $file ) {
 	// ファイルがないなら無問題
 	if( ! file_exists( $file ) ) return true;
@@ -25,6 +27,11 @@
 	return false;
   }
 
+  // 並列化が可能か執念深く調べる
+  $use_para = false;
+  $use_para = (function_exists( "pcntl_fork" ) && function_exists( "posix_setsid" ) && function_exists( "pcntl_signal" ) && function_exists("pcntl_setpriority"));
+
+
   $settings = Settings::factory();
 
   $temp_xml_bs  = $settings->temp_xml."_bs";
@@ -43,8 +50,14 @@
   		exec( $cmdline );
   		$cmdline = $settings->epgdump." /BS ".$settings->temp_data." ".$temp_xml_bs;
   		exec( $cmdline );
-		$cmdline = INSTALL_PATH."/storeProgram.php BS ".$temp_xml_bs." >/dev/null 2>&1 &";
-		exec( $cmdline );
+  		if( $use_para ) {
+			$cmdline = INSTALL_PATH."/storeProgram.php BS ".$temp_xml_bs." >/dev/null 2>&1 &";
+			exec( $cmdline );
+		}
+		else {
+			storeProgram( "BS", $temp_xml_bs );
+			if( file_exists( $temp_xml_bs ) ) @unlink( $temp_xml_bs );
+		}
   		if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
 	}
 
@@ -57,8 +70,14 @@
 			exec( $cmdline );
 			$cmdline = $settings->epgdump." /CS ".$settings->temp_data." ".$temp_xml_cs1;
 			exec( $cmdline );
-			$cmdline = INSTALL_PATH."/storeProgram.php CS ".$temp_xml_cs1." >/dev/null 2>&1 &";
-			exec( $cmdline );
+			if( $use_para ) {
+				$cmdline = INSTALL_PATH."/storeProgram.php CS ".$temp_xml_cs1." >/dev/null 2>&1 &";
+				exec( $cmdline );
+			}
+			else {
+				storeProgram( "CS", $temp_xml_cs1 );
+				if( file_exists( $temp_xml_cs1 ) ) @unlink( $temp_xml_cs1 );
+			}
 			if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
 		}
 		$num = DBRecord::countRecords(  RESERVE_TBL, "WHERE complete = '0' AND (type = 'BS' OR type = 'CS') AND endtime > now() AND starttime < addtime( now(), '00:03:05')" );
@@ -67,8 +86,14 @@
 			exec( $cmdline );
 			$cmdline = $settings->epgdump." /CS ".$settings->temp_data." ".$temp_xml_cs2;
 			exec( $cmdline );
-			$cmdline = INSTALL_PATH."/storeProgram.php CS ".$temp_xml_cs2." >/dev/null 2>&1 &";
-			exec( $cmdline );
+			if( $use_para ) {
+				$cmdline = INSTALL_PATH."/storeProgram.php CS ".$temp_xml_cs2." >/dev/null 2>&1 &";
+				exec( $cmdline );
+			}
+			else {
+				storeProgram( "CS", $temp_xml_cs2 );
+				if( file_exists( $temp_xml_cs2 ) ) @unlink( $temp_xml_cs2 );
+			}
 			if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
 	  	}
   	}
@@ -84,11 +109,22 @@
 			exec( $cmdline );
 			$cmdline = $settings->epgdump." ".$key." ".$settings->temp_data." ".$temp_xml_gr.$value."";
 			exec( $cmdline );
-			$cmdline = INSTALL_PATH."/storeProgram.php GR ".$temp_xml_gr.$value." >/dev/null 2>&1 &";
-			exec( $cmdline );
+			if( $use_para ) {
+				$cmdline = INSTALL_PATH."/storeProgram.php GR ".$temp_xml_gr.$value." >/dev/null 2>&1 &";
+				exec( $cmdline );
+			}
+			else {
+				storeProgram( "GR", $temp_xml_gr.$value );
+				if( file_exists( $temp_xml_gr.$value ) ) @unlink( $temp_xml_gr.$value );
+			}
 			if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
   		}
   	}
   }
+  // 並列ならこれで終わりにする
+  if( $use_para ) exit();
 
+  garbageClean();			//  不要プログラム削除
+  doKeywordReservation();	// キーワード予約
+  exit();
 ?>
--- a/mediatomb.php	Fri Mar 12 15:32:27 2010 +0900
+++ b/mediatomb.php	Sat Mar 13 18:27:33 2010 +0900
@@ -1,7 +1,8 @@
 #!/usr/bin/php
 <?php
-
-include_once('config.php');
+$script_path = dirname( __FILE__ );
+chdir( $script_path );
+include_once( $script_path . '/config.php');
 include_once(INSTALL_PATH.'/DBRecord.class.php');
 include_once(INSTALL_PATH.'/reclib.php');
 include_once(INSTALL_PATH.'/Settings.class.php');
--- a/recomplete.php	Fri Mar 12 15:32:27 2010 +0900
+++ b/recomplete.php	Sat Mar 13 18:27:33 2010 +0900
@@ -1,6 +1,8 @@
 #!/usr/bin/php
 <?php
-include_once( "config.php" );
+$script_path = dirname( __FILE__ );
+chdir( $script_path );
+include_once( $script_path . '/config.php');
 include_once( INSTALL_PATH . "/DBRecord.class.php" );
 include_once( INSTALL_PATH . "/Settings.class.php" );
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/storeProgram.inc.php	Sat Mar 13 18:27:33 2010 +0900
@@ -0,0 +1,158 @@
+<?php
+
+function garbageClean() {
+	// 不要なプログラムの削除
+	// 8日以上前のプログラムを消す
+	$arr = array();
+	$arr = DBRecord::createRecords(  PROGRAM_TBL, "WHERE endtime < subdate( now(), 8 )" );
+	foreach( $arr as $val ) $val->delete();
+	
+	// 8日以上先のデータがあれば消す
+	$arr = array();
+	$arr = DBRecord::createRecords(  PROGRAM_TBL, "WHERE starttime  > adddate( now(), 8 )" );
+	foreach( $arr as $val ) $val->delete();
+}
+
+function doKeywordReservation() {
+  // キーワード自動録画予約
+ 	$arr = array();
+	$arr = Keyword::createKeywords();
+	foreach( $arr as $val ) {
+		try {
+			$val->reservation();
+		}
+		catch( Exception $e ) {
+		// 無視
+		}
+	}
+}
+
+function storeProgram( $type, $xmlfile ) {
+	global $BS_CHANNEL_MAP, $GR_CHANNEL_MAP, $CS_CHANNEL_MAP;
+	// チャンネルマップファイルの準備
+	$map = array();
+	if( $type == "BS" ) $map = $BS_CHANNEL_MAP;
+	else if( $type == "GR") $map = $GR_CHANNEL_MAP;
+	else if( $type == "CS") $map = $CS_CHANNEL_MAP;
+	
+	// XML parse
+  	$xml = @simplexml_load_file( $xmlfile );
+	if( $xml === false ) {
+		return;	// XMLが読み取れないなら何もしない
+	}
+	// channel抽出
+	foreach( $xml->channel as $ch ) {
+		$disc = $ch['id'];
+	 try {
+		// チャンネルデータを探す
+		$num = DBRecord::countRecords( CHANNEL_TBL , "WHERE channel_disc = '" . $disc ."'" );
+		if( $num == 0 ) {
+			if( array_key_exists( "$disc", $map ) ) {
+				// チャンネルデータがないなら新規作成
+				$rec = new DBRecord( CHANNEL_TBL );
+				$rec->type = $type;
+				$rec->channel = $map["$disc"];
+				$rec->channel_disc = $disc;
+				$rec->name = $ch->{'display-name'};
+			}
+		}
+		else {
+			// 存在した場合も、とりあえずチャンネル名は更新する
+			$rec = new DBRecord(CHANNEL_TBL, "channel_disc", $disc );
+			$rec->name = $ch->{'display-name'};
+		}
+	 }
+	 catch( Exception $e ) {
+		// 無視
+	 }
+	}
+	// channel 終了
+	
+	// programme 取得
+	
+	foreach( $xml->programme as $program ) {
+		$channel_disc = $program['channel']; 
+		if( ! array_key_exists( "$channel_disc", $map ) ) continue;
+		
+		$channel = $map["$channel_disc"];
+		$starttime = str_replace(" +0900", '', $program['start'] );
+		$endtime = str_replace( " +0900", '', $program['stop'] );
+		$title = $program->title;
+		$desc = $program->desc;
+		$cat_ja = "";
+		$cat_en = "";
+		foreach( $program->category as $cat ) {
+			if( $cat['lang'] == "ja_JP" ) $cat_ja = $cat;
+			if( $cat['lang'] == "en" ) $cat_en = $cat;
+		}
+		$program_disc = md5( $channel_disc . $starttime . $endtime );
+		// printf( "%s %s %s %s %s %s %s \n", $program_disc, $channel, $starttime, $endtime, $title, $desc, $cat_ja );
+		try {
+		 // カテゴリを処理する
+		 $category_disc = md5( $cat_ja . $cat_en );
+		 $num = DBRecord::countRecords(CATEGORY_TBL, "WHERE category_disc = '".$category_disc."'" );
+		 $cat_rec = null;
+		 if( $num == 0 ) {
+			// 新規カテゴリの追加
+			$cat_rec = new DBRecord( CATEGORY_TBL );
+			$cat_rec->name_jp = $cat_ja;
+			$cat_rec->name_en = $cat_en;
+		 	$cat_rec->category_disc = $category_disc;
+		 }
+		 else
+			$cat_rec = new DBRecord(CATEGORY_TBL, "category_disc" , $category_disc );
+		  //
+		 $channel_rec = new DBRecord(CHANNEL_TBL, "channel_disc", $channel_disc );
+		 $num = DBRecord::countRecords(PROGRAM_TBL, "WHERE program_disc = '".$program_disc."'" );
+		 if( $num == 0 ) {
+			// 新規番組
+			// 重複チェック 同時間帯にある番組
+			$options = "WHERE channel_disc = '".$channel_disc."' ".
+				"AND starttime < '". $endtime ."' AND endtime > '".$starttime."'";
+			$battings = DBRecord::countRecords(PROGRAM_TBL, $options );
+			if( $battings > 0 ) {
+				// 重複発生=おそらく放映時間の変更
+				$records = DBRecord::createRecords(PROGRAM_TBL, $options );
+				foreach( $records as $rec ) {
+					// 自動録画予約された番組は放映時間変更と同時にいったん削除する
+					try {
+						$reserve = new DBRecord(RESERVE_TBL, "program_id", $rec->id );
+						if( $reserve->autorec ) {
+							Reservation::cancel( $reserve->id );
+						}
+					}
+					catch( Exception $e ) {
+						//無視
+					}
+					// 番組削除
+					$rec->delete();
+				}
+			}
+			// //
+			$rec = new DBRecord( PROGRAM_TBL );
+			$rec->channel_disc = $channel_disc;
+			$rec->channel_id = $channel_rec->id;
+			$rec->type = $type;
+			$rec->channel = $channel_rec->channel;
+			$rec->title = $title;
+			$rec->description = $desc;
+			$rec->category_id = $cat_rec->id;
+			$rec->starttime = $starttime;
+			$rec->endtime = $endtime;
+			$rec->program_disc = $program_disc;
+		 }
+		 else {
+			// 番組内容更新
+			$rec = new DBRecord( PROGRAM_TBL, "program_disc", $program_disc );
+			$rec->title = $title;
+		 	$rec->description = $desc;
+			$rec->category_id = $cat_rec->id;
+		 }
+		}
+		catch(Exception $e) {
+			exit( $e->getMessage() );
+		}
+	}
+	// Programme取得完了
+}
+?>
\ No newline at end of file
--- a/storeProgram.php	Fri Mar 12 15:32:27 2010 +0900
+++ b/storeProgram.php	Sat Mar 13 18:27:33 2010 +0900
@@ -1,10 +1,12 @@
 #!/usr/bin/php
 <?php
+  $script_path = dirname( __FILE__ );
+  chdir( $script_path );
+  include_once( $script_path . '/config.php');
 
   $type = $argv[1];	// BS CS GR
   $file = $argv[2];	// XMLファイル
 
-
   // SIGTERMシグナル
   function handler( $signo = 0 ) {
 	global $file;
@@ -24,17 +26,16 @@
 	pcntl_signal(SIGTERM, "handler");
   }
 
-
   // デーモン化
   daemon();
   // プライオリティ低に
   pcntl_setpriority(20);
 
-  include_once('config.php');
   include_once( INSTALL_PATH . '/DBRecord.class.php' );
   include_once( INSTALL_PATH . '/Reservation.class.php' );
   include_once( INSTALL_PATH . '/Keyword.class.php' );
   include_once( INSTALL_PATH . '/Settings.class.php' );
+  include_once( INSTALL_PATH . '/storeProgram.inc.php' );
 
   $settings = Settings::factory();
 
@@ -42,156 +43,6 @@
 	storeProgram( $type, $file );
 	@unlink( $file );
   }
-
-  // 不要なプログラムの削除
-  // 8日以上前のプログラムを消す
-  $arr = array();
-  $arr = DBRecord::createRecords(  PROGRAM_TBL, "WHERE endtime < subdate( now(), 8 )" );
-  foreach( $arr as $val ) $val->delete();
-
-  // 8日以上先のデータがあれば消す
-  $arr = array();
-  $arr = DBRecord::createRecords(  PROGRAM_TBL, "WHERE starttime  > adddate( now(), 8 )" );
-  foreach( $arr as $val ) $val->delete();
-
-  // キーワード自動録画予約
-  $arr = array();
-  $arr = Keyword::createKeywords();
-  foreach( $arr as $val ) {
-	try {
-		$val->reservation();
-	}
-	catch( Exception $e ) {
-		// 無視
-	}
-  }
-  exit();
-
-  function storeProgram( $type, $xmlfile ) {
-	global $BS_CHANNEL_MAP, $GR_CHANNEL_MAP, $CS_CHANNEL_MAP;
-	// チャンネルマップファイルの準備
-	$map = array();
-	if( $type == "BS" ) $map = $BS_CHANNEL_MAP;
-	else if( $type == "GR") $map = $GR_CHANNEL_MAP;
-	else if( $type == "CS") $map = $CS_CHANNEL_MAP;
-
-	// XML parse
-  	$xml = @simplexml_load_file( $xmlfile );
-	if( $xml === false ) {
-		return;	// XMLが読み取れないなら何もしない
-	}
-	// channel抽出
-	foreach( $xml->channel as $ch ) {
-		$disc = $ch['id'];
-	 try {
-		// チャンネルデータを探す
-		$num = DBRecord::countRecords( CHANNEL_TBL , "WHERE channel_disc = '" . $disc ."'" );
-		if( $num == 0 ) {
-			if( array_key_exists( "$disc", $map ) ) {
-				// チャンネルデータがないなら新規作成
-				$rec = new DBRecord( CHANNEL_TBL );
-				$rec->type = $type;
-				$rec->channel = $map["$disc"];
-				$rec->channel_disc = $disc;
-				$rec->name = $ch->{'display-name'};
-			}
-		}
-		else {
-			// 存在した場合も、とりあえずチャンネル名は更新する
-			$rec = new DBRecord(CHANNEL_TBL, "channel_disc", $disc );
-			$rec->name = $ch->{'display-name'};
-		}
-	 }
-	 catch( Exception $e ) {
-		// 無視
-	 }
-	}
-	// channel 終了
-
-	// programme 取得
-
-	foreach( $xml->programme as $program ) {
-		$channel_disc = $program['channel'];
-		if( ! array_key_exists( "$channel_disc", $map ) ) continue;
-
-		$channel = $map["$channel_disc"];
-		$starttime = str_replace(" +0900", '', $program['start'] );
-		$endtime = str_replace( " +0900", '', $program['stop'] );
-		$title = $program->title;
-		$desc = $program->desc;
-		$cat_ja = "";
-		$cat_en = "";
-		foreach( $program->category as $cat ) {
-			if( $cat['lang'] == "ja_JP" ) $cat_ja = $cat;
-			if( $cat['lang'] == "en" ) $cat_en = $cat;
-		}
-		$program_disc = md5( $channel_disc . $starttime . $endtime );
-		// printf( "%s %s %s %s %s %s %s \n", $program_disc, $channel, $starttime, $endtime, $title, $desc, $cat_ja );
-		try {
-		 // カテゴリを処理する
-		 $category_disc = md5( $cat_ja . $cat_en );
-		 $num = DBRecord::countRecords(CATEGORY_TBL, "WHERE category_disc = '".$category_disc."'" );
-		 $cat_rec = null;
-		 if( $num == 0 ) {
-			// 新規カテゴリの追加
-			$cat_rec = new DBRecord( CATEGORY_TBL );
-			$cat_rec->name_jp = $cat_ja;
-			$cat_rec->name_en = $cat_en;
-		 	$cat_rec->category_disc = $category_disc;
-		 }
-		 else
-			$cat_rec = new DBRecord(CATEGORY_TBL, "category_disc" , $category_disc );
-		  //
-		 $channel_rec = new DBRecord(CHANNEL_TBL, "channel_disc", $channel_disc );
-		 $num = DBRecord::countRecords(PROGRAM_TBL, "WHERE program_disc = '".$program_disc."'" );
-		 if( $num == 0 ) {
-			// 新規番組
-			// 重複チェック 同時間帯にある番組
-			$options = "WHERE channel_disc = '".$channel_disc."' ".
-				"AND starttime < '". $endtime ."' AND endtime > '".$starttime."'";
-			$battings = DBRecord::countRecords(PROGRAM_TBL, $options );
-			if( $battings > 0 ) {
-				// 重複発生=おそらく放映時間の変更
-				$records = DBRecord::createRecords(PROGRAM_TBL, $options );
-				foreach( $records as $rec ) {
-					// 自動録画予約された番組は放映時間変更と同時にいったん削除する
-					try {
-						$reserve = new DBRecord(RESERVE_TBL, "program_id", $rec->id );
-						if( $reserve->autorec ) {
-							Reservation::cancel( $reserve->id );
-						}
-					}
-					catch( Exception $e ) {
-						//無視
-					}
-					// 番組削除
-					$rec->delete();
-				}
-			}
-			// //
-			$rec = new DBRecord( PROGRAM_TBL );
-			$rec->channel_disc = $channel_disc;
-			$rec->channel_id = $channel_rec->id;
-			$rec->type = $type;
-			$rec->channel = $channel_rec->channel;
-			$rec->title = $title;
-			$rec->description = $desc;
-			$rec->category_id = $cat_rec->id;
-			$rec->starttime = $starttime;
-			$rec->endtime = $endtime;
-			$rec->program_disc = $program_disc;
-		 }
-		 else {
-			// 番組内容更新
-			$rec = new DBRecord( PROGRAM_TBL, "program_disc", $program_disc );
-			$rec->title = $title;
-		 	$rec->description = $desc;
-			$rec->category_id = $cat_rec->id;
-		 }
-		}
-		catch(Exception $e) {
-			exit( $e->getMessage() );
-		}
-	}
-  }
+  garbageClean();			//  不要プログラム削除
+  doKeywordReservation();	// キーワード予約
 ?>
--- a/upgrade_db.php	Fri Mar 12 15:32:27 2010 +0900
+++ b/upgrade_db.php	Sat Mar 13 18:27:33 2010 +0900
@@ -1,6 +1,8 @@
 #!/usr/bin/php
 <?php
-include_once('config.php');
+$script_path = dirname( __FILE__ );
+chdir( $script_path );
+include_once($script_path . '/config.php');
 include_once(INSTALL_PATH . '/Settings.class.php' );
 
 
@@ -194,4 +196,4 @@
 }
 else
 	exit( "DBの接続に失敗\n" );
-?>
\ No newline at end of file
+?>