URL: http://metabolomics.jp/ */ if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, see MassFunctions.php.' ); } $wgExtensionFunctions[] = 'efSetupMassFunctions'; $wgExtensionCredits['parserhook'][] = array( 'name' => 'MassFunctions', 'version' => '0.2', 'author' => 'K, Suwa', 'description' => 'Mass functions', 'url' => 'http://metabolomics.jp/wiki/Help:Extension/MassFunctions', ); $wgHooks['LanguageGetMagic'][] = 'efMassFunctionsLanguageGetMagic'; class MassFunctions { var $url = 'http://www.massbank.jp/api/services/MassBankAPI?wsdl'; /** * Output Mass viewer applet * e.g. {{#masstemplate:KO00001}} => "" * * @param $parser Parser Parent parser * @param $keioid target id * @return string Parts string */ function spectraApplet( ) { global $egMassJarPath; $argv = func_get_args(); $num = count( $argv ); if( $num <= 1 || $num % 2 == 0 ) return $num; $count = ( $num - 1 ) / 2; $html = "{{#javawithparam:DisplayAll.class\n|DisplayAll3.jar\n|$egMassJarPath\n|100%\n|" . (200*$count) . "\n|num=" . $count . ";"; $exFunctions = new ExFunctions(); for( $i = 1; $i <= $count; $i ++ ){ $peak = substr( trim( $exFunctions->searchLine( $argv[0], "^peak1=", "Persist", $argv[$i*2-1] ) ), 16 ); $prec = trim( $exFunctions->searchLine( $argv[0], " PRECURSOR_M/Z ", "Persist", $argv[$i*2-1] ) ); if( strlen( $prec ) > 0 ) $prec = substr( $prec, 40 ); $html .= "id" . $i . "=" . $argv[$i*2-1] . ";name" . $i . "=" . $argv[$i*2] . ";precursor" . $i . "=$prec;peak" . $i . "=$peak;"; } $html = substr( $html, 0, strlen( $html ) - 1 ) . "}}\n\n"; return array( $html, 'noparse' => false ); } /** * Output Mass record * e.g. {{#masstemplate:KO00001}} => "
...
" * * @param $parser Parser Parent parser * @param $keioid target id * @return string Parts string */ function massRecordInfoWithApp( &$parser, $keioid ) { global $egMassJarPath; $ids = array( $keioid ); $params = array("ids" => $ids ); $soap = new SoapClient( $this->url ); try { $res = $soap->getRecordInfo( $params ); } catch (SoapFault $e) { return ""; } $ret = $res->return; $info = $ret->info; $lines = split( "\n", $info ); $precursor = ""; $peak = ""; $title = ""; for( $i = 0; $i < count( $lines ); $i ++ ){ if( preg_match( '/^MS\$FOCUSED_ION: PRECURSOR_M\/Z .*$/', $lines[$i] ) ){ $precursor = trim( substr( $lines[$i], 30 ) ); } else if( preg_match( '/^PK\$PEAK: m\/z int. rel.int.$/', $lines[$i] ) ){ $i ++; for( ; $i < count( $lines ); $i ++ ){ if( preg_match( '/^ .*$/', $lines[$i] ) ){ $mz = split( ' ', trim( $lines[$i] ) ); $peak .= $mz[0] . "," . $mz[2] . ":"; } else break; } } else if( preg_match( '/^RECORD_TITLE:.*$/', $lines[$i] ) ){ $title = preg_replace( '/;/', ':', trim( substr( $lines[$i], 13 ) ) ); } } return array( "

Applet

\n\n{{#javawithparam:DisplayAll.class\n|DisplayAll3.jar\n|$egMassJarPath\n|100%\n|200\n|num=1;\nid1=$keioid;\nname1=$title;\nprecursor1=$precursor;\npeak1=$peak\n}}\n\n

Image

\n\n[[Image:Mass:$keioid.png]]\n\n
\n" . $info . "\n
\n", 'noparse' => false ); } } function efSetupMassFunctions() { global $wgParser; $massFunctions = new MassFunctions; $wgParser->setFunctionHook( 'spectraapplet', array( &$massFunctions, 'spectraApplet' ) ); $wgParser->setFunctionHook( 'massrecordinfowithapp', array( &$massFunctions, 'massRecordInfoWithApp' ) ); } function efMassFunctionsLanguageGetMagic( &$magicWords, $langCode ) { $magicWords['spectraapplet'] = array( 0, 'spectraapplet' ); $magicWords['massrecordinfowithapp'] = array( 0, 'massrecordinfowithapp' ); return true; }