16
16
use DateTime ;
17
17
use DateTimeZone ;
18
18
use Display ;
19
+ use DocumentManager ;
19
20
use Event ;
20
21
use Exception ;
21
22
use ExtraFieldValue ;
24
25
use MessageManager ;
25
26
use Symfony \Component \HttpFoundation \File \UploadedFile ;
26
27
use Template ;
28
+ use Throwable ;
27
29
use UserManager ;
28
30
29
31
use const PHP_ROUND_HALF_UP ;
@@ -696,26 +698,29 @@ public static function addLegacyFileToResource(
696
698
697
699
if (!self ::legacyFileUsable ($ filePath )) {
698
700
error_log ("LEGACY_FILE: Cannot attach to {$ class } – file not found or unreadable: {$ basename }" );
701
+
699
702
return false ;
700
703
}
701
704
702
705
// If the repository doesn't expose addFile(), use the Asset flow.
703
706
if (!method_exists ($ repo , 'addFile ' )) {
704
- error_log ("LEGACY_FILE: Repository " .get_class ($ repo )." has no addFile(), falling back to Asset flow " );
707
+ error_log ('LEGACY_FILE: Repository ' .$ repo ::class.' has no addFile(), falling back to Asset flow ' );
708
+
705
709
return self ::attachLegacyFileToResource ($ filePath , $ resource , $ fileName );
706
710
}
707
711
708
712
try {
709
713
$ mimeType = self ::legacyDetectMime ($ filePath );
710
- $ finalName = $ fileName !== '' ? $ fileName : $ basename ;
714
+ $ finalName = '' !== $ fileName ? $ fileName : $ basename ;
711
715
712
716
// UploadedFile in "test mode" (last arg true) avoids PHP upload checks.
713
717
$ uploaded = new UploadedFile ($ filePath , $ finalName , $ mimeType , null , true );
714
718
$ repo ->addFile ($ resource , $ uploaded , $ description );
715
719
716
720
return true ;
717
- } catch (\Throwable $ e ) {
718
- error_log ("LEGACY_FILE EXCEPTION (addFile): " .$ e ->getMessage ());
721
+ } catch (Throwable $ e ) {
722
+ error_log ('LEGACY_FILE EXCEPTION (addFile): ' .$ e ->getMessage ());
723
+
719
724
return false ;
720
725
}
721
726
}
@@ -736,11 +741,13 @@ public static function attachLegacyFileToResource(
736
741
737
742
if (!self ::legacyFileUsable ($ filePath )) {
738
743
error_log ("LEGACY_FILE: Cannot attach Asset to {$ class } – file not found or unreadable: {$ basename }" );
744
+
739
745
return false ;
740
746
}
741
747
742
748
if (!method_exists ($ resource , 'getResourceNode ' ) || null === $ resource ->getResourceNode ()) {
743
749
error_log ("LEGACY_FILE: Resource has no ResourceNode – cannot attach Asset (class: {$ class }) " );
750
+
744
751
return false ;
745
752
}
746
753
@@ -751,22 +758,23 @@ public static function attachLegacyFileToResource(
751
758
if (method_exists ($ assetRepo , 'createFromLocalPath ' )) {
752
759
$ asset = $ assetRepo ->createFromLocalPath (
753
760
$ filePath ,
754
- $ fileName !== '' ? $ fileName : $ basename
761
+ '' !== $ fileName ? $ fileName : $ basename
755
762
);
756
763
} else {
757
764
// Fallback: simulate an upload-like array for createFromRequest().
758
765
$ mimeType = self ::legacyDetectMime ($ filePath );
759
766
$ fakeUpload = [
760
767
'tmp_name ' => $ filePath ,
761
- 'name ' => $ fileName !== '' ? $ fileName : $ basename ,
762
- 'type ' => $ mimeType ,
763
- 'size ' => @filesize ($ filePath ) ?: null ,
764
- 'error ' => 0 ,
768
+ 'name ' => '' !== $ fileName ? $ fileName : $ basename ,
769
+ 'type ' => $ mimeType ,
770
+ 'size ' => @filesize ($ filePath ) ?: null ,
771
+ 'error ' => 0 ,
765
772
];
766
773
767
774
$ asset = (new Asset ())
768
775
->setTitle ($ fakeUpload ['name ' ])
769
- ->setCompressed (false );
776
+ ->setCompressed (false )
777
+ ;
770
778
771
779
// AssetRepository::createFromRequest(Asset $asset, array $uploadLike)
772
780
$ assetRepo ->createFromRequest ($ asset , $ fakeUpload );
@@ -775,20 +783,24 @@ public static function attachLegacyFileToResource(
775
783
// Attach to the resource's node.
776
784
if (method_exists ($ assetRepo , 'attachToNode ' )) {
777
785
$ assetRepo ->attachToNode ($ asset , $ resource ->getResourceNode ());
786
+
778
787
return true ;
779
788
}
780
789
781
790
// If the resource repository exposes a direct helper:
782
791
$ repo = self ::guessResourceRepository ($ resource );
783
792
if ($ repo && method_exists ($ repo , 'attachAssetToResource ' )) {
784
793
$ repo ->attachAssetToResource ($ resource , $ asset );
794
+
785
795
return true ;
786
796
}
787
797
788
- error_log ("LEGACY_FILE: No method to attach Asset to node (missing attachToNode/attachAssetToResource) " );
798
+ error_log ('LEGACY_FILE: No method to attach Asset to node (missing attachToNode/attachAssetToResource) ' );
799
+
789
800
return false ;
790
- } catch (\Throwable $ e ) {
791
- error_log ("LEGACY_FILE EXCEPTION (Asset attach): " .$ e ->getMessage ());
801
+ } catch (Throwable $ e ) {
802
+ error_log ('LEGACY_FILE EXCEPTION (Asset attach): ' .$ e ->getMessage ());
803
+
792
804
return false ;
793
805
}
794
806
}
@@ -804,6 +816,7 @@ public static function firstExistingPath(array $candidates): ?string
804
816
return $ p ;
805
817
}
806
818
}
819
+
807
820
return null ;
808
821
}
809
822
@@ -815,6 +828,7 @@ private static function legacyFileUsable(string $filePath): bool
815
828
private static function legacyDetectMime (string $ filePath ): string
816
829
{
817
830
$ mime = @mime_content_type ($ filePath );
831
+
818
832
return $ mime ?: 'application/octet-stream ' ;
819
833
}
820
834
@@ -825,10 +839,11 @@ private static function legacyDetectMime(string $filePath): string
825
839
private static function guessResourceRepository (AbstractResource $ resource ): ?ResourceRepository
826
840
{
827
841
try {
828
- $ em = \Database::getManager ();
829
- $ repo = $ em ->getRepository (get_class ($ resource ));
842
+ $ em = Database::getManager ();
843
+ $ repo = $ em ->getRepository ($ resource ::class);
844
+
830
845
return $ repo instanceof ResourceRepository ? $ repo : null ;
831
- } catch (\ Throwable $ e ) {
846
+ } catch (Throwable $ e ) {
832
847
return null ;
833
848
}
834
849
}
@@ -842,7 +857,7 @@ public static function attachLegacyFileWithPublicUrl(
842
857
AbstractResource $ ownerResource ,
843
858
string $ fileName = ''
844
859
): array {
845
- $ basename = $ fileName !== '' ? $ fileName : basename ($ filePath );
860
+ $ basename = '' !== $ fileName ? $ fileName : basename ($ filePath );
846
861
847
862
if (!self ::legacyFileUsable ($ filePath )) {
848
863
return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => "File not found or unreadable: $ basename " ];
@@ -858,25 +873,25 @@ public static function attachLegacyFileWithPublicUrl(
858
873
$ mimeType = self ::legacyDetectMime ($ filePath );
859
874
$ fakeUpload = [
860
875
'tmp_name ' => $ filePath ,
861
- 'name ' => $ basename ,
862
- 'type ' => $ mimeType ,
863
- 'size ' => @filesize ($ filePath ) ?: null ,
864
- 'error ' => 0 ,
876
+ 'name ' => $ basename ,
877
+ 'type ' => $ mimeType ,
878
+ 'size ' => @filesize ($ filePath ) ?: null ,
879
+ 'error ' => 0 ,
865
880
];
866
881
$ asset = (new Asset ())->setTitle ($ basename )->setCompressed (false );
867
882
$ assetRepo ->createFromRequest ($ asset , $ fakeUpload );
868
883
}
869
884
870
885
if (!method_exists ($ ownerResource , 'getResourceNode ' ) || null === $ ownerResource ->getResourceNode ()) {
871
- return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => " Owner resource has no ResourceNode " ];
886
+ return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => ' Owner resource has no ResourceNode ' ];
872
887
}
873
888
874
889
if (method_exists ($ assetRepo , 'attachToNode ' )) {
875
890
$ assetRepo ->attachToNode ($ asset , $ ownerResource ->getResourceNode ());
876
891
} else {
877
892
$ repo = self ::guessResourceRepository ($ ownerResource );
878
893
if (!$ repo || !method_exists ($ repo , 'attachAssetToResource ' )) {
879
- return ['ok ' => false , 'asset ' => $ asset , 'url ' => null , 'error ' => " No way to attach asset to node " ];
894
+ return ['ok ' => false , 'asset ' => $ asset , 'url ' => null , 'error ' => ' No way to attach asset to node ' ];
880
895
}
881
896
$ repo ->attachAssetToResource ($ ownerResource , $ asset );
882
897
}
@@ -890,8 +905,7 @@ public static function attachLegacyFileWithPublicUrl(
890
905
}
891
906
892
907
return ['ok ' => true , 'asset ' => $ asset , 'url ' => $ url , 'error ' => null ];
893
-
894
- } catch (\Throwable $ e ) {
908
+ } catch (Throwable $ e ) {
895
909
return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => $ e ->getMessage ()];
896
910
}
897
911
}
@@ -906,11 +920,11 @@ public static function rewriteLegacyCourseUrlsToAssets(
906
920
string $ backupRoot ,
907
921
array $ extraRoots = []
908
922
): string {
909
- if ($ html === '' ) {
923
+ if ('' === $ html ) {
910
924
return $ html ;
911
925
}
912
926
913
- $ sources = \ DocumentManager::get_resources_from_source_html ($ html , false , TOOL_DOCUMENT , 1 );
927
+ $ sources = DocumentManager::get_resources_from_source_html ($ html , false , TOOL_DOCUMENT , 1 );
914
928
if (empty ($ sources )) {
915
929
return $ html ;
916
930
}
@@ -919,16 +933,16 @@ public static function rewriteLegacyCourseUrlsToAssets(
919
933
920
934
foreach ($ sources as $ s ) {
921
935
[$ realUrl , $ scope , $ kind ] = $ s ;
922
- if ($ scope !== ' local ' ) {
936
+ if (' local ' !== $ scope ) {
923
937
continue ;
924
938
}
925
939
926
940
$ pos = strpos ($ realUrl , 'document/ ' );
927
- if ($ pos === false ) {
941
+ if (false === $ pos ) {
928
942
continue ;
929
943
}
930
944
931
- $ relAfterDocument = ltrim (substr ($ realUrl , $ pos + strlen ('document/ ' )), '/ ' );
945
+ $ relAfterDocument = ltrim (substr ($ realUrl , $ pos + \ strlen ('document/ ' )), '/ ' );
932
946
933
947
$ candidates = [];
934
948
foreach ($ roots as $ root ) {
@@ -944,7 +958,8 @@ public static function rewriteLegacyCourseUrlsToAssets(
944
958
945
959
$ attached = self ::attachLegacyFileWithPublicUrl ($ filePath , $ ownerResource , basename ($ filePath ));
946
960
if (!$ attached ['ok ' ] || empty ($ attached ['url ' ])) {
947
- error_log ("LEGACY_REWRITE: failed for $ realUrl ( " .$ attached ['error ' ].") " );
961
+ error_log ("LEGACY_REWRITE: failed for $ realUrl ( " .$ attached ['error ' ].') ' );
962
+
948
963
continue ;
949
964
}
950
965
0 commit comments