Moving from MediaWiki to SharePoint O365 - filePathFlattener.php

This is the source code of the filePathFlattener-script, which will move all files from the images folder-structure of a MediaWiki installation to two folders: one for all the images, and one for all the other files.

It requires there to be an images folder, a filesFlat folder and an imagesFlat folder. You of course also need to update the rootPath on line 4.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
//set the path where the images are, relative to the path of this script
global $rootPath; global $imagesPath;
$rootPath = "C:/something/UniServerZ/www/wikimigration/fileProcessing/";
$imagesPath = "images/";

//keep a tab on how many files were moved
global $moveCounter;
$moveCounter= 0;

//move files, starting from the root 'images' path
moveFiles($rootPath.$imagesPath);
echo "$moveCounter files copied";


//recursive function 'moveFiles' starting from a path relative to the script path
function moveFiles($fullPath){
 global $moveCounter;
 global $rootPath;
 global $imagesPath;
 
 //see what's in the path and iterate over all entries
 $filesAndFolders = scandir($fullPath);
 foreach($filesAndFolders as $fileOrFolder){
  
  //skip the special entries '.' (this path) and '..' (one up)
  if($fileOrFolder == '.' or $fileOrFolder == '..')
   continue;
  
  //check whether the entry is a file or again a folder
  if(is_dir($fullPath.$fileOrFolder)){
   //if it's a folder, move all files from inside it (recursive)
   $folder = $fileOrFolder;
   moveFiles("$fullPath/$folder/");
  }
  else {
   $file = $fileOrFolder;
   //if it's a file, check whether it's an image file or not
   if( strpos(strtolower($file), ".png") === false
     and strpos(strtolower($file), ".jpg") === false
     and strpos(strtolower($file), ".jpeg") === false
     and strpos(strtolower($file), ".bmp") === false
     and strpos(strtolower($file), ".gif") === false
     and strpos(strtolower($file), ".exif") === false
     and strpos(strtolower($file), ".tiff") === false){
    
    //try to move the file - if something goes wrong, give output
    if(rename($fullPath.$file,$rootPath."filesFlat/".$file) === false)
     echo "issue with $path/$file\r\n";
    else
     $moveCounter++;
   }
   else{
    //try to move the image - if something goes wrong, give output
    if(rename($fullPath.$file,$rootPath."imagesFlat/".$file) === false)
     echo "issue with $fullPath/$file\r\n";
    else
     $moveCounter++;
   }
  }
 }
}
?>

No comments:

Post a Comment