File: phpfunctions.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 (177 lines) | stat: -rw-r--r-- 5,734 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
<?php

class phpfunctions extends PhDTheme {
    const OPEN_CHUNK = 0x01;
    const CLOSE_CHUNK = 0x02;
    const OPENED_CHUNK = 0x03;

    protected $elementmap = array(
        'set'                   => 'format_set',
        'refentry'              => 'format_refentry',
        'refname'               => 'format_refname',
    );

    protected $textmap =        array(
        'pubdate'               => 'format_pubdate',
        'refname'               => 'format_refname_text',
        'title'                 => array(
            /* DEFAULT */          false,
            'set'               => array(
                /* DEFAULT */      'format_bookname',
                'set'           => false,
            ),
        ),
    );

    public $role        = false;

    /**
    * We do generate multiple chunks
    *
    * @var boolean
    */
    protected $chunked = true;

    protected $CURRENT_ID = "";

    /* Current Chunk settings */
    protected $cchunk          = array();
    /* Default Chunk settings */
    protected $dchunk          = array(
        "funcname"                  => array(),
        "firstrefname"              => true,
    );

    protected $isChunk = false;

    /* Common properties for all functions pages */
    protected $bookName = "";
    protected $date = "";
    protected $outputdir = "";
    protected $isFunctionRefSet = false;

    public function __construct(array $IDs, array $filenames, $format = "man3", $chunked = true) {
        parent::__construct($IDs);
        $this->format = $format;
        $this->outputdir = $GLOBALS['OPTIONS']['output_dir'] . $this->format . DIRECTORY_SEPARATOR;
        if(!file_exists($this->outputdir) || is_file($this->outputdir)) mkdir($this->outputdir) or die("Can't create the cache directory");
    }

    public function header($index) {
        return ".TH " . strtoupper($this->cchunk["funcname"][$index]) . " 3 \"" . $this->date . "\" \"PHP Documentation Group\" \"" . $this->bookName . "\"";
    }

    public function writeChunk($stream) {
        if (!isset($this->cchunk["funcname"][0]))
             return;
        $index = 0;
        rewind($stream);

        $filename = $this->cchunk["funcname"][$index] . '.3.gz';
        $gzfile = gzopen($this->outputdir . $filename, "w9");

        gzwrite($gzfile, $this->header($index));
        gzwrite($gzfile, stream_get_contents($stream));
        gzclose($gzfile);
        v("Wrote %s", $this->outputdir . $filename, VERBOSE_CHUNK_WRITING);

        while(isset($this->cchunk["funcname"][++$index])) {
            $filename = $this->cchunk["funcname"][$index] . '.3.gz';
            rewind($stream);
            // Replace the default function name by the alternative one
            $content = preg_replace('/'.$this->cchunk["funcname"][0].'/',
                $this->cchunk["funcname"][$index], stream_get_contents($stream), 1);

            $gzfile = gzopen($this->outputdir . $filename, "w9");

            gzwrite($gzfile, $this->header($index));
            gzwrite($gzfile, $content);
            gzclose($gzfile);

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

    public function appendData($data, $isChunk) {
        if (!$this->isFunctionRefSet)
            return 0;
        switch($this->isChunk) {
        case self::CLOSE_CHUNK:
            $stream = array_pop($this->streams);
            $retval = (trim($data) === "") ? false : fwrite($stream, $data);
            $this->writeChunk($stream);
            fclose($stream);

            $this->isChunk = null;
            $this->cchunk = array();
            return $retval;
            break;

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

        case self::OPENED_CHUNK:
            $stream = end($this->streams);
            // Remove whitespace nodes
            $retval = ($data != "\n" && trim($data) === "") ? false : fwrite($stream, $data);
            return $retval;
        default:
            return 0;
        }
    }

    public function format_bookname($value, $tag) {
        $this->bookName = $value;
        return false;
    }

    public function format_pubdate($value, $tag) {
        $this->date = $value;
        return false;
    }

    public function format_set($open, $name, $attrs, $props) {
        if (isset($attrs[PhDReader::XMLNS_XML]["id"]) && $attrs[PhDReader::XMLNS_XML]["id"] == "funcref") {
            $this->isFunctionRefSet = $open;
        }
        return false;
    }

    public function format_refentry($open, $name, $attrs, $props) {
        if (!$this->isFunctionRefSet)
            return false;
        if ($open) {
            $this->isChunk = self::OPEN_CHUNK;
            $this->format->newChunk();
            $this->cchunk = $this->dchunk;
        } else {
            $this->isChunk = self::CLOSE_CHUNK;
            $this->format->closeChunk();
        }
        return false;
    }

    public function format_refname($open, $name, $attrs, $props) {
        if ($open) {
            return (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) ? false : "";
        } else {
            if (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) {
                $this->cchunk["firstrefname"] = false;
                return false;
            }
            return "";
        }
    }

    public function format_refname_text($value, $tag) {
        $this->cchunk["funcname"][] = $this->format->toValidName(trim($value));
        if (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) {
            return false;
        }
        return "";
    }



}