@@ -22,124 +22,126 @@ public static function convert()
2222 // Default opencast server if playlist belongs to no opencast server
2323 $ default_config_id = \Config::get ()->OPENCAST_DEFAULT_SERVER ;
2424
25- // Migrate existing playlists to Opencast
26- $ playlists = Playlists::findBySQL ('service_playlist_id IS NULL ' );
27-
28- // Try three times
29- $ tries = 0 ;
30- while (!empty ($ playlists ) && $ tries < 10 ) {
31- foreach ($ playlists as $ playlist ) {
32- $ config_id = $ playlist ->config_id ?? null ;
33- if (empty ($ config_id )) {
34- // Default opencast server if playlist belongs to no opencast server
35- $ config_id = $ default_config_id ;
36- }
25+ try {
26+ // Migrate existing playlists to Opencast
27+ $ playlists = Playlists::findBySQL ('service_playlist_id IS NULL ' );
3728
38- // Get playlist videos
39- $ playlist_videos = self ::getPlaylistVideos ($ playlist );
40-
41- $ api_playlists_client = ApiPlaylistsClient::getInstance ($ config_id );
42- $ oc_playlist = $ api_playlists_client ->createPlaylist (
43- self ::getOcPlaylistData ($ playlist , $ playlist_videos )
44- );
45-
46- sleep (2 );
47-
48- if ($ oc_playlist ) {
49- // Store oc playlist reference in Stud.IP if successfully created
50- $ playlist ->config_id = $ config_id ;
51- $ playlist ->service_playlist_id = $ oc_playlist ->id ;
52- $ playlist ->store ();
53-
54- Playlists::checkPlaylistACL ($ oc_playlist , $ playlist );
55-
56- // Store entry ids
57- for ($ i = 0 ; $ i < count ($ playlist_videos ); $ i ++) {
58- $ stmt = $ db ->prepare ("UPDATE oc_playlist_video
59- SET `service_entry_id` = :service_entry_id
60- WHERE playlist_id = :playlist_id AND video_id = :video_id
61- " );
62- $ stmt ->execute ($ data = [
63- 'service_entry_id ' => $ oc_playlist ->entries [$ i ]->id ,
64- 'playlist_id ' => $ playlist ->id ,
65- 'video_id ' => $ playlist_videos [$ i ]['id ' ]
66- ]);
29+ // Try three times
30+ $ tries = 0 ;
31+ while (!empty ($ playlists ) && $ tries < 10 ) {
32+ foreach ($ playlists as $ playlist ) {
33+ $ config_id = $ playlist ->config_id ?? null ;
34+ if (empty ($ config_id )) {
35+ // Default opencast server if playlist belongs to no opencast server
36+ $ config_id = $ default_config_id ;
37+ }
38+
39+ // Get playlist videos
40+ $ playlist_videos = self ::getPlaylistVideos ($ playlist );
41+
42+ $ api_playlists_client = ApiPlaylistsClient::getInstance ($ config_id );
43+ $ oc_playlist = $ api_playlists_client ->createPlaylist (
44+ self ::getOcPlaylistData ($ playlist , $ playlist_videos )
45+ );
46+
47+ if ($ oc_playlist ) {
48+ // Store oc playlist reference in Stud.IP if successfully created
49+ $ playlist ->config_id = $ config_id ;
50+ $ playlist ->service_playlist_id = $ oc_playlist ->id ;
51+ $ playlist ->store ();
52+
53+ Playlists::checkPlaylistACL ($ oc_playlist , $ playlist );
54+
55+ // Store entry ids
56+ for ($ i = 0 ; $ i < count ($ playlist_videos ); $ i ++) {
57+ $ stmt = $ db ->prepare ("UPDATE oc_playlist_video
58+ SET `service_entry_id` = :service_entry_id
59+ WHERE playlist_id = :playlist_id AND video_id = :video_id
60+ " );
61+ $ stmt ->execute ($ data = [
62+ 'service_entry_id ' => $ oc_playlist ->entries [$ i ]->id ,
63+ 'playlist_id ' => $ playlist ->id ,
64+ 'video_id ' => $ playlist_videos [$ i ]['id ' ]
65+ ]);
66+ }
6767 }
6868 }
69- }
7069
71- $ playlists = Playlists::findBySQL ('service_playlist_id IS NULL ' );
72- $ tries ++;
73- }
70+ $ playlists = Playlists::findBySQL ('service_playlist_id IS NULL ' );
71+ $ tries ++;
72+ }
7473
75- // What is the point of letting the process continue if there are still playlists with null service_playlist_id of duplicated service_playlist_ids?
76- $ duplicate_service_playlist_ids = $ db ->query (
77- "SELECT service_playlist_id, COUNT(*) as count
78- FROM oc_playlist
79- WHERE service_playlist_id IS NOT NULL
80- GROUP BY service_playlist_id
81- HAVING count > 1 "
82- )->fetchAll (\PDO ::FETCH_ASSOC );
83-
84- if (!empty ($ playlists ) || !empty ($ duplicate_service_playlist_ids )) {
85- $ message = "Migration failed due to invalid data records: \n" ;
86- if (!empty ($ playlists )) {
87- $ message .= "Playlists with null service_playlist_id: \n" ;
88- foreach ($ playlists as $ playlist ) {
89- $ message .= "Playlist ID: {$ playlist ->id }\n" ;
74+ // What is the point of letting the process continue if there are still playlists with null service_playlist_id of duplicated service_playlist_ids?
75+ $ duplicate_service_playlist_ids = $ db ->query (
76+ "SELECT service_playlist_id, COUNT(*) as count
77+ FROM oc_playlist
78+ WHERE service_playlist_id IS NOT NULL
79+ GROUP BY service_playlist_id
80+ HAVING count > 1 "
81+ )->fetchAll (\PDO ::FETCH_ASSOC );
82+
83+ if (!empty ($ playlists ) || !empty ($ duplicate_service_playlist_ids )) {
84+ $ message = "Migration failed due to invalid data records: \n" ;
85+ if (!empty ($ playlists )) {
86+ $ message .= "Playlists with null service_playlist_id: \n" ;
87+ foreach ($ playlists as $ playlist ) {
88+ $ message .= "Playlist ID: {$ playlist ->id }\n" ;
89+ }
9090 }
91- }
92- if (! empty ( $ duplicate_service_playlist_ids )) {
93- $ message .= " Duplicate service_playlist_ids: \n" ;
94- foreach ( $ duplicate_service_playlist_ids as $ record) {
95- $ message .= " Service Playlist ID: { $ record [ ' service_playlist_id ' ]} , Count: { $ record [ ' count ' ]}\n" ;
91+ if (! empty ( $ duplicate_service_playlist_ids )) {
92+ $ message .= " Duplicate service_playlist_ids: \n" ;
93+ foreach ( $ duplicate_service_playlist_ids as $ record ) {
94+ $ message .= " Service Playlist ID: { $ record[ ' service_playlist_id ' ]} , Count: { $ record [ ' count ' ]}\n" ;
95+ }
9696 }
97+ throw new \Exception ($ message );
9798 }
98- throw new \Exception ($ message );
99- }
10099
101- // We need another step to make sure config id is set and it is not null before altering the table with not-null config_id.
102- $ null_config_playlists = Playlists::findBySQL ('config_id IS NULL ' );
103-
104- while (!empty ($ null_config_playlists )) {
105- foreach ($ null_config_playlists as $ null_config_playlist ) {
106- // Store config id with default config id.
107- $ null_config_playlist ->config_id = $ default_config_id ;
108- $ null_config_playlist ->store ();
109- }
100+ // We need another step to make sure config id is set and it is not null before altering the table with not-null config_id.
110101 $ null_config_playlists = Playlists::findBySQL ('config_id IS NULL ' );
111- }
112-
113- // Forbid playlist without related oc playlist
114- // First drop foreign key constraint
115- // Then change column to not null
116- // Then add foreign key constraint again
117- $ db ->exec ('ALTER TABLE `oc_playlist`
118- DROP FOREIGN KEY `oc_playlist_ibfk_1`,
119- CHANGE COLUMN `config_id` `config_id` int NOT NULL,
120- CHANGE COLUMN `service_playlist_id` `service_playlist_id` varchar(64) UNIQUE NOT NULL,
121- ADD FOREIGN KEY `oc_playlist_ibfk_1` (`config_id`) REFERENCES `oc_config` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT '
122- );
123-
124- // Forbid playlist video without related oc playlist entry
125- $ db ->exec ('ALTER TABLE `oc_playlist_video`
126- CHANGE COLUMN `service_entry_id` `service_entry_id` int NOT NULL '
127- );
128102
129- \SimpleOrMap::expireTableScheme ();
130-
131- // Add playlists sync cronjob
132- $ scheduler = \CronjobScheduler::getInstance ();
103+ while (!empty ($ null_config_playlists )) {
104+ foreach ($ null_config_playlists as $ null_config_playlist ) {
105+ // Store config id with default config id.
106+ $ null_config_playlist ->config_id = $ default_config_id ;
107+ $ null_config_playlist ->store ();
108+ }
109+ $ null_config_playlists = Playlists::findBySQL ('config_id IS NULL ' );
110+ }
133111
134- if (!$ task_id = \CronjobTask::findByFilename (self ::CRONJOBS_DIR . 'opencast_sync_playlists.php ' )[0 ]->task_id ) {
135- $ task_id = $ scheduler ->registerTask (self ::CRONJOBS_DIR . 'opencast_sync_playlists.php ' , true );
136- }
112+ // Forbid playlist without related oc playlist
113+ // First drop foreign key constraint
114+ // Then change column to not null
115+ // Then add foreign key constraint again
116+ $ db ->exec ('ALTER TABLE `oc_playlist`
117+ DROP FOREIGN KEY `oc_playlist_ibfk_1`,
118+ CHANGE COLUMN `config_id` `config_id` int NOT NULL,
119+ CHANGE COLUMN `service_playlist_id` `service_playlist_id` varchar(64) UNIQUE NOT NULL,
120+ ADD FOREIGN KEY `oc_playlist_ibfk_1` (`config_id`) REFERENCES `oc_config` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT '
121+ );
122+
123+ // Forbid playlist video without related oc playlist entry
124+ $ db ->exec ('ALTER TABLE `oc_playlist_video`
125+ CHANGE COLUMN `service_entry_id` `service_entry_id` int NOT NULL '
126+ );
127+
128+ \SimpleOrMap::expireTableScheme ();
129+
130+ // Add playlists sync cronjob
131+ $ scheduler = \CronjobScheduler::getInstance ();
132+
133+ if (!$ task_id = \CronjobTask::findByFilename (self ::CRONJOBS_DIR . 'opencast_sync_playlists.php ' )[0 ]->task_id ) {
134+ $ task_id = $ scheduler ->registerTask (self ::CRONJOBS_DIR . 'opencast_sync_playlists.php ' , true );
135+ }
137136
138- // add the new cronjobs
139- if ($ task_id ) {
140- $ scheduler ->cancelByTask ($ task_id );
141- $ scheduler ->schedulePeriodic ($ task_id , -10 ); // negative value means "every x minutes"
142- \CronjobSchedule::findByTask_id ($ task_id )[0 ]->activate ();
137+ // add the new cronjobs
138+ if ($ task_id ) {
139+ $ scheduler ->cancelByTask ($ task_id );
140+ $ scheduler ->schedulePeriodic ($ task_id , -10 ); // negative value means "every x minutes"
141+ \CronjobSchedule::findByTask_id ($ task_id )[0 ]->activate ();
142+ }
143+ } catch (\Throwable $ th ) {
144+ throw new \Exception ('Migration fehlgeschlagen: ' . $ th ->getMessage ());
143145 }
144146 }
145147
0 commit comments