summaryrefslogtreecommitdiffstats
path: root/extensions/TinyMCE_MW.php
blob: d208fa19c5428b8e7083450c750d02a500bfc05b (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/*
TinyMCE_MW.php - MediaWiki extension - version 0.5.4
        by Joseph P. Socoloski III
        If you already have pages written in Mediawiki wikitext, this extension enables
        Moxiecode's TinyMCE and does not break Mediawiki wikitext. Also, TinyMCE_MW 
        has a new 'msword' configuration theme.  msword follows the MS Office 2003
        toolbar layout. Call TinyMCE's simple, advanced and a built-in msword theme from 
        LocalSettings.php.  TinyMCE_MW was built and tested on Mediawiki-1.10.0.  
        Successfully tested CategoryTree extension for compatibility with new tags.
		-Improved custom tag parsing for repeat and categorytree; added sql2wiki

        References:     http://meta.wikimedia.org/wiki/Image:Wiki-refcard.png
                                http://meta.wikimedia.org/wiki/Cheatsheet
                                http://meta.wikimedia.org/wiki/Help:HTML_in_wikitext
                                http://wiki.moxiecode.com/index.php/TinyMCE:Index
                                http://www.mediawiki.org/wiki/Extension:CategoryTree
                                http://www.mediawiki.org/wiki/Manual:Parameters_to_index.php
                                
        NOTE:To change the default font and size for TinyMCE, add these two lines to your
        theme's editor_content.css body{} section:
        font-family: Arial;
        font-size: 14px;
        NOTE:To decrease the space between lines after a carriage return place this line 
        to your theme's editor_content.css:
        p {margin: 0; padding: 0;}           
BUGS:   - Does not support Wikitext Bullet list
                - Does not support Wikitext Numbered list
                - Does not support Wikitext Redirect to another article
                - Does not support Wikitext Tables
TODO:   - Enable Ajax usage
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/


if( !defined( 'MEDIAWIKI' ) ) {
  die();
}

$wgExtensionCredits['other'][] = array(

        "name" => "TinyMCE MW extension",
        "author" => "Joseph P. Socoloski III",
        "version" => "0.5.4",
        "url" => "http://www.mediawiki.org/wiki/Extension:TinyMCE_MW",
        "description" => "Easily implement Moxiecode's TinyMCE into MediaWiki"
 );

# REGISTER HOOKS
$wgHooks['ParserBeforeStrip'][]                 = 'wfTinymceParserCut';
$wgHooks['ParserAfterTidy'][]                   = 'wfTinymceParserPaste';
$wgHooks['ArticleAfterFetchContent'][] 			= 'wfCheckBeforeEdit';
$wgHooks['EditPage::showEditForm:initial'][]    = 'wfTinymceAddScript';

##Process the raw wikidb code before any internal processing is applied
function wfTinymceParserCut ($q, $text) {

        global $wgTitle;
        global $wgTempText, $wgUseTinymce;
        
        $ns_allowed = true;
        $ns = $wgTitle->getNamespace();
        
        #if (in_array($ns, $wgexcludedNamespaces)) $ns_allowed = false;
        $wgTempText = $text;#get text
        #$text = "";
        
        return true;
}

##Process the wgTempText code (wikitext and html) and reformat it into html friendly $text
function wfTinymceParserPaste ($q, $text) {

        global $wgOut, $wgTitle, $wgParser;
        global $wgTempText, $wgTinymceToken, $wgUseTinymce;
        
        $List = array();
        
        $ns_allowed = true;
        $ns = $wgTitle->getNamespace();
        
        # TinyMCE can NOT be enabled for any pages that have data tags
        if ($ns_allowed and $wgUseTinymce) {
        
                $tinymcetext = $wgTempText; 
                
                ## EXTENSION TAGS | ADD HERE ##
				#Custom tags may ONLY be entered in the regular editor NOT the HTML Source editor
				#Allow_inputbox_tags
				while (preg_match("|&lt;inputbox&gt;(.*?)&lt;/inputbox&gt;|is", $tinymcetext, $a)) {
				 	$r = preg_replace("| |i", "", $a[0]);#erase all the whitespace
				 	$r = preg_replace("|</p><p>|i", "<br/>", $a[0]);#sometimes </p><p> instead of br
					$r = preg_replace("|<br.*?>|i", "\n", $r);
					$tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);#htmlentities()
				}
				#Allow_repeat_tags
				while (preg_match("|&lt;repeat.*?&gt;(.*?)&lt;/repeat&gt;|is", $tinymcetext, $a)) {
				 	$r = preg_replace("| |i", "", $a[0]);#erase all the whitespace
				 	$r = preg_replace("|</p><p>|i", "<br/>", $a[0]);#sometimes </p><p> instead of br
					 #<repeat table="Service_Center_Table" sort="sc_num"></repeat>
					$r = preg_replace("|<br.*?>|i", "\n", $r);
					$tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);#htmlentities()
				}
				#Allow_categorytree_tags
				while (preg_match("|&lt;categorytree.*?&gt;(.*?)&lt;/categorytree&gt;|is", $tinymcetext, $a)) {
				 	$r = preg_replace("| |i", "", $a[0]);#erase all the whitespace
				 	$r = preg_replace("|</p><p>|i", "<br/>", $a[0]);#sometimes </p><p> instead of br
					$r = preg_replace("|<br.*?>|i", "\n", $r);
					$tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);#htmlentities()
				}
				#Allow_includeonlyx_tags
				while (preg_match("|&lt;includeonly&gt;(.*?)&lt;/includeonly&gt;|is", $tinymcetext, $a)) {
				 	$r = preg_replace("| |i", "", $a[0]);#erase all the whitespace
				 	$r = preg_replace("|</p><p>|i", "<br/>", $a[0]);#sometimes </p><p> instead of br
					$r = preg_replace("|<br.*?>|i", "\n", $r);
					$tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);#htmlentities()
				}
				#Allow_repeat_tags
				while (preg_match("|&lt;sql2wiki.*?&gt;(.*?)&lt;/sql2wiki&gt;|is", $tinymcetext, $a)) {
				 	$r = preg_replace("| |i", "", $a[0]);#erase all the whitespace
				 	$r = preg_replace("|</p><p>|i", "<br/>", $a[0]);#sometimes </p><p> instead of br
					 #<repeat table="Service_Center_Table" sort="sc_num"></repeat>
					$r = preg_replace("|<br.*?>|i", "\n", $r);
					$tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);#htmlentities()
				}
                ## EXTENSION TAGS END ##
                
                #Allow_a_tags
                        $i = 0;
                        $ta = md5("aopen");
                        while (preg_match("|(<a.*?>)|i", $tinymcetext, $a)) {
                                $j = $ta."_".md5($i);
                                $List[$j]["content"] = $a[0];
                                $List[$j]["index"] = $j;
                                $tinymcetext = str_replace($a[0], $j, $tinymcetext);
                                $i++;
                        }
                        $i = 0;
                        $ta = md5("aclose");
                        while (preg_match("|(</a>)|i", $tinymcetext, $a)) {
                                $j = $ta."_".md5($i);
                                $List[$j]["content"] = $a[0];
                                $List[$j]["index"] = $j;
                                $tinymcetext = str_replace($a[0], $j, $tinymcetext);
                                $i++;
                        }
        
                #Allow_img_tags
                        $i = 0;
                        $timg = md5("img");
                        while (preg_match("|(<img[^>]*?/>)|i", $tinymcetext, $a)) {
                                $j = $timg."_".md5($i);
                                $List[$j]["content"] = $a[0];
                                $List[$j]["index"] = $j;
                                $tinymcetext = str_replace($a[0], $j, $tinymcetext);
                                $i++;
                        }

                ## MEDIAWIKI WIKITEXT HANDLING ##
                #'''''bold and italic'''''
                        while (preg_match("|'''''.*?'''''|is", $tinymcetext, $a)) {
                                $value = implode(",", $a);
                                $value = str_replace("'''''", "", $value);
                                $r = preg_replace("|'''''.*?'''''|is", "<i><strong>".$value."</strong></i>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                #'''bold'''
                        while (preg_match("|'''.*?'''|is", $tinymcetext, $a)) {
                                $value = implode(",", $a);
                                $value = str_replace("'''", "", $value);
                                $r = preg_replace("|'''.*?'''|is", "<strong>".$value."</strong>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                #''italic''
                        while (preg_match("|''.*?''|is", $tinymcetext, $a)) {
                                $value = implode(",", $a);
                                $value = str_replace("''", "", $value);
                                $r = preg_replace("|''.*?''|is", "<i>".$value."</i>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                        
                #=====level 4=====
                        while (preg_match("|=====.*?=====|is", $tinymcetext, $a)) {
                                $value = implode(",", $a);
                                $value = str_replace("=====", "", $value);
                                $r = preg_replace("|=====.*?=====|is", "<h5>".$value."</h5>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                #====level 3====
                        while (preg_match("|====.*?====|is", $tinymcetext, $a)) {
                                $value = implode(",", $a);
                                $value = str_replace("====", "", $value);
                                $r = preg_replace("|====.*?====|is", "<h4>".$value."</h4>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                #===level 2===
                        while (preg_match("|===.*?===|is", $tinymcetext, $a)) {
                                $value = implode(",", $a);
                                $value = str_replace("===", "", $value);
                                $r = preg_replace("|===.*?===|is", "<h3>".$value."</h3>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                #==heading==
                        while (preg_match("|==.*?==|is", $tinymcetext, $a)) {
                                $value = implode(",", $a);
                                $value = str_replace("==", "", $value);
                                $r = preg_replace("|==.*?==|is", "<h2>".$value."</h2>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                #==heading 1==
                ## Does not support <h1> tags because "|=.*?=|is" grabs too much
                #---- horizontal line
                        while (preg_match("|----|is", $tinymcetext, $a)) {
                                $r = preg_replace("|----|is", "<hr>", $a[0]);
                                $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
                        }
                ## MEDIAWIKI WIKITEXT HANDLING END ##
                
		$tagList = array("pre", "math", "gallery", "nowiki", "html");
		foreach($tagList as $tag) {
			while (preg_match("|&lt;($tag.*?)&gt;(.*?)&lt;/$tag&gt;|is", $tinymcetext, $a)) {	
				$r = preg_replace("|<br.*?>|i", "", $a[0]);
				$r = preg_replace("|&nbsp;|i", " ", $r);
				$tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
			}
		}

		foreach($q->mTagHooks as $tag => $func) {
			while (preg_match("|&lt;($tag.*?)&gt;(.*?)&lt;/$tag&gt;|is", $tinymcetext, $a)) {	
				$r = preg_replace("|<br.*?>|i", "", $a[0]);
				$r = preg_replace("|&nbsp;|i", " ", $r);
				$tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext);
			}
		}
                $state = new StripState;
                $x =& $state;
                $tinymcetext = $q->strip($tinymcetext, $x);
                # optional remove <p></p> 
                #$tinymcetext = preg_replace('/<p[^>]*>/','',$tinymcetext);//Remove the start <p> or <p attr="">
#                $tinymcetext = preg_replace('/<\/p>/', '<br />', $tinymcetext); // Replace the end
				#$tinymcetext = preg_replace('/<\/p>/', '\n', $tinymcetext); // Replace the end
				#$tinymcetext = preg_replace("|<br/>|i", "\n", $tinymcetext);
                $tinymcetext = preg_replace("/<\/?tbody>/i","", $tinymcetext);
                $tinymcetext = preg_replace("/$wgTinymceToken/i","", $tinymcetext); 
                $tinymcetext = Sanitizer::removeHTMLtags( $tinymcetext, array( &$q, 'attributeStripCallback' ) );
                $tinymcetext = $q->replaceVariables($tinymcetext);
                $tinymcetext = $q->stripToc( $tinymcetext );         
                $tinymcetext = $q->replaceInternalLinks( $tinymcetext );
                $tinymcetext = $q->replaceExternalLinks( $tinymcetext );
                $tinymcetext = str_replace($q->mUniqPrefix."NOPARSE", "", $tinymcetext);
                $tinymcetext = $q->doMagicLinks( $tinymcetext );
                $tinymcetext = $q->formatHeadings( $tinymcetext, true );
                $q->replaceLinkHolders( $tinymcetext );
                $tinymcetext = $q->unstripNoWiki( $tinymcetext, $state );
                $tinymcetext = $q->unstrip($tinymcetext, $state);
        
                foreach($List as $item) {
                        $tinymcetext = str_replace($item["index"], $item["content"], $tinymcetext);
                        $i++;
                }
                
                $text = $tinymcetext;
        }
        return true;
}


function wfTinymceAddScript ($q) { 

        global $wgOut, $wgTitle, $wgScriptPath, $wgMyWikiURL; 
        global $wgTempText, $wgTinymceDir, $wgTinymceTheme, $wgExt_valid_elements, $wgUseTinymce;

        $wgTinymceDir = "tinymce";
        $ns_allowed = true;
        $ns = $wgTitle->getNamespace();

        if ($ns_allowed && $wgUseTinymce)
        {
         	# following from http://rorlach.de/mediawiki/index.php/Toggle_TinyMCE
			$wgOut->addScript("<script language=\"javascript\" type=\"text/javascript\">
var tinyMCEmode = true;
function toggleEditorMode(wpTextbox1,eleToggleLink) {
    try {
        if(tinyMCEmode) {
            tinyMCE.execCommand(\"mceRemoveControl\", false, wpTextbox1);
            tinyMCEmode = false;
            if(eleToggleLink)
                eleToggleLink.innerHTML    =    \"Enable Advanced Editor\";
        } else {
            tinyMCE.execCommand(\"mceAddControl\", false, wpTextbox1);
            tinyMCEmode = true;
            if(eleToggleLink)
                eleToggleLink.innerHTML    =    \"Disable Advanced Editor\";
        }
    } catch(e) {
        //error handling
    }
}
</script>");

        if (($wgTinymceTheme == "simple")){
                $wgOut->addScript( "<script language=\"javascript\" type=\"text/javascript\" src=\"$wgScriptPath/extensions/$wgTinymceDir/jscripts/tiny_mce/tiny_mce.js\"></script><script language=\"javascript\" type=\"text/javascript\">tinyMCE.init({
        mode : \"textareas\",
        theme : \"simple\",
        convert_newlines_to_brs : false,
        apply_source_formatting : true,
        relative_urls : false,
        remove_script_host : true,
		document_base_url : \"$wgMyWikiURL\",
        extended_valid_elements : \"$wgExt_valid_elements\"
});</script>" );
} elseif(($wgTinymceTheme == "advanced")) {
        $wgOut->addScript( "<script language=\"javascript\" type=\"text/javascript\" src=\"$wgScriptPath/extensions/$wgTinymceDir/jscripts/tiny_mce/tiny_mce.js\"></script><script language=\"javascript\" type=\"text/javascript\">tinyMCE.init({
        mode : \"textareas\",
        theme : \"advanced\",
        convert_newlines_to_brs : false,
        apply_source_formatting : true,
        relative_urls : false,
        remove_script_host : true,
		document_base_url : \"$wgMyWikiURL\",
        extended_valid_elements : \"$wgExt_valid_elements\"
});</script>" );
}elseif(($wgTinymceTheme == "msword")) {
        $wgOut->addScript("<script language=\"javascript\" type=\"text/javascript\" src=\"$wgScriptPath/extensions/$wgTinymceDir/jscripts/tiny_mce/tiny_mce.js\"></script><script language=\"javascript\" type=\"text/javascript\">tinyMCE.init({
        mode : \"textareas\",
        theme : \"advanced\",
        extended_valid_elements : \"$wgExt_valid_elements\",
        convert_newlines_to_brs : false,
        apply_source_formatting : true,
        relative_urls : false,
        remove_script_host : true,
		document_base_url : \"$wgMyWikiURL\",
        plugins : \"style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras\",
        theme_advanced_buttons1 : \"newdocument,save,|,print,|,iespell,|cut,copy,paste,pastetext,pasteword,|,undo,redo,|,link,unlink,image,hr,anchor,code,|,search,replace,|,tablecontrols,|,help\",
        theme_advanced_buttons2 : \"styleselect,formatselect,fontselect,fontsizeselect,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,cleanup,backcolor,forecolor,removeformat\",
        theme_advanced_buttons3 : \"insertdate,inserttime,|,sub,sup,|,charmap,emotions,media,|,ltr,rtl,|,fullscreen\",
        theme_advanced_toolbar_location : \"top\",
        theme_advanced_toolbar_align : \"left\"
});</script>");
}else{$wgOut->addScript("**TINYMCE NOT ENABLED: FIX wgTinymceTheme**<script language=\"javascript\" type=\"text/javascript\"></script>" );}
		#Since editing add the button
		$wgOut->addHTML("<p align=\"right\"><a id=\"toggleLink\" href=\"#\" title=\"toggle TinyMCE\" onclick=\"toggleEditorMode('wpTextbox1',this);\">Hide Editor</a></p>");
}
else{$wgOut->addScript("<script language=\"javascript\" type=\"text/javascript\"></script>" ); $wgUseTinymce = true; }
        return true;
}

# Check existing article for any tags we don't want TinyMCE parsing...
function wfCheckBeforeEdit ($q, $text) { 
	global $wgUseTinymce;
	
	if (preg_match("|&lt;(data.*?)&gt;(.*?)&lt;/data&gt;|is", $text, $a)) {
		$wgUseTinymce = false;
	}
	elseif(preg_match("|<(data.*?)>(.*?)</data>|is", $text, $a)) {
		$wgUseTinymce = false;}
	else{$wgUseTinymce = true;}
	return true;
}

?>