URL: http://metabolomics.jp/ */ if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, see ExMolFunctions.php.' ); } $wgExtensionCredits['parserhook'][] = array( 'name' => 'ExMolFunctions', 'version' => '0.1', 'author' => 'K, Suwa', 'description' => 'Link and page which receive template and arguments', 'url' => 'http://metabolomics.jp/wiki/Help:Extension/ExMolFunctions', ); $wgHooks['ParserBeforeStrip'][] = 'efExMolFunctionsView'; $wgHooks['ArticleSave'][] = 'efExMolFunctionsDataInsert'; global $egMolNamespace; $egMolNamespace = "Mol"; function efExMolFunctionsView( &$parser, &$text, &$strip_state ) { global $wgTitle, $egMolNamespace; if( ereg( "^$egMolNamespace:.*$", $wgTitle ) && ereg( "\nM END *(\n|$)", $text ) !== FALSE ) { $text = "[[Image:{{PAGENAME}}.png]]\n
" . $text . "\n
"; } return true; } function efExMolFunctionsDataInsert( &$parser, &$user, &$text, &$summary, $minor, $watch, $sectionanchor, &$flags ) { global $wgExtraNamespaces, $egMolNamespace, $egJavaBin, $egMolCalculatorJar, $egMolImageJar, $egTmpPath, $wgTitle, $egMolPath; # check namespace $targetNamespace = $parser->getTitle()->getNamespace(); $molNamespace = array_search( $egMolNamespace, $wgExtraNamespaces ); if( $molNamespace === FALSE || $targetNamespace != $molNamespace ) return true; # process $procText = $text; $lines = split( "\n", $procText ); $wikitext = ''; # calculate $mol = ""; for( $i = 0; $i < count( $lines ); $i ++ ){ if( stripos( $lines[$i], 'S SKP' ) === 0 ){ $count = trim( substr( $lines[$i], 6 ) ); $i += $count; } else $mol .= $lines[$i] . "\n"; } $mol = escapeshellcmd( escapeshellarg( $mol ) ); $res = `$egJavaBin -jar $egMolCalculatorJar $mol`; $data = split( "\n", $res ); $datas = array(); $datas['formula'] = trim( substr( $data[0], 8 ) ); $datas['extmass'] = trim( substr( $data[1], 10 ) ); $datas['avemass'] = trim( substr( $data[2], 12 ) ); $datas['smiles'] = trim( substr( $data[3], 7 ) ); $page_title = $wgTitle->getText(); # parameter $width = 100; $height = 100; $autodraw = ''; $option = ''; # insert if( stripos( $procText, 'S SKP' ) === FALSE ) { # insert for( $i = 0; $i < count( $lines ); $i ++ ) { $pos = stripos( $lines[$i], 'M END' ); if( $pos !== FALSE && $pos == 0 ) { $wikitext .= "S SKP 5\n"; $wikitext .= 'ID ' . $page_title. "\n"; $wikitext .= 'FORMULA ' . $datas['formula']. "\n"; $wikitext .= 'EXACTMASS ' . $datas['extmass']. "\n"; $wikitext .= 'AVERAGEMASS ' . $datas['avemass']. "\n"; $wikitext .= 'SMILES ' . $datas['smiles']. "\n"; $wikitext .= $lines[$i]. "\n"; } else { $wikitext .= $lines[$i]. "\n"; } } } else { # fix $molflags = array(); $idflag = false; $molflags['formula'] = false; $molflags['extmass'] = false; $molflags['avemass'] = false; $molflags['smiles'] = false; for( $i = 0; $i < count( $lines ); $i ++ ) { if( stripos( $lines[$i], 'S SKP' ) === 0 ) { $count = trim( substr( $lines[$i], 6 ) ); $tmptext = ''; for( $j = 1; $j <= $count; $j ++ ) { $pos = stripos( $lines[$i+$j], 'ID ' ); if( $pos !== FALSE && $pos == 0 ) { $idflag = true; } $pos = stripos( $lines[$i+$j], 'FORMULA ' ); if( $pos !== FALSE && $pos == 0 ) { $molflags['formula'] = true; } $pos = stripos( $lines[$i+$j], 'EXACTMASS ' ); if( $pos !== FALSE && $pos == 0 ) { $molflags['extmass'] = true; } $pos = stripos( $lines[$i+$j], 'AVERAGEMASS ' ); if( $pos !== FALSE && $pos == 0 ) { $molflags['avemass'] = true; } $pos = stripos( $lines[$i+$j], 'SMILES ' ); if( $pos !== FALSE && $pos == 0 ) { $molflags['smiles'] = true; } $pos = stripos( $lines[$i+$j], 'X ' ); if( $pos !== FALSE && $pos == 0 && strlen( $lines[$i+$j] ) > 3 ) { $width = trim( substr( $lines[$i+$j], 2 ) ); if( !ereg( '^[0-9]+$', $width ) ) $width = 100; if( $width > 1000 ) $width = 1000; } $pos = stripos( $lines[$i+$j], 'Y ' ); if( $pos !== FALSE && $pos == 0 && strlen( $lines[$i+$j] ) > 3 ) { $height = trim( substr( $lines[$i+$j], 2 ) ); if( !ereg( '^[0-9]+$', $height ) ) $height = 100; if( $height > 1000 ) $height = 1000; } $pos = stripos( $lines[$i+$j], 'AUTODRAW ' ); if( $pos !== FALSE && $pos == 0 && strlen( $lines[$i+$j] ) > 10 ) { $autodraw = trim( substr( $lines[$i+$j], 9 ) ); if( strcasecmp( $autodraw, 'true' ) != 0 ) $autodraw = '-a'; else $autodraw = ''; } $pos = stripos( $lines[$i+$j], 'OPTION ' ); if( $pos !== FALSE && $pos == 0 && strlen( $lines[$i+$j] ) > 8 ) { $option = escapeshellarg( escapeshellcmd( trim( substr( $lines[$i+$j], 7 ) ) ) ); } $tmptext .= $lines[$i+$j] . "\n"; } if( !$idflag ) { $tmptext .= 'ID ' . $page_title . "\n"; $count ++; } if( !$molflags['formula'] ) { $tmptext .= 'FORMULA ' . $datas['formula'] . "\n"; $count ++; } if( !$molflags['extmass'] ) { $tmptext .= 'EXACTMASS ' . $datas['extmass'] . "\n"; $count ++; } if( !$molflags['avemass'] ) { $tmptext .= 'AVERAGEMASS ' . $datas['avemass'] . "\n"; $count ++; } if( !$molflags['smiles'] ) { $tmptext .= 'SMILES ' . $datas['smiles'] . "\n"; $count ++; } while( strlen( $count ) < 3 ) $count = ' ' . $count; $wikitext .= "S SKP" . $count . "\n" . $tmptext; $i += $j - 1; } else { $wikitext .= $lines[$i] . "\n"; } } } # if( stripos( $wikitext, '
' ) !== 0 )
#		$wikitext = '
' . $wikitext;
#	$wikitext = trim( $wikitext );
#	if( strripos( $wikitext, '
' ) != strlen( $wikitext ) - 6 ) # $wikitext .= "\n
"; $text = ereg_replace( " *\n", " \n", $wikitext ); # create and add/update image $id = $parser->getTitle()->getText(); # create image and save to egTmpPath $file = "$egTmpPath/$id.png"; $nfile = "$egTmpPath/" . $id . "n.png"; $delete_flag = false; if( !isset( $_SERVER["DOCUMENT_ROOT"] ) || strlen( $_SERVER["DOCUMENT_ROOT"] ) == 0 ) { $path = "/tmp"; $delete_flag = true; } else { $path = $_SERVER["DOCUMENT_ROOT"] . $egMolPath . "/" . substr( $id, 0, 4 ); if( !file_exists( $path ) ){ mkdir( $path ); } else if( !is_dir( $path ) ){ $path = "/tmp"; $delete_flag = true; } } # `$egJavaBin -jar $egMolImageJar $file '$wikitext' $width $height $autodraw $option`; # temporary change. ### /* $molfile = "$path/$id.mol"; $mfh = fopen( "$molfile", "w" ); fwrite( $mfh, "$wikitext" ); fclose( $mfh ); `DISPLAY=localhost:1.0 $egJavaBin -jar $egMolImageJar $autodraw -p -s 15 -l 2 -o "$egTmpPath" "$molfile"`; # over ################ $base = wfBaseName( $file ); $title = Title::makeTitleSafe( NS_IMAGE, $base ); if( !is_object( $title ) ){ # error return true; } $image = wfLocalFile( $title ); $archive = $image->publish( $file ); if( WikiError::isError( $archive ) || !$archive->isGood() ) { # error return true; } if( !$image->recordUpload( $archive->value, "", "" ) ) { return true; # error } unlink( $file ); if( $delete_flag ) unlink( $molfile ); */ efExMolFunctionsCreateImage( $path, $id, $file, $wikitext, $autodraw, $delete_flag, "" ); efExMolFunctionsCreateImage( $path, $id . "n", $nfile, $wikitext, $autodraw, $delete_flag, "-n" ); return true; } function efExMolFunctionsCreateImage( $path, $id, $file, $wikitext, $autodraw, $delete_flag, $number_flag ) { global $egJavaBin, $egMolImageJar, $egTmpPath; $molfile = "$path/$id.mol"; $mfh = fopen( "$molfile", "w" ); fwrite( $mfh, "$wikitext" ); fclose( $mfh ); `DISPLAY=localhost:1.0 $egJavaBin -jar $egMolImageJar $autodraw $number_flag -p -s 15 -l 2 -o "$egTmpPath" "$molfile"`; $base = wfBaseName( $file ); $title = Title::makeTitleSafe( NS_IMAGE, $base ); if( !is_object( $title ) ){ # error return true; } $image = wfLocalFile( $title ); $archive = $image->publish( $file ); if( WikiError::isError( $archive ) || !$archive->isGood() ) { # error return true; } if( !$image->recordUpload( $archive->value, "", "" ) ) { return true; # error } unlink( $file ); if( $delete_flag ) unlink( $molfile ); }