脚本循环遍历html / php / js文件以构建已使用资源的列表

脚本循环遍历html / php / js文件以构建已使用资源的列表

问题描述:

As some of my websites have progressed, the server has become cluttered with files no longer in use be it due to versioning, jquery plugins no longer being used, etc...

I'm thinking about writing a script using grep with some regex but if theres already something that exists, it would make things easier.

Can someone point me in the right direction of a script / program that I can feed a listing of html / php / js files that could loop through them, reading the code and tell me what .php, .js, .jpg, etc... files are included?

The script could look at src='', include(), require(), etc...

I'm not looking for someone to do it for me; just a starting point on how to proceed or if something already exists.

由于我的一些网站已取得进展,服务器因版本控制而变得混乱不再使用的文件 ,jquery插件不再被使用等等... p>

我正在考虑使用grep和一些正则表达式来编写脚本,但如果已经存在某些东西,它会使事情变得更容易。 p>

有人能指出我在脚本/程序的正确方向,我可以提供可以循环遍历它们的html / php / js文件列表,阅读代码并告诉我是什么 包含.php,.js,.jpg等文件? p>

脚本可以查看src ='',include(),require()等等... p>

我不是在寻找有人为我做这件事; 只是一个关于如何进行或某些事情已经存在的起点。 p> div>

for images, scripts etc you can use firebug for firefox (just click the net tag)

In php you could use "var_dump( get_included_files() )" (at the end of your code) to get all included files for that particular page.

This thread might give you some answers although probably only works for Java.

Know that javascript code might include few more js/css files in not so grep-friendly way:

var extraScript = document.createElement('script');
extraScript.src = scriptUrl;
document.head.appendChild(extraScript);

//or
document.write('<script src="' + scriptUrl + '"></script>');

Also php include*() may also use variable or expression which is also impossible to grep as it needs to be executed.

To counter all that you should use javascript to parse rendered HTML after all other js has been executed or use firebug (Net tab) or Chrome dev tools, and for PHP use get_included_files() to get list of included php files, and write finds into a file or db or whatever...Doing this may take a while.

I'm not sure if such a solution already exists for PHP (probably), but if I find it I'll let you know.