File: make-partial.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 (236 lines) | stat: -rwxr-xr-x 6,207 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env php
<?php
/** vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
 *
 * Script to trigger partial builds of the PEAR manual
 *
 * LICENSE: This source file is subject to version 3.0 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * 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 web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @author    Martin Jansen <mj@php.net>
 * @copyright 2005 The PEAR Group
 * @version   CVS: $Id: make-partial.php 197121 2005-09-28 15:47:51Z techtonik $
 */

// NOTE: originally from peardoc:/make-partial.php ;
// these files should be kept in sync

if (substr(PHP_VERSION, 0, 1) == "4") {
    require_once "PHP/Compat.php";
    $components = PHP_Compat::loadVersion('5.0.0');
}

require_once "Console/Getopt.php";
$console = new Console_Getopt;
$args = $console->getopt($console->readPHPArgv(), array(), 
                         array("format=", "include=", "help"));

// {{{ gather arguments

$format = "html";
$sections = array();

$incflag = false;
foreach ($args[0] as $arg) {
    if ($arg[0] == "--help") {
        showHelp();
        exit(0);
    } elseif ($arg[0] == "--format") {
        $format = $arg[1];
    } elseif ($arg[0] == '--include') {
        $sections[] = $arg[1];
        $incflag = true;
    }    
}

if ($incflag) {
    // collect other space delimited names as section names
    foreach ($args[1] as $arg) {
        $sections[] = $arg;
    }
}

// }}}

$hasReadline = true;
if (!function_exists("readline")) {
    $hasReadline = false;
    echo "Warning: The readline extension could not be found!\n";
    if (count($sections) == 0) {
        showHelp();
        echo "Exiting because no --include parameters were specified.\n";
        exit(1);
    }
}

// recover manual.xml.in if the script was terminated unexpectedly
restoreFile();

copy("manual.xml.in", "manual.xml.in.partial-backup");
register_shutdown_function("restoreFile", filemtime("manual.xml.in"));

$file = file("manual.xml.in");
if (!$file) {
    echo "Error: Unable to read manual.xml.in!";
    exit(1);
}

$newFile = "";
$partStack = array();
$includePart = true;
$notInPart = true;

/**
 * Loop through the file and build a new file depending on the users
 * choice.
 */
foreach ($file as $line) {
    // <part id="foo">
    if (preg_match("/<part id=\"([a-z-]+)\">/", $line, $matches)) {
        $inPart = true;

        if ($sections) {
            echo "Including ". $matches[1] ."? ";
            if ($includePart = inString($sections, $matches[1])) {
                echo "YES\n";
            } else {
                echo "NO\n";
            }
        } else if ($hasReadline) {
            $include = readline("Include " . $matches[1] . "? [NO] ");
            $includePart = evaluate($include);
        }

        if ($includePart == true) {
            $newFile .= $line;
        }

        continue;
    }

    // </part>
    if (preg_match("/<\/part>/", $line)) {
        if (count($partStack) > 0) {
            $newFile .= implode("", $partStack);
            $partStack = array();
        }

        if ($includePart == true) {
            $newFile .= $line;
        }
        $includePart = false;
        $inPart = false;

        continue;
    }

    // <title>
    if ($inPart == true && $includePart && preg_match("/<title/", $line)) {
        $partStack[] = $line;
        continue;
    }

    // the rest
    if ($inPart == true) {
        if ($includePart == false) {
            continue;
        }
        
        if (preg_match("/(\s\t)*&([a-z0-9\.-]+);/", $line, $matches)) {

            if ($sections) {
                echo "Including ". $matches[2] ."? ";
                if ($include = inString($sections, $matches[2])) {
                    echo "YES\n";
                } else {
                    echo "NO\n";
                }
            } else if ($hasReadline) {
                $include = evaluate(readline("Include " . $matches[2] . "? [NO] "));
            }
            
            if ($include == true) {
                $partStack[] = $line;
            }
        }
    } else {
        $newFile .= $line;
    }
}

file_put_contents("manual.xml.in", $newFile);

// {{{ Run the build scripts

$cmd = "make " . $format;
passthru($cmd);

// }}}
// {{{ Helper functions

/**
 * Restores the original manual.xml.in file
 */
function restoreFile($savedmtime = null) {
    if (!is_file("manual.xml.in.partial-backup")) {
        return;
    }

    unlink("manual.xml.in");
    rename("manual.xml.in.partial-backup", "manual.xml.in");
    if ($savedmtime) touch("manual.xml.in", $savedmtime);
}

/**
 * Evaluates the return value of readline()
 *
 * If the first parameter is either "y" or "yes" the method will
 * return true. Otherwise false.
 */
function evaluate($str) {
    if ($str == 'y' || $str == "yes") {
        return true;
    }
    return false;
}

/**
 * Checks if one element of the first parameter is part of the second parameter
 *
 * @param  array List of needles
 * @param  string Haystack
 * @return boolean True if one of the needles is in the haystack,
 *                 false otherwise.
 */
function inString($needle, $haystack)
{
    foreach ((array) $needle as $n) {
        if (stripos($haystack, $n) !== false) {
            return true;
        }
    }
    return false;
}

/**
 * Prints a usage notice for the script
 *
 * @return void
 */
function showHelp()
{
    echo "Usage: make-partial.php [--format <format>] [--include <section1>] [--include <section2>] ...\n";
    echo "       make-partial.php --help\n";
    echo "\n";
    echo "  --format <format>    Which format to build. Can be one of 'html', 'pearweb'.\n";
    echo "                       Default is 'html'.\n";
    echo "  --include <section>  Automatically include certain sections of the manual\n";
    echo "                       without asking for them explicitely.  This also works\n";
    echo "                       on setups where the readline extension is not available.\n";
    echo "  --help               Prints this help text.\n\n";
}

// }}}