File: entity-usage.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 (157 lines) | stat: -rwxr-xr-x 4,849 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
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
#!/usr/bin/php -q
<?php
/*
  +----------------------------------------------------------------------+
  | PHP Version 4                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2010 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.0 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_0.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:    Nuno Lopes <nlopess@php.net>                             |
  +----------------------------------------------------------------------+

  $Id: entity-usage.php 293138 2010-01-05 10:21:11Z rquadling $
*/


if ($argc > 3 || $argc < 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>
  Find which files use the specified entity.

  Usage:
  <?php echo $argv[0];?> <entity> [<language-code>]

  <entity> is the entity you want to search.

  <language-code> must be a valid language code used in the repository, or
  'all' for all languages. Defaults to en.

<?php
  exit;
}

// CONFIG SECTION
$docdir = "../"; // Main directory of the PHP documentation (one dir up in cvs)

/*********************************************************************/
/* Nothing to modify below this line                                 */
/*********************************************************************/

global $usage;

// Long runtime
set_time_limit(0);

// Default values
$langcodes = array("en");

// Parameter value copying
if ($argc == 3) { 
    $langcodes = array($argv[2]);
    if ($argv[2] === 'all') {
        $langcodes = array("ar", "cs", "de", "el", "en", "es", "fi",
                           "fr", "he", "hk", "hu", "it", "ja", "kr",
                           "lt", "nl", "pt", "pl", "pt_BR", "ro",
                           "ru", "sk", "sl", "sv", "tr", "tw", "zh");
    }
}

/*********************************************************************/
/* Here starts the functions part                                    */
/*********************************************************************/

// Checks a diretory of phpdoc XML files
function check_dir($dir, $entity)
{
    // Collect files and diretcories in these arrays
    $directories = array();
    $files = array();
    
    // Skip old and unused functions directories (theoretically
    // it should only be in the English tree, but we are smart
    // and check for other language trees too...)
    if (preg_match("!/([a-z]{2}|pt_BR)/functions!", $dir)) {
        return;
    }
    
    // Open and traverse the directory
    $handle = @opendir($dir);
    while ($file = @readdir($handle)) {
      
      // Collect directories and XML files
      if ($file != 'CVS' && $file != '.' &&
          $file != '..' && is_dir($dir.$file)) {
        $directories[] = $file;
      }
      elseif (strstr($file, ".xml")) {
        $files[] = $file;
      }

    }
    @closedir($handle);
      
    // Sort files and directories
    sort($directories);
    sort($files);
      
    // Files first...
    foreach ($files as $file) {
      check_file($dir.$file, $entity);
    }

    // then the subdirs
    foreach ($directories as $file) {
      check_dir($dir.$file."/", $entity);
    }
} // check_dir() function end

function check_file ($filename, $entity)
{
    global $usage;

    // Read in file contents
    $contents = file_get_contents($filename);
    
    // Find all entity usage in this file
    if (preg_match("/&$entity;/U", $contents) == 1) {
        echo $filename . "\n";
        $usage++;
    }

} // check_file() function end
  
/*********************************************************************/
/* Here starts the program                                           */
/*********************************************************************/

// Chechking all languages
foreach ($langcodes as $langcode) {

    $usage = 0;

    // Check for directory validity
    if (!@is_dir($docdir . $langcode)) {
        echo "The $langcode language code is not valid\n";
        continue;
    }
      
    // If directory is OK, start with the header
    echo "\nSearching in $docdir$langcode ...\n";
    
    // Check the requested directory
    check_dir("$docdir$langcode/", $argv[1]);

    echo "\nFiles found: $usage\n\n";

}

echo "Done!\n";

?>