File: grep-files.php

package info (click to toggle)
php-doc 20100521-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 59,992 kB
  • ctags: 4,085
  • sloc: xml: 796,833; php: 21,338; cpp: 500; sh: 117; makefile: 58; awk: 28
file content (87 lines) | stat: -rwxr-xr-x 2,924 bytes parent folder | download
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
#!/usr/bin/php -q
<?php
/*  
  +----------------------------------------------------------------------+
  | PHP Version 4                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2010 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Authors:    Philip Olson <philip@php.net>                            |
  +----------------------------------------------------------------------+
 
  $Id: grep-files.php 298923 2010-05-03 19:05:27Z philip $
*/

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
	echo "This script requires a non-Windows operating system.\n";
	exit;
}

$editor = getenv('EDITOR');

if ($_SERVER['argc'] == 2 &&
	in_array($_SERVER['argv'][1], array('--help', '-help', '-h', '-?')) 
	|| 
	$_SERVER['argc'] < 2) {

	echo "\nFind and edit files by grepping for a case insensitive string.\n";
	echo "I will open EDITOR if it's set, and the [language directory] is optional.\n\n";
	echo "Usage:    {$_SERVER['argv'][0]} <string> [langdir]\n";
	echo "Example:  {$_SERVER['argv'][0]} 'PHP3' en\n";
	if ($editor) {
		echo "Note:     Your EDITOR is set to: {$editor}\n\n";
	} else {
		echo "Note:     You do not have an EDITOR set so I will output a string of files\n\n";
	}
	exit;
}

$langdir = '';
if (!empty($_SERVER['argv'][2])) {
	$dir = trim($_SERVER['argv'][2]);
	if (is_dir($dir)) {
		$langdir = rtrim($dir, '/') . '/';
	} else {
		echo "INFO: The desired language directory ({$dir}) does not exist. Skipping...\n";
	}
}

$search_str	= trim($_SERVER['argv'][1]);
$output		= trim(shell_exec("egrep --exclude=\*.svn\* -i -r '{$search_str}' {$langdir}* "));

if (empty($output)) {
	echo "INFO: No matches for string '{$search_str}'\n";
	exit;
}

$files = array();
foreach (explode("\n", $output) as $line) {

	$filename	= trim(substr($line, 0, strpos($line, ':')));
	$ext		= pathinfo($filename, PATHINFO_EXTENSION);

	if (($ext === 'xml' || $ext === 'ent') && $filename[0] !== '.') {
		$files[$filename] = 'open';
	}
}

if (empty($files)) {
	echo "INFO: No matches for string '{$search_str}'\n";
	exit;
}

$files_str = implode(' ', array_keys($files));

if ($editor) {
	shell_exec($editor . ' ' . $files_str);
} else {
	echo $files_str;
}