replacing deprecated getText, etc
[lhc/web/wiklou.git] / includes / Content.php
1 <?php
2
3 /**
4 * A content object represents page content, e.g. the text to show on a page.
5 *
6 */
7 abstract class Content {
8
9 public function __construct( Title $title, $revId, $modelName ) { #FIXME: really need revId? annoying! #FIXME: really $title? or just when parsing, every time?
10 $this->mModelName = $modelName;
11 $this->mTitle = $title;
12 $this->mRevId = $revId;
13 }
14
15 public function getModelName() {
16 return $this->mModelName;
17 }
18
19 public function getTitle() {
20 return $this->mTitle;
21 }
22
23 public function getRevId() {
24 return $this->mRevId;
25 }
26
27 public abstract function getSearchText( $obj );
28
29 public abstract function getWikitextForTransclusion( $obj );
30
31 public abstract function getParserOutput( ParserOptions $options = NULL );
32
33 public abstract function getRawData( );
34
35 public function getHtml( ParserOptions $options ) {
36 $po = $this->getParserOutput( $options );
37 return $po->getText();
38 }
39
40 public function getIndexUpdateJobs( ParserOptions $options , $recursive = true ) {
41 $po = $this->getParserOutput( $options );
42 $update = new LinksUpdate( $this->mTitle, $po, $recursive );
43 return $update;
44 }
45
46 public function getRedirectChain() {
47 return null;
48 }
49
50 public function getSection( $section ) { #FIXME: should this return text? or a Content object? or what??
51 return null;
52 }
53
54 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
55 }
56
57 class TextContent extends Content {
58 public function __construct( $text, Title $title, $revId, $modelName ) {
59 parent::__construct($title, $revId, $modelName);
60
61 $this->mText = $text;
62 }
63
64 public function getSearchText( $obj ) {
65 return $this->getRawData();
66 }
67
68 public function getWikitextForTransclusion( $obj ) {
69 return $this->getRawData();
70 }
71
72
73 public function getParserOutput( ParserOptions $options = null ) {
74 # generic implementation, relying on $this->getHtml()
75
76 $html = $this->getHtml( $options );
77 $po = new ParserOutput( $html );
78
79 if ( $this->mTitle ) $po->setTitleText( $this->mTitle->getText() );
80
81 #TODO: cache settings, etc?
82
83 return $po;
84 }
85
86 public function getHtml( ParserOptions $options ) {
87 $html = "";
88 $html .= "<pre class=\"mw-code\" dir=\"ltr\">\n";
89 $html .= htmlspecialchars( $this->getRawData() );
90 $html .= "\n</pre>\n";
91
92 return $html;
93 }
94
95
96 public function getRawData( ) {
97 $text = $this->mText;
98 return $text;
99 }
100
101 public function getRedirectChain() {
102 #XXX: really do this for all text, or just in WikitextContent
103 $text = $this->getRawData();
104 return Title::newFromRedirectArray( $text );
105 }
106 }
107
108 class WikitextContent extends TextContent {
109 public function __construct( $text, Title $title, $revId = null) {
110 parent::__construct($text, $title, $revId, CONTENT_MODEL_WIKITEXT);
111
112 $this->mDefaultParserOptions = null;
113 }
114
115 public function getDefaultParserOptions() {
116 global $wgUser, $wgContLang;
117
118 if ( !$this->mDefaultParserOptions ) {
119 #TODO: use static member?!
120 $this->mDefaultParserOptions = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
121 }
122
123 return $this->mDefaultParserOptions;
124 }
125
126 public function getParserOutput( ParserOptions $options = null ) {
127 global $wgParser;
128
129 #TODO: quick local cache: if $options is NULL, use ->mParserOutput!
130 #FIXME: need setParserOutput, so we can use stuff from the parser cache??
131 #FIXME: ...or we somehow need to know the parser cache key??
132
133 if ( !$options ) {
134 $options = $this->getDefaultParserOptions();
135 }
136
137 $po = $wgParser->parse( $this->mText, $this->getTitle(), $options, true, true, $this->mRevId );
138
139 return $po;
140 }
141
142 public function getSection( $section ) {
143 global $wgParser;
144
145 $text = $this->getRawData();
146 return $wgParser->getSection( $text, $section, false );
147 }
148
149 }
150
151 class MessageContent extends TextContent {
152 public function __construct( $msg_key, $params = null, $options = null ) {
153 parent::__construct(null, null, null, CONTENT_MODEL_WIKITEXT);
154
155 $this->mMessageKey = $msg_key;
156
157 $this->mParameters = $params;
158
159 if ( !$options ) $options = array();
160 $this->mOptions = $options;
161
162 $this->mHtmlOptions = null;
163 }
164
165
166 public function getHtml( ParserOptions $options ) {
167 $opt = array_merge( $this->mOptions, array('parse') );
168 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
169 }
170
171
172 public function getRawData( ) {
173 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
174
175 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
176 }
177
178 }
179
180
181 class JavaScriptContent extends TextContent {
182 public function __construct( $text, Title $title, $revId = null ) {
183 parent::__construct($text, $title, $revId, CONTENT_MODEL_JAVASCRIPT);
184 }
185
186 public function getHtml( ParserOptions $options ) {
187 $html = "";
188 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
189 $html .= htmlspecialchars( $this->getRawData() );
190 $html .= "\n</pre>\n";
191
192 return $html;
193 }
194
195 }
196
197 class CssContent extends TextContent {
198 public function __construct( $text, Title $title, $revId = null ) {
199 parent::__construct($text, $title, $revId, CONTENT_MODEL_CSS);
200 }
201
202 public function getHtml( ParserOptions $options ) {
203 $html = "";
204 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
205 $html .= htmlspecialchars( $this->getRawData() );
206 $html .= "\n</pre>\n";
207
208 return $html;
209 }
210 }
211
212 #FIXME: special type for redirects?!
213 #FIXME: special type for message-based pseudo-content? with raw html?
214
215 #TODO: MultipartMultipart < WikipageContent (Main + Links + X)
216 #TODO: LinksContent < LanguageLinksContent, CategoriesContent
217 #EXAMPLE: CoordinatesContent
218 #EXAMPLE: WikidataContent