comparison storeProgram.php @ 83:f873e3251487

new: EPG取得の並列化テストを追加
author yoneda <yoneda@recorder.localnet.mda.or.jp>
date Sun, 28 Feb 2010 23:58:36 +0900
parents
children 80cc05b420af
comparison
equal deleted inserted replaced
82:330c83ae8c95 83:f873e3251487
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 // ゃd
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 // c潟<ゃ羣
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 // c潟若帥「
88 $num = DBRecord::countRecords( CHANNEL_TBL , "WHERE channel_disc = '" . $disc ."'" );
89 if( $num == 0 ) {
90 // c潟若帥域鋎
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 // 絖翫c潟贋違
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 // 牙私膣腟丈紊眼cゃ
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 ?>