6a3ecdb57f424428837c2a2ddc82c7724824578f
[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 = null, $revId = null, $modelName = null ) { #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 # TODO: EditPage::mergeChanges( Content $a, Content $b )
58 # TODO: Wikipage::isCountable(Content $a)
59 # TODO: Title::newFromRedirectRecurse( $this->getRawText() );
60
61 # TODO: isCacheable( )
62 # TODO: getSize( )
63
64 # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
65 # TODO: WikiPage::replaceSection( $section, $text, $sectionTitle = '', $edittime = null )
66 # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags )
67
68 # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
69
70 }
71
72 class TextContent extends Content {
73 public function __construct( $text, Title $title = null, $revId = null, $modelName = null ) {
74 parent::__construct($title, $revId, $modelName);
75
76 $this->mText = $text;
77 }
78
79 public function getSearchText( $obj ) {
80 return $this->getRawData();
81 }
82
83 public function getWikitextForTransclusion( $obj ) {
84 return $this->getRawData();
85 }
86
87
88 public function getParserOutput( ParserOptions $options = null ) {
89 # generic implementation, relying on $this->getHtml()
90
91 $html = $this->getHtml( $options );
92 $po = new ParserOutput( $html );
93
94 if ( $this->mTitle ) $po->setTitleText( $this->mTitle->getText() );
95
96 #TODO: cache settings, etc?
97
98 return $po;
99 }
100
101 public function getHtml( ParserOptions $options ) {
102 $html = "";
103 $html .= "<pre class=\"mw-code\" dir=\"ltr\">\n";
104 $html .= htmlspecialchars( $this->getRawData() );
105 $html .= "\n</pre>\n";
106
107 return $html;
108 }
109
110
111 public function getRawData( ) {
112 $text = $this->mText;
113 return $text;
114 }
115
116 public function getRedirectChain() {
117 #XXX: really do this for all text, or just in WikitextContent
118 $text = $this->getRawData();
119 return Title::newFromRedirectArray( $text );
120 }
121 }
122
123 class WikitextContent extends TextContent {
124 public function __construct( $text, Title $title, $revId = null) {
125 parent::__construct($text, $title, $revId, CONTENT_MODEL_WIKITEXT);
126
127 $this->mDefaultParserOptions = null;
128 }
129
130 public function getDefaultParserOptions() {
131 global $wgUser, $wgContLang;
132
133 if ( !$this->mDefaultParserOptions ) {
134 #TODO: use static member?!
135 $this->mDefaultParserOptions = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
136 }
137
138 return $this->mDefaultParserOptions;
139 }
140
141 public function getParserOutput( ParserOptions $options = null ) {
142 global $wgParser;
143
144 #TODO: quick local cache: if $options is NULL, use ->mParserOutput!
145 #FIXME: need setParserOutput, so we can use stuff from the parser cache??
146 #FIXME: ...or we somehow need to know the parser cache key??
147
148 if ( !$options ) {
149 $options = $this->getDefaultParserOptions();
150 }
151
152 $po = $wgParser->parse( $this->mText, $this->getTitle(), $options, true, true, $this->mRevId );
153
154 return $po;
155 }
156
157 public function getSection( $section ) {
158 global $wgParser;
159
160 $text = $this->getRawData();
161 return $wgParser->getSection( $text, $section, false );
162 }
163
164 }
165
166 class MessageContent extends TextContent {
167 public function __construct( $msg_key, $params = null, $options = null ) {
168 parent::__construct(null, null, null, CONTENT_MODEL_WIKITEXT);
169
170 $this->mMessageKey = $msg_key;
171
172 $this->mParameters = $params;
173
174 if ( !$options ) $options = array();
175 $this->mOptions = $options;
176
177 $this->mHtmlOptions = null;
178 }
179
180
181 public function getHtml( ParserOptions $options ) {
182 $opt = array_merge( $this->mOptions, array('parse') );
183 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
184 }
185
186
187 public function getRawData( ) {
188 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
189
190 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
191 }
192
193 }
194
195
196 class JavaScriptContent extends TextContent {
197 public function __construct( $text, Title $title, $revId = null ) {
198 parent::__construct($text, $title, $revId, CONTENT_MODEL_JAVASCRIPT);
199 }
200
201 public function getHtml( ParserOptions $options ) {
202 $html = "";
203 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
204 $html .= htmlspecialchars( $this->getRawData() );
205 $html .= "\n</pre>\n";
206
207 return $html;
208 }
209
210 }
211
212 class CssContent extends TextContent {
213 public function __construct( $text, Title $title, $revId = null ) {
214 parent::__construct($text, $title, $revId, CONTENT_MODEL_CSS);
215 }
216
217 public function getHtml( ParserOptions $options ) {
218 $html = "";
219 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
220 $html .= htmlspecialchars( $this->getRawData() );
221 $html .= "\n</pre>\n";
222
223 return $html;
224 }
225 }
226
227 #FIXME: special type for redirects?!
228 #FIXME: special type for message-based pseudo-content? with raw html?
229
230 #TODO: MultipartMultipart < WikipageContent (Main + Links + X)
231 #TODO: LinksContent < LanguageLinksContent, CategoriesContent
232 #EXAMPLE: CoordinatesContent
233 #EXAMPLE: WikidataContent