SpecialCite_body.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. if (!defined('MEDIAWIKI')) die();
  3. global $wgContLang, $wgContLanguageCode, $wgCiteDefaultText;
  4. $dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
  5. $code = $wgContLang->lc( $wgContLanguageCode );
  6. $file = file_exists( "${dir}cite_text-$code" ) ? "${dir}cite_text-$code" : "${dir}cite_text";
  7. $wgCiteDefaultText = file_get_contents( $file );
  8. class SpecialCite extends SpecialPage {
  9. function SpecialCite() {
  10. SpecialPage::SpecialPage( 'Cite' );
  11. }
  12. function execute( $par ) {
  13. global $wgOut, $wgRequest, $wgUseTidy;
  14. wfLoadExtensionMessages( 'SpecialCite' );
  15. // Having tidy on causes whitespace and <pre> tags to
  16. // be generated around the output of the CiteOutput
  17. // class TODO FIXME.
  18. $wgUseTidy = false;
  19. $this->setHeaders();
  20. $this->outputHeader();
  21. $page = isset( $par ) ? $par : $wgRequest->getText( 'page' );
  22. $id = $wgRequest->getInt( 'id' );
  23. $title = Title::newFromText( $page );
  24. if ( $title ) {
  25. $article = new Article( $title );
  26. }
  27. $cform = new CiteForm( $title );
  28. if ( !$title || ! $article->exists() )
  29. $cform->execute();
  30. else {
  31. $cform->execute();
  32. $cout = new CiteOutput( $title, $article, $id );
  33. $cout->execute();
  34. }
  35. }
  36. }
  37. class CiteForm {
  38. var $mTitle;
  39. function CiteForm( &$title ) {
  40. $this->mTitle =& $title;
  41. }
  42. function execute() {
  43. global $wgOut, $wgTitle;
  44. $wgOut->addHTML(
  45. Xml::element( 'form',
  46. array(
  47. 'id' => 'specialcite',
  48. 'method' => 'get',
  49. 'action' => $wgTitle->escapeLocalUrl()
  50. ),
  51. null
  52. ) .
  53. Xml::openElement( 'label' ) .
  54. wfMsgHtml( 'cite_page' ) . ' ' .
  55. Xml::element( 'input',
  56. array(
  57. 'type' => 'text',
  58. 'size' => 30,
  59. 'name' => 'page',
  60. 'value' => is_object( $this->mTitle ) ? $this->mTitle->getPrefixedText() : ''
  61. ),
  62. ''
  63. ) .
  64. ' ' .
  65. Xml::element( 'input',
  66. array(
  67. 'type' => 'submit',
  68. 'value' => wfMsgHtml( 'cite_submit' )
  69. ),
  70. ''
  71. ) .
  72. Xml::closeElement( 'label' ) .
  73. Xml::closeElement( 'form' )
  74. );
  75. }
  76. }
  77. class CiteOutput {
  78. var $mTitle, $mArticle, $mId;
  79. var $mParser, $mParserOptions;
  80. function CiteOutput( &$title, &$article, $id ) {
  81. global $wgHooks, $wgParser;
  82. $this->mTitle =& $title;
  83. $this->mArticle =& $article;
  84. $this->mId = $id;
  85. $wgHooks['ParserGetVariableValueVarCache'][] = array( $this, 'varCache' );
  86. $this->genParserOptions();
  87. $this->genParser();
  88. $wgParser->setHook( 'citation', array( $this, 'CiteParse' ) );
  89. }
  90. function execute() {
  91. global $wgOut, $wgUser, $wgParser, $wgHooks, $wgCiteDefaultText;
  92. $wgHooks['ParserGetVariableValueTs'][] = array( $this, 'timestamp' );
  93. $msg = wfMsgForContentNoTrans( 'cite_text' );
  94. if( $msg == '' ) {
  95. $msg = $wgCiteDefaultText;
  96. }
  97. $this->mArticle->fetchContent( $this->mId, false );
  98. $ret = $wgParser->parse( $msg, $this->mTitle, $this->mParserOptions, false, true, $this->mArticle->getRevIdFetched() );
  99. $wgOut->addHTML( $ret->getText() );
  100. }
  101. function genParserOptions() {
  102. global $wgUser;
  103. $this->mParserOptions = ParserOptions::newFromUser( $wgUser );
  104. $this->mParserOptions->setDateFormat( MW_DATE_DEFAULT );
  105. $this->mParserOptions->setEditSection( false );
  106. }
  107. function genParser() {
  108. $this->mParser = new Parser;
  109. }
  110. function CiteParse( $in, $argv ) {
  111. global $wgTitle;
  112. $ret = $this->mParser->parse( $in, $wgTitle, $this->mParserOptions, false );
  113. return $ret->getText();
  114. }
  115. function varCache() { return false; }
  116. function timestamp( &$parser, &$ts ) {
  117. if ( isset( $parser->mTagHooks['citation'] ) )
  118. $ts = wfTimestamp( TS_UNIX, $this->mArticle->getTimestamp() );
  119. return true;
  120. }
  121. }