adapted WikiPage and Article (work in progress)
[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 ) {
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 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
47 }
48
49 class TextContent extends Content {
50 public function __construct( $text, Title $title, $revId, $modelName ) {
51 parent::__construct($title, $revId, $modelName);
52
53 $this->mText = $text;
54 }
55
56 public function getSearchText( $obj ) {
57 return $this->getRawData();
58 }
59
60 public function getWikitextForTransclusion( $obj ) {
61 return $this->getRawData();
62 }
63
64
65 public function getParserOutput( ParserOptions $options = null ) {
66 # generic implementation, relying on $this->getHtml()
67
68 $html = $this->getHtml( $options );
69 $po = new ParserOutput( $html );
70
71 if ( $this->mTitle ) $po->setTitleText( $this->mTitle->getText() );
72
73 #TODO: cache settings, etc?
74
75 return $po;
76 }
77
78 public function getHtml( ParserOptions $options ) {
79 $html = "";
80 $html .= "<pre class=\"mw-code\" dir=\"ltr\">\n";
81 $html .= htmlspecialchars( $this->getRawData() );
82 $html .= "\n</pre>\n";
83
84 return $html;
85 }
86
87
88 public function getRawData( ) {
89 $text = $this->mText;
90 return $text;
91 }
92
93 }
94
95 class WikitextContent extends TextContent {
96 public function __construct( $text, Title $title, $revId = null) {
97 parent::__construct($text, $title, $revId, CONTENT_MODEL_WIKITEXT);
98
99 $this->mDefaultParserOptions = null;
100 }
101
102 public function getDefaultParserOptions() {
103 global $wgUser, $wgContLang;
104
105 if ( !$this->mDefaultParserOptions ) {
106 #TODO: use static member?!
107 $this->mDefaultParserOptions = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
108 }
109
110 return $this->mDefaultParserOptions;
111 }
112
113 public function getParserOutput( ParserOptions $options = null ) {
114 global $wgParser;
115
116 #TODO: quick local cache: if $options is NULL, use ->mParserOutput!
117 #FIXME: need setParserOutput, so we can use stuff from the parser cache??
118 #FIXME: ...or we somehow need to know the parser cache key??
119
120 if ( !$options ) {
121 $options = $this->getDefaultParserOptions();
122 }
123
124 $po = $wgParser->parse( $this->mText, $this->getTitle(), $options );
125
126 return $po;
127 }
128
129 }
130
131 class MessageContent extends TextContent {
132 public function __construct( $msg_key, $params = null, $options = null ) {
133 parent::__construct(null, null, null, CONTENT_MODEL_WIKITEXT);
134
135 $this->mMessageKey = $msg_key;
136
137 $this->mParameters = $params;
138
139 if ( !$options ) $options = array();
140 $this->mOptions = $options;
141
142 $this->mHtmlOptions = null;
143 }
144
145
146 public function getHtml( ParserOptions $options ) {
147 $opt = array_merge( $this->mOptions, array('parse') );
148 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
149 }
150
151
152 public function getRawData( ) {
153 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
154
155 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
156 }
157
158 }
159
160
161 class JavaScriptContent extends TextContent {
162 public function __construct( $text, Title $title, $revId = null ) {
163 parent::__construct($text, $title, $revId, CONTENT_MODEL_JAVASCRIPT);
164 }
165
166 public function getHtml( ParserOptions $options ) {
167 $html = "";
168 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
169 $html .= htmlspecialchars( $this->getRawData() );
170 $html .= "\n</pre>\n";
171
172 return $html;
173 }
174
175 }
176
177 class CssContent extends TextContent {
178 public function __construct( $text, Title $title, $revId = null ) {
179 parent::__construct($text, $title, $revId, CONTENT_MODEL_CSS);
180 }
181
182 public function getHtml( ParserOptions $options ) {
183 $html = "";
184 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
185 $html .= htmlspecialchars( $this->getRawData() );
186 $html .= "\n</pre>\n";
187
188 return $html;
189 }
190 }
191
192 #FIXME: special type for redirects?!
193 #FIXME: special type for message-based pseudo-content? with raw html?
194
195 #TODO: MultipartMultipart < WikipageContent (Main + Links + X)
196 #TODO: LinksContent < LanguageLinksContent, CategoriesContent
197 #EXAMPLE: CoordinatesContent
198 #EXAMPLE: WikidataContent