source: trunk/Modules/Export/ProcessTask.php@ 869

Last change on this file since 869 was 869, checked in by chronos, 9 years ago
  • Modified: Export task processing made more readable using classes. Task queue is processed and shown in TimeQueued order.
File size: 7.5 KB
Line 
1<?php
2
3ini_set('memory_limit', '100M');
4
5include_once(dirname(__FILE__).'/../../includes/Global.php');
6include_once(dirname(__FILE__).'/../../includes/zip.lib.php');
7
8include_once('Export.php');
9include_once('ExportOutput.php');
10
11class Task extends Model
12{
13 var $Id;
14
15 function SetProgress($Progress)
16 {
17 $this->System->Database->query('UPDATE `ExportTask` SET `Progress`='.$Progress.' WHERE `Id`='.$this->Id);
18 }
19}
20
21class TaskExport extends Task
22{
23 function DeleteOldFiles($deldir)
24 {
25 if(file_exists($deldir.'/'))
26 {
27 $Dir = opendir($deldir.'/') ;
28 while(($File = readdir($Dir)) !== false)
29 {
30 if(($File != '..') and ($File != '.') and (!is_dir($File))) unlink($deldir.'/'.$File);
31 }
32 closedir($Dir);
33 }
34 }
35
36 function rcopy($src, $dst)
37 {
38 if (file_exists($dst)) $this->DeleteOldFiles($dst);
39 if (is_dir($src)) {
40 if (!file_exists($dst)) mkdir($dst, 0777, true);
41 $files = scandir($src);
42 foreach ($files as $file)
43 if ($file != "." and $file != ".." and (!is_dir($src.'/'.$file))) $this->rcopy($src.'/'.$file, $dst.'/'.$file);
44 }
45 else if (file_exists($src)) copy($src, $dst);
46 }
47
48 function GetWinZPatch($patch)
49 {
50 //TMP="Z:\\a\\www\\wowpreklad\\tmp\\Export\\${EXPORTID}\\"
51 $patch = str_replace(DIRECTORY_SEPARATOR, '\\', $patch);
52 $patch = 'Z:'.$patch;
53 return $patch;
54 }
55
56 function MPQPack($packdir)
57 {
58 if(file_exists($packdir.DIRECTORY_SEPARATOR))
59 {
60 $Dir = opendir($packdir.DIRECTORY_SEPARATOR);
61 while(($File = readdir($Dir)) !== false)
62 {
63 if(($File != '..') and ($File != '.')) {
64 $File = str_replace('/', DIRECTORY_SEPARATOR, $File);
65 $InDir = '';
66 if (strpos($packdir, 'dbc') !== false) {
67 $InDir = 'DBFilesClient\\';
68 if (DIRECTORY_SEPARATOR == '/') // linux
69 $InDir = 'DBFilesClient\\\\';
70 }
71 if (strpos($packdir.$File, 'luaGlobal') !== false) {
72 $InDir = 'Interface\\FrameXML\\';
73 if (DIRECTORY_SEPARATOR == '/') // linux
74 $InDir = 'Interface\\\\FrameXML\\\\';
75 }
76 if (strpos($packdir.$File, 'luaGlue') !== false) {
77 $InDir = 'Interface\\GlueXML\\';
78 if (DIRECTORY_SEPARATOR == '/') // linux
79 $InDir = 'Interface\\\\GlueXML\\\\';
80 }
81
82 if (DIRECTORY_SEPARATOR == '/') // linux
83 echo exec('wine cmd /C mpq.exe "'.$this->GetWinZPatch($packdir.DIRECTORY_SEPARATOR.
84 '..'.DIRECTORY_SEPARATOR).'patch-5.MPQ" '.$InDir.basename($File).' "'.
85 $this->GetWinZPatch($packdir.DIRECTORY_SEPARATOR).$File.'" ');
86
87 if (DIRECTORY_SEPARATOR == '\\') // windows
88 echo exec('mpq.exe "'.$packdir.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.
89 'patch-5.MPQ" '.$InDir.basename($File).' "'.$packdir.DIRECTORY_SEPARATOR.$File.'" ');
90
91 echo "\n";
92 }
93 }
94 closedir($Dir);
95 }
96 }
97
98 function ExportDBC($Task)
99 {
100 $Export = new Export($this->System);
101 $Export->Id = $Task['ExportId'];
102 $Export->Init();
103
104 echo(StrFTime("%d/%m/%Y %H:%M:%S", time()).': Generování DBC souborů pro export '.$Export->Id.'.. '."\n");
105 $this->SetProgress(10);
106
107 echo('Mazání starých souborů...'."\n");
108
109 // Delete old files
110 $this->DeleteOldFiles($Export->TempDir.'dbc');
111 if(file_exists($Export->TempDir.'CzWoW_DBC.zip')) unlink($Export->TempDir.'CzWoW_DBC.zip');
112
113 $this->SetProgress(20);
114 if(function_exists('gzcompress'))
115 {
116 $Export->ExportToDBC();
117 echo('Komprese...'."\n");
118 exec('zip -r -j '.$Export->TempDir.'CzWoW_DBC.zip '.$Export->TempDir.'dbc/');
119 $this->SetProgress(80);
120 } else echo('Funkce pro tvorbu Zip souboru není podporována!'."\n");
121 }
122
123 function ExportEXE($Task)
124 {
125 $Export = new ExportAddon($this->System);
126 $Export->Id = $Task['ExportId'];
127 $Export->Init();
128 $Export->LoadFilters();
129
130 echo(StrFTime("%d/%m/%Y %H:%M:%S", time()).': Generování EXE souboru pro export '.$Export->Id.'.. '."\n");
131 $nsifile = 'install.nsi';
132
133 // Delete old files
134 $this->SetProgress(1);
135 echo('Delete old files...'."\n");
136 $this->DeleteOldFiles($Export->TempDir.'dbc');
137 $this->DeleteOldFiles($Export->TempDir.'lua');
138 $this->DeleteOldFiles($Export->TempDir.'CzWoW');
139
140 // copy need files
141 $this->SetProgress(5);
142 echo('Copy files...'."\n");
143 $this->rcopy('files/'.$Export->ClientVersion['Version'].'/patch-5.MPQ', $Export->TempDir.'patch-5.MPQ');
144 $this->rcopy('files/'.$Export->ClientVersion['Version'].'/'.$nsifile, $Export->TempDir.$nsifile);
145 $this->rcopy('files/'.$Export->ClientVersion['Version'].'/WowLua.exe', $Export->TempDir.'WowLua.exe');
146 $this->rcopy('files/'.$Export->ClientVersion['Version'].'/CzWoW/', $Export->TempDir.'CzWoW/');
147 $this->rcopy('files/WoW.ico', $Export->TempDir.'WoW.ico');
148 $this->rcopy('files/Fonts/', $Export->TempDir.'Fonts/');
149
150 $this->SetProgress(10);
151 echo('Export lua...'."\n");
152 $Export->ExportToLua();
153 $this->SetProgress(15);
154 echo('Export CzWoW...'."\n");
155 $Export->MakeAddon();
156 $this->SetProgress(30);
157 echo('Export dbc...'."\n");
158 $Export->ExportToDBC();
159
160 echo('Create readme...'."\n");
161 $this->SetProgress(60);
162 $File = new FileStream();
163 $File->CreateFile($Export->TempDir.'ReadMe.htm');
164 $File->WriteLine($Export->GetReadme());
165 unset($File);
166
167 echo('Packing files...'."\n");
168 $this->SetProgress(70);
169 $workdir = str_replace('/',DIRECTORY_SEPARATOR, $Export->TempDir);
170 $this->MPQPack($workdir.'lua');
171 $this->SetProgress(80);
172 $this->MPQPack($workdir.'dbc');
173
174 $this->SetProgress(90);
175 echo('Creating instalator...'."\n");
176 if (DIRECTORY_SEPARATOR == '/') // linux
177 echo exec('makensis '.$workdir.$nsifile);
178
179 if (DIRECTORY_SEPARATOR == '\\') // windows
180 echo exec('"'.$workdir.$nsifile.'" '); //"c:\Program Files (x86)\NSIS\makensisw.exe" /Xscriptcmd
181 }
182
183 function Run()
184 {
185 global $Config;
186
187 while(1)
188 {
189 $DbResult = $this->Database->query('SELECT `ExportTask`.`Id`, `ExportTask`.`Export`, '.
190 '`Export`.`Id` AS `ExportId`, `Export`.`OutputType` AS `ExportOutput` FROM `ExportTask` '.
191 'LEFT JOIN `Export` ON `Export`.`Id` = `ExportTask`.`Export` WHERE '.
192 '(`ExportTask`.`TimeFinish` IS NULL) ORDER BY `TimeQueued`');
193 while($Task = $DbResult->fetch_assoc())
194 {
195 $this->Id = $Task['Id'];
196 if($Task['ExportId'] != '')
197 {
198 try
199 {
200 $this->SetProgress(0);
201 $this->Database->update('ExportTask', '`Id`='.$this->Id, array('TimeStart' => 'NOW()'));
202
203 if($Task['ExportOutput'] == 9)
204 {
205 $this->ExportDBC($Task);
206 } else
207 if($Task['ExportOutput'] == 10)
208 {
209 $this->ExportEXE($Task);
210 } else echo('Unknown export output type '.$Task['ExportOutput']."\n");
211
212 $this->SetProgress(100);
213 $this->Database->update('ExportTask', '`Id`='.$this->Id, array('TimeFinish' => 'NOW()'));
214 echo ("\n"."Hotovo"."\n");
215 } catch (Exception $E)
216 {
217 echo 'Caught exception: ', $E->getMessage(), "\n";
218 }
219 } else
220 {
221 $this->Database->query('DELETE FROM `ExportTask` WHERE `Id`='.$this->Id);
222 echo('Export '.$Task['Export'].' nenalezen. Jeho úloha smazána.'."\n");
223 }
224 }
225 sleep($Config['ExportTaskProcessPeriod']);
226 }
227 }
228}
229
230//LoadCommandLineParameters();
231$System = new Core();
232$System->DoNotShowPage = true;
233$System->Run();
234
235$Task = new TaskExport($System);
236$Task->Run();
Note: See TracBrowser for help on using the repository browser.