~ubuntu-branches/ubuntu/saucy/mediawiki-extensions/saucy

« back to all changes in this revision

Viewing changes to dist/mediawiki-extensions-fckeditor/usr/share/mediawiki-extensions/fckeditor/mw14/Parser_OldPP.body.php

  • Committer: Bazaar Package Importer
  • Author(s): Romain Beauxis
  • Date: 2010-05-04 15:13:35 UTC
  • mfrom: (0.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100504151335-54qeucg3ec108q28
Tags: 2.2
* Added Replaces:/Conflicts: to allow a proper upgrade.
Closes: #580066
* Fixed package descriptions.
Closes: #579667
* Patched mediawiki-extensions-fckeditor to make it work with
  php 5.3. The fix may not be perfect but at least it work.
  Not closing the bug (#579822) for now..

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class Parser_OldPP extends Parser {
 
4
    /**
 
5
     * parse any parentheses in format ((title|part|part))
 
6
     * and call callbacks to get a replacement text for any found piece
 
7
     *
 
8
     * @param string $text The text to parse
 
9
     * @param array $callbacks rules in form:
 
10
     *     '{' => array(                # opening parentheses
 
11
     *                    'end' => '}',   # closing parentheses
 
12
     *                    'cb' => array(2 => callback,    # replacement callback to call if {{..}} is found
 
13
     *                                  3 => callback     # replacement callback to call if {{{..}}} is found
 
14
     *                                  )
 
15
     *                    )
 
16
     *                     'min' => 2,     # Minimum parenthesis count in cb
 
17
     *                     'max' => 3,     # Maximum parenthesis count in cb
 
18
     * @private
 
19
     */
 
20
    function replace_callback ($text, $callbacks) {
 
21
        wfProfileIn( __METHOD__ );
 
22
        $openingBraceStack = array();    # this array will hold a stack of parentheses which are not closed yet
 
23
        $lastOpeningBrace = -1;            # last not closed parentheses
 
24
 
 
25
        $validOpeningBraces = implode( '', array_keys( $callbacks ) );
 
26
 
 
27
        $i = 0;
 
28
        while ( $i < strlen( $text ) ) {
 
29
            # Find next opening brace, closing brace or pipe
 
30
            if ( $lastOpeningBrace == -1 ) {
 
31
                $currentClosing = '';
 
32
                $search = $validOpeningBraces;
 
33
            } else {
 
34
                $currentClosing = $openingBraceStack[$lastOpeningBrace]['braceEnd'];
 
35
                $search = $validOpeningBraces . '|' . $currentClosing;
 
36
            }
 
37
            $rule = null;
 
38
            $i += strcspn( $text, $search, $i );
 
39
            if ( $i < strlen( $text ) ) {
 
40
                if ( $text[$i] == '|' ) {
 
41
                    $found = 'pipe';
 
42
                } elseif ( $text[$i] == $currentClosing ) {
 
43
                    $found = 'close';
 
44
                } elseif ( isset( $callbacks[$text[$i]] ) ) {
 
45
                    $found = 'open';
 
46
                    $rule = $callbacks[$text[$i]];
 
47
                } else {
 
48
                    # Some versions of PHP have a strcspn which stops on null characters
 
49
                    # Ignore and continue
 
50
                    ++$i;
 
51
                    continue;
 
52
                }
 
53
            } else {
 
54
                # All done
 
55
                break;
 
56
            }
 
57
 
 
58
            if ( $found == 'open' ) {
 
59
                # found opening brace, let's add it to parentheses stack
 
60
                $piece = array('brace' => $text[$i],
 
61
                               'braceEnd' => $rule['end'],
 
62
                               'title' => '',
 
63
                               'parts' => null);
 
64
 
 
65
                # count opening brace characters
 
66
                $piece['count'] = strspn( $text, $piece['brace'], $i );
 
67
                $piece['startAt'] = $piece['partStart'] = $i + $piece['count'];
 
68
                $i += $piece['count'];
 
69
 
 
70
                # we need to add to stack only if opening brace count is enough for one of the rules
 
71
                if ( $piece['count'] >= $rule['min'] ) {
 
72
                    $lastOpeningBrace ++;
 
73
                    $openingBraceStack[$lastOpeningBrace] = $piece;
 
74
                }
 
75
            } elseif ( $found == 'close' ) {
 
76
                # lets check if it is enough characters for closing brace
 
77
                $maxCount = $openingBraceStack[$lastOpeningBrace]['count'];
 
78
                $count = strspn( $text, $text[$i], $i, $maxCount );
 
79
 
 
80
                # check for maximum matching characters (if there are 5 closing
 
81
                # characters, we will probably need only 3 - depending on the rules)
 
82
                $matchingCount = 0;
 
83
                $matchingCallback = null;
 
84
                $cbType = $callbacks[$openingBraceStack[$lastOpeningBrace]['brace']];
 
85
                if ( $count > $cbType['max'] ) {
 
86
                    # The specified maximum exists in the callback array, unless the caller
 
87
                    # has made an error
 
88
                    $matchingCount = $cbType['max'];
 
89
                } else {
 
90
                    # Count is less than the maximum
 
91
                    # Skip any gaps in the callback array to find the true largest match
 
92
                    # Need to use array_key_exists not isset because the callback can be null
 
93
                    $matchingCount = $count;
 
94
                    while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $cbType['cb'] ) ) {
 
95
                        --$matchingCount;
 
96
                    }
 
97
                }
 
98
 
 
99
                if ($matchingCount <= 0) {
 
100
                    $i += $count;
 
101
                    continue;
 
102
                }
 
103
                $matchingCallback = $cbType['cb'][$matchingCount];
 
104
 
 
105
                # let's set a title or last part (if '|' was found)
 
106
                if (null === $openingBraceStack[$lastOpeningBrace]['parts']) {
 
107
                    $openingBraceStack[$lastOpeningBrace]['title'] =
 
108
                        substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'],
 
109
                        $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
 
110
                } else {
 
111
                    $openingBraceStack[$lastOpeningBrace]['parts'][] =
 
112
                        substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'],
 
113
                        $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
 
114
                }
 
115
 
 
116
                $pieceStart = $openingBraceStack[$lastOpeningBrace]['startAt'] - $matchingCount;
 
117
                $pieceEnd = $i + $matchingCount;
 
118
 
 
119
                if( is_callable( $matchingCallback ) ) {
 
120
                    $cbArgs = array (
 
121
                                     'text' => substr($text, $pieceStart, $pieceEnd - $pieceStart),
 
122
                                     'title' => trim($openingBraceStack[$lastOpeningBrace]['title']),
 
123
                                     'parts' => $openingBraceStack[$lastOpeningBrace]['parts'],
 
124
                                     'lineStart' => (($pieceStart > 0) && ($text[$pieceStart-1] == "\n")),
 
125
                                     );
 
126
                    # finally we can call a user callback and replace piece of text
 
127
                    $replaceWith = call_user_func( $matchingCallback, $cbArgs );
 
128
                    $text = substr($text, 0, $pieceStart) . $replaceWith . substr($text, $pieceEnd);
 
129
                    $i = $pieceStart + strlen($replaceWith);
 
130
                } else {
 
131
                    # null value for callback means that parentheses should be parsed, but not replaced
 
132
                    $i += $matchingCount;
 
133
                }
 
134
 
 
135
                # reset last opening parentheses, but keep it in case there are unused characters
 
136
                $piece = array('brace' => $openingBraceStack[$lastOpeningBrace]['brace'],
 
137
                               'braceEnd' => $openingBraceStack[$lastOpeningBrace]['braceEnd'],
 
138
                               'count' => $openingBraceStack[$lastOpeningBrace]['count'],
 
139
                               'title' => '',
 
140
                               'parts' => null,
 
141
                               'startAt' => $openingBraceStack[$lastOpeningBrace]['startAt']);
 
142
                $openingBraceStack[$lastOpeningBrace--] = null;
 
143
 
 
144
                if ($matchingCount < $piece['count']) {
 
145
                    $piece['count'] -= $matchingCount;
 
146
                    $piece['startAt'] -= $matchingCount;
 
147
                    $piece['partStart'] = $piece['startAt'];
 
148
                    # do we still qualify for any callback with remaining count?
 
149
                    $currentCbList = $callbacks[$piece['brace']]['cb'];
 
150
                    while ( $piece['count'] ) {
 
151
                        if ( array_key_exists( $piece['count'], $currentCbList ) ) {
 
152
                            $lastOpeningBrace++;
 
153
                            $openingBraceStack[$lastOpeningBrace] = $piece;
 
154
                            break;
 
155
                        }
 
156
                        --$piece['count'];
 
157
                    }
 
158
                }
 
159
            } elseif ( $found == 'pipe' ) {
 
160
                # lets set a title if it is a first separator, or next part otherwise
 
161
                if (null === $openingBraceStack[$lastOpeningBrace]['parts']) {
 
162
                    $openingBraceStack[$lastOpeningBrace]['title'] =
 
163
                        substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'],
 
164
                        $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
 
165
                    $openingBraceStack[$lastOpeningBrace]['parts'] = array();
 
166
                } else {
 
167
                    $openingBraceStack[$lastOpeningBrace]['parts'][] =
 
168
                        substr($text, $openingBraceStack[$lastOpeningBrace]['partStart'],
 
169
                        $i - $openingBraceStack[$lastOpeningBrace]['partStart']);
 
170
                }
 
171
                $openingBraceStack[$lastOpeningBrace]['partStart'] = ++$i;
 
172
            }
 
173
        }
 
174
 
 
175
        wfProfileOut( __METHOD__ );
 
176
        return $text;
 
177
    }
 
178
}