File: switch-to-newstyle.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 (136 lines) | stat: -rw-r--r-- 4,086 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
<?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:    Mehdi Achour <didou@php.net>                             |
+----------------------------------------------------------------------+

$Id: switch-to-newstyle.php 293138 2010-01-05 10:21:11Z rquadling $
*/

if (PHP_SAPI !== 'cli') {
    echo "This script is ment to be run under CLI\n";
    exit(1);
}

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

    echo "Switch whitespace ready documentation to the new style\n\n";
    echo "Usage:      {$_SERVER['argv'][0]} <path_to_extension>\n";
    echo "            --help, -help, -h, -?      - to get this help\n";
    die;

}

// We only deal with the functions directory
$fullpath_dir = rtrim($_SERVER['argv'][1], '/') . '/functions/';

if (!is_dir($fullpath_dir)) {
    echo "ERROR: ($fullpath_dir) is not a directory.\n";
    exit(1);
}

$counts = array('old' => 0, 'new' => 0);
foreach (glob($fullpath_dir . "*.xml") as $file) {
    
    $old = file_get_contents($file);
    
    // Check if file is already using the new style and if so, skip it
    if (false !== strpos($old, '<refsect1 role=')) {
        $counts['new']++;
        continue;
    }
    
    // Switch description and examples
    $new = str_replace(
    array('<refsect1>', '<title>Description</title>', "  </para>\n  <para>\n   <example>"),
    array("\n".' <refsect1 role="description">', '&reftitle.description;', "  </para>\n </refsect1>\n\n <refsect1 role=\"examples\">\n  &reftitle.examples;\n  <para>\n   <example>"),
    $old);

    // Remove splitted from .. which doesn't make sense anymore
    $new = preg_replace('@<!-- splitted [^>]*>\s*\n@', '', $new);

    // Switch see also
    $new = preg_replace(
    '!  <para>
   See also:? <function>([^<]*)</function>\.?
  </para>
 </refsect1>!',
 ' </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>\1</function></member>
   </simplelist>
  </para>
 </refsect1>',
 $new);
 
 $result = array(); 
 // Write parameters and return values
 preg_match('@<methodsynopsis>([(.\n]*)</methodsynopsis>@m', $old, $result);

 if (count($result)) {
     $params = array();
     preg_match_all('@<parameter[^>]*>([^<]*)</parameter>@', $result[1], $params);
     if (count($params) && count($params[1])) {
         $buffer = '</refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
';
         foreach ($params[1] as $param) {
             $buffer .="    <varlistentry>
     <term><parameter>$param</parameter></term>
     <listitem>
      <para>
      </para>
     </listitem>
    </varlistentry>
";
         }

         $buffer .= '   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
  </para>
 </refsect1>';
     }
     $new = preg_replace('!</refsect1>!', $buffer, $new, 1);
 }
 
 if ($new === $old) {
     continue;
 }
 $counts['old']++;

 $fp = fopen($file, 'w');
 fputs($fp, $new);
 fclose($fp);

}

echo "Modified file information:\n";
echo "Old --> New: {$counts['old']}\n";
echo "Already New: {$counts['new']}\n";