integration with Title and Revision (work in progress)
[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 getDefaultModelFor( Title $title ) {
17 global $wgNamespaceContentModels;
18
19 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
20 # because it is used to initialized the mContentModelName memebr.
21
22 $ns = $title->getNamespace();
23
24 $ext = false;
25 $m = null;
26 $model = null;
27
28 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
29 $model = $wgNamespaceContentModels[ $ns ];
30 }
31
32 # hook can determin default model
33 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
34 if ( $model ) return $model;
35 }
36
37 # Could this page contain custom CSS or JavaScript, based on the title?
38 $isCssOrJsPage = ( NS_MEDIAWIKI == $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
39 if ( $isCssOrJsPage ) $ext = $m[1];
40
41 # hook can force js/css
42 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
43
44 # Is this a .css subpage of a user page?
45 $isJsCssSubpage = ( NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
46 if ( $isJsCssSubpage ) $ext = $m[1];
47
48 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
49 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT || $model === null );
50 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
51
52 # hook can override $isWikitext
53 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
54
55 if ( !$isWikitext ) {
56
57 if ( $ext == 'js' )
58 return CONTENT_MODEL_JAVASCRIPT;
59 else if ( $ext == 'css' )
60 return CONTENT_MODEL_CSS;
61
62 if ( $model )
63 return $model;
64 else
65 return CONTENT_MODEL_TEXT;
66 }
67
68 # we established that is must be wikitext
69 return CONTENT_MODEL_WIKITEXT;
70 }
71
72 public static function getForTitle( Title $title ) {
73 $modelName = $title->getContentModelName();
74 return ContenteHandler::getForModelName( $modelName );
75 }
76
77 public static function getForContent( Content $content ) {
78 $modelName = $content->getModelName();
79 return ContenteHandler::getForModelName( $modelName );
80 }
81
82 public static function getForModelName( $modelName ) {
83 global $wgContentHandlers;
84
85 if ( empty( $wgContentHandlers[$modelName] ) ) {
86 #FIXME: hook here!
87 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
88 }
89
90 if ( is_string( $wgContentHandlers[$modelName] ) ) {
91 $class = $wgContentHandlers[$modelName];
92 $wgContentHandlers[$modelName] = new $class( $modelName );
93 }
94
95 return $wgContentHandlers[$modelName];
96 }
97
98
99 public function __construct( $modelName, $formats ) {
100 $this->mModelName = $modelName;
101 $this->mSupportedFormats = $formats;
102 }
103
104 public function getModelName() {
105 # for wikitext: wikitext; in the future: wikiast, wikidom?
106 # for wikidata: wikidata
107 return $this->mModelName;
108 }
109
110
111 public function getSupportedFormats() {
112 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
113 # for wikidata: "application/json", "application/x-php", etc
114 return $this->mSupportedFormats;
115 }
116
117 public function getDefaultFormat() {
118 return $this->mSupportedFormats[0];
119 }
120
121 public abstract function serialize( $obj, $format = null );
122 # for wikitext, do nothing (in the future: serialise ast/dom)
123 # for wikidata: serialize arrays to json
124
125 public abstract function unserialize( $blob, $format = null );
126 # for wikitext, do nothing (in the future: parse into ast/dom)
127 # for wikidata: serialize arrays to json
128
129
130 public function getSearchText( $obj ) {
131 # for wikitext, return wikitext
132 # for wikidata, return pseudo-wikitext composed of property values (or some such)
133 $text = $this->serialize( $obj );
134 return $text; # return the default serialization.
135 }
136
137 public function getWikitextForTransclusion( $obj ) {
138 # for wikitext, return text
139 # for wikidata, return false, or some generated wikitext
140 $text = $this->serialize( $obj );
141 return '<pre>' . $text . '</pre>'; # return a pre-formatted block containing the default serialization.
142 }
143
144 public abstract function render( $obj, Title $title, ParserOptions $options, $revid = null );
145 # returns a ParserOutput instance!
146 # are parser options, generic?!
147
148 public abstract function doPreSaveTransform( $title, $obj );
149
150 # TODO: getPreloadText()
151 # TODO: preprocess()
152
153 /**
154 * Return an Article object suitable for viewing the given object
155 *
156 * @param type $title
157 * @param type $obj
158 * @return \Article
159 * @todo Article is being refactored into an action class, keep track of that
160 */
161 public function createArticle( $title, $obj ) {
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 $title
170 * @param type $obj
171 * @param type $article
172 * @return \EditPage
173 */
174 public function createEditPage( $title, $obj, $article ) {
175 $editPage = new EditPage($article);
176 return $editPage;
177 }
178
179 /**
180 public function updatePage( $title, $obj ) {
181 }
182 **/
183
184 public function getDiffEngine( $article ) {
185 $de = new DifferenceEngine( $article->getContext() );
186 return $de;
187 }
188
189 public function getIndexUpdateJobs( $title, $parserOutput, $recursive = true ) {
190 # for wikitext, create a LinksUpdate object
191 # for wikidata: serialize arrays to json
192 $update = new LinksUpdate( $title, $parserOutput, $recursive );
193 return $update;
194 }
195
196 #XXX: is the native model for wikitext a string or the parser output? parse early or parse late?
197 }
198
199 abstract class WikitextContentHandler extends ContentHandler {
200 }
201
202 abstract class JavaScriptContentHandler extends WikitextHandler {
203 }
204
205 abstract class CssContentHandler extends WikitextHandler {
206 }