fixed several bugs (this is still a mess)
[lhc/web/wiklou.git] / includes / ContentHandler.php
1 <?php
2
3 /**
4 * A content handler knows how do deal with a specific type of content on a wiki page.
5 * Content is stored in the database in a serialized form (using a serialization format aka mime type)
6 * and is be unserialized into it's native PHP represenation (the content model).
7 *
8 * Some content types have a flat model, that is, their native represenation is the
9 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
10 * this also applies to wikitext (mediawiki's default content type), but wikitext
11 * content may be represented by a DOM or AST structure in the future.
12 *
13 */
14 abstract class ContentHandler {
15
16 public static function getContentText( Content $content ) {
17 if ( !$content ) return '';
18
19 if ( $content instanceof TextContent ) {
20 #XXX: or check by model name?
21 #XXX: or define $content->allowRawData()?
22 return $content->getRawData();
23 }
24
25 return null;
26 }
27
28 public static function makeContent( $text, Title $title, $format = null, $revId = null ) {
29 $handler = ContentHandler::getForTitle( $title );
30
31 #FIXME: pass revid?
32 return $handler->unserialize( $text, $title, $format );
33 }
34
35 public static function getDefaultModelFor( Title $title ) {
36 global $wgNamespaceContentModels;
37
38 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
39 # because it is used to initialized the mContentModelName memebr.
40
41 $ns = $title->getNamespace();
42
43 $ext = false;
44 $m = null;
45 $model = null;
46
47 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
48 $model = $wgNamespaceContentModels[ $ns ];
49 }
50
51 # hook can determin default model
52 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
53 if ( $model ) return $model;
54 }
55
56 # Could this page contain custom CSS or JavaScript, based on the title?
57 $isCssOrJsPage = ( NS_MEDIAWIKI == $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
58 if ( $isCssOrJsPage ) $ext = $m[1];
59
60 # hook can force js/css
61 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
62
63 # Is this a .css subpage of a user page?
64 $isJsCssSubpage = ( NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
65 if ( $isJsCssSubpage ) $ext = $m[1];
66
67 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
68 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT || $model === null );
69 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
70
71 # hook can override $isWikitext
72 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
73
74 if ( !$isWikitext ) {
75
76 if ( $ext == 'js' )
77 return CONTENT_MODEL_JAVASCRIPT;
78 else if ( $ext == 'css' )
79 return CONTENT_MODEL_CSS;
80
81 if ( $model )
82 return $model;
83 else
84 return CONTENT_MODEL_TEXT;
85 }
86
87 # we established that is must be wikitext
88 return CONTENT_MODEL_WIKITEXT;
89 }
90
91 public static function getForTitle( Title $title ) {
92 $modelName = $title->getContentModelName();
93 return ContenteHandler::getForModelName( $modelName );
94 }
95
96 public static function getForContent( Content $content ) {
97 $modelName = $content->getModelName();
98 return ContenteHandler::getForModelName( $modelName );
99 }
100
101 public static function getForModelName( $modelName ) {
102 global $wgContentHandlers;
103
104 if ( empty( $wgContentHandlers[$modelName] ) ) {
105 #FIXME: hook here!
106 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
107 }
108
109 if ( is_string( $wgContentHandlers[$modelName] ) ) {
110 $class = $wgContentHandlers[$modelName];
111 $wgContentHandlers[$modelName] = new $class( $modelName );
112 }
113
114 return $wgContentHandlers[$modelName];
115 }
116
117 # ----------------------------------------------------------------------------------------------------------
118 public function __construct( $modelName, $formats ) {
119 $this->mModelName = $modelName;
120 $this->mSupportedFormats = $formats;
121 }
122
123 public function getModelName() {
124 # for wikitext: wikitext; in the future: wikiast, wikidom?
125 # for wikidata: wikidata
126 return $this->mModelName;
127 }
128
129
130 public function getSupportedFormats() {
131 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
132 # for wikidata: "application/json", "application/x-php", etc
133 return $this->mSupportedFormats;
134 }
135
136 public function getDefaultFormat() {
137 return $this->mSupportedFormats[0];
138 }
139
140 public abstract function serialize( Content $content, Title $title, $format = null );
141
142 public abstract function unserialize( $blob, Title $title, $format = null ); #FIXME: ...and revId?
143
144 # public abstract function doPreSaveTransform( $title, $obj ); #TODO...
145
146 /**
147 * Return an Article object suitable for viewing the given object
148 *
149 * @param type $title
150 * @param type $obj
151 * @return \Article
152 * @todo Article is being refactored into an action class, keep track of that
153 */
154 public function createArticle( Title $title, $obj ) { #TODO: use this!
155 $article = new Article($title);
156 return $article;
157 }
158
159 /**
160 * Return an EditPage object suitable for editing the given object
161 *
162 * @param type $title
163 * @param type $obj
164 * @param type $article
165 * @return \EditPage
166 */
167 public function createEditPage( Title $title, $obj, Article $article ) { #TODO: use this!
168 $editPage = new EditPage($article);
169 return $editPage;
170 }
171
172 /**
173 public function updatePage( $title, $obj ) {
174 }
175 **/
176
177 public function getDiffEngine( Article $article ) {
178 $de = new DifferenceEngine( $article->getContext() );
179 return $de;
180 }
181
182 public function getIndexUpdateJobs( Title $title, ParserOutput $parserOutput, $recursive = true ) {
183 # for wikitext, create a LinksUpdate object
184 # for wikidata: serialize arrays to json
185 $update = new LinksUpdate( $title, $parserOutput, $recursive );
186 return $update;
187 }
188
189 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
190 }
191
192
193 abstract class TextContentHandler extends ContentHandler {
194
195 public function __construct( $modelName, $formats ) {
196 super::__construct( $modelName, $formats );
197 }
198
199 public function serialize( Content $content, Title $title, $format = null ) {
200 return $content->getRawData();
201 }
202
203 }
204 class WikitextContentHandler extends TextContentHandler {
205
206 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
207 super::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
208 }
209
210 public function unserialize( $text, Title $title, $format = null ) {
211 return new WikitextContent($text, $title);
212 }
213
214 }
215
216 class JavaScriptContentHandler extends TextContentHandler {
217
218 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
219 super::__construct( $modelName, array( 'text/javascript' ) );
220 }
221
222 public function unserialize( $text, Title $title, $format = null ) {
223 return new JavaScriptContent($text, $title);
224 }
225
226 }
227
228 class CssContentHandler extends TextContentHandler {
229
230 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
231 super::__construct( $modelName, array( 'text/css' ) );
232 }
233
234 public function unserialize( $text, Title $title, $format = null ) {
235 return new CssContent($text, $title);
236 }
237
238 }