| | 26 | function __construct() { |
| | 27 | global $wgParser; |
| | 28 | parent::Parser(); |
| | 29 | |
| | 30 | foreach ($wgParser->getTags() as $h) { |
| | 31 | if (!in_array($h, array("pre"))) { |
| | 32 | $this->setHook($h, array($this, "fck_genericTagHook")); |
| | 33 | } |
| | 34 | } |
| | 35 | } |
| | 36 | |
| | 37 | /** |
| | 38 | * Callback function for custom tags |
| | 39 | * |
| | 40 | * @param string $str Input |
| | 41 | * @param array $argv Arguments |
| | 42 | * @return string |
| | 43 | */ |
| | 44 | function fck_genericTagHook( $str, $argv, $parser ) { |
| | 45 | if (empty($argv)) { |
| | 46 | $ret = "<span class=\"fck_mw_".$this->fck_mw_taghook."\" _fck_mw_customtag=\"true\" _fck_mw_tagname=\"".$this->fck_mw_taghook."\">"; |
| | 47 | } |
| | 48 | else { |
| | 49 | $ret = "<span class=\"fck_mw_".$this->fck_mw_taghook."\" _fck_mw_customtag=\"true\" _fck_mw_tagname=\"".$this->fck_mw_taghook."\""; |
| | 50 | foreach ($argv as $key=>$value) { |
| | 51 | $ret .= " ".$key."=\"".$value."\""; |
| | 52 | } |
| | 53 | $ret .=">"; |
| | 54 | } |
| | 55 | if (is_null($str)) { |
| | 56 | $ret = substr($ret, 0, -1) . " />"; |
| | 57 | } |
| | 58 | else { |
| | 59 | $ret .= htmlspecialchars($str); |
| | 60 | $ret .= "</span>"; |
| | 61 | } |
| | 62 | return $ret; |
| | 63 | } |
| | 64 | |
| | 65 | /** |
| | 66 | * Callback function for custom tags |
| | 67 | * |
| | 68 | * @param string $tagName tag name, eg. nowiki, math |
| | 69 | * @param string $str Input |
| | 70 | * @param array $argv Arguments |
| | 71 | * @return string |
| | 72 | */ |
| | 73 | function fck_wikiTag( $tagName, $str, $argv = array()) { |
| | 74 | if (empty($argv)) { |
| | 75 | $ret = "<span class=\"fck_mw_".$tagName."\">"; |
| | 76 | } |
| | 77 | else { |
| | 78 | $ret = "<span class=\"fck_mw_".$tagName."\""; |
| | 79 | foreach ($argv as $key=>$value) { |
| | 80 | $ret .= " ".$key."=\"".$value."\""; |
| | 81 | } |
| | 82 | $ret .=">"; |
| | 83 | } |
| | 84 | if (is_null($str)) { |
| | 85 | $ret = substr($ret, 0, -1) . " />"; |
| | 86 | } |
| | 87 | else { |
| | 88 | $ret .= htmlspecialchars($str); |
| | 89 | $ret .= "</span>"; |
| | 90 | } |
| | 91 | return $ret; |
| | 92 | } |
| | 93 | |
| | 94 | /** |
| | 95 | * Strips and renders nowiki, pre, math, hiero |
| | 96 | * If $render is set, performs necessary rendering operations on plugins |
| | 97 | * Returns the text, and fills an array with data needed in unstrip() |
| | 98 | * |
| | 99 | * @param StripState $state |
| | 100 | * |
| | 101 | * @param bool $stripcomments when set, HTML comments <!-- like this --> |
| | 102 | * will be stripped in addition to other tags. This is important |
| | 103 | * for section editing, where these comments cause confusion when |
| | 104 | * counting the sections in the wikisource |
| | 105 | * |
| | 106 | * @param array dontstrip contains tags which should not be stripped; |
| | 107 | * used to prevent stipping of <gallery> when saving (fixes bug 2700) |
| | 108 | * |
| | 109 | * @private |
| | 110 | */ |
| | 111 | function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) { |
| | 112 | global $wgContLang; |
| | 113 | |
| | 114 | wfProfileIn( __METHOD__ ); |
| | 115 | $render = ($this->mOutputType == OT_HTML); |
| | 116 | |
| | 117 | $uniq_prefix = $this->mUniqPrefix; |
| | 118 | $commentState = new ReplacementArray; |
| | 119 | $nowikiItems = array(); |
| | 120 | $generalItems = array(); |
| | 121 | |
| | 122 | $elements = array_merge( |
| | 123 | array( 'nowiki', 'gallery' ), |
| | 124 | array_keys( $this->mTagHooks ) ); |
| | 125 | global $wgRawHtml; |
| | 126 | if( $wgRawHtml ) { |
| | 127 | $elements[] = 'html'; |
| | 128 | } |
| | 129 | if( $this->mOptions->getUseTeX() ) { |
| | 130 | $elements[] = 'math'; |
| | 131 | } |
| | 132 | |
| | 133 | # Removing $dontstrip tags from $elements list (currently only 'gallery', fixing bug 2700) |
| | 134 | foreach ( $elements AS $k => $v ) { |
| | 135 | if ( !in_array ( $v , $dontstrip ) ) continue; |
| | 136 | unset ( $elements[$k] ); |
| | 137 | } |
| | 138 | |
| | 139 | $matches = array(); |
| | 140 | $text = Parser::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix ); |
| | 141 | |
| | 142 | foreach( $matches as $marker => $data ) { |
| | 143 | list( $element, $content, $params, $tag ) = $data; |
| | 144 | if( $render ) { |
| | 145 | $tagName = strtolower( $element ); |
| | 146 | wfProfileIn( __METHOD__."-render-$tagName" ); |
| | 147 | switch( $tagName ) { |
| | 148 | case '!--': |
| | 149 | // Comment |
| | 150 | if( substr( $tag, -3 ) == '-->' ) { |
| | 151 | $output = $tag; |
| | 152 | } else { |
| | 153 | // Unclosed comment in input. |
| | 154 | // Close it so later stripping can remove it |
| | 155 | $output = "$tag-->"; |
| | 156 | } |
| | 157 | break; |
| | 158 | case 'html': |
| | 159 | if( $wgRawHtml ) { |
| | 160 | $output = $content; |
| | 161 | break; |
| | 162 | } |
| | 163 | // Shouldn't happen otherwise. :) |
| | 164 | case 'nowiki': |
| | 165 | $output = $this->fck_wikiTag('nowiki', $content, $params); //required by FCKeditor |
| | 166 | break; |
| | 167 | case 'math': |
| | 168 | $output = $wgContLang->armourMath( MathRenderer::renderMath( $content ) ); |
| | 169 | break; |
| | 170 | case 'gallery': |
| | 171 | $output = $this->renderImageGallery( $content, $params ); |
| | 172 | break; |
| | 173 | default: |
| | 174 | if( isset( $this->mTagHooks[$tagName] ) ) { |
| | 175 | $this->fck_mw_taghook = $tagName; //required by FCKeditor |
| | 176 | $output = call_user_func_array( $this->mTagHooks[$tagName], |
| | 177 | array( $content, $params, $this ) ); |
| | 178 | } else { |
| | 179 | throw new MWException( "Invalid call hook $element" ); |
| | 180 | } |
| | 181 | } |
| | 182 | wfProfileOut( __METHOD__."-render-$tagName" ); |
| | 183 | } else { |
| | 184 | // Just stripping tags; keep the source |
| | 185 | $output = $tag; |
| | 186 | } |
| | 187 | |
| | 188 | // Unstrip the output, to support recursive strip() calls |
| | 189 | $output = $state->unstripBoth( $output ); |
| | 190 | |
| | 191 | if( !$stripcomments && $element == '!--' ) { |
| | 192 | $commentState->setPair( $marker, $output ); |
| | 193 | } elseif ( $element == 'html' || $element == 'nowiki' ) { |
| | 194 | $nowikiItems[$marker] = $output; |
| | 195 | } else { |
| | 196 | $generalItems[$marker] = $output; |
| | 197 | } |
| | 198 | } |
| | 199 | # Add the new items to the state |
| | 200 | # We do this after the loop instead of during it to avoid slowing |
| | 201 | # down the recursive unstrip |
| | 202 | $state->nowiki->mergeArray( $nowikiItems ); |
| | 203 | $state->general->mergeArray( $generalItems ); |
| | 204 | |
| | 205 | # Unstrip comments unless explicitly told otherwise. |
| | 206 | # (The comments are always stripped prior to this point, so as to |
| | 207 | # not invoke any extension tags / parser hooks contained within |
| | 208 | # a comment.) |
| | 209 | if ( !$stripcomments ) { |
| | 210 | // Put them all back and forget them |
| | 211 | $text = $commentState->replace( $text ); |
| | 212 | } |
| | 213 | |
| | 214 | wfProfileOut( __METHOD__ ); |
| | 215 | return $text; |
| | 216 | } |
| | 217 | |
| 123 | | function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) { |
| 124 | | $dontstrip[] = "gallery"; |
| 125 | | //$dontstrip[] = "nowiki"; |
| 126 | | //$dontstrip[] = "noinclude"; |
| 127 | | //$dontstrip[] = "includeonly"; |
| 128 | | |
| 129 | | return parent::strip($text, $state, $stripcomments , $dontstrip ); |
| 130 | | } |
| 131 | | /** |
| 132 | | * Callback function for <ref> |
| 133 | | * |
| 134 | | * @param string $str Input |
| 135 | | * @param array $argv Arguments |
| 136 | | * @return string |
| 137 | | */ |
| 138 | | function ref( $str, $argv, $parser ) { |
| 139 | | if (empty($argv)) { |
| 140 | | $ret = "<span class=\"fck_mw_ref\">"; |
| 141 | | } |
| 142 | | else { |
| 143 | | $ret = "<span class=\"fck_mw_ref\""; |
| 144 | | foreach ($argv as $key=>$value) { |
| 145 | | $ret .= " ".$key."=\"".$value."\""; |
| 146 | | } |
| 147 | | $ret .=">"; |
| 148 | | } |
| 149 | | if (is_null($str)) { |
| 150 | | $ret = substr($ret, 0, -1) . " />"; |
| 151 | | } |
| 152 | | else { |
| 153 | | $ret .= htmlspecialchars($str); |
| 154 | | $ret .= "</span>"; |
| 155 | | } |
| 156 | | return $ret; |
| 157 | | } |
| 158 | | /** |
| 159 | | * Callback function for <references> |
| 160 | | * |
| 161 | | * @param string $str Input |
| 162 | | * @param array $argv Arguments |
| 163 | | * @return string |
| 164 | | */ |
| 165 | | function references( $str, $argv, $parser ) { |
| 166 | | if (empty($argv)) { |
| 167 | | $ret = "<span class=\"fck_mw_references\">"; |
| 168 | | } |
| 169 | | else { |
| 170 | | $ret = "<span class=\"fck_mw_references\""; |
| 171 | | foreach ($argv as $key=>$value) { |
| 172 | | $ret .= " ".$key."=\"".$value."\""; |
| 173 | | } |
| 174 | | $ret .=">"; |
| 175 | | } |
| 176 | | if (is_null($str)) { |
| 177 | | $ret = substr($ret, 0, -1) . " />"; |
| 178 | | } |
| 179 | | else { |
| 180 | | $ret .= htmlspecialchars($str); |
| 181 | | $ret .= "</span>"; |
| 182 | | } |
| 183 | | |
| 184 | | return $ret; |
| 185 | | } |