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