/extensions/Chemistry/SpecialChemicalsources_body.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 234 lines · 161 code · 30 blank · 43 comment · 41 complexity · b2731e02efcab5d919f29f4f274f83b5 MD5 · raw file

  1. <?php
  2. if ( ! defined( 'MEDIAWIKI' ) )
  3. die();
  4. class SpecialChemicalsources extends SpecialPage {
  5. private $Parameters, $Prefix;
  6. /**
  7. * To write the i18n so that it functions with another listpage:
  8. * 1) copy this file to the i18n of your choice
  9. * 2) Choose your prefix (the best way, if this i18n file is called YourPrefix, then choose that as your Prefix
  10. * 3) Replace all occurences of ChemFunctions with the name of your Prefix.
  11. * 4) $wgYourPrefix_Parameters should contain the names of the parameters as they can be given to your specialpage
  12. * In the ListPage you have created, the Magiccodes are then $ParameterName, so if the Parameter is called 'WhAtEvEr',
  13. * then in the ListPage $WhAtEvEr will be replaced with the value of the parameter WhAtEvEr
  14. * 5) In the internationalisation messages, you need the following values:
  15. * 1) The name of your specialpage
  16. * 2) YourPrefix_ListPage -> the name of the page which will hold the links, this page should be in the project namespace
  17. * 3) YourPrefix_SearchExplanation -> a text which can appear as help above the input boxes, when no parameter is given to the page
  18. * 4) YourPrefix_DataList -> If the ListPage does not exist, this is a html string that is displayed as alternative
  19. * 5) For each of the parameters in $wgYourPrefix_Parameters you should have a YourPrefix_ParameterName,
  20. * containing the string as you want it displayed.
  21. * So, if you have a parameter 'WhAtEvEr', you should have a 'YourPrefix_WhAtEvEr' with value 'Whatever'
  22. */
  23. /**
  24. * This is a list of all the possible parameters supplied to Special:ChemicalSources
  25. * Note: The names must be the same (also same case) as supplied in wgChemFunctions_Messages after the 'chemFunctions_'
  26. *
  27. * Variables to be handled in parameters:
  28. * CAS = The CAS-number of the chemical
  29. * EINECS = The EINECS number of the chemical
  30. * Name = The name of the chemical (not specific)
  31. * Formula = The formula of the chemical (not by definition specific)
  32. * PubChem = The PubChem number of the chemical
  33. * SMILES = The SMILES notation of the chemical
  34. * InChI = The InChI notation of the chemical
  35. * ATCCode = The ATCCode for the chemical
  36. * KEGG = The KEGG for the chemical
  37. * RTECS = The RTECS code for the chemical
  38. * DrugBank = The DrugBank code for the chemical
  39. * ECNumber = The EC Number for the compound
  40. */
  41. protected $wgChemFunctions_Prefix = "chemFunctions";
  42. protected $wgChemFunctions_Parameters = array (
  43. 'CAS',
  44. 'Formula',
  45. 'Name',
  46. 'EINECS',
  47. 'CHEBI',
  48. 'PubChem',
  49. 'SMILES',
  50. 'InChI',
  51. 'ATCCode',
  52. 'DrugBank',
  53. 'KEGG',
  54. 'ECNumber',
  55. 'RTECS',
  56. );
  57. public function __construct() {
  58. parent::__construct( 'ChemicalSources' );
  59. }
  60. public function execute( $params ) {
  61. global $wgOut, $wgRequest, $wgChemFunctions_Parameters, $wgChemFunctions_Prefix;
  62. $this->setHeaders();
  63. $this->outputHeader();
  64. $this->Parameters = $this->wgChemFunctions_Parameters;
  65. $this->Prefix = $this->wgChemFunctions_Prefix;
  66. $Params = $wgRequest->getValues();
  67. $ParamsCheck = "";
  68. foreach ( $this->Parameters as $key ) {
  69. if ( isset( $Params [$key] ) )
  70. $ParamsCheck .= $Params [$key];
  71. }
  72. if ( $ParamsCheck ) {
  73. $transParams = $this->TransposeAndCheckParams( $Params );
  74. $this->OutputListPage( $transParams );
  75. } else {
  76. $Params = $this->getParams();
  77. }
  78. }
  79. # Check the parameters supplied, make the mixed parameters, and put them into the transpose matrix.
  80. function TransposeAndCheckParams( $Params ) {
  81. if ( isset( $Params['CAS'] ) )
  82. $Params['CAS'] = preg_replace( '/[^0-9\-]/', "", $Params['CAS'] );
  83. else $Params['CAS'] = '';
  84. if ( isset( $Params['EINECS'] ) )
  85. $Params['EINECS'] = preg_replace( '/[^0-9\-]/', "", $Params['EINECS'] );
  86. else $Params['EINECS'] = '';
  87. if ( isset( $Params['CHEBI'] ) )
  88. $Params['CHEBI'] = preg_replace( '/[^0-9\-]/', "", $Params['CHEBI'] );
  89. else $Params['CHEBI'] = '';
  90. if ( isset( $Params['PubChem'] ) )
  91. $Params['PubChem'] = preg_replace( '/[^0-9\-]/', "", $Params['PubChem'] );
  92. else $Params['PubChem'] = '';
  93. if ( isset( $Params['SMILES'] ) )
  94. $Params['SMILES'] = preg_replace( '/\ /', "", $Params['SMILES'] );
  95. else $Params['SMILES'] = '';
  96. if ( isset( $Params['InChI'] ) )
  97. $Params['InChI'] = preg_replace( '/\ /', "", $Params['InChI'] );
  98. else $Params['InChI'] = '';
  99. if ( isset( $Params['ATCCode'] ) )
  100. $Params['ATCCode'] = preg_replace( '/[^0-9\-]/', "", $Params['ATCCode'] );
  101. else $Params['ATCCode'] = '';
  102. if ( isset( $Params['KEGG'] ) )
  103. $Params['KEGG'] = preg_replace( '/[^C0-9\-]/', "", $Params['KEGG'] );
  104. else $Params['KEGG'] = '';
  105. if ( isset( $Params['RTECS'] ) )
  106. $Params['RTECS'] = preg_replace( '/\ /', "", $Params['RTECS'] );
  107. else $Params['RTECS'] = '';
  108. if ( isset( $Params['ECNumber'] ) )
  109. $Params['ECNumber'] = preg_replace( '/[^0-9\-]/', "", $Params['ECNumber'] );
  110. else $Params['ECNumber'] = '';
  111. if ( isset( $Params['Drugbank'] ) )
  112. $Params['Drugbank'] = preg_replace( '/[^0-9\-]/', "", $Params['Drugbank'] );
  113. else $Params['Drugbank'] = '';
  114. if ( isset( $Params['Formula'] ) )
  115. $Params['Formula'] = preg_replace( '/\ /', "" , $Params['Formula'] );
  116. else $Params['Formula'] = '';
  117. if ( isset( $Params['Name'] ) )
  118. $Params['Name'] = preg_replace( '/\ /', "%20", $Params['Name'] );
  119. else $Params['Name'] = '';
  120. # Create some new from old ones
  121. $TEMPCASNAMEFORMULA = $Params["CAS"];
  122. if ( empty ( $TEMPCASNAMEFORMULA ) ) {
  123. $TEMPCASNAMEFORMULA = $Params["Formula"];
  124. }
  125. if ( empty ( $TEMPCASNAMEFORMULA ) ) {
  126. $TEMPCASNAMEFORMULA = $Params["Name"];
  127. }
  128. $TEMPNAMEFORMULA = $Params["Name"];
  129. if ( empty ( $TEMPNAMEFORMULA ) ) {
  130. $TEMPNAMEFORMULA = $Params["Formula"];
  131. }
  132. $TEMPCASFORMULA = $Params["CAS"];
  133. if ( empty ( $TEMPCASFORMULA ) ) {
  134. $TEMPCASFORMULA = $Params["Formula"];
  135. }
  136. $TEMPCASNAME = $Params["CAS"];
  137. if ( empty ( $TEMPCASNAME ) ) {
  138. $TEMPCASNAME = $Params["Name"];
  139. }
  140. # Put the parameters into the transpose array:
  141. $transParams = array(
  142. "\$MIXCASNameFormula" => $TEMPCASNAMEFORMULA,
  143. "\$MIXCASName" => $TEMPCASNAME,
  144. "\$MIXCASFormula" => $TEMPCASFORMULA,
  145. "\$MIXNameFormula" => $TEMPNAMEFORMULA
  146. );
  147. foreach ( $this->Parameters as $key ) {
  148. if ( isset( $Params[$key] ) ) {
  149. $transParams["\$" . $key] = $Params[$key] ;
  150. } else {
  151. $transParams["\$" . $key] = "" ;
  152. }
  153. }
  154. return $transParams;
  155. }
  156. # Create the actual page
  157. function OutputListPage( $transParams ) {
  158. global $wgOut;
  159. # check all the parameters before we put them in the page
  160. foreach ( $transParams as $key => $value ) {
  161. $transParams[$key] = wfUrlEncode( htmlentities( preg_replace( "/\<.*?\>/", "", $value ) ) );
  162. }
  163. # First, see if we have a custom list setup
  164. $bstitle = Title::makeTitleSafe( NS_PROJECT, wfMsg( $this->Prefix . '_ListPage' ) );
  165. if ( $bstitle ) {
  166. $revision = Revision::newFromTitle( $bstitle );
  167. if ( $revision ) {
  168. $bstext = $revision->getText();
  169. if ( $bstext ) {
  170. $bstext = strtr( $bstext, $transParams );
  171. $wgOut->addWikiText( $bstext );
  172. }
  173. } else {
  174. $bstext = wfMsg( $this->Prefix . '_DataList' );
  175. $bstext = strtr( $bstext, $transParams );
  176. $wgOut->addHTML( $bstext );
  177. }
  178. }
  179. }
  180. # If no parameters supplied, get them!
  181. function getParams() {
  182. global $wgOut;
  183. $action = $this->getTitle()->escapeLocalUrl();
  184. $go = htmlspecialchars( wfMsg( "go" ) );
  185. $wgOut->addWikitext ( wfMsg( $this->Prefix . '_SearchExplanation' ) );
  186. $wgOut->addHTML( "<table><tr><td>" );
  187. foreach ( $this->Parameters as $key ) {
  188. $this->GetParam_Row( $this->Prefix . "_" . $key, $key, $action, $go );
  189. }
  190. $wgOut->addHTML( "</table>" );
  191. }
  192. # Creates a table row
  193. function GetParam_Row( $p, $q, $action, $go ) {
  194. global $wgOut;
  195. $wgOut->addHTML ( wfMsg( $p ) . wfMsg( 'colon-separator' ) );
  196. $wgOut->addHTML( "</td><td>
  197. <form action=\"$action\" method='post'>
  198. <input name=\"$q\" id=\"$q\" />
  199. <input type='submit' value=\"$go\" />
  200. </form>
  201. </td></tr>" );
  202. $wgOut->addHTML( "<tr><td>" );
  203. }
  204. }