messing with replaceSection()
[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 #XXX: or define $content->getDefaultWikiText()?
23 return $content->getRawData();
24 }
25
26 #XXX: this must not be used for editing, otherwise we may loose data:
27 #XXX: e.g. if this returns the "main" text from a multipart page, all attachments would be lost
28
29 return null;
30 }
31
32 public static function makeContent( $text, Title $title, $modelName = null, $format = null ) {
33 if ( !$modelName ) {
34 $modelName = $title->getContentModelName();
35 }
36
37 $handler = ContentHandler::getForModelName( $modelName );
38 return $handler->unserialize( $text, $format );
39 }
40
41 public static function getDefaultModelFor( Title $title ) {
42 global $wgNamespaceContentModels;
43
44 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
45 # because it is used to initialized the mContentModelName memebr.
46
47 $ns = $title->getNamespace();
48
49 $ext = false;
50 $m = null;
51 $model = null;
52
53 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
54 $model = $wgNamespaceContentModels[ $ns ];
55 }
56
57 # hook can determin default model
58 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
59 if ( $model ) return $model;
60 }
61
62 # Could this page contain custom CSS or JavaScript, based on the title?
63 $isCssOrJsPage = ( NS_MEDIAWIKI == $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
64 if ( $isCssOrJsPage ) $ext = $m[1];
65
66 # hook can force js/css
67 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
68
69 # Is this a .css subpage of a user page?
70 $isJsCssSubpage = ( NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
71 if ( $isJsCssSubpage ) $ext = $m[1];
72
73 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
74 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT || $model === null );
75 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
76
77 # hook can override $isWikitext
78 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
79
80 if ( !$isWikitext ) {
81
82 if ( $ext == 'js' )
83 return CONTENT_MODEL_JAVASCRIPT;
84 else if ( $ext == 'css' )
85 return CONTENT_MODEL_CSS;
86
87 if ( $model )
88 return $model;
89 else
90 return CONTENT_MODEL_TEXT;
91 }
92
93 # we established that is must be wikitext
94 return CONTENT_MODEL_WIKITEXT;
95 }
96
97 public static function getForTitle( Title $title ) {
98 $modelName = $title->getContentModelName();
99 return ContentHandler::getForModelName( $modelName );
100 }
101
102 public static function getForContent( Content $content ) {
103 $modelName = $content->getModelName();
104 return ContentHandler::getForModelName( $modelName );
105 }
106
107 public static function getForModelName( $modelName ) {
108 global $wgContentHandlers;
109
110 if ( empty( $wgContentHandlers[$modelName] ) ) {
111 #FIXME: hook here!
112 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
113 }
114
115 if ( is_string( $wgContentHandlers[$modelName] ) ) {
116 $class = $wgContentHandlers[$modelName];
117 $wgContentHandlers[$modelName] = new $class( $modelName );
118 }
119
120 return $wgContentHandlers[$modelName];
121 }
122
123 # ----------------------------------------------------------------------------------------------------------
124 public function __construct( $modelName, $formats ) {
125 $this->mModelName = $modelName;
126 $this->mSupportedFormats = $formats;
127 }
128
129 public function getModelName() {
130 # for wikitext: wikitext; in the future: wikiast, wikidom?
131 # for wikidata: wikidata
132 return $this->mModelName;
133 }
134
135
136 public function getSupportedFormats() {
137 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
138 # for wikidata: "application/json", "application/x-php", etc
139 return $this->mSupportedFormats;
140 }
141
142 public function getDefaultFormat() {
143 return $this->mSupportedFormats[0];
144 }
145
146 public abstract function serialize( Content $content, $format = null );
147
148 public abstract function unserialize( $blob, $format = null );
149
150 public abstract function emptyContent();
151
152 # public abstract function doPreSaveTransform( $title, $obj ); #TODO...
153
154 /**
155 * Return an Article object suitable for viewing the given object
156 *
157 * NOTE: does *not* do special handling for Image and Category pages!
158 * Use Article::newFromTitle() for that!
159 *
160 * @param type $title
161 * @return \Article
162 * @todo Article is being refactored into an action class, keep track of that
163 */
164 public function createArticle( Title $title ) {
165 #XXX: assert that $title->getContentModelName() == $this->getModelname()?
166 $article = new Article($title);
167 return $article;
168 }
169
170 /**
171 * Return an EditPage object suitable for editing the given object
172 *
173 * @param type $article
174 * @return \EditPage
175 */
176 public function createEditPage( Article $article ) {
177 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
178 $editPage = new EditPage( $article );
179 return $editPage;
180 }
181
182 /**
183 * Return an ExternalEdit object suitable for editing the given object
184 *
185 * @param type $article
186 * @return \ExternalEdit
187 */
188 public function createExternalEdit( IContextSource $context ) {
189 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
190 $externalEdit = new ExternalEdit( $context );
191 return $externalEdit;
192 }
193
194 /**
195 public function updatePage( $title, $obj ) {
196 }
197 **/
198
199 public function getDiffEngine( Article $article ) {
200 $de = new DifferenceEngine( $article->getContext() );
201 return $de;
202 }
203
204 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
205
206 #TODO: how to handle extra message for JS/CSS previews??
207 #TODO: Article::showCssOrJsPage ---> specialized classes!
208
209 #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
210 }
211
212
213 abstract class TextContentHandler extends ContentHandler {
214
215 public function __construct( $modelName, $formats ) {
216 parent::__construct( $modelName, $formats );
217 }
218
219 public function serialize( Content $content, $format = null ) {
220 #FIXME: assert format
221 return $content->getRawData();
222 }
223
224 }
225 class WikitextContentHandler extends TextContentHandler {
226
227 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
228 parent::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
229 }
230
231 public function unserialize( $text, $format = null ) {
232 #FIXME: assert format
233 return new WikitextContent($text);
234 }
235
236 public function emptyContent() {
237 return new WikitextContent("");
238 }
239
240 }
241
242 class JavaScriptContentHandler extends TextContentHandler {
243
244 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
245 parent::__construct( $modelName, array( 'text/javascript' ) );
246 }
247
248 public function unserialize( $text, $format = null ) {
249 return new JavaScriptContent($text);
250 }
251
252 public function emptyContent() {
253 return new JavaScriptContent("");
254 }
255 }
256
257 class CssContentHandler extends TextContentHandler {
258
259 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
260 parent::__construct( $modelName, array( 'text/css' ) );
261 }
262
263 public function unserialize( $text, $format = null ) {
264 return new CssContent($text);
265 }
266
267 public function emptyContent() {
268 return new CssContent("");
269 }
270
271 }