File: pearweb.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 (324 lines) | stat: -rwxr-xr-x 10,004 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
require_once $ROOT . '/themes/pear/peartheme.php';

/**
* PEAR theme for the many php files used for pearweb
*
* @package PhD
* @version CVS: $Id: pearweb.php 276839 2009-03-07 19:14:45Z cweiske $
*/
class pearweb extends peartheme
{
    /**
    * Array of file resources
    *
    * @var array
    */
    protected $streams = array();

    protected $writeit = false;

    public $outputdir = '';

    /**
    * URL prefix for all API doc link generated with <phd:pearapi>.
    * On pearweb, the manual is at "/manual/$lang/". This means that
    *  we can use a relative URI here to make the links work on mirrors, too.
    *
    * @var string
    */
    public $phd_pearapi_urlprefix = '../../package/';


    /**
     * Constructor
     *
     * @param array  $IDs     Array of IDs to build
     * @param string $ext     Filename extension to use
     * @param bool   $chunked Whether to chunk the output into individual files
     */
    public function __construct($IDs, $ext = "php", $chunked = true)
    {
        parent::__construct($IDs, $ext, $chunked);
        $this->outputdir = $GLOBALS['OPTIONS']['output_dir'] . $this->ext . DIRECTORY_SEPARATOR;
        if (!file_exists($this->outputdir) || is_file($this->outputdir)) {
            mkdir($this->outputdir) or die("Can't create the cache directory");
        } else {
            if (file_exists($this->outputdir . "index.php")) {
                unlink($this->outputdir . "index.php");
            }
        }
    }

    /**
     * Write an individual chunk of the manual
     *
     * @param string   $id     ID of the chunk
     * @param resource $stream Stream containing the contents of the chunk
     *
     * @return void
     */
    public function writeChunk($id, $stream)
    {
        rewind($stream);

        // Create random filename when the chunk doesn't have an ID
        if ($id === null) {
            $filename    = tempnam($this->outputdir, 'phd');
            $newfilename = $this->outputdir . basename($filename, '.tmp') . '.' . $this->ext;
            if (rename($filename, $newfilename)) {
                $filename = basename($filename, '.tmp');
            } else {
                throw new Exception("Cannot rename $filename to $newfilename");
            }
            trigger_error("Chunk without an ID found, TOC will NOT work. Wrote content to $newfilename.", E_USER_WARNING);
        } else {
            $filename = $id;
        }
        $filename .= '.' . $this->ext;

        $contents = $this->cleanHtml(stream_get_contents($stream));

        file_put_contents($this->outputdir . $filename, $this->header($id));
        file_put_contents($this->outputdir . $filename, $contents, FILE_APPEND);
        file_put_contents($this->outputdir . $filename, $this->footer($id), FILE_APPEND);

        v("Wrote %s", $this->outputdir . $filename, VERBOSE_CHUNK_WRITING);
    }

    /**
     * Append data to the streams.
     *
     * @param string                                       $data    Data to write
     * @param PhDReader::CLOSE_CHUNK|PhDReader::OPEN_CHUNK $isChunk constant
     *
     * @return int|false
     */
    public function appendData($data, $isChunk)
    {
        switch($isChunk) {
        case PhDReader::CLOSE_CHUNK:
            $id = $this->CURRENT_ID;

            $stream = array_pop($this->streams);
            $retval = fwrite($stream, $data);
            $this->writeChunk($id, $stream);
            fclose($stream);

            $this->CURRENT_ID = null;
            return $retval;
            break;

        case PhDReader::OPEN_CHUNK:
            $this->streams[] = fopen("php://temp/maxmemory", "r+");

        default:
            $stream = end($this->streams);
            $retval = fwrite($stream, $data);
            return $retval;
        }
    }

    /**
     * Add the header to this file.
     *
     * @param string $id The id of this chunk
     *
     * @return string
     */
    public function header($id)
    {
        $ext = "." . $this->ext;

        $parent = PhDHelper::getParent($id);

        if (!$parent || $parent == "ROOT")
            return '<?php
sendManualHeaders("UTF-8","en");
setupNavigation(array(
  "home" => array("index.php", "PEAR Manual"),
  "prev" => array("#", ""),
  "next" => array("#", ""),
  "up"   => array("#", ""),
  "toc"  => array(
    array("#", ""))));
manualHeader("index.php", "PEAR Manual");
?>
';

        // Fetch the siblings information
        $toc = array();
        $siblings = PhDHelper::getChildren($parent);
        foreach ($siblings as $sibling => $array) {
            $toc[] = array($sibling.$ext, empty($array["sdesc"]) ? $array["ldesc"] : $array["sdesc"]);
        }

        // Build the PEAR navigation table
        $nav = array(
            'home' => array('index' . $ext, 'PEAR Manual'),
            'prev' => $this->createPrev($id, $parent, $siblings),
            'next' => $this->createNext($id, $parent, $siblings),
            'up'   => array($this->getFilename($parent).$ext, PhDHelper::getDescription($parent, true)),
            'toc'  => $toc
        );
        return "<?php \n" .
            "sendManualHeaders(\"UTF-8\", \"{$this->lang}\");\n" .
            "setupNavigation(" . var_export($nav, true) . ");\n" .
            'manualHeader("'
                . $this->getFilename($id).$ext . '", '
                . var_export(PhDHelper::getDescription($id, true), true)
            . ');' . "\n" .
            "?>\n";
    }

