forked from backdrop-contrib/coder_upgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coder_upgrade.inc
170 lines (156 loc) · 4.35 KB
/
coder_upgrade.inc
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* @file
* Provides constants and utility functions.
*
* Copyright 2008-11 by Jim Berry ("solotandem", http://drupal.org/user/240748)
*/
/**
* The default directory to store modules to be converted.
* Relative to public stream wrapper path.
*/
define('DEADWOOD_DIR', 'coder_upgrade');
/**
* The default directory to store modules to be converted.
* Relative to public stream wrapper path.
*/
define('DEADWOOD_OLD', 'coder_upgrade/old');
/**
* The default directory to store converted modules.
* Relative to public stream wrapper path.
*/
define('DEADWOOD_NEW', 'coder_upgrade/new');
/**
* The default directory to store patch files.
* Relative to public stream wrapper path.
*/
define('DEADWOOD_PATCH', 'coder_upgrade/patch');
/**
* Passes a string through t() and wraps the result in html entity <p>.
*/
function tp($string, $args = array()) {
return '<p>' . t($string, $args) . '</p>';
}
/**
* Scans a directory and finds all first-level directories beneath it.
*
* TODO Replace this with a call to file_scan_directory in include/files.inc.
*
* @param string $path Directory path.
*
* @return Array of directory names.
*/
function coder_upgrade_scan_directory($path) {
static $ignore = array('.', '..', '.svn');
$dirs = array();
$path = $path . '/';
if (!is_dir($path)) {
return $dirs;
}
$files = scandir($path);
foreach ($files as $file) {
$file_path = $path . $file;
if (is_dir($file_path) && !in_array(basename($file_path), $ignore)) {
$dirs[] = $file;
}
}
return $dirs;
}
/**
* Removes all files from a directory and optionally removes the directory.
*
* @param string $path Directory path.
*/
function coder_upgrade_clean_directory($path, $remove_me = FALSE) {
$path = $path . '/';
if (!is_dir($path)) {
return;
}
$files = scandir($path);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$file_path = $path . $file;
if (is_dir($file_path)) {
coder_upgrade_clean_directory($file_path, TRUE);
}
else {
file_unmanaged_delete($file_path);
}
}
}
if ($remove_me) {
rmdir($path);
}
}
/**
* Returns full directory path relative to sites directory.
*
* @param string $name
* Name of the directory.
* @param boolean $add_slash
* Indicates whether to add a trailing slash.
* @param boolean $stream_format
* Indicates whether to use the actual path or a stream protocol.
*
* @return string
* A string of the directory path.
*/
function coder_upgrade_directory_path($name, $add_slash = TRUE, $stream_format = FALSE) {
$config = config('coder_upgrade.settings');
$slash = $add_slash ? '/' : '';
$prefix_no_slash = $stream_format ? file_default_scheme() . ':/' : file_stream_path();
$prefix = $prefix_no_slash . '/';
switch ($name) {
case 'base':
return $prefix . $config->get('coder_upgrade_dir') . $slash;
case 'old':
return $prefix . $config->get('coder_upgrade_dir_old') . $slash;
case 'new':
return $prefix . $config->get('coder_upgrade_dir_new') . $slash;
case 'patch':
return $prefix . $config->get('coder_upgrade_dir_patch') . $slash;
case '':
return $prefix_no_slash; // @todo Is this correct with a stream format?
default:
return $prefix . $name . $slash;
}
}
/**
* Returns path to file or files directory.
*
* @param string $type
* Type of file to return path to. If blank, return directory path.
*
* @return string
* Path to file or directory.
*/
function coder_upgrade_path($type = '') {
static $path = '';
if (!$path) {
$path = coder_upgrade_directory_path('base', FALSE);
}
return $type ? $path . '/' . $type . '.txt' : $path;
}
/**
* Returns the local public directory path.
*
* Adapted from function removed from core on 2010-09-01
* (see http://drupal.org/cvs?commit=415020).
*
* @return string
* A string containing the directory path of a stream. FALSE is returned if
* the scheme is invalid or a wrapper could not be instantiated.
*/
function file_stream_path($scheme = 'public') {
global $_coder_upgrade_files_base;
if (isset($_coder_upgrade_files_base)) {
// This is being run as a separate process outside of Backdrop.
return $_coder_upgrade_files_base;
}
elseif ($wrapper = file_stream_wrapper_get_instance_by_scheme($scheme)) {
return $wrapper->getDirectoryPath();
}
else {
return FALSE;
}
}