integration with Title and Revision (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 #TODO: cache settings, etc?
72
73 return $po;
74 }
75
76 public function getHtml( ParserOptions $options ) {
77 $html = "";
78 $html .= "<pre class=\"mw-code\" dir=\"ltr\">\n";
79 $html .= htmlspecialchars( $this->getRawData() );
80 $html .= "\n</pre>\n";
81
82 return $html;
83 }
84
85
86 public function getRawData( ) {
87 global $wgParser, $wgUser;
88
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
132 class JavaScriptContent extends TextContent {
133 public function __construct( $text, Title $title, $revId = null ) {
134 parent::__construct($text, $title, $revId, CONTENT_MODEL_JAVASCRIPT);
135 }
136
137 public function getHtml( ParserOptions $options ) {
138 $html = "";
139 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
140 $html .= htmlspecialchars( $this->getRawData() );
141 $html .= "\n</pre>\n";
142
143 return $html;
144 }
145
146 }
147
148 class CssContent extends TextContent {
149 public function __construct( $text, Title $title, $revId = null ) {
150 parent::__construct($text, $title, $revId, CONTENT_MODEL_CSS);
151 }
152
153 public function getHtml( ParserOptions $options ) {
154 $html = "";
155 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
156 $html .= htmlspecialchars( $this->getRawData() );
157 $html .= "\n</pre>\n";
158
159 return $html;
160 }
161 }
162
163 #TODO: MultipartMultipart < WikipageContent (Main + Links + X)
164 #TODO: LinksContent < LanguageLinksContent, CategoriesContent
165 #EXAMPLE: CoordinatesContent
166 #EXAMPLE: WikidataContent