00001 <?php
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00032 class SRFBibTeX extends SMWResultPrinter {
00033 protected $m_title = '';
00034 protected $m_description = '';
00035
00036 public function getMimeType( $res ) {
00037 return 'text/bibtex';
00038 }
00039
00040 public function getFileName( $res ) {
00041 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
00042 return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
00043 } else {
00044 return 'BibTeX.bib';
00045 }
00046 }
00047
00048 public function getQueryMode( $context ) {
00049 return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES:SMWQuery::MODE_NONE;
00050 }
00051
00052 public function getName() {
00053 return wfMsg( 'srf_printername_bibtex' );
00054 }
00055
00056 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00057 global $wgSitename;
00058 $result = '';
00059
00060 if ( $outputmode == SMW_OUTPUT_FILE ) {
00061 if ( $this->m_title == '' ) {
00062 $this->m_title = $wgSitename;
00063 }
00064
00065 $items = array();
00066
00067 while ( $row = $res->getNext() ) {
00068 $items[] = $this->getItemForResultRow( $row )->text();
00069 }
00070
00071 $result = implode( '', $items );
00072 } else {
00073 if ( $this->getSearchLabel( $outputmode ) ) {
00074 $label = $this->getSearchLabel( $outputmode );
00075 } else {
00076 $label = wfMsgForContent( 'srf_bibtex_link' );
00077 }
00078
00079 $link = $res->getQueryLink( $label );
00080 $link->setParameter( 'bibtex', 'format' );
00081
00082 if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
00083 $link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
00084 }
00085
00086 $result .= $link->getText( $outputmode, $this->mLinker );
00087 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML );
00088 }
00089
00090 return $result;
00091 }
00092
00102 protected function getItemForResultRow( array $row ) {
00103 $type = '';
00104 $address = '';
00105 $annote = '';
00106 $author = '';
00107 $booktitle = '';
00108 $chapter = '';
00109 $crossref = '';
00110 $doi = '';
00111 $edition = '';
00112 $editor = '';
00113 $eprint = '';
00114 $howpublished = '';
00115 $institution = '';
00116 $journal = '';
00117 $key = '';
00118 $month = '';
00119 $note = '';
00120 $number = '';
00121 $organization = '';
00122 $pages = '';
00123 $publisher = '';
00124 $school = '';
00125 $series = '';
00126 $title = '';
00127 $type = '';
00128 $url = '';
00129 $volume = '';
00130 $year = '';
00131
00132 foreach ( $row as $field ) {
00133 $req = $field->getPrintRequest();
00134 $label = strtolower( $req->getLabel() );
00135 $var = false;
00136
00137 switch ( $label ) {
00138 case 'type': $var =& $type; break;
00139 case 'address': $var =& $address; break;
00140 case 'annote': $var =& $annote; break;
00141 case 'booktitle': $var =& $booktitle; break;
00142 case 'chapter': $var =& $chapter; break;
00143 case 'crossref': $var =& $crossref; break;
00144 case 'doi': $var =& $doi; break;
00145 case 'edition': $var =& $edition; break;
00146 case 'eprint': $var =& $eprint; break;
00147 case 'howpublished': $var =& $howpublished; break;
00148 case 'institution': $var =& $institution; break;
00149 case 'journal': $var =& $journal; break;
00150 case 'key': $var =& $key; break;
00151 case 'note': $var =& $note; break;
00152 case 'number': $var =& $number; break;
00153 case 'organization': $var =& $organization; break;
00154 case 'pages': $var =& $pages; break;
00155 case 'publisher': $var =& $publisher; break;
00156 case 'school': $var =& $school; break;
00157 case 'series': $var =& $series; break;
00158 case 'title': $var =& $title; break;
00159 case 'url': $var =& $url; break;
00160 case 'year': $var =& $year; break;
00161 case 'month': $var =& $month; break;
00162 case 'volume': case 'journal_volume': $var =& $volume; break;
00163 }
00164
00165 if ( $var !== false ) {
00166 $dataValue = $field->getNextDataValue();
00167
00168 if ( $dataValue !== false ) {
00169 $var = $dataValue->getShortWikiText();
00170 }
00171
00172 unset( $var );
00173 }
00174 else {
00175 switch ( $label ) {
00176 case 'author': case 'authors': case 'editor' : case 'editors':
00177 $wikiTexts = array();
00178 while ( ( $dataValue = $field->getNextDataValue() ) !== false ) {
00179 $wikiTexts[] = $dataValue->getShortWikiText();
00180 }
00181 $wikiText = $GLOBALS['wgLang']->listToText( $wikiTexts );
00182
00183 if ( $label == 'author' || $label == 'authors' ) {
00184 $author = $wikiText;
00185 }
00186 else {
00187 $author = $editor;
00188 }
00189 break;
00190 case 'date':
00191 $dataValue = $field->getNextDataValue();
00192
00193 if ( $dataValue !== false && get_class( $dataValue ) == 'SMWTimeValue' ) {
00194 $year = $dataValue->getYear();
00195 $month = $dataValue->getMonth();
00196 }
00197 break;
00198 }
00199 }
00200 }
00201
00202 return new SMWBibTeXEntry( $type, $address, $annote, $author, $booktitle, $chapter, $crossref, $doi, $edition, $editor, $eprint, $howpublished, $institution, $journal, $key, $month, $note, $number, $organization, $pages, $publisher, $school, $series, $title, $url, $volume, $year );
00203 }
00204
00205 public function getParameters() {
00206 return array_merge( parent::getParameters(), $this->exportFormatParameters() );
00207 }
00208
00209 }
00210
00215 class SMWBibTeXEntry {
00216 private $bibTeXtype;
00217 private $URI;
00218 private $fields = array();
00219
00220 public function __construct( $type, $address, $annote, $author, $booktitle, $chapter, $crossref, $doi, $edition, $editor, $eprint, $howpublished, $institution, $journal, $key, $month, $note, $number, $organization, $pages, $publisher, $school, $series, $title, $url, $volume, $year ) {
00221 if ( $type ) $this->bibTeXtype = ucfirst( $type ); else $this->bibTeXtype = 'Book';
00222
00223 $fields = array();
00224
00225 if ( $address ) $fields['address'] = $address;
00226 if ( $annote ) $fields['annote'] = $annote;
00227 if ( $author ) $fields['author'] = $author;
00228 if ( $booktitle ) $fields['booktitle'] = $booktitle;
00229 if ( $chapter ) $fields['chapter'] = $chapter;
00230 if ( $crossref ) $fields['crossref'] = $crossref;
00231 if ( $doi ) $fields['doi'] = $doi;
00232 if ( $edition ) $fields['edition'] = $edition;
00233 if ( $editor ) $fields['editor'] = $editor;
00234 if ( $eprint ) $fields['eprint'] = $eprint;
00235 if ( $howpublished ) $fields['howpublished'] = $howpublished;
00236 if ( $institution ) $fields['institution'] = $institution;
00237 if ( $journal ) $fields['journal'] = $journal;
00238 if ( $key ) $fields['key'] = $key;
00239 if ( $month ) $fields['month'] = $month;
00240 if ( $note ) $fields['note'] = $note;
00241 if ( $number ) $fields['number'] = $number;
00242 if ( $organization ) $fields['organization'] = $organization;
00243 if ( $pages ) $fields['pages'] = $pages;
00244 if ( $publisher ) $fields['publisher'] = $publisher;
00245 if ( $school ) $fields['school'] = $school;
00246 if ( $series ) $fields['series'] = $series;
00247 if ( $title ) $fields['title'] = $title;
00248 if ( $url ) $fields['url'] = $url;
00249 if ( $volume ) $fields['volume'] = $volume;
00250 if ( $year ) $fields['year'] = $year;
00251
00252 $this->fields = $fields;
00253
00254
00255 $URI = '';
00256 if ( $author ) {
00257 $authors = explode( ',', $author );
00258 $authors = explode( wfMsg( 'and' ), $authors[0] );
00259 $arrayAuthor = explode( ' ', $authors[0], 2 );
00260 $URI .= str_replace( ' ', '', $arrayAuthor[array_key_exists( 1, $arrayAuthor ) ? 1 : 0] );
00261 }
00262
00263 if ( $year ) {
00264 $URI .= $year;
00265 }
00266
00267 if ( $title ) {
00268 foreach ( explode( ' ', $title ) as $titleWord ) {
00269 $charsTitleWord = preg_split( '//', $titleWord, - 1, PREG_SPLIT_NO_EMPTY );
00270
00271 if ( !empty( $charsTitleWord ) ) {
00272 $URI .= $charsTitleWord[0];
00273 }
00274 }
00275 }
00276
00277 $this->URI = strtolower( $URI );
00278 }
00279
00280
00284 public function text() {
00285 $text = '@' . $this->bibTeXtype . '{' . $this->URI . ",\r\n";
00286
00287 foreach ( $this->fields as $key => $value ) {
00288 $text .= ' ' . $key . ' = "' . $value . '", ' . "\r\n";
00289 }
00290
00291 $text .= "}\r\n\r\n";
00292
00293 return $text;
00294 }
00295
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387