URL: http://metabolomics.jp/ */ if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, see MolFunctions.php.' ); } $wgExtensionFunctions[] = 'efSetupMolFunctions'; $wgExtensionCredits['parserhook'][] = array( 'name' => 'MolFunctions', 'version' => '0.1', 'author' => 'K,Suwa', 'description' => 'Information involved MOL', 'url' => 'http://metabolomics.jp/wiki/Help:Extension/MolFunctions', ); $wgHooks['LanguageGetMagic'][] = 'efMolFunctionsLanguageGetMagic'; class MolFunctions { var $mMolPrevTarget = ""; var $mMolFormula = ""; var $mMolAMass = ""; var $mMolEMass = ""; var $mMolSmiles = ""; function formula( &$parser, $id = '' ) { global $title; if( strlen( $id ) == 0 ) $this->getMolData( $title ); else $this->getMolData( $id ); return $this->mMolFormula; } function avemass( &$parser, $id = '' ) { global $title; if( strlen( $id ) == 0 ) $this->getMolData( $title ); else $this->getMolData( $id ); return $this->mMolAMass; } function extmass( &$parser, $id = '' ) { global $title; if( strlen( $id ) == 0 ) $this->getMolData( $title ); else $this->getMolData( $id ); return $this->mMolEMass; } function smiles( &$parser, $id = '' ) { global $title; if( strlen( $id ) == 0 ) $this->getMolData( $title ); else $this->getMolData( $id ); return $this->mMolSmiles; } function getMolData( $title ) { global $mMolPrevTarget, $egMolPath; # if( !isset( $egMolPath ) ) # $egMolPath = "/mol"; $title = preg_replace( "/[^-0-9a-zA-Z\.*\[\]]/", "", $title ); if( strlen( $title ) == 0 || strncmp( $title, $mMolPrevTarget, 12 ) == 0 ){ return; } $mMolPrevTarget = $title; if( !isset( $egMolPath ) ){ $this->mMolFormula = ""; $this->mMolAMass = ""; $this->mMolEMass = ""; $this->mMolSmiles = ""; return; } $this->mMolFormula = ""; $this->mMolAMass = ""; $this->mMolEMass = ""; $this->mMolSmiles = ""; if( !isset( $_SERVER["DOCUMENT_ROOT"] ) || strlen( $_SERVER["DOCUMENT_ROOT"] ) == 0 ) $path = "/var/www" . $egMolPath; else $path = $_SERVER["DOCUMENT_ROOT"] . $egMolPath; $titles = $this->searchFiles( $path, $title ); for( $i = 0; $i < count( $titles ) ; $i ++){ if( !ereg( ".mol$", $titles[$i] ) ) continue; $titles[$i] = ereg_replace( ".*:", "", $titles[$i] ); $titles[$i] = ereg_replace( ".mol$", "", $titles[$i] ); $filename = $path . "/" . mb_substr( $titles[$i], 0, 4 ) . "/" . $titles[$i] . ".mol"; if( !file_exists( $filename ) ){ $this->mMolFormula .= "&&" . $titles[$i] . "&&Invalid path"; $this->mMolAMass .= "&&" . $titles[$i] . "&&Invalid path"; $this->mMolEMass .= "&&" . $titles[$i] . "&&Invalid path"; $this->mMolSmiles .= "&&" . $titles[$i] . "&&Invalid path"; continue; } $file = fopen( $filename, 'r' ); while( !feof( $file ) ){ $line = fgets( $file ); if( preg_match( "/^FORMULA\t/", $line ) ){ $formula = $line; $formula = ereg_replace( "^FORMULA\t", "", $formula ); $this->mMolFormula .= "&&" . $titles[$i] . "&&" . trim( $formula ) . "\n"; } else if( preg_match( "/^AVERAGEMASS\t/", $line ) ){ $aMass = $line; $aMass = ereg_replace( "^AVERAGEMASS\t", "", $aMass ); $pos = strpos( $aMass, "." ); if( $pos !== false ){ if( strlen( $aMass ) > $pos+4 ){ $aMass = substr( $aMass, 0, $pos+5 ); } } $this->mMolAMass .= "&&" . $titles[$i] . "&&" . trim( $aMass ) . "\n"; } else if( preg_match( "/^EXACTMASS\t/", $line ) ){ $eMass = $line; $eMass = ereg_replace( "^EXACTMASS\t", "", $eMass ); $pos = strpos( $eMass, "." ); if( $pos !== false ){ if( strlen( $eMass ) > $pos+4 ){ $eMass = substr( $eMass, 0, $pos+5 ); } } $this->mMolEMass .= "&&" . $titles[$i] . "&&" . trim( $eMass ) . "\n"; } else if( preg_match( "/^SMILES\t/", $line ) ){ $smiles = $line; $smiles = ereg_replace( "^SMILES\t", "", $smiles ); $unit = 40; $smiles2 = substr( $smiles, 0, $unit+1 ); for( $j = $unit; $j < strlen( $smiles ); $j += $unit ){ $smiles2 .= ";
" . substr( $smiles, $j, $unit+1 ); } $this->mMolSmiles .= "&&" . $titles[$i] . "&&" . trim( $smiles2 ) . "\n"; } } fclose( $file ); } } function searchFiles( $path, $title ) { $targets = array(); $dh = opendir( $path ); while( $subdir = readdir( $dh ) ){ if( $subdir == "." || $subdir == ".." ) continue; $subh = opendir( $path . "/" . $subdir ); while( $file = readdir( $subh ) ){ $fullpath = $path ."/" . $subdir . "/" . $file; # if( $file == "." || $file == ".." || is_link( $fullpath ) || !is_file( $fullpath ) ) if( is_link( $fullpath ) || !is_file( $fullpath ) ) continue; if( ereg( ".*$title.*", $file ) ){ $targets[] = $file; } } closedir( $subh ); } closedir( $dh ); return $targets; } } function efSetupMolFunctions() { global $wgParser; $molFunctions = new MolFunctions; $wgParser->setFunctionHook( 'formula', array( &$molFunctions, 'formula' ) ); $wgParser->setFunctionHook( 'avemass', array( &$molFunctions, 'avemass' ) ); $wgParser->setFunctionHook( 'extmass', array( &$molFunctions, 'extmass' ) ); $wgParser->setFunctionHook( 'smiles', array( &$molFunctions, 'smiles' ) ); } function efMolFunctionsLanguageGetMagic( &$magicWords, $langCode ) { $magicWords['formula'] = array( 0, 'formula' ); $magicWords['avemass'] = array( 0, 'avemass' ); $magicWords['extmass'] = array( 0, 'extmass' ); $magicWords['smiles'] = array( 0, 'smiles' ); return true; }