mediawiki-extensions-Cite/SpecialCite_body.php

180 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2010-04-17 21:07:37 +00:00
if ( !defined( 'MEDIAWIKI' ) ) die();
2011-04-29 10:44:56 +00:00
global $wgContLang, $wgContLanguageCode, $wgCiteDefaultText;
$dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
$code = $wgContLang->lc( $wgContLanguageCode );
$file = file_exists( "${dir}cite_text-$code" ) ? "${dir}cite_text-$code" : "${dir}cite_text";
$wgCiteDefaultText = file_get_contents( $file );
class SpecialCite extends SpecialPage {
function __construct() {
parent::__construct( 'Cite' );
}
function execute( $par ) {
2010-07-24 21:12:27 +00:00
global $wgRequest, $wgUseTidy;
// Having tidy on causes whitespace and <pre> tags to
// be generated around the output of the CiteOutput
// class TODO FIXME.
$wgUseTidy = false;
$this->setHeaders();
$this->outputHeader();
$page = $par !== null ? $par : $wgRequest->getText( 'page' );
$title = Title::newFromText( $page );
$cform = new CiteForm( $title );
$cform->execute();
if ( $title && $title->exists() ) {
$id = $wgRequest->getInt( 'id' );
$cout = new CiteOutput( $title, $id );
$cout->execute();
}
}
}
class CiteForm {
2011-05-28 20:44:24 +00:00
/**
* @var Title
*/
var $mTitle;
function __construct( &$title ) {
$this->mTitle =& $title;
}
function execute() {
global $wgOut, $wgScript;
$wgOut->addHTML(
Xml::openElement( 'form',
array(
'id' => 'specialcite',
'method' => 'get',
'action' => $wgScript
) ) .
Html::hidden( 'title', SpecialPage::getTitleFor( 'Cite' )->getPrefixedDBkey() ) .
Xml::openElement( 'label' ) .
wfMsgHtml( 'cite_page' ) . ' ' .
Xml::element( 'input',
array(
'type' => 'text',
2008-05-01 12:48:22 +00:00
'size' => 30,
'name' => 'page',
'value' => is_object( $this->mTitle ) ? $this->mTitle->getPrefixedText() : ''
),
''
) .
' ' .
Xml::element( 'input',
array(
'type' => 'submit',
'value' => wfMsgHtml( 'cite_submit' )
),
''
) .
Xml::closeElement( 'label' ) .
Xml::closeElement( 'form' )
);
}
}
class CiteOutput {
2011-05-28 20:44:24 +00:00
/**
* @var Title
*/
var $mTitle;
/**
* @var Article
*/
var $mArticle;
var $mId;
/**
* @var Parser
*/
var $mParser;
/**
* @var ParserOptions
*/
var $mParserOptions;
var $mSpTitle;
function __construct( $title, $id ) {
global $wgHooks, $wgParser;
$this->mTitle = $title;
$this->mArticle = new Article( $title );
$this->mId = $id;
$wgHooks['ParserGetVariableValueVarCache'][] = array( $this, 'varCache' );
$this->genParserOptions();
$this->genParser();
$wgParser->setHook( 'citation', array( $this, 'CiteParse' ) );
}
function execute() {
2010-07-24 21:32:07 +00:00
global $wgOut, $wgParser, $wgHooks, $wgCiteDefaultText;
$wgHooks['ParserGetVariableValueTs'][] = array( $this, 'timestamp' );
$msg = wfMsgForContentNoTrans( 'cite_text' );
2010-04-17 21:07:37 +00:00
if ( $msg == '' ) {
$msg = $wgCiteDefaultText;
}
$ret = $wgParser->parse( $msg, $this->mTitle, $this->mParserOptions, false, true, $this->getRevId() );
2008-11-06 22:20:29 +00:00
$wgOut->addHTML( $ret->getText() );
}
function genParserOptions() {
global $wgUser;
$this->mParserOptions = ParserOptions::newFromUser( $wgUser );
$this->mParserOptions->setDateFormat( MW_DATE_DEFAULT );
$this->mParserOptions->setEditSection( false );
}
function genParser() {
$this->mParser = new Parser;
$this->mSpTitle = SpecialPage::getTitleFor( 'Cite' );
}
function CiteParse( $in, $argv ) {
$ret = $this->mParser->parse( $in, $this->mSpTitle, $this->mParserOptions, false );
return $ret->getText();
}
2011-05-28 20:44:24 +00:00
function varCache() {
return false;
}
function timestamp( &$parser, &$ts ) {
2011-05-28 20:44:24 +00:00
if ( isset( $parser->mTagHooks['citation'] ) ) {
$ts = wfTimestamp( TS_UNIX, $this->mArticle->getTimestamp() );
2011-05-28 20:44:24 +00:00
}
return true;
}
function getRevId() {
if ( $this->mId ) {
return $this->mId;
} else {
return $this->mTitle->getLatestRevID();
}
}
}