33namespace YesWiki \Core ;
44
55use Composer \Script \Event ;
6+ use ZipArchive ;
7+ use Throwable ;
68
79class ComposerScriptsHelper
810{
@@ -23,4 +25,187 @@ public static function postInstall(Event $event)
2325 rmdir ('vendor/stefangabos/zebra_image/examples ' );
2426 }
2527 }
28+
29+ public static function postUpdate (Event $ event )
30+ {
31+ self ::postInstall ($ event );
32+ // update pdfjs-dist
33+
34+ // first get list of files
35+ $ data = self ::getPdfJsDistFiles ();
36+ $ fileData = self ::extractPdfJsDistFileUrl ($ data );
37+ self ::updatePdfJsDist ($ fileData );
38+ }
39+
40+ private static function getPdfJsDistFiles (): array
41+ {
42+ $ url = "https://api.github.com/repos/mozilla/pdf.js/releases/latest " ;
43+ try {
44+ $ content = file_get_contents ($ url , false , stream_context_create ([
45+ 'http ' => [
46+ 'user_agent ' => "Composer " ,
47+ "timeout " => 5 // total timeout in seconds
48+ ]
49+ ]));
50+ } catch (Throwable $ th ) {
51+ return [];
52+ }
53+ if (!empty ($ content ) && is_string ($ content )) {
54+ $ data = json_decode ($ content , true );
55+ if (is_array ($ data )) {
56+ return $ data ;
57+ }
58+ }
59+ return [];
60+ }
61+
62+ /**
63+ * @param array $data
64+ * @return array ['fileName' => string, 'url' => string]
65+ */
66+ private static function extractPdfJsDistFileUrl (array $ data ): array
67+ {
68+ if (!empty ($ data ['assets ' ]) && is_array ($ data ['assets ' ])) {
69+ foreach ($ data ['assets ' ] as $ asset ) {
70+ if (!empty ($ asset ['name ' ]) && is_string ($ asset ['name ' ]) &&
71+ !empty ($ asset ['browser_download_url ' ]) && is_string ($ asset ['browser_download_url ' ]) &&
72+ preg_match ("/^pdfjs-(\d+)\.(\d+)\.(\d+)-dist\.zip$/i " , $ asset ['name ' ], $ match )) {
73+ return [
74+ 'fileName ' => $ asset ['name ' ],
75+ 'url ' => $ asset ['browser_download_url ' ],
76+ 'major_revision ' => $ match [1 ],
77+ 'minor_revision ' => $ match [2 ],
78+ 'buxfix_revision ' => $ match [3 ]
79+ ];
80+ }
81+ }
82+ }
83+ return [];
84+ }
85+
86+ private static function updatePdfJsDist (array $ params )
87+ {
88+ if (!empty ($ params ['url ' ])) {
89+ if (is_dir ('tools/attach/libs/vendor/ ' )) {
90+ if (!is_dir ('tools/attach/libs/vendor/pdfjs-dist/ ' ) ||
91+ self ::pdfJsDistNeedsUpdate ('tools/attach/libs/vendor/pdfjs-dist/revision.json ' , $ params )) {
92+ try {
93+ $ zipContent = file_get_contents ($ params ['url ' ], false , stream_context_create ([
94+ 'http ' => [
95+ 'user_agent ' => "Composer " ,
96+ "timeout " => 15 // total timeout in seconds
97+ ]
98+ ]));
99+ if (!empty ($ zipContent )) {
100+ $ temp = tmpfile ();
101+ if ($ temp ) {
102+ $ tmpFilename = stream_get_meta_data ($ temp )['uri ' ];
103+ if (file_put_contents ($ tmpFilename , $ zipContent ) !== false ) {
104+ $ zip = new ZipArchive ();
105+ if ($ zip ->open ($ tmpFilename )) {
106+ if (is_dir ('tools/attach/libs/vendor/pdfjs-dist/ ' )) {
107+ self ::deleteFolder ('tools/attach/libs/vendor/pdfjs-dist/ ' );
108+ }
109+ if (!is_dir ('tools/attach/libs/vendor/pdfjs-dist/ ' )) {
110+ $ zip ->extractTo ('tools/attach/libs/vendor/pdfjs-dist/ ' );
111+ file_put_contents (
112+ 'tools/attach/libs/vendor/pdfjs-dist/revision.json ' ,
113+ json_encode ([
114+ 'major_revision ' => $ params ['major_revision ' ],
115+ 'minor_revision ' => $ params ['minor_revision ' ],
116+ 'buxfix_revision ' => $ params ['buxfix_revision ' ]
117+ ])
118+ );
119+ echo "Pdfjs-dist updated ! \n" ;
120+ }
121+ $ zip ->close ();
122+ } else {
123+ echo "!! Zip not downloaded : $ zipContent \n" ;
124+ }
125+ } else {
126+ echo "erro while putting zip into $ tmpFileName \n" ;
127+ }
128+ } else {
129+ echo "Not possible to create a tempfile ! \n" ;
130+ }
131+ }
132+ } catch (Throwable $ th ) {
133+ echo "error {$ th ->getMessage ()}\n" ;
134+ if (isset ($ zipContent )) {
135+ echo "zipContent: $ zipContent \n" ;
136+ }
137+ }
138+ if (isset ($ temp ) && $ temp ) {
139+ fclose ($ temp );
140+ }
141+ }
142+ }
143+ }
144+ }
145+
146+ private static function pdfJsDistNeedsUpdate (string $ filePath , array $ params ): bool
147+ {
148+ if (!is_file ($ filePath )) {
149+ return true ;
150+ }
151+ try {
152+ $ jsonContent = file_get_contents ($ filePath );
153+ if (!empty ($ jsonContent )) {
154+ $ versionsData = json_decode ($ jsonContent , true );
155+ if (is_array ($ versionsData )) {
156+ if (empty ($ versionsData ['major_revision ' ]) ||
157+ $ versionsData ['major_revision ' ] > $ params ['major_revision ' ] ||
158+ empty ($ versionsData ['minor_revision ' ]) ||
159+ $ versionsData ['minor_revision ' ] > $ params ['minor_revision ' ] ||
160+ empty ($ versionsData ['buxfix_revision ' ]) ||
161+ $ versionsData ['buxfix_revision ' ] > $ params ['buxfix_revision ' ]
162+ ) {
163+ return true ;
164+ }
165+ }
166+ }
167+ } catch (Throwable $ th ) {
168+ }
169+ return false ;
170+ }
171+
172+ private static function deleteFolder ($ path )
173+ {
174+ $ file2ignore = array ('. ' , '.. ' );
175+ if (is_link ($ path )) {
176+ return unlink ($ path ) !== false ;
177+ } else {
178+ if ($ res = opendir ($ path )) {
179+ $ continue = true ;
180+ while (($ file = readdir ($ res )) !== false && $ continue ) {
181+ if (!in_array ($ file , $ file2ignore )) {
182+ $ continue = self ::delete ($ path . '/ ' . $ file );
183+ }
184+ }
185+ closedir ($ res );
186+ }
187+ if ($ continue ) {
188+ return rmdir ($ path ) !== false ;
189+ } else {
190+ return false ;
191+ }
192+ }
193+ return false ;
194+ }
195+
196+ private static function delete ($ path )
197+ {
198+ if (empty ($ path )) {
199+ return false ;
200+ }
201+ if (is_file ($ path )) {
202+ if (unlink ($ path )) {
203+ return true ;
204+ }
205+ return false ;
206+ }
207+ if (is_dir ($ path )) {
208+ return self ::deleteFolder ($ path );
209+ }
210+ }
26211}
0 commit comments