@@ -537,105 +537,59 @@ function upgrade_delete_orphaned_file_records() {
537
537
function upgrade_core_licenses () {
538
538
global $ CFG , $ DB ;
539
539
540
- $ corelicenses = [];
541
-
542
- $ license = new stdClass ();
543
- $ license ->shortname = 'unknown ' ;
544
- $ license ->fullname = 'Licence not specified ' ;
545
- $ license ->source = '' ;
546
- $ license ->enabled = 1 ;
547
- $ license ->version = '2010033100 ' ;
548
- $ license ->custom = 0 ;
549
- $ corelicenses [] = $ license ;
550
-
551
- $ license = new stdClass ();
552
- $ license ->shortname = 'allrightsreserved ' ;
553
- $ license ->fullname = 'All rights reserved ' ;
554
- $ license ->source = 'https://en.wikipedia.org/wiki/All_rights_reserved ' ;
555
- $ license ->enabled = 1 ;
556
- $ license ->version = '2010033100 ' ;
557
- $ license ->custom = 0 ;
558
- $ corelicenses [] = $ license ;
559
-
560
- $ license = new stdClass ();
561
- $ license ->shortname = 'public ' ;
562
- $ license ->fullname = 'Public domain ' ;
563
- $ license ->source = 'https://en.wikipedia.org/wiki/Public_domain ' ;
564
- $ license ->enabled = 1 ;
565
- $ license ->version = '2010033100 ' ;
566
- $ license ->custom = 0 ;
567
- $ corelicenses [] = $ license ;
568
-
569
- $ license = new stdClass ();
570
- $ license ->shortname = 'cc ' ;
571
- $ license ->fullname = 'Creative Commons ' ;
572
- $ license ->source = 'https://creativecommons.org/licenses/by/3.0/ ' ;
573
- $ license ->enabled = 1 ;
574
- $ license ->version = '2010033100 ' ;
575
- $ license ->custom = 0 ;
576
- $ corelicenses [] = $ license ;
577
-
578
- $ license = new stdClass ();
579
- $ license ->shortname = 'cc-nd ' ;
580
- $ license ->fullname = 'Creative Commons - NoDerivs ' ;
581
- $ license ->source = 'https://creativecommons.org/licenses/by-nd/3.0/ ' ;
582
- $ license ->enabled = 1 ;
583
- $ license ->version = '2010033100 ' ;
584
- $ license ->custom = 0 ;
585
- $ corelicenses [] = $ license ;
586
-
587
- $ license = new stdClass ();
588
- $ license ->shortname = 'cc-nc-nd ' ;
589
- $ license ->fullname = 'Creative Commons - No Commercial NoDerivs ' ;
590
- $ license ->source = 'https://creativecommons.org/licenses/by-nc-nd/3.0/ ' ;
591
- $ license ->enabled = 1 ;
592
- $ license ->version = '2010033100 ' ;
593
- $ license ->custom = 0 ;
594
- $ corelicenses [] = $ license ;
595
-
596
- $ license = new stdClass ();
597
- $ license ->shortname = 'cc-nc ' ;
598
- $ license ->fullname = 'Creative Commons - No Commercial ' ;
599
- $ license ->source = 'https://creativecommons.org/licenses/by-nc/3.0/ ' ;
600
- $ license ->enabled = 1 ;
601
- $ license ->version = '2010033100 ' ;
602
- $ license ->custom = 0 ;
603
- $ corelicenses [] = $ license ;
604
-
605
- $ license = new stdClass ();
606
- $ license ->shortname = 'cc-nc-sa ' ;
607
- $ license ->fullname = 'Creative Commons - No Commercial ShareAlike ' ;
608
- $ license ->source = 'https://creativecommons.org/licenses/by-nc-sa/3.0/ ' ;
609
- $ license ->enabled = 1 ;
610
- $ license ->version = '2010033100 ' ;
611
- $ license ->custom = 0 ;
612
- $ corelicenses [] = $ license ;
613
-
614
- $ license = new stdClass ();
615
- $ license ->shortname = 'cc-sa ' ;
616
- $ license ->fullname = 'Creative Commons - ShareAlike ' ;
617
- $ license ->source = 'https://creativecommons.org/licenses/by-sa/3.0/ ' ;
618
- $ license ->enabled = 1 ;
619
- $ license ->version = '2010033100 ' ;
620
- $ license ->custom = 0 ;
621
- $ corelicenses [] = $ license ;
622
-
623
- foreach ($ corelicenses as $ corelicense ) {
624
- // Check for current license to maintain idempotence.
625
- $ currentlicense = $ DB ->get_record ('license ' , ['shortname ' => $ corelicense ->shortname ]);
626
- if (!empty ($ currentlicense )) {
627
- $ corelicense ->id = $ currentlicense ->id ;
628
- // Remember if the license was enabled before upgrade.
629
- $ corelicense ->enabled = $ currentlicense ->enabled ;
630
- $ DB ->update_record ('license ' , $ corelicense );
631
- } else if (!isset ($ CFG ->upgraderunning ) || during_initial_install ()) {
632
- // Only install missing core licenses if not upgrading or during initial install.
633
- $ DB ->insert_record ('license ' , $ corelicense );
540
+ $ expectedlicenses = json_decode (file_get_contents ($ CFG ->dirroot . '/lib/licenses.json ' ))->licenses ;
541
+ if (!is_array ($ expectedlicenses )) {
542
+ $ expectedlicenses = [];
543
+ }
544
+ $ corelicenses = $ DB ->get_records ('license ' , ['custom ' => 0 ]);
545
+
546
+ // Disable core licenses which are no longer current.
547
+ $ todisable = array_diff (
548
+ array_map (fn ($ license ) => $ license ->shortname , $ corelicenses ),
549
+ array_map (fn ($ license ) => $ license ->shortname , $ expectedlicenses ),
550
+ );
551
+
552
+ // Disable any old *core* license that does not exist in the licenses.json file.
553
+ if (count ($ todisable )) {
554
+ [$ where , $ params ] = $ DB ->get_in_or_equal ($ todisable , SQL_PARAMS_NAMED );
555
+ $ DB ->set_field_select (
556
+ 'license ' ,
557
+ 'enabled ' ,
558
+ 0 ,
559
+ "shortname {$ where }" ,
560
+ $ params
561
+ );
562
+ }
563
+
564
+ // Add any new licenses.
565
+ foreach ($ expectedlicenses as $ expectedlicense ) {
566
+ if (!$ expectedlicense ->enabled ) {
567
+ // Skip any license which is no longer enabled.
568
+ continue ;
569
+ }
570
+ if (!$ DB ->record_exists ('license ' , ['shortname ' => $ expectedlicense ->shortname ])) {
571
+ // If the license replaces an older one, check whether this old license was enabled or not.
572
+ $ isreplacement = false ;
573
+ foreach (array_reverse ($ expectedlicense ->replaces ?? []) as $ item ) {
574
+ foreach ($ corelicenses as $ corelicense ) {
575
+ if ($ corelicense ->shortname === $ item ) {
576
+ $ expectedlicense ->enabled = $ corelicense ->enabled ;
577
+ // Also, keep the old sort order.
578
+ $ expectedlicense ->sortorder = $ corelicense ->sortorder * 100 ;
579
+ $ isreplacement = true ;
580
+ break 2 ;
581
+ }
582
+ }
583
+ }
584
+ if (!isset ($ CFG ->upgraderunning ) || during_initial_install () || $ isreplacement ) {
585
+ // Only install missing core licenses if not upgrading or during initial installation.
586
+ $ DB ->insert_record ('license ' , $ expectedlicense );
587
+ }
634
588
}
635
589
}
636
590
637
- // Add sortorder to all licenses.
638
- $ licenses = $ DB ->get_records ('license ' );
591
+ // Add/renumber sortorder to all licenses.
592
+ $ licenses = $ DB ->get_records ('license ' , null , ' sortorder ' );
639
593
$ sortorder = 1 ;
640
594
foreach ($ licenses as $ license ) {
641
595
$ license ->sortorder = $ sortorder ++;
0 commit comments