-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobsolete_checker.php
executable file
·350 lines (320 loc) · 10.5 KB
/
obsolete_checker.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/php
<?php
/**
* TYPO3ObsoleteMethods Class
* By Bastian Bringenberg <[email protected]>
*
* #########
* # USAGE #
* #########
*
* See Readme File
*
* ###########
* # Licence #
* ###########
*
* See License File
*
* ##############
* # Repository #
* ##############
*
* Fork me on GitHub
* https://github.com/bbnetz/TYPO3ObsoleteMethods
*
*
*/
/**
* Class TYPO3ObsoleteMethods
* @author Bastian Bringenberg <[email protected]>
* @link https://github.com/bbnetz/TYPO3ObsoleteMethods
*
*/
class TYPO3ObsoleteMethods {
/**
* @var string $obsoleteList Location in the Wiki where all deprecated methods are located
*/
protected $obsoleteList = 'http://wiki.typo3.org/TYPO3_6.0_Extension_Migration_Tips';
/**
* @var string $html the html from the wiki
*/
protected $html = null;
/**
* @var array contains all obsolete static methods from the core in form ( per array entry ): array('class', 'methodName', 'informationText')
*/
protected $obsoleteStaticMethods = array();
/**
* @var array contains all obsolete non static methods from the core in form ( per array entry ): array('class', 'methodName', 'informationText')
*/
protected $obsoleteNonStaticMethods = array();
/**
* @var array $ignoredFiles list of files to ignore
*/
protected $ignoredFiles = array('README', 'readme', 'Changelog', 'ChangeLog','changelog');
/**
* @var array $checkableExtensions containing all pathes to possible Extensions
*/
protected $checkableExtensions = array();
/**
* @var boolean $searchNonStatic if set will also check for non static methods. Warning: not yet stable could show wrong informations
*/
protected $searchNonStatic = false;
/**
* function __construct
* Constructor for TYPO3ObsoleteMethods
*
* @return void
*/
public function __construct() {
$ops = getopt('',
array(
'',
'instancesPath::',
'extensionPath::',
'ignoredFiles::',
'searchNonStatic::',
)
);
if(isset($ops['instancesPath']))
$this->checkableExtensions = array_merge($this->checkableExtensions, $this->getExtensionPathesByInstance($ops['instancesPath']));
if(isset($ops['extensionPath']))
$this->checkableExtensions = array_merge($this->checkableExtensions, $this->getExtensionPathesFromExtensionList($ops['extensionPath']));
if(isset($ops['ignoredFiles']))
$this->ignoredFiles = array_merge($this->ignoredFiles, $this->getIgnoredFiles($ops['ignoredFiles']));
if(isset($ops['searchNonStatic']))
$this->searchNonStatic = true;
if(count($this->checkableExtensions) == 0)
die('No Extension to Check.');
}
/**
* function start
* Doing all the work
*
* @return void
*/
public function start() {
$this->html = $this->getCurrentHTML($this->obsoleteList);
$this->obsoleteStaticMethods = $this->fetchStaticMethodFromHTML($this->html);
$this->obsoleteNonStaticMethods = $this->fetchNonStaticMethodFromHTML($this->html);
$this->checkExtensions($this->checkableExtensions);
$output = '';
foreach($this->obsoleteStaticMethods as $ext ) {
if($ext['used'])
$output .= 'Static Method: '.$ext['class'].'::'.$ext['methodname'].' - '.$ext['note'].PHP_EOL;
}
foreach($this->obsoleteNonStaticMethods as $ext ) {
if($ext['used'])
$output .= 'NonStatic Method: '.$ext['class'].'::'.$ext['methodname'].' - '.$ext['note'].PHP_EOL;
}
if($output != '')
echo PHP_EOL.PHP_EOL.PHP_EOL.'Helping hands for your obsolete methods:'.PHP_EOL.'========================================'.PHP_EOL;
echo $output;
}
/**
* function getCurrentHTML
* Gets $url and creates HTMLString from it
*
* @param string $url to fetch the HTML from
* @return string the HTML from $url
*/
protected function getCurrentHTML($url) {
return file_get_contents($url);
}
/**
* function fetchStaticMethodFromHTML
* Itterates trough the Tables of $this->obsoleteList and collects informations from the first Table
*
* @param string $html the html to check in
* @return array see $this->obsoleteStaticMethods for more informations
*/
protected function fetchStaticMethodFromHTML($html) {
$array = array();
preg_match_all('/<table class="usertable sortable">(.*?)<\/table>/sm', $html, $tables);
preg_match_all('/<tr>(.*?)<\/tr>/s', $tables[0][0], $rows);
foreach($rows[1] as $number => $row) {
if($number == 0 ) continue;
$array[] = $this->buildSingleMethodEntryFromTableRow($row);
}
return $array;
}
/**
* function fetchNonStaticMethodFromHTML
* Itterates trough the Tables of $this->obsoleteList and collects informations from the second Table
*
* @param string $html the html to check in
* @return array see $this->obsoleteNonStaticMethods for more informations
*/
protected function fetchNonStaticMethodFromHTML($html) {
$array = array();
preg_match_all('/<table class="usertable sortable">(.*?)<\/table>/sm', $html, $tables);
preg_match_all('/<tr>(.*?)<\/tr>/s', $tables[0][1], $rows);
foreach($rows[1] as $number => $row) {
if($number == 0 ) continue;
$array[] = $this->buildSingleMethodEntryFromTableRow($row);
}
return $array;
}
/**
* function buildSingleMethodEntryFromTableRow
* Gets a TD Row and returns as method struct
*
* @param string $row the table data row
* @return array $method a struct for the methods
*/
protected function buildSingleMethodEntryFromTableRow($row) {
$method = array(
'class' => '',
'methodname' => '',
'note' => '',
'used' => false
);
$row = str_replace(array('>', '<'), array('>', '<'), $row);
$tbs = explode('</td>', $row);
$tbs[0] = str_replace(array('<td>', '</td>'), array('', ''), $tbs[0]);
$tbs[1] = str_replace(array('<td>', '</td>'), array('', ''), $tbs[1]);
$splitted_field = explode('::', $tbs[0]);
$method['class'] = trim($splitted_field[0]);
if(isset($splitted_field[1]))
$method['methodname'] = trim($splitted_field[1]);
$method['note'] = trim($tbs[1]);
return $method;
}
/**
* function checkExtensions
* itterates trough $extensionList
*
* @param array $extensionList containing all pathes
* @return void
*/
protected function checkExtensions($extensionList) {
foreach($extensionList as $extensionPath )
$this->checkExtension($extensionPath);
}
/**
* function checkExtension
* checks the extension for obsolete static and non-static methods
* Echos on Error
*
* @param string $extensionPath The path to the extension
* @return void
*/
protected function checkExtension($extensionPath) {
for($i = 0; $i < count($this->obsoleteStaticMethods) -1; $i++) {
$filename = $this->php_grep($this->obsoleteStaticMethods[$i]['class'].'::'.$this->obsoleteStaticMethods[$i]['methodname'], $extensionPath);
if($filename) {
$filenames = explode(PHP_EOL, $filename);
foreach($filenames as $filename) {
if($filename !== '')
echo 'StaticMethod: '.$this->obsoleteStaticMethods[$i]['class'].'::'.$this->obsoleteStaticMethods[$i]['methodname'].' in '.$filename.PHP_EOL;
}
$this->obsoleteStaticMethods[$i]['used'] = true;
}
}
if($this->searchNonStatic)
for($i = 0; $i < count($this->obsoleteStaticMethods) -1; $i++) {
$error = $this->php_grep($this->obsoleteNonStaticMethods[$i]['class'], $extensionPath);
if($filename = $this->php_grep($this->obsoleteNonStaticMethods[$i]['class'], $extensionPath)) {
$filenames = explode(PHP_EOL, $filename);
foreach($filenames as $filename)
if(trim($filename) !== '')
if(strpos(file_get_contents($filename), $this->obsoleteStaticMethods[$i]['methodname']) !== FALSE) {
echo 'NonStaticMethod: '.$this->obsoleteStaticMethods[$i]['class'].'::'.$this->obsoleteStaticMethods[$i]['methodname'].' in '.$filename.PHP_EOL;
$this->obsoleteNonStaticMethods[$i]['used'] = true;
}
}
}
}
/**
* function php_grep
* works like grep -R
*
* @see http://www.cafewebmaster.com/search-text-files-recursively-php-grep
* @param string $q the search string
* @param string $path the path to check
* @return string the method and the path to the error
*/
protected function php_grep($q, $path){
$fp = opendir($path);
$ret = '';
while($f = readdir($fp)){
if($this->ignoreFile($f)) continue;
if( preg_match('#^\.+$#', $f) ) continue; // ignore symbolic links
$file_full_path = $path.DIRECTORY_SEPARATOR.$f;
if(is_dir($file_full_path)) {
$ret .= $this->php_grep($q, $file_full_path);
} else if( stristr(file_get_contents($file_full_path), $q) ) {
$ret .= $file_full_path.PHP_EOL;
}
}
return $ret;
}
/**
* function ignoreFile
* Checks if $file is ignored
*
* @param string $file the file name to check
* @return boolean true if $file is ignored
*/
protected function ignoreFile($file) {
return in_array($file, $this->ignoredFiles);
}
/**
* function getIgnoredFiles
* Separates the comma list and trims each entry
*
* @param string $ignoredFiles comma separated list of files
* @return array of trimmed files
*/
public function getIgnoredFiles($ignoredFiles) {
$ignoredFiles = explode(',', $ignoredFiles);
$files = array();
foreach($ignoredFiles as $file) {
$files[] = trim($file);
}
return $files;
}
/**
* function getExtensionPathesFromExtensionList
* Separates the comma list and trims each entry
*
* @param string $pathesList comma separated list of directories
* @return array of trimmed extensionPathes
*/
public function getExtensionPathesFromExtensionList($pathesList) {
$pathesList = explode(',', $pathesList);
$files = array();
foreach($pathesList as $file) {
$files[] = realpath(trim($file));
}
return $files;
}
/**
* function getExtensionPathesByInstance
* Gets all Extensions from $instancesList
*
* @param string $instancesList comma separated list of instances
* @return array of Extensions
*/
public function getExtensionPathesByInstance($instancesList) {
$instancesList = explode(',', $instancesList);
$files = array();
foreach($instancesList as $file) {
$files = array_merge($files, $this->getExtensionsFromSingleInstance(realpath(trim($file))));
}
return $files;
}
/**
* function getExtensionsFromSingleInstance
* Checks TYPO3 Instances to get all Extensions from it
*
* @param string $file a path to the TYPO3 instance
* @return array of Extensions
*/
public function getExtensionsFromSingleInstance($file) {
if($file{strlen($file)-1} != DIRECTORY_SEPARATOR ) $file .= DIRECTORY_SEPARATOR;
return glob($file.'typo3conf'.DIRECTORY_SEPARATOR.'ext'.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
}
}
$typo3obsoletemethods = new TYPO3ObsoleteMethods();
$typo3obsoletemethods -> start();