    /**
     * Create the footer for the given page id and return it.
     *
     * In this instance, we return raw php with the pearweb manual footer call.
     *
     * @param string $id Page ID
     *
     * @return string Footer code
     */
    public function footer($id)
    {
        $ext = '.' . $this->ext;
        $parent = PhDHelper::getParent($id);

        return '<?php manualFooter("'
            . $this->getFilename($id) . $ext . '", '
            . var_export(PhDHelper::getDescription($id, true), true)
            . '); ?>';
    }

    /**
     * Create the previous page link information
     *
     * @param string $id       ID of the page
     * @param string $parent   ID of the parent element
     * @param array  $siblings array of siblings
     *
     * @return array(0=>filename,1=>description)
     */
    protected function createPrev($id, $parent, $siblings)
    {
        if (!isset($siblings[$id]) || $parent == 'ROOT') {
            return array(null, null);
        }
        $ext = '.' .$this->ext;

        // Seek to $id
        while (list($tmp,) = each($siblings)) {
            if ($tmp == $id) {
                // Set the internal pointer back to $id
                if (prev($siblings) === false) {
                    end($siblings);
                }
                break;
            }
        }
        $tmp = prev($siblings);
        if ($tmp) {
            while (!empty($tmp["children"])) {
                $tmp = end($tmp["children"]);
            }
            return array(
                $tmp["filename"].$ext,
                htmlspecialchars(empty($tmp["sdesc"]) ? $tmp["ldesc"] : $tmp["sdesc"])
            );
            break;
        }

        return array(PhDHelper::getFilename($parent).$ext, PhDHelper::getDescription($parent, false));
    }

    /**
     * Create the next page link information
     *
     * @param string $id       ID of the page
     * @param string $parent   ID of the parent element
     * @param array  $siblings array of siblings
     *
     * @return array(0=>filename,1=>description)
     */
    protected function createNext($id, $parent, $siblings)
    {
        $ext = '.' .$this->ext;
        $next = array(null, null);
        // {{{ Create the "next" link
        if (!empty($siblings[$id]["children"])) {
            $tmp = reset($siblings[$id]["children"]);
            return array(
                $tmp["filename"].$ext,
                htmlspecialchars(empty($tmp["sdesc"]) ? $tmp["ldesc"] : $tmp["sdesc"])
            );
        }
        do {
            if (!isset($siblings[$id])) {
                break;
            }

            // Seek to $id
            while (list($tmp,) = each($siblings)) {
                if ($tmp == $id) {
                    break;
                }
            }

            $tmp = current($siblings);
            prev($siblings); // Reset the internal pointer to previous pos
            if ($tmp) {
                $next = array(
                    $tmp["filename"].$ext,
                    htmlspecialchars(empty($tmp["sdesc"]) ? $tmp["ldesc"] : $tmp["sdesc"])
                );
                break;
            }

            // We are the end element in this chapter
            $grandpa = PhDHelper::getParent($parent);
            if (!$grandpa || $grandpa == "ROOT") {
                // There is no next relative
                break;
            }

            $siblings = PhDHelper::getChildren($grandpa);
            $id       = $parent;
            $parent   = $grandpa;
        } while (true);
        return $next;
    }

    /**
     * Destructor - if guide.php exists, this must be the index file copy it over.
     */
    public function __destruct()
    {
        if (file_exists($this->outputdir . "guide.php") && !file_exists($this->outputdir . "index.php")) {
            copy($this->outputdir . "guide.php", $this->outputdir . "index.php");
        }
    }

    /**
     * use for formatting a Q&A set - is this even used?
     *
     * @param bool         $open  if a chunk is open we should append to
     * @param unknown_type $name  The name
     * @param unknown_type $attrs attributes
     *
     * @return string
     */
    public function format_qandaset($open, $name, $attrs)
    {
        if ($open) {
            $this->cchunk["qandaentry"] = array();
            $this->appendData(null, PhDReader::OPEN_CHUNK);
            return '';
        }

        $stream = array_pop($this->streams);
        rewind($stream);
        return parent::qandaset($stream);
    }

}