Extension talk:TinyMCE MW

Code TinyMCE_MW.php
<?php /* TinyMCE_MW.php - MediaWiki extension - version 0.2.1 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 0.2.0 was built and tested on Mediawiki-1.10.0. Tested CategoryTree extension for compatibility with new tags and it worked. 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.modxcms.com/index.php/TinyMCE http://www.mediawiki.org/wiki/Extension:CategoryTree 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.2.1", "url" => "http://www.mediawiki.org/wiki/Extension:TinyMCE_MW", "description" => "Easily implement Moxiecode's TinyMCE into MediaWiki" );

$wgHooks['ParserBeforeStrip'][] 		= 'wfTinymceParserCut'; $wgHooks['ParserAfterTidy'][] 			= 'wfTinymceParserPaste'; $wgHooks['EditPage::showEditForm:initial'][] 	= 'wfTinymceAddScript';
 * 1) REGISTER HOOKS

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

global $wgTitle; global $wgTempText; $ns_allowed = true; $ns = $wgTitle->getNamespace; #if (in_array($ns, $wgexcludedNamespaces)) $ns_allowed = false; if ($ns_allowed) { $wgTempText = $text;#get text }	return true; }
 * 1) DEBUG
 * 2) $wgOut->addScript("document.write(\"my text\" $wgTempText); ");

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

global $wgOut, $wgTitle, $wgParser; global $wgTempText, $wgTinymceToken; $List  = array; $ns_allowed = true; $ns = $wgTitle->getNamespace; if ($ns_allowed) { $tinymcetext = $wgTempText; #Allow_a_tags $i = 0; $ta = md5("aopen"); while (preg_match("|()|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("|()|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("|(]*?/>)|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", " ".$value." ", $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", " ".$value." ", $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", "".$value."", $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", " ".$value." ", $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", " ".$value." ", $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", " ".$value." ", $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", " ".$value." ", $a[0]); $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext); }		# horizontal line while (preg_match("||is", $tinymcetext, $a)) { $r = preg_replace("||is", " ", $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("||i", "", $a[0]); $r = preg_replace("| |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("||i", "", $a[0]); $r = preg_replace("| |i", " ", $r); $tinymcetext = str_replace($a[0], html_entity_decode($r), $tinymcetext); }		}		$state = new StripState; $x =& $state; $tinymcetext = $q->strip($tinymcetext, $x); # optional remove $tinymcetext = preg_replace('/]*>/','',$tinymcetext);//Remove the start or 		$tinymcetext = preg_replace('/<\/p>/', ' ', $tinymcetext); // Replace the end $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; global $wgTinymceDir, $wgTinymceTheme, $wgExt_valid_elements;

$wgTinymceDir = "tinymce"; $ns_allowed = true; $ns = $wgTitle->getNamespace; if ($ns_allowed && ($wgTinymceTheme == "simple")){ $wgOut->addScript( " tinyMCE.init({	mode : \"textareas\",	theme : \"simple\",	convert_newlines_to_brs : true,	extended_valid_elements : \"$wgExt_valid_elements\" }); " ); } elseif($ns_allowed && ($wgTinymceTheme == "advanced")) { $wgOut->addScript( " tinyMCE.init({	mode : \"textareas\",	theme : \"advanced\",	convert_newlines_to_brs : true,	extended_valid_elements : \"$wgExt_valid_elements\" }); " ); }elseif($ns_allowed && ($wgTinymceTheme == "msword")) { $wgOut->addScript(" tinyMCE.init({	mode : \"textareas\",	theme : \"advanced\",	extended_valid_elements : \"$wgExt_valid_elements\",	convert_newlines_to_brs : true,	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\" }); "); }else{if ($ns_allowed){ $wgOut->addScript("**TINYMCE NOT ENABLED: FIX wgTinymceTheme**".$text. " " );} }

return true; }

?>