Advertisement
Guest User

Untitled

a guest
Jun 5th, 2011
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.65 KB | None | 0 0
  1. class SRFBibTeX extends SMWResultPrinter {
  2.     protected $m_title = '';
  3.     protected $m_description = '';
  4.  
  5.     public function getMimeType( $res ) {
  6.         return 'text/bibtex';
  7.     }
  8.  
  9.     public function getFileName( $res ) {
  10.         if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
  11.             return str_replace( ' ', '_', $this->getSearchLabel( SMW_OUTPUT_WIKI ) ) . '.bib';
  12.         } else {
  13.             return 'BibTeX.bib';
  14.         }
  15.     }
  16.  
  17.     public function getQueryMode( $context ) {
  18.         return ( $context == SMWQueryProcessor::SPECIAL_PAGE ) ? SMWQuery::MODE_INSTANCES:SMWQuery::MODE_NONE;
  19.     }
  20.  
  21.     public function getName() {
  22.         return wfMsg( 'srf_printername_bibtex' );
  23.     }
  24.  
  25.     protected function getResultText( SMWQueryResult $res, $outputmode ) {
  26.         global $wgSitename;
  27.         $result = '';
  28.         $items = array();
  29.         if ( $outputmode == SMW_OUTPUT_FILE ) { // make file
  30.             if ( $this->m_title == '' ) {
  31.                 $this->m_title = $wgSitename;
  32.             }
  33.             $row = $res->getNext();
  34.             while ( $row !== false ) {
  35.                 $type = '';
  36.                 $address = '';
  37.                 $annote = '';
  38.                 $author = '';
  39.                 $booktitle = '';
  40.                 $chapter = '';
  41.                 $crossref = '';
  42.                 $doi = '';
  43.                 $edition = '';
  44.                 $editor = '';
  45.                 $eprint = '';
  46.                 $howpublished = '';
  47.                 $institution = '';
  48.                 $journal = '';
  49.                 $key = '';
  50.                 $month = '';
  51.                 $note = '';
  52.                 $number = '';
  53.                 $organization = '';
  54.                 $pages = '';
  55.                 $publisher = '';
  56.                 $school = '';
  57.                 $series = '';
  58.                 $title = '';
  59.                 $type = '';
  60.                 $url = '';
  61.                 $volume = '';
  62.                 $year = '';
  63.  
  64.                 foreach ( $row as $field ) {
  65.                     $req = $field->getPrintRequest();
  66.  
  67.                     if ( ( strtolower( $req->getLabel() ) == "type" ) ) {
  68.                         $value = current( $field->getContent() );
  69.                         if ( $value !== false ) {
  70.                         $type = $value->getShortWikiText();
  71.                         }
  72.                     }
  73.  
  74.  
  75.                     if ( ( strtolower( $req->getLabel() ) == "address" ) ) {
  76.                         $value = current( $field->getContent() );
  77.                         if ( $value !== false ) {
  78.                         $address = $value->getShortWikiText();
  79.                         }
  80.                     }
  81.                     if ( ( strtolower( $req->getLabel() ) == "annote" ) ) {
  82.                         $value = current( $field->getContent() );
  83.                         if ( $value !== false ) {
  84.                         $annote = $value->getShortWikiText();
  85.                         }
  86.                     }
  87.                     /* for flexibility, we permit 'author' or 'authors' */
  88.                     if ( ( strtolower( $req->getLabel() ) == "author" ) || ( strtolower( $req->getLabel() ) == "authors" )  ) {
  89.                         foreach ( $field->getContent() as $value ) {
  90.                             $author .= ( $author ? ' and ':'' ) . $value->getShortWikiText();
  91.                         }
  92.                     }
  93.                     if ( ( strtolower( $req->getLabel() ) == "booktitle" ) ) {
  94.                         $value = current( $field->getContent() );
  95.                         if ( $value !== false ) {
  96.                         $booktitle = $value->getShortWikiText();
  97.                         }
  98.                     }
  99.                     if ( ( strtolower( $req->getLabel() ) == "chapter" ) ) {
  100.                         $value = current( $field->getContent() );
  101.                         if ( $value !== false ) {
  102.                         $chapter = $value->getShortWikiText();
  103.                         }
  104.                     }
  105.                     if ( ( strtolower( $req->getLabel() ) == "crossref" ) ) {
  106.                         $value = current( $field->getContent() );
  107.                         if ( $value !== false ) {
  108.                         $crossref = $value->getShortWikiText();
  109.                         }
  110.                     }
  111.                     if ( ( strtolower( $req->getLabel() ) == "doi" ) ) {
  112.                         $value = current( $field->getContent() );
  113.                         if ( $value !== false ) {
  114.                         $doi = $value->getShortWikiText();
  115.                         }
  116.                     }
  117.                     if ( ( strtolower( $req->getLabel() ) == "edition" ) ) {
  118.                         $value = current( $field->getContent() );
  119.                         if ( $value !== false ) {
  120.                         $edition = $value->getShortWikiText();
  121.                         }
  122.                     }
  123.                     /* for flexibility, we permit 'editor' or 'editors' */
  124.                     if ( ( strtolower( $req->getLabel() ) == "editor" ) || ( strtolower( $req->getLabel() ) == "editors" ) ) {
  125.                         foreach ( $field->getContent() as $value ) {
  126.                             $editor .= ( $editor ? ' and ':'' ) . $value->getShortWikiText();
  127.                         }
  128.                     }
  129.                     if ( ( strtolower( $req->getLabel() ) == "eprint" ) ) {
  130.                         $value = current( $field->getContent() );
  131.                         if ( $value !== false ) {
  132.                         $eprint = $value->getShortWikiText();
  133.                         }
  134.                     }
  135.                     if ( ( strtolower( $req->getLabel() ) == "howpublished" ) ) {
  136.                         $value = current( $field->getContent() );
  137.                         if ( $value !== false ) {
  138.                         $howpublished = $value->getShortWikiText();
  139.                         }
  140.                     }
  141.                     if ( ( strtolower( $req->getLabel() ) == "institution" ) ) {
  142.                         $value = current( $field->getContent() );
  143.                         if ( $value !== false ) {
  144.                         $institution = $value->getShortWikiText();
  145.                         }
  146.                     }
  147.                     if ( ( strtolower( $req->getLabel() ) == "journal" ) ) {
  148.                         $value = current( $field->getContent() );
  149.                         if ( $value !== false ) {
  150.                         $journal = $value->getShortWikiText();
  151.                         }
  152.                     }
  153.                     if ( ( strtolower( $req->getLabel() ) == "key" ) ) {
  154.                         $value = current( $field->getContent() );
  155.                         if ( $value !== false ) {
  156.                         $key = $value->getShortWikiText();
  157.                         }
  158.                     }
  159.                     if ( ( strtolower( $req->getLabel() ) == "note" ) ) {
  160.                         $value = current( $field->getContent() );
  161.                         if ( $value !== false ) {
  162.                         $note = $value->getShortWikiText();
  163.                         }
  164.                     }
  165.                     if ( ( strtolower( $req->getLabel() ) == "number" ) ) {
  166.                         $value = current( $field->getContent() );
  167.                         if ( $value !== false ) {
  168.                         $number = $value->getShortWikiText();
  169.                         }
  170.                     }
  171.                     if ( ( strtolower( $req->getLabel() ) == "organization" ) ) {
  172.                         $value = current( $field->getContent() );
  173.                         if ( $value !== false ) {
  174.                         $organization = $value->getShortWikiText();
  175.                         }
  176.                     }
  177.                     if ( ( strtolower( $req->getLabel() ) == "pages" ) ) {
  178.                         $value = current( $field->getContent() );
  179.                         if ( $value !== false ) {
  180.                         $pages = $value->getShortWikiText();
  181.                         }
  182.                     }
  183.                     if ( ( strtolower( $req->getLabel() ) == "publisher" ) ) {
  184.                         $value = current( $field->getContent() );
  185.                         if ( $value !== false ) {
  186.                         $publisher = $value->getShortWikiText();
  187.                         }
  188.                     }
  189.                     if ( ( strtolower( $req->getLabel() ) == "school" ) ) {
  190.                         $value = current( $field->getContent() );
  191.                         if ( $value !== false ) {
  192.                         $school = $value->getShortWikiText();
  193.                         }
  194.                     }
  195.                     if ( ( strtolower( $req->getLabel() ) == "series" ) ) {
  196.                         $value = current( $field->getContent() );
  197.                         if ( $value !== false ) {
  198.                         $series = $value->getShortWikiText();
  199.                         }
  200.                     }
  201.                     if ( ( strtolower( $req->getLabel() ) == "title" ) ) {
  202.                         $value = current( $field->getContent() );
  203.                         if ( $value !== false ) {
  204.                         $title = $value->getShortWikiText();
  205.                         }
  206.                     }
  207.                     if ( ( strtolower( $req->getLabel() ) == "url" ) ) {
  208.                         $value = current( $field->getContent() );
  209.                         if ( $value !== false ) {
  210.                         $url = $value->getShortWikiText();
  211.                         }
  212.                     }
  213.                     if ( ( strtolower( $req->getLabel() ) == "volume" ) || ( strtolower( $req->getLabel() ) == "journal_volume" ) ) {
  214.                         $value = current( $field->getContent() );
  215.                         if ( $value !== false ) {
  216.                         $volume = $value->getShortWikiText();
  217.                         }
  218.                     }
  219.  
  220.  
  221.  
  222.                     /*if we input a full date for the "month" and "year" BibTeX attributes, extract the month and year*/
  223.                     if ( ( strtolower( $req->getLabel() ) == "date" ) ) {
  224.                         $value = current( $field->getContent() );
  225.                         if ( get_class( $value ) == 'SMWTimeValue' ) {
  226.                             if ( $value !== false ) {
  227.                             $year = $value->getYear();
  228.                             $month = $value->getMonth();
  229.                             }
  230.                         }
  231.                     }
  232.  
  233.                     if ( ( strtolower( $req->getLabel() ) == "year" ) ) {
  234.                         $value = current( $field->getContent() );
  235.                         if ( $value !== false ) {
  236.                         $year = $value->getShortWikiText();
  237.                         }
  238.                     }
  239.                     if ( ( strtolower( $req->getLabel() ) == "month" ) ) {
  240.                         $value = current( $field->getContent() );
  241.                         if ( $value !== false ) {
  242.                         $month = $value->getShortWikiText();
  243.                         }
  244.                     }
  245.  
  246.  
  247.                 }
  248.                 $items[] = 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 );
  249.                 $row = $res->getNext();
  250.             }
  251.             foreach ( $items as $item ) {
  252.                 $result .= $item->text();
  253.             }
  254.         } else { // just make link to export
  255.             if ( $this->getSearchLabel( $outputmode ) ) {
  256.                 $label = $this->getSearchLabel( $outputmode );
  257.             } else {
  258.                 $label = wfMsgForContent( 'srf_bibtex_link' );
  259.             }
  260.             $link = $res->getQueryLink( $label );
  261.             $link->setParameter( 'bibtex', 'format' );
  262.             if ( $this->getSearchLabel( SMW_OUTPUT_WIKI ) != '' ) {
  263.                 $link->setParameter( $this->getSearchLabel( SMW_OUTPUT_WIKI ), 'searchlabel' );
  264.             }
  265.             $result .= $link->getText( $outputmode, $this->mLinker );
  266.             $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed
  267.         }
  268.         return $result;
  269.     }
  270.  
  271.     public function getParameters() {
  272.         if ( defined( 'SMW_SUPPORTS_VALIDATOR' ) ) {
  273.             return array_merge( parent::getParameters(), $this->exportFormatParameters() );
  274.         }
  275.         else {
  276.             return parent::exportFormatParameters();
  277.         }
  278.     }
  279.  
  280. }
  281.  
  282. /**
  283.  * Represents a single entry in an BibTeX
  284.  * @ingroup SMWQuery
  285.  */
  286. class SMWBibTeXEntry {
  287.     private $bibTeXtype;
  288.     private $URI;
  289.     private $fields = array();
  290.  
  291.     public function 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 ) {
  292.         if ( $type ) $this->bibTeXtype = ucfirst( $type ); else $this->bibTeXtype = 'Book';
  293.  
  294.         $fields = array();
  295.  
  296.         if ( $address ) $fields['address'] = $address;
  297.         if ( $annote ) $fields['annote'] = $annote;
  298.         if ( $author ) $fields['author'] = $author;
  299.         if ( $booktitle ) $fields['booktitle'] = $booktitle;
  300.         if ( $chapter ) $fields['chapter'] = $chapter;
  301.         if ( $crossref ) $fields['crossref'] = $crossref;
  302.         if ( $doi ) $fields['doi'] = $doi;
  303.         if ( $edition ) $fields['edition'] = $edition;
  304.         if ( $editor ) $fields['editor'] = $editor;
  305.         if ( $eprint ) $fields['eprint'] = $eprint;
  306.         if ( $howpublished ) $fields['howpublished'] = $howpublished;
  307.         if ( $institution ) $fields['institution'] = $institution;
  308.         if ( $journal ) $fields['journal'] = $journal;
  309.         if ( $key ) $fields['key'] = $key;
  310.         if ( $month ) $fields['month'] = $month;
  311.         if ( $note ) $fields['note'] = $note;
  312.         if ( $number ) $fields['number'] = $number;
  313.         if ( $organization ) $fields['organization'] = $organization;
  314.         if ( $pages ) $fields['pages'] = $pages;
  315.         if ( $publisher ) $fields['publisher'] = $publisher;
  316.         if ( $school ) $fields['school'] = $school;
  317.         if ( $series ) $fields['series'] = $series;
  318.         if ( $title ) $fields['title'] = $title;
  319.         if ( $url ) $fields['url'] = $url;
  320.         if ( $volume ) $fields['volume'] = $volume;
  321.         if ( $year ) $fields['year'] = $year;
  322.  
  323.         $this->fields = $fields;
  324.  
  325.         // generating the URI: author last name + year + first letters of title
  326.         $URI = '';
  327.         if ( $author ) {
  328.             $arrayAuthor = explode( ' ', $author );
  329.             if ( array_key_exists( 1, $arrayAuthor ) ) $URI .= $arrayAuthor[1];
  330.         }
  331.         if ( $year ) $URI .= $year;
  332.         if ( $title ) {
  333.             $arrayTitle = explode( ' ', $title );
  334.             foreach ( $arrayTitle as $titleWord ) {
  335.                         $charsTitleWord = preg_split( '//', $titleWord, - 1, PREG_SPLIT_NO_EMPTY );
  336.                         $URI .= $charsTitleWord[0];
  337.                     }
  338.         }
  339.         $this->URI = strtolower( $URI );
  340.     }
  341.  
  342.  
  343.     /**
  344.      * Creates the BibTeX output for a single item.
  345.      */
  346.     public function text() {
  347.         $text  = '@' . $this->bibTeXtype . '{' . $this->URI . ",\r\n";
  348.         foreach ( $this->fields as $key => $value ) {
  349.             $text .= '  ' . $key . ' = "' . $value . '", ' . "\r\n";
  350.         }
  351.         $text .= "}\r\n\r\n";
  352.  
  353.         return $text;
  354.     }
  355.  
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement