forked from jleyva/moodle-block_configurablereports
-
Notifications
You must be signed in to change notification settings - Fork 2
/
m19tom20.php
executable file
·238 lines (195 loc) · 5.95 KB
/
m19tom20.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
// Uncomment this line for using this tool
die;
/*
*
*
*
* This script uses some lines of code of check_db_syntax by stronk7
*/
if (isset($_SERVER['REMOTE_ADDR'])) {
define('LINEFEED', "<br />");
} else {
define('LINEFEED', "\n");
}
/// Rules
$rules = array();
$rules[] = array(
"exp"=>"/([^>])get_records\(([^,]+)\)/m",
"func"=>"",
"repl"=>"$1\$DB->get_records($2)"
);
$rules[] = array(
"exp"=>"/([^>])(get_record[s]?\([^,]+,)([^\)]+)(\))/m",
"func"=>"add_conditions",
"repl"=>""
);
$rules[] = array(
"exp"=>"/([^>])(get_field?\([^,]+,[^,]+,)([^\)]+)(\))/m",
"func"=>"add_conditions",
"repl"=>""
);
$rules[] = array(
"exp"=>"/([^>])(set_field?\([^,]+,[^,]+,[^,]+,)([^\)]+)(\))/m",
"func"=>"add_conditions",
"repl"=>""
);
$rules[] = array(
"exp"=>"/([^>])(update_record|insert_record|delete_record|count_records|sql_substr)/m",
"func"=>"",
"repl"=>"$1\$DB->$2"
);
$rules[] = array(
"exp"=>'/ENUM(VALUES)?=".*?"/',
"func"=>"",
"repl"=>""
);
$rules[] = array(
"exp"=>"/(global[ ]+)([^;]*;)/",
"func"=>"",
"repl"=>"$1\$DB, $2"
);
$rules[] = array(
"exp"=>"/(addslashes\()([^\)]+)(\))/",
"func"=>"",
"repl"=>'$2'
);
$rules[] = array(
"exp"=>'/(\{?\$CFG->prefix\}?)([^ ,]+)/',
"func"=>"",
"repl"=>'{$2}'
);
$rules[] = array(
"exp"=>"/print_(box|box_start|box_end|container_end)/m",
"func"=>"",
"repl"=>"echo \$OUTPUT->$1"
);
$rules[] = array(
"exp"=>"/([\s\t]*)print_heading\(([^\)]+)\)/m",
"func"=>"change_print_heading",
"repl"=>""
);
$rules[] = array(
"exp"=>"/([\s\t]*)print_header\(([^\)]+)\)/m",
"func"=>"change_print_header",
"repl"=>""
);
$rules[] = array(
"exp"=>"/([\s\t]*)print_footer\(([^\)]*)\)/m",
"func"=>"change_print_footer",
"repl"=>""
);
// Add conditions to dml functions
function add_conditions($matches){
$conditions = explode(',',$matches[3]);
$cond = "array(";
for($i=0;$i<count($conditions);$i += 2)
$cond .= $conditions[$i]." => ".$conditions[$i+1].",";
$cond = substr($cond,0,-1);
$cond .= ")";
return $matches[1]."\$DB->".$matches[2].$cond.$matches[4];
}
// Change the print heading
function change_print_heading($matches){
$newcode = '';
$args = explode(',',$matches[2]);
if(isset($args[1]))
unset($args[1]);
$args = implode(',',$args);
$newcode .= $matches[1].'echo $OUTPUT->heading('.$args.')';
return $newcode;
}
// Change the print header
function change_print_header($matches){
$newcode = '';
$args = explode(',',$matches[2]);
$newcode .= $matches[1].'$PAGE->set_context($context);';
$newcode .= $matches[1].'$PAGE->set_pagelayout(\'incourse\');';
$newcode .= (isset($args[0]))? $matches[1].'$PAGE->set_title('.$args[0].');' : '';
$newcode .= (isset($args[1]))? $matches[1].'$PAGE->set_heading('.$args[1].');' : '';
$newcode .= (isset($args[5]))? $matches[1].'$PAGE->set_cacheable('.$args[5].');' : '';
$newcode .= (isset($args[6]))? $matches[1].'$PAGE->set_button('.$args[6].');' : '';
$newcode .= (isset($args[7]))? $matches[1].'$PAGE->set_headingmenu('.$args[7].');' : '';
$newcode .= $matches[1].'echo $OUTPUT->header()';
return $newcode;
}
// Change the print footer
function change_print_footer($matches){
$newcode = $matches[1].'echo $OUTPUT->footer()';
return $newcode;
}
/// Getting current dir
$dir = dirname(__FILE__);
// Create version.php
if(!file_exists("$dir/version.php")){
$content = utf8_encode("<?php\n \$plugin->version = 2007101509;\n?>");
file_put_contents("$dir/version.php", $content);
echo "Created version.php file".LINEFEED;
}
// Rename lang dirs
$langdirpath = "$dir/lang";
$langdir = opendir($langdirpath);
while (false !== ($file=readdir($langdir))) {
$fullpath = $langdirpath . '/' . $file;
if (substr($file, 0, 1)=='.' || $file=='CVS') { /// Exclude some dirs
continue;
}
if (is_dir($fullpath)) {
rename($fullpath, $langdirpath . '/' . str_replace('_utf8','',$file));
echo "Renamed lang dir $file".LINEFEED;
}
}
// Create string (pluginname)
//* Change array name to $capabilities only. [Warning only]
//* Remove all references to "admin" from access.php as it is not needed. [Warning only]
//* Rename "legacy" to "archetypes" in line with core code. [Optional]
//* Add new "manager" role. --Frank Ralf 11:22, 14 November 2010 (UTC)
/// Process starts here
$files = files_to_check($dir);
foreach ($files as $file) {
$modify = false;
//echo " - $file: ";
$content = file_get_contents($file);
foreach($rules as $r){
if($r['func']){
$content = preg_replace_callback($r['exp'],$r['func'],$content);
}
else{
$content = preg_replace($r['exp'],$r['repl'],$content);
}
}
/// The file need to be updated?
file_put_contents($file, $content);
}
/**
* Given one full path, return one array with all the files to check
*/
function files_to_check($path) {
$results = array();
$pending = array();
$dir = opendir($path);
while (false !== ($file=readdir($dir))) {
$fullpath = $path . '/' . $file;
if (substr($file, 0, 1)=='.' || $file=='CVS' || $file == 'm19tom20.php') { /// Exclude some dirs
continue;
}
if (is_dir($fullpath)) { /// Process dirs later
$pending[] = $fullpath;
continue;
}
if (is_file($fullpath) && strpos($file, basename(__FILE__))!==false) { /// Exclude me
continue;
}
if (is_file($fullpath) && strpos($file, '.php')===false && strpos($file, '.html')===false && strpos($file,'.xml')===false) { /// Exclude some files
continue;
}
if (!in_array($fullpath, $results)) { /// Add file if doesn't exists
$results[$fullpath] = $fullpath;
}
}
closedir($dir);
foreach ($pending as $pend) {
$results = array_merge($results, files_to_check($pend));
}
return $results;
}