SpecialBibTexImport_body.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. if( !defined( 'MEDIAWIKI' ) ) {
  3. echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
  4. exit( 1 );
  5. }
  6. class SpecialBibTexImport extends SpecialPage {
  7. function SpecialBibTexImport() {
  8. parent::__construct( 'BibTeXImport', 'bibteximport' );
  9. }
  10. function execute( $par ) {
  11. global $wgOut, $wgUser;
  12. $wgOut->setArticleRelated( false );
  13. $wgOut->setPagetitle( wfMsg( 'bibteximport' ) );
  14. if (IsSet($_FILES['users_file'])) {
  15. $wgOut->addHTML( $this->AnalizeArticles($_FILES['users_file']) );
  16. } else if ( IsSet($_POST['import_to_wiki']) ) {
  17. $wgOut->addHTML( $this->ImportArticles() );
  18. } else {
  19. $wgOut->addHTML( $this->MakeForm() );
  20. }
  21. }
  22. function MakeForm() {
  23. $titleObj = Title::makeTitle( NS_SPECIAL, 'BibTeXImport' );
  24. $action = $titleObj->escapeLocalURL();
  25. $output = '';
  26. $output.='<form enctype="multipart/form-data" method="post" action="'.$action.'">';
  27. $output.='<p>' . wfMsg('bibteximport-form-caption') . ':';
  28. $output.='<input name="users_file" type="file" size=40 /></p>';
  29. $output.='<p><input type="submit" value="'.wfMsg( 'bibteximport-form-button' ).'" /></p>';
  30. // XXX we should really wrap these strings for i18n
  31. $output.= <<< INTRO
  32. <p>Upload a BibTeX file, then select the papers to import. We'll
  33. add a summary page to AcaWiki for each one. Use the links on the
  34. next page to add summaries.</p>
  35. <p>You can export BibTeX from reference management software such
  36. as EndNote, RefWorks, and Zotero. BibTeX files often end with a
  37. .bib (file extension). If you have any questions please feel
  38. free to <a href="mailto:admin@acawiki.org">contact us</a>.</p>
  39. INTRO;
  40. $output.='</table>';
  41. $output.='</form>';
  42. return $output;
  43. }
  44. function AnalizeArticles($fileinfo) {
  45. global $IP, $wgOut;
  46. require_once(dirname(__FILE__) . "/phpbib/bibliography.php");
  47. $extracted = 0 ;
  48. $titleObj = Title::makeTitle( NS_SPECIAL, 'BibTeXImport' );
  49. $action = $titleObj->escapeLocalURL();
  50. $output_select='';
  51. $output_select.='<form enctype="multipart/form-data" method="post" action="'.$action.'" name="correctarticles">';
  52. $output_select.='<table style="width: 100%; border :0; ">';
  53. $myBIB=new Bibliography($fileinfo['tmp_name']);
  54. foreach($myBIB->biblio["title"] as $bibkey=>$title) {
  55. $output_select.='<tr> <td><input name="bibkey_-_' . $bibkey . '" type="checkbox"/></td> <td>'.wfMsg( 'bibteximport-title' ).'</td> <td><input type="text" name="title_-_'. $bibkey .'" value="' . $title . '" size="60"/></td></tr>';
  56. //$output_select.= $this->AnalizeArticlesFieldLine($myBIB, "author", $bibkey);
  57. if(isset($myBIB->biblio["author"][$bibkey])) {
  58. $output_select.='<tr><td></td><td>'.wfMsg( 'bibteximport-author' ).'</td><td><input type="text" name="author_-_'. $bibkey .'" value="' . implode(", ",explode(" and ", $myBIB->biblio["author"][$bibkey])) . '" size="60"/></td></tr>';
  59. }
  60. if(isset($myBIB->biblio["month"][$bibkey])) { $month = $myBIB->biblio["month"][$bibkey] . '/'; } else { $month = ''; }
  61. if(isset($myBIB->biblio["year"][$bibkey])) { $output_select.='<tr><td></td><td>'.wfMsg( 'bibteximport-date' ).'</td><td><input type="text" name="date_-_'. $bibkey .'" value="' . $month . $myBIB->biblio["year"][$bibkey] . '" size="60"/></td></tr>'; }
  62. $output_select.= $this->AnalizeArticlesFieldLine($myBIB, "journal", $bibkey);
  63. $output_select.= $this->AnalizeArticlesFieldLine($myBIB, "volume", $bibkey);
  64. $output_select.= $this->AnalizeArticlesFieldLine($myBIB, "url", $bibkey);
  65. $output_select.= $this->AnalizeArticlesFieldLine($myBIB, "doi", $bibkey);
  66. if(isset($myBIB->biblio["note"][$bibkey])) {
  67. $output_select.='<tr><td></td><td>'.wfMsg( 'bibteximport-note' ).'</td><td><textarea name="note_-_'. $bibkey .'" rows="4" cols="50">' . $myBIB->biblio["note"][$bibkey] . '</textarea></td></tr>';
  68. }
  69. $output_select.='<tr><td><br/></td><td></td><td></td></tr>';
  70. $extracted++;
  71. }
  72. $output_select.='<tr><td></td><td></td><td><input type="submit" name="import_to_wiki" value="'.wfMsg( 'bibteximport-import-button' ).'" /></td></tr>';
  73. $output_select.='</table>';
  74. $output_select.='</form>';
  75. $output=wfMsg( 'bibteximport-log-summary-extracter' ).$extracted.'<br />';
  76. $output.='<h2>'.wfMsg( 'bibteximport-select-data' ).'</h2>';
  77. $output.=$output_select;
  78. return $output;
  79. }
  80. function AnalizeArticlesFieldLine($BIB, $field, $bibkey) {
  81. if(isset($BIB->biblio[$field][$bibkey])) { return '<tr><td></td><td>'.wfMsg( 'bibteximport-'. $field ).'</td><td><input type="text" name="' . $field . '_-_' . $bibkey .'" value="' . $BIB->biblio[$field][$bibkey] . '" size="60"/></td></tr>'; }
  82. else { return ''; }
  83. }
  84. function ImportArticles() {
  85. $output = '<h2>'.wfMsg( 'bibteximport-import-imported' ).'</h2>';
  86. $console = '<h3>'.wfMsg( 'bibteximport-import-console' ).'</h3>';
  87. $articles = array();
  88. $bibkey ='';
  89. $title = ''; $content = '';
  90. //We first parse the post data
  91. foreach ($_POST as $key => $value) {
  92. $keyword_key = explode('_-_',$key);
  93. if($keyword_key[0] == 'bibkey' && $value == 'on') {
  94. //if bibkey not empty create the page before doing more processing
  95. if($bibkey !='') {
  96. //create the page
  97. $output .= $this->Createpage($title,$content);
  98. }
  99. //prepare the new temp variables
  100. $bibkey = $keyword_key[1];
  101. $console .= wfMsg( 'bibteximport-import-newarticle' ) . ' ' . $bibkey .'<br/>';
  102. $title = ''; $content = '';
  103. }
  104. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'title' ) {
  105. $console .= wfMsg( 'bibteximport-title' ) . ' ' . $value .'<br/>' ;
  106. $title = $value;
  107. }
  108. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'author' ) {
  109. $console .= wfMsg( 'bibteximport-author' ) . ' ' . $value .'<br/>' ;
  110. $content.= "|authors=" . $value . "\r\n";
  111. }
  112. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'date' ) {
  113. $console .= wfMsg( 'bibteximport-date' ) . ' ' . $value .'<br/>' ;
  114. $content.= "|pub_date=" . $value . "\r\n";
  115. }
  116. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'journal' ) {
  117. $console .= wfMsg( 'bibteximport-journal' ) . ' ' . $value .'<br/>' ;
  118. $content.= "|journal=" . $value . "\r\n";
  119. }
  120. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'volume' ) {
  121. $console .= wfMsg( 'bibteximport-volume' ) . ' ' . $value .'<br/>' ;
  122. $content.= "|journal_volume=" . $value . "\r\n";
  123. }
  124. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'url' ) {
  125. $console .= wfMsg( 'bibteximport-url' ) . ' ' . $value .'<br/>' ;
  126. $content.= "|url=" . $value . "\r\n";
  127. }
  128. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'doi' ) {
  129. $console .= wfMsg( 'bibteximport-doi' ) . ' ' . $value .'<br/>' ;
  130. $content.= "|doi=" . $value . "\r\n";
  131. }
  132. else if( $keyword_key[1] == $bibkey && $keyword_key[0] == 'note' ) {
  133. $console .= wfMsg( 'bibteximport-note' ) . ' ' . $value .'<br/>' ;
  134. $content.= "|summary=" . $value . "\r\n";
  135. }
  136. }
  137. if($bibkey !='') {
  138. //create the page
  139. $output .= $this->Createpage($title,$content);
  140. }
  141. $output.=$console;
  142. return $output;
  143. }
  144. function Createpage($title,$content) {
  145. global $IP;
  146. $content = "{{Summary\r\n|title=" . $title . "\r\n" . $content . "}}";
  147. $articleTitle = Title::newFromText($title);
  148. $article = new Article($articleTitle);
  149. if( !$article->exists() )
  150. {
  151. $article->doEdit($content, 'BibTeX auto import ' . date('Y-m-d h:i:s') );
  152. if($article)
  153. {
  154. return '<a href="' . $articleTitle->escapeFullURL() . '">' . $articleTitle->getText() . '</a> <br/>';
  155. } else return wfMsg('bibteximport-error-importing') . $title .'<br/>';
  156. }
  157. else
  158. {
  159. return wfMsg('bibteximport-error-article-exists') . $title .'<br/>';
  160. }
  161. }
  162. }