-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoik-uploads.php
120 lines (105 loc) · 3.08 KB
/
oik-uploads.php
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php // (C) Copyright Bobbing Wide 2018
/**
* Syntax: oikwp oik-uploads.php source_dir
*
* when run from the oik-batch folder
*
* Compares the files in source_dir with the files in the wp-content/uploads folder
* ( using wp_upload_dir()? )
* Copying missing files
*
*
*
* For WPMS
* - No support yet
* - subdomain install pass the url= parameter
* - subdirectory install we need to set url= and path=
*
*/
oik_uploads_loaded();
/**
* Copy uploaded files from the given folder
*/
function oik_uploads_loaded() {
$source_dir = oik_batch_query_value_from_argv( 1, null );
if ( !$source_dir ) {
echo "Syntax: oikwp oik-uploads.php source_dir" . PHP_EOL;
$source_dir = "/backups-qw/bigram.co.uk/public_html/wp-content/uploads";
echo "where source_dir is $source_dir" . PHP_EOL;
die();
}
echo "Source: $source_dir" . PHP_EOL;
$target_dir = oik_uploads_target_dir();
echo "Target: $target_dir" . PHP_EOL;
copy_missing_files( $source_dir, $target_dir, $source_dir );
}
/**
* Determines the base target directory for uploads
*
* @return string target directory e.g. /apache/htdocs/bigram/wp-content/uploads
*/
function oik_uploads_target_dir() {
$uploads = wp_get_upload_dir();
$target_dir = bw_array_get( $uploads, "basedir", null );
return $target_dir;
}
/**
* Copies missing files from source dir to target_dir
*
* @param string $source_dir fully qualified source directory
* @param string $target_dir target directory base
* @param string $base source directory base
*/
function copy_missing_files( $source_dir, $target_dir, $base) {
$files = glob( $source_dir .'/*' );
foreach ( $files as $file ) {
if ( is_dir( $file ) ) {
copy_missing_files( $file, $target_dir, $base );
} else {
copy_missing_file( $file, $target_dir, $base );
}
}
}
/**
* Copies a missing file
*
* Creates the target directory if required.
*
* @param string $file fully qualified source file
* @param string $target_dir target directory base
* @param string $base source directory base
*/
function copy_missing_file( $file, $target_dir, $base ) {
//echo "copying missing file?" . $file;
$target_file = str_replace( $base, $target_dir, $file );
if ( !file_exists( $target_file ) ) {
echo "Target: $target_file" . PHP_EOL;
wp_mkdir_p( dirname( $target_file ) );
copy( $file, $target_file );
} else {
copy_different_files( $file, $target_file );
}
}
/**
* Copy different files - maybe?
*
* Files in the master may be different from the local version for a number of reasons
* e.g.
* - Images may have been rotated.
* - Case sensitivity related issues in a Windows system
* - Files may not be kosher
*
* @param string $source_file
* @param string $target_file
*/
function copy_different_files( $source_file, $target_file ) {
$source_mtime = filemtime( $source_file );
$target_mtime = filemtime( $target_file );
if ( $source_mtime > $target_mtime ) {
echo "Files different. Need investigation" . PHP_EOL;
echo "$source_file $source_mtime" . PHP_EOL;
echo "$target_file $target_mtime" . PHP_EOL;
//copy( $source_file,$target_file );
//gob();
}
}