comparison storeProgram.php @ 87:36ac7c416bd7

merged with upstream
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Mon, 01 Mar 2010 20:51:36 +0900
parents f873e3251487
children 80cc05b420af
comparison
equal deleted inserted replaced
75:11f63ae04a96 87:36ac7c416bd7
1 #!/usr/bin/php
2 <?php
3
4 $type = $argv[1]; // BS CS GR
5 $file = $argv[2]; // XMLファイル
6
7
8 // SIGTERMシグナル
9 function handler( $signo = 0 ) {
10 global $file;
11 if( file_exists( $file ) ) {
12 @unlink( $file );
13 }
14 exit();
15 }
16
17 // デーモン化
18 function daemon() {
19 if( pcntl_fork() != 0 )
20 exit();
21 posix_setsid();
22 if( pcntl_fork() != 0 )
23 exit;
24 pcntl_signal(SIGTERM, "handler");
25 }
26
27
28 // デーモン化
29 daemon();
30 // プライオリティ低に
31 pcntl_setpriority(20);
32
33 include_once('config.php');
34 include_once( INSTALL_PATH . '/DBRecord.class.php' );
35 include_once( INSTALL_PATH . '/Reservation.class.php' );
36 include_once( INSTALL_PATH . '/Keyword.class.php' );
37 include_once( INSTALL_PATH . '/Settings.class.php' );
38
39 $settings = Settings::factory();
40
41 if( file_exists( $file ) ) {
42 storeProgram( $type, $file );
43 @unlink( $file );
44 }
45
46 // 不要なプログラムの削除
47 // 8日以上前のプログラムを消す
48 $arr = array();
49 $arr = DBRecord::createRecords( PROGRAM_TBL, "WHERE endtime < subdate( now(), 8 )" );
50 foreach( $arr as $val ) $val->delete();
51
52 // 8日以上先のデータがあれば消す
53 $arr = array();
54 $arr = DBRecord::createRecords( PROGRAM_TBL, "WHERE starttime > adddate( now(), 8 )" );
55 foreach( $arr as $val ) $val->delete();
56
57 // キーワード自動録画予約
58 $arr = array();
59 $arr = Keyword::createKeywords();
60 foreach( $arr as $val ) {
61 try {
62 $val->reservation();
63 }
64 catch( Exception $e ) {
65 // 無視
66 }
67 }
68 exit();
69
70 function storeProgram( $type, $xmlfile ) {
71 global $BS_CHANNEL_MAP, $GR_CHANNEL_MAP, $CS_CHANNEL_MAP;
72 // チャンネルマップファイルの準備
73 $map = array();
74 if( $type == "BS" ) $map = $BS_CHANNEL_MAP;
75 else if( $type == "GR") $map = $GR_CHANNEL_MAP;
76 else if( $type == "CS") $map = $CS_CHANNEL_MAP;
77
78 // XML parse
79 $xml = @simplexml_load_file( $xmlfile );
80 if( $xml === false ) {
81 return; // XMLが読み取れないなら何もしない
82 }
83 // channel抽出
84 foreach( $xml->channel as $ch ) {
85 $disc = $ch['id'];
86 try {
87 // チャンネルデータを探す
88 $num = DBRecord::countRecords( CHANNEL_TBL , "WHERE channel_disc = '" . $disc ."'" );
89 if( $num == 0 ) {
90 // チャンネルデータがないなら新規作成
91 $rec = new DBRecord( CHANNEL_TBL );
92 $rec->type = $type;
93 $rec->channel = $map["$disc"];
94 $rec->channel_disc = $disc;
95 $rec->name = $ch->{'display-name'};
96 }
97 else {
98 // 存在した場合も、とりあえずチャンネル名は更新する
99 $rec = new DBRecord(CHANNEL_TBL, "channel_disc", $disc );
100 $rec->name = $ch->{'display-name'};
101 }
102 }
103 catch( Exception $e ) {
104 // 無視
105 }
106 }
107 // channel 終了
108
109 // programme 取得
110
111 foreach( $xml->programme as $program ) {
112 $channel_disc = $program['channel'];
113 $channel = $map["$channel_disc"];
114 $starttime = str_replace(" +0900", '', $program['start'] );
115 $endtime = str_replace( " +0900", '', $program['stop'] );
116 $title = $program->title;
117 $desc = $program->desc;
118 $cat_ja = "";
119 $cat_en = "";
120 foreach( $program->category as $cat ) {
121 if( $cat['lang'] == "ja_JP" ) $cat_ja = $cat;
122 if( $cat['lang'] == "en" ) $cat_en = $cat;
123 }
124 $program_disc = md5( $channel_disc . $starttime . $endtime );
125 // printf( "%s %s %s %s %s %s %s \n", $program_disc, $channel, $starttime, $endtime, $title, $desc, $cat_ja );
126 try {
127 // カテゴリを処理する
128 $category_disc = md5( $cat_ja . $cat_en );
129 $num = DBRecord::countRecords(CATEGORY_TBL, "WHERE category_disc = '".$category_disc."'" );
130 $cat_rec = null;
131 if( $num == 0 ) {
132 // 新規カテゴリの追加
133 $cat_rec = new DBRecord( CATEGORY_TBL );
134 $cat_rec->name_jp = $cat_ja;
135 $cat_rec->name_en = $cat_en;
136 $cat_rec->category_disc = $category_disc;
137 }
138 else
139 $cat_rec = new DBRecord(CATEGORY_TBL, "category_disc" , $category_disc );
140 //
141 $channel_rec = new DBRecord(CHANNEL_TBL, "channel_disc", $channel_disc );
142 $num = DBRecord::countRecords(PROGRAM_TBL, "WHERE program_disc = '".$program_disc."'" );
143 if( $num == 0 ) {
144 // 新規番組
145 // 重複チェック 同時間帯にある番組
146 $options = "WHERE channel_disc = '".$channel_disc."' ".
147 "AND starttime < '". $endtime ."' AND endtime > '".$starttime."'";
148 $battings = DBRecord::countRecords(PROGRAM_TBL, $options );
149 if( $battings > 0 ) {
150 // 重複発生=おそらく放映時間の変更
151 $records = DBRecord::createRecords(PROGRAM_TBL, $options );
152 foreach( $records as $rec ) {
153 // 自動録画予約された番組は放映時間変更と同時にいったん削除する
154 try {
155 $reserve = new DBRecord(RESERVE_TBL, "program_id", $rec->id );
156 if( $reserve->autorec ) {
157 Reservation::cancel( $reserve->id );
158 }
159 }
160 catch( Exception $e ) {
161 //無視
162 }
163 // 番組削除
164 $rec->delete();
165 }
166 }
167 // //
168 $rec = new DBRecord( PROGRAM_TBL );
169 $rec->channel_disc = $channel_disc;
170 $rec->channel_id = $channel_rec->id;
171 $rec->type = $type;
172 $rec->channel = $channel_rec->channel;
173 $rec->title = $title;
174 $rec->description = $desc;
175 $rec->category_id = $cat_rec->id;
176 $rec->starttime = $starttime;
177 $rec->endtime = $endtime;
178 $rec->program_disc = $program_disc;
179 }
180 else {
181 // 番組内容更新
182 $rec = new DBRecord( PROGRAM_TBL, "program_disc", $program_disc );
183 $rec->title = $title;
184 $rec->description = $desc;
185 $rec->category_id = $cat_rec->id;
186 }
187 }
188 catch(Exception $e) {
189 exit( $e->getMessage() );
190 }
191 }
192 }
193 ?>