summaryrefslogtreecommitdiff
path: root/extensions/Cite/SpecialCite_body.php
blob: 0425c8506b9bb9bb445502fb97227a4a48a4a93a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php

class SpecialCite extends SpecialPage {
	function __construct() {
		parent::__construct( 'Cite' );
	}

	function execute( $par ) {
		global $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 : $this->getRequest()->getText( 'page' );
		$title = Title::newFromText( $page );

		$cform = new CiteForm( $title );
		$cform->execute();

		if ( $title && $title->exists() ) {
			$id = $this->getRequest()->getInt( 'id' );
			$cout = new CiteOutput( $title, $id );
			$cout->execute();
		}
	}
}

class CiteForm {
	/**
	 * @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' ) .
					wfMessage( 'cite_page' )->escaped() . ' ' .
					Xml::element( 'input',
						array(
							'type' => 'text',
							'size' => 30,
							'name' => 'page',
							'value' => is_object( $this->mTitle ) ? $this->mTitle->getPrefixedText() : ''
						),
						''
					) .
					' ' .
					Xml::element( 'input',
						array(
							'type' => 'submit',
							'value' => wfMessage( 'cite_submit' )->escaped()
						),
						''
					) .
				Xml::closeElement( 'label' ) .
			Xml::closeElement( 'form' )
		);
	}
}

class CiteOutput {
	/**
	 * @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() {
		global $wgOut, $wgParser, $wgHooks;

		$wgHooks['ParserGetVariableValueTs'][] = array( $this, 'timestamp' );

		$msg = wfMessage( 'cite_text' )->inContentLanguage()->plain();
		if ( $msg == '' ) {
			# With MediaWiki 1.20 the plain text files were deleted and the text moved into SpecialCite.i18n.php
			# This code is kept for b/c in case an installation has its own file "cite_text-xx"
			# for a previously not supported language.
			global $wgContLang, $wgContLanguageCode;
			$dir = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
			$code = $wgContLang->lc( $wgContLanguageCode );
			if ( file_exists( "${dir}cite_text-$code" ) ) {
				$msg = file_get_contents( "${dir}cite_text-$code" );
			} elseif( file_exists( "${dir}cite_text" ) ){
				$msg = file_get_contents( "${dir}cite_text" );
			}
		}
		$ret = $wgParser->parse( $msg, $this->mTitle, $this->mParserOptions, false, true, $this->getRevId() );
		$wgOut->addModules( 'ext.specialcite' );
		$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();
	}

	function varCache() {
		return false;
	}

	function timestamp( &$parser, &$ts ) {
		if ( isset( $parser->mTagHooks['citation'] ) ) {
			$ts = wfTimestamp( TS_UNIX, $this->mArticle->getTimestamp() );
		}

		return true;
	}

	function getRevId() {
		if ( $this->mId ) {
			return $this->mId;
		} else {
			return $this->mTitle->getLatestRevID();
		}
	}
}