reworking EditPage to use the content object - work in horrible 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 getContentText( Content $content = null ) {
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->getNativeData();
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 #TODO: log this incident!
30 return null;
31 }
32
33 public static function makeContent( $text, Title $title, $modelName = null, $format = null ) {
34 if ( !$modelName ) {
35 $modelName = $title->getContentModelName();
36 }
37
38 $handler = ContentHandler::getForModelName( $modelName );
39 return $handler->unserialize( $text, $format );
40 }
41
42 public static function getDefaultModelFor( Title $title ) {
43 global $wgNamespaceContentModels;
44
45 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
46 # because it is used to initialized the mContentModelName memebr.
47
48 $ns = $title->getNamespace();
49
50 $ext = false;
51 $m = null;
52 $model = null;
53
54 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
55 $model = $wgNamespaceContentModels[ $ns ];
56 }
57
58 # hook can determin default model
59 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
60 if ( $model ) return $model;
61 }
62
63 # Could this page contain custom CSS or JavaScript, based on the title?
64 $isCssOrJsPage = ( NS_MEDIAWIKI == $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
65 if ( $isCssOrJsPage ) $ext = $m[1];
66
67 # hook can force js/css
68 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
69
70 # Is this a .css subpage of a user page?
71 $isJsCssSubpage = ( NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
72 if ( $isJsCssSubpage ) $ext = $m[1];
73
74 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
75 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT || $model === null );
76 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
77
78 # hook can override $isWikitext
79 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
80
81 if ( !$isWikitext ) {
82
83 if ( $ext == 'js' )
84 return CONTENT_MODEL_JAVASCRIPT;
85 else if ( $ext == 'css' )
86 return CONTENT_MODEL_CSS;
87
88 if ( $model )
89 return $model;
90 else
91 return CONTENT_MODEL_TEXT;
92 }
93
94 # we established that is must be wikitext
95 return CONTENT_MODEL_WIKITEXT;
96 }
97
98 public static function getForTitle( Title $title ) {
99 $modelName = $title->getContentModelName();
100 return ContentHandler::getForModelName( $modelName );
101 }
102
103 public static function getForContent( Content $content ) {
104 $modelName = $content->getModelName();
105 return ContentHandler::getForModelName( $modelName );
106 }
107
108 /**
109 * @static
110 * @param $modelName String the name of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
111 * @return ContentHandler
112 * @throws MWException
113 */
114 public static function getForModelName( $modelName ) {
115 global $wgContentHandlers;
116
117 if ( empty( $wgContentHandlers[$modelName] ) ) {
118 #FIXME: hook here!
119 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
120 }
121
122 if ( is_string( $wgContentHandlers[$modelName] ) ) {
123 $class = $wgContentHandlers[$modelName];
124 $wgContentHandlers[$modelName] = new $class( $modelName );
125 }
126
127 return $wgContentHandlers[$modelName];
128 }
129
130 # ----------------------------------------------------------------------------------------------------------
131 public function __construct( $modelName, $formats ) {
132 $this->mModelName = $modelName;
133 $this->mSupportedFormats = $formats;
134 }
135
136 public function getModelName() {
137 # for wikitext: wikitext; in the future: wikiast, wikidom?
138 # for wikidata: wikidata
139 return $this->mModelName;
140 }
141
142
143 public function getSupportedFormats() {
144 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
145 # for wikidata: "application/json", "application/x-php", etc
146 return $this->mSupportedFormats;
147 }
148
149 public function getDefaultFormat() {
150 return $this->mSupportedFormats[0];
151 }
152
153 /**
154 * @abstract
155 * @param Content $content
156 * @param null $format
157 * @return String
158 */
159 public abstract function serialize( Content $content, $format = null );
160
161 /**
162 * @abstract
163 * @param $blob String
164 * @param null $format
165 * @return Content
166 */
167 public abstract function unserialize( $blob, $format = null );
168
169 public abstract function emptyContent();
170
171 /**
172 * Return an Article object suitable for viewing the given object
173 *
174 * NOTE: does *not* do special handling for Image and Category pages!
175 * Use Article::newFromTitle() for that!
176 *
177 * @param type $title
178 * @return \Article
179 * @todo Article is being refactored into an action class, keep track of that
180 */
181 public function createArticle( Title $title ) {
182 #XXX: assert that $title->getContentModelName() == $this->getModelname()?
183 $article = new Article($title);
184 return $article;
185 }
186
187 /**
188 * Return an EditPage object suitable for editing the given object
189 *
190 * @param type $article
191 * @return \EditPage
192 */
193 public function createEditPage( Article $article ) {
194 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
195 $editPage = new EditPage( $article );
196 return $editPage;
197 }
198
199 /**
200 * Return an ExternalEdit object suitable for editing the given object
201 *
202 * @param type $article
203 * @return \ExternalEdit
204 */
205 public function createExternalEdit( IContextSource $context ) {
206 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
207 $externalEdit = new ExternalEdit( $context );
208 return $externalEdit;
209 }
210
211 /**
212 public function updatePage( $title, $obj ) {
213 }
214 **/
215
216 public function getDiffEngine( Article $article ) { #FIXME: change interface of diff engine? or accept content objects here=?
217 $de = new DifferenceEngine( $article->getContext() );
218 return $de;
219 }
220
221 /**
222 * attempts to merge differences between three versions.
223 * Returns a new Content object for a clean merge and false for failure or a conflict.
224 *
225 * This default implementation always returns false.
226 *
227 * @param $oldContent String
228 * @param $myContent String
229 * @param $yourContent String
230 * @return Content|Bool
231 */
232 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
233 return false;
234 }
235
236 /**
237 * Return an applicable autosummary if one exists for the given edit.
238 *
239 * @param $oldContent Content: the previous text of the page.
240 * @param $newContent Content: The submitted text of the page.
241 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
242 *
243 * @return string An appropriate autosummary, or an empty string.
244 */
245 public function getAutosummary( Content $oldContent, Content $newContent, $flags ) {
246 global $wgContLang;
247
248 # Decide what kind of autosummary is needed.
249
250 # Redirect autosummaries
251 $ot = $oldContent->getRedirectTarget();
252 $rt = $newContent->getRedirectTarget();
253
254 if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
255
256 $truncatedtext = $newContent->getTextForSummary(
257 250
258 - strlen( wfMsgForContent( 'autoredircomment' ) )
259 - strlen( $rt->getFullText() ) );
260
261 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
262 }
263
264 # New page autosummaries
265 if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
266 # If they're making a new article, give its text, truncated, in the summary.
267
268 $truncatedtext = $newContent->getTextForSummary(
269 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
270
271 return wfMsgForContent( 'autosumm-new', $truncatedtext );
272 }
273
274 # Blanking autosummaries
275 if ( $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
276 return wfMsgForContent( 'autosumm-blank' );
277 } elseif ( $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
278 # Removing more than 90% of the article
279
280 $truncatedtext = $newContent->getTextForSummary(
281 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
282
283 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
284 }
285
286 # If we reach this point, there's no applicable autosummary for our case, so our
287 # autosummary is empty.
288 return '';
289 }
290
291 /**
292 * Auto-generates a deletion reason
293 *
294 * @param $title Title: the page's title
295 * @param &$hasHistory Boolean: whether the page has a history
296 * @return mixed String containing deletion reason or empty string, or boolean false
297 * if no revision occurred
298 */
299 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
300 global $wgContLang;
301
302 $dbw = wfGetDB( DB_MASTER );
303
304 // Get the last revision
305 $rev = Revision::newFromTitle( $title );
306
307 if ( is_null( $rev ) ) {
308 return false;
309 }
310
311 // Get the article's contents
312 $content = $rev->getContent();
313 $blank = false;
314
315 // If the page is blank, use the text from the previous revision,
316 // which can only be blank if there's a move/import/protect dummy revision involved
317 if ( $content->getSize() == 0 ) {
318 $prev = $rev->getPrevious();
319
320 if ( $prev ) {
321 $content = $rev->getContent();
322 $blank = true;
323 }
324 }
325
326 // Find out if there was only one contributor
327 // Only scan the last 20 revisions
328 $res = $dbw->select( 'revision', 'rev_user_text',
329 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
330 __METHOD__,
331 array( 'LIMIT' => 20 )
332 );
333
334 if ( $res === false ) {
335 // This page has no revisions, which is very weird
336 return false;
337 }
338
339 $hasHistory = ( $res->numRows() > 1 );
340 $row = $dbw->fetchObject( $res );
341
342 if ( $row ) { // $row is false if the only contributor is hidden
343 $onlyAuthor = $row->rev_user_text;
344 // Try to find a second contributor
345 foreach ( $res as $row ) {
346 if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
347 $onlyAuthor = false;
348 break;
349 }
350 }
351 } else {
352 $onlyAuthor = false;
353 }
354
355 // Generate the summary with a '$1' placeholder
356 if ( $blank ) {
357 // The current revision is blank and the one before is also
358 // blank. It's just not our lucky day
359 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
360 } else {
361 if ( $onlyAuthor ) {
362 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
363 } else {
364 $reason = wfMsgForContent( 'excontent', '$1' );
365 }
366 }
367
368 if ( $reason == '-' ) {
369 // Allow these UI messages to be blanked out cleanly
370 return '';
371 }
372
373 // Max content length = max comment length - length of the comment (excl. $1)
374 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
375
376 // Now replace the '$1' placeholder
377 $reason = str_replace( '$1', $text, $reason );
378
379 return $reason;
380 }
381
382 /**
383 * Get the Content object that needs to be saved in order to undo all revisions
384 * between $undo and $undoafter. Revisions must belong to the same page,
385 * must exist and must not be deleted
386 * @param $undo Revision
387 * @param $undoafter null|Revision Must be an earlier revision than $undo
388 * @return mixed string on success, false on failure
389 */
390 public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter = null ) {
391 $cur_content = $current->getContent();
392
393 if ( empty( $cur_content ) ) {
394 return false; // no page
395 }
396
397 $undo_content = $undo->getContent();
398 $undoafter_content = $undoafter->getContent();
399
400 if ( $cur_content->equals( $undo_content ) ) {
401 # No use doing a merge if it's just a straight revert.
402 return $undoafter_content;
403 }
404
405 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
406
407 return $undone_content;
408 }
409
410 #TODO: how to handle extra message for JS/CSS previews??
411 #TODO: Article::showCssOrJsPage ---> specialized classes!
412
413 #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
414 }
415
416
417 abstract class TextContentHandler extends ContentHandler {
418
419 public function __construct( $modelName, $formats ) {
420 parent::__construct( $modelName, $formats );
421 }
422
423 public function serialize( Content $content, $format = null ) {
424 #FIXME: assert format
425 return $content->getNativeData();
426 }
427
428 /**
429 * attempts to merge differences between three versions.
430 * Returns a new Content object for a clean merge and false for failure or a conflict.
431 *
432 * This text-based implementation uses wfMerge().
433 *
434 * @param $oldContent String
435 * @param $myContent String
436 * @param $yourContent String
437 * @return Content|Bool
438 */
439 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
440 $format = $this->getDefaultFormat();
441
442 $old = $this->serialize( $oldContent, $format );
443 $mine = $this->serialize( $myContent, $format );
444 $yours = $this->serialize( $yourContent, $format );
445
446 $ok = wfMerge( $old, $mine, $yours, $result );
447
448 if ( !$ok ) return false;
449 if ( !$result ) return $this->emptyContent();
450
451 $mergedContent = $this->unserialize( $result, $format );
452 return $mergedContent;
453 }
454
455
456 }
457 class WikitextContentHandler extends TextContentHandler {
458
459 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
460 parent::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
461 }
462
463 public function unserialize( $text, $format = null ) {
464 #FIXME: assert format
465 return new WikitextContent($text);
466 }
467
468 public function emptyContent() {
469 return new WikitextContent("");
470 }
471
472
473 }
474
475 class JavaScriptContentHandler extends TextContentHandler {
476
477 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
478 parent::__construct( $modelName, array( 'text/javascript' ) );
479 }
480
481 public function unserialize( $text, $format = null ) {
482 return new JavaScriptContent($text);
483 }
484
485 public function emptyContent() {
486 return new JavaScriptContent("");
487 }
488 }
489
490 class CssContentHandler extends TextContentHandler {
491
492 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
493 parent::__construct( $modelName, array( 'text/css' ) );
494 }
495
496 public function unserialize( $text, $format = null ) {
497 return new CssContent($text);
498 }
499
500 public function emptyContent() {
501 return new CssContent("");
502 }
503
504 }