fix var name
[lhc/web/wiklou.git] / includes / ContentHandler.php
1 <?php
2
3 class MWContentSerializationException extends MWException {
4
5 }
6
7
8 /**
9 * A content handler knows how do deal with a specific type of content on a wiki page.
10 * Content is stored in the database in a serialized form (using a serialization format aka mime type)
11 * and is be unserialized into it's native PHP represenation (the content model), which is wrappe in
12 * an instance of the appropriate subclass of Content.
13 *
14 * ContentHandler instances are stateless singletons that serve, among other things, as a factory for
15 * Content objects. Generally, there is one subclass of ContentHandler and one subclass of Content
16 * for every type of content model.
17 *
18 * Some content types have a flat model, that is, their native represenation is the
19 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
20 * this also applies to wikitext (mediawiki's default content type), but wikitext
21 * content may be represented by a DOM or AST structure in the future.
22 *
23 * @since 1.WD
24 */
25 abstract class ContentHandler {
26
27 /**
28 * Conveniance function for getting flat text from a Content object. This should only
29 * be used in the context of backwards compatibility with code that is not yet able
30 * to handle Content objects!
31 *
32 * If $content is null, this method returns the empty string.
33 *
34 * If $content is an instance of TextContent, this method returns the flat text as returned by $content->getNativeData().
35 *
36 * If $content is not a TextContent object, the bahaviour of this method depends on the global $wgContentHandlerTextFallback:
37 * * If $wgContentHandlerTextFallback is 'fail' and $content is not a TextContent object, an MWException is thrown.
38 * * If $wgContentHandlerTextFallback is 'serialize' and $content is not a TextContent object, $content->serialize()
39 * is called to get a string form of the content.
40 * * If $wgContentHandlerTextFallback is 'ignore' and $content is not a TextContent object, this method returns null.
41 * * otherwise, the behaviour is undefined.
42 *
43 * @since WD.1
44 *
45 * @static
46 * @param Content|null $content
47 * @return null|string the textual form of $content, if available
48 * @throws MWException if $content is not an instance of TextContent and $wgContentHandlerTextFallback was set to 'fail'.
49 */
50 public static function getContentText( Content $content = null ) {
51 global $wgContentHandlerTextFallback;
52
53 if ( is_null( $content ) ) {
54 return '';
55 }
56
57 if ( $content instanceof TextContent ) {
58 return $content->getNativeData();
59 }
60
61 if ( $wgContentHandlerTextFallback == 'fail' ) {
62 throw new MWException( "Attempt to get text from Content with model " . $content->getModelName() );
63 }
64
65 if ( $wgContentHandlerTextFallback == 'serialize' ) {
66 return $content->serialize();
67 }
68
69 return null;
70 }
71
72 /**
73 * Conveniance function for creating a Content object from a given textual representation.
74 *
75 * $text will be deserialized into a Content object of the model specified by $modelName (or,
76 * if that is not given, $title->getContentModelName()) using the given format.
77 *
78 * @since WD.1
79 *
80 * @static
81 * @param string $text the textual represenation, will be unserialized to create the Content object
82 * @param Title $title the title of the page this text belongs to, required as a context for deserialization
83 * @param null|String $modelName the model to deserialize to. If not provided, $title->getContentModelName() is used.
84 * @param null|String $format the format to use for deserialization. If not given, the model's default format is used.
85 *
86 * @return Content a Content object representing $text
87 * @throw MWException if $model or $format is not supported or if $text can not be unserialized using $format.
88 */
89 public static function makeContent( $text, Title $title, $modelName = null, $format = null ) {
90
91 if ( is_null( $modelName ) ) {
92 $modelName = $title->getContentModelName();
93 }
94
95 $handler = ContentHandler::getForModelName( $modelName );
96 return $handler->unserializeContent( $text, $format );
97 }
98
99 /**
100 * Returns the name of the default content model to be used for the page with the given title.
101 *
102 * Note: There should rarely be need to call this method directly.
103 * To determine the actual content model for a given page, use Title::getContentModelName().
104 *
105 * Which model is to be used per default for the page is determined based on several factors:
106 * * The global setting $wgNamespaceContentModels specifies a content model per namespace.
107 * * The hook DefaultModelFor may be used to override the page's default model.
108 * * Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript model if they end in .js or .css, respectively.
109 * * Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
110 * * The hook TitleIsCssOrJsPage may be used to force a page to use the CSS or JavaScript model if they end in .js or .css, respectively.
111 * * The hook TitleIsWikitextPage may be used to force a page to use the wikitext model.
112 *
113 * If none of the above applies, the wikitext model is used.
114 *
115 * Note: this is used by, and may thus not use, Title::getContentModelName()
116 *
117 * @since WD.1
118 *
119 * @static
120 * @param Title $title
121 * @return null|string default model name for the page given by $title
122 */
123 public static function getDefaultModelFor( Title $title ) {
124 global $wgNamespaceContentModels;
125
126 // NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
127 // because it is used to initialized the mContentModelName memebr.
128
129 $ns = $title->getNamespace();
130
131 $ext = false;
132 $m = null;
133 $model = null;
134
135 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
136 $model = $wgNamespaceContentModels[ $ns ];
137 }
138
139 // hook can determin default model
140 if ( !wfRunHooks( 'ContentHandlerDefaultModelFor', array( $title, &$model ) ) ) {
141 if ( !is_null( $model ) ) {
142 return $model;
143 }
144 }
145
146 // Could this page contain custom CSS or JavaScript, based on the title?
147 $isCssOrJsPage = NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
148 if ( $isCssOrJsPage ) {
149 $ext = $m[1];
150 }
151
152 // hook can force js/css
153 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
154
155 // Is this a .css subpage of a user page?
156 $isJsCssSubpage = NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
157 if ( $isJsCssSubpage ) {
158 $ext = $m[1];
159 }
160
161 // is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
162 $isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
163 $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
164
165 // hook can override $isWikitext
166 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
167
168 if ( !$isWikitext ) {
169 switch ( $ext ) {
170 case 'js':
171 return CONTENT_MODEL_JAVASCRIPT;
172 case 'css':
173 return CONTENT_MODEL_CSS;
174 default:
175 return is_null( $model ) ? CONTENT_MODEL_TEXT : $model;
176 }
177 }
178
179 // we established that is must be wikitext
180
181 return CONTENT_MODEL_WIKITEXT;
182 }
183
184 /**
185 * returns the appropriate ContentHandler singleton for the given title
186 *
187 * @since WD.1
188 *
189 * @static
190 * @param Title $title
191 * @return ContentHandler
192 */
193 public static function getForTitle( Title $title ) {
194 $modelName = $title->getContentModelName();
195 return ContentHandler::getForModelName( $modelName );
196 }
197
198 /**
199 * returns the appropriate ContentHandler singleton for the given Content object
200 *
201 * @since WD.1
202 *
203 * @static
204 * @param Content $content
205 * @return ContentHandler
206 */
207 public static function getForContent( Content $content ) {
208 $modelName = $content->getModelName();
209 return ContentHandler::getForModelName( $modelName );
210 }
211
212 /**
213 * returns the ContentHandler singleton for the given model name. Use the CONTENT_MODEL_XXX constants to
214 * identify the desired content model.
215 *
216 * ContentHandler singletons are take from the global $wgContentHandlers array. Keys in that array are
217 * model names, the values are either ContentHandler singleton objects, or strings specifying the appropriate
218 * subclass of ContentHandler.
219 *
220 * If a class name in encountered when looking up the singleton for a given model name, the class is
221 * instantiated and the class name is replaced by te resulting singleton in $wgContentHandlers.
222 *
223 * If no ContentHandler is defined for the desired $modelName, the ContentHandler may be provided by the
224 * a ContentHandlerForModelName hook. if no Contenthandler can be determined, an MWException is raised.
225 *
226 * @since WD.1
227 *
228 * @static
229 * @param $modelName String the name of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
230 * @return ContentHandler the ContentHandler singleton for handling the model given by $modelName
231 * @throws MWException if no handler is known for $modelName.
232 */
233 public static function getForModelName( $modelName ) {
234 global $wgContentHandlers;
235
236 if ( empty( $wgContentHandlers[$modelName] ) ) {
237 $handler = null;
238
239 wfRunHooks( 'ContentHandlerForModelName', array( $modelName, &$handler ) );
240
241 if ( $handler ) { // NOTE: may be a string or an object, either is fine!
242 $wgContentHandlers[$modelName] = $handler;
243 } else {
244 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
245 }
246 }
247
248 if ( is_string( $wgContentHandlers[$modelName] ) ) {
249 $class = $wgContentHandlers[$modelName];
250 $wgContentHandlers[$modelName] = new $class( $modelName );
251 }
252
253 return $wgContentHandlers[$modelName];
254 }
255
256 // ----------------------------------------------------------------------------------------------------------
257
258 /**
259 * Constructor, initializing the ContentHandler instance with it's model name and a list of supported formats.
260 * Values for the parameters are typically provided as literals by subclasses' constructors.
261 *
262 * @param String $modelName (use CONTENT_MODEL_XXX constants).
263 * @param array $formats list for supported serialization formats (typically as MIME types)
264 */
265 public function __construct( $modelName, $formats ) {
266 $this->mModelName = $modelName;
267 $this->mSupportedFormats = $formats;
268 }
269
270
271 /**
272 * Serializes Content object of the type supported by this ContentHandler.
273 *
274 * @since WD.1
275 *
276 * @abstract
277 * @param Content $content the Content object to serialize
278 * @param null $format the desired serialization format
279 * @return String serialized form of the content
280 */
281 public abstract function serializeContent( Content $content, $format = null );
282
283 /**
284 * Unserializes a Content object of the type supported by this ContentHandler.
285 *
286 * @since WD.1
287 *
288 * @abstract
289 * @param $blob String serialized form of the content
290 * @param null $format the format used for serialization
291 * @return Content the Content object created by deserializing $blob
292 */
293 public abstract function unserializeContent( $blob, $format = null );
294
295 /**
296 * Creates an empty Content object of the type supported by this ContentHandler.
297 *
298 * @since WD.1
299 *
300 * @return Content
301 */
302 public abstract function makeEmptyContent();
303
304 /**
305 * Returns the model name that identifies the content model this ContentHandler can handle.
306 * Use with the CONTENT_MODEL_XXX constants.
307 *
308 * @since WD.1
309 *
310 * @return String the model name
311 */
312 public function getModelName() {
313 return $this->mModelName;
314 }
315
316 /**
317 * Throws an MWException if $modelName is not the content model handeled by this ContentHandler.
318 *
319 * @since WD.1
320 *
321 * @param String $modelName the model name to check
322 */
323 protected function checkModelName( $modelName ) {
324 if ( $modelName !== $this->mModelName ) {
325 throw new MWException( "Bad content model: expected " . $this->mModelName . " but got found " . $modelName );
326 }
327 }
328
329 /**
330 * Returns a list of serialization formats supported by the serializeContent() and unserializeContent() methods of
331 * this ContentHandler.
332 *
333 * @since WD.1
334 *
335 * @return array of serialization formats as MIME type like strings
336 */
337 public function getSupportedFormats() {
338 return $this->mSupportedFormats;
339 }
340
341 /**
342 * The format used for serialization/deserialization per default by this ContentHandler.
343 *
344 * This default implementation will return the first element of the array of formats
345 * that was passed to the constructor.
346 *
347 * @since WD.1
348 *
349 * @return String the name of the default serialiozation format as a MIME type
350 */
351 public function getDefaultFormat() {
352 return $this->mSupportedFormats[0];
353 }
354
355 /**
356 * Returns true if $format is a serialization format supported by this ContentHandler,
357 * and false otherwise.
358 *
359 * Note that if $format is null, this method always returns true, because null
360 * means "use the default format".
361 *
362 * @since WD.1
363 *
364 * @param String $format the serialization format to check
365 * @return bool
366 */
367 public function isSupportedFormat( $format ) {
368
369 if ( !$format ) {
370 return true; // this means "use the default"
371 }
372
373 return in_array( $format, $this->mSupportedFormats );
374 }
375
376 /**
377 * Throws an MWException if isSupportedFormat( $format ) is not true. Convenient
378 * for checking whether a format provided as a parameter is actually supported.
379 *
380 * @param String $format the serialization format to check
381 */
382 protected function checkFormat( $format ) {
383 if ( !$this->isSupportedFormat( $format ) ) {
384 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
385 }
386 }
387
388 /**
389 * Returns overrides for action handlers.
390 * Classes listed here will be used instead of the default one when
391 * (and only when) $wgActions[$action] === true. This allows subclasses
392 * to override the default action handlers.
393 *
394 * @since WD.1
395 *
396 * @return Array
397 */
398 public function getActionOverrides() {
399 return array();
400 }
401
402 /**
403 * Return an Article object suitable for viewing the given object
404 *
405 * NOTE: does *not* do special handling for Image and Category pages!
406 * Use Article::newFromTitle() for that!
407 *
408 * @since WD.1
409 *
410 * @param Title $title
411 * @return Article
412 * @todo Article is being refactored into an action class, keep track of that
413 * @todo Article really defines the view of the content... rename this method to createViewPage ?
414 */
415 public function createArticle( Title $title ) {
416 $this->checkModelName( $title->getContentModelName() );
417
418 $article = new Article($title);
419 return $article;
420 }
421
422 /**
423 * Return an EditPage object suitable for editing the given object
424 *
425 * @since WD.1
426 *
427 * @param Article $article
428 * @return EditPage
429 */
430 public function createEditPage( Article $article ) {
431 $this->checkModelName( $article->getContentModelName() );
432
433 $editPage = new EditPage( $article );
434 return $editPage;
435 }
436
437 /**
438 * Return an ExternalEdit object suitable for editing the given object
439 *
440 * @since WD.1
441 *
442 * @param IContextSource $context
443 * @return ExternalEdit
444 * @todo does anyone or anythign actually use the external edit facility? Can we just deprecate and ignore it?
445 */
446 public function createExternalEdit( IContextSource $context ) {
447 $this->checkModelName( $context->getTitle()->getContentModelName() );
448
449 $externalEdit = new ExternalEdit( $context );
450 return $externalEdit;
451 }
452
453 /**
454 * Factory
455 * @since WD.1
456 *
457 * @param $context IContextSource context to use, anything else will be ignored
458 * @param $old Integer old ID we want to show and diff with.
459 * @param $new String either 'prev' or 'next'.
460 * @param $rcid Integer ??? FIXME (default 0)
461 * @param $refreshCache boolean If set, refreshes the diff cache
462 * @param $unhide boolean If set, allow viewing deleted revs
463 *
464 * @return DifferenceEngine
465 */
466 public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
467 $refreshCache = false, $unhide = false ) {
468
469 $this->checkModelName( $context->getTitle()->getContentModelName() );
470
471 $diffEngineClass = $this->getDiffEngineClass();
472
473 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
474 }
475
476 /**
477 * Returns the name of the diff engine to use.
478 *
479 * @since WD.1
480 *
481 * @return string
482 */
483 protected function getDiffEngineClass() {
484 return 'DifferenceEngine';
485 }
486
487 /**
488 * attempts to merge differences between three versions.
489 * Returns a new Content object for a clean merge and false for failure or a conflict.
490 *
491 * This default implementation always returns false.
492 *
493 * @since WD.1
494 *
495 * @param $oldContent String
496 * @param $myContent String
497 * @param $yourContent String
498 * @return Content|Bool
499 */
500 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
501 return false;
502 }
503
504 /**
505 * Return an applicable autosummary if one exists for the given edit.
506 *
507 * @since WD.1
508 *
509 * @param $oldContent Content|null: the previous text of the page.
510 * @param $newContent Content|null: The submitted text of the page.
511 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
512 *
513 * @return string An appropriate autosummary, or an empty string.
514 */
515 public function getAutosummary( Content $oldContent = null, Content $newContent = null, $flags ) {
516 global $wgContLang;
517
518 // Decide what kind of autosummary is needed.
519
520 // Redirect autosummaries
521
522 /**
523 * @var $ot Title
524 * @var $rt Title
525 */
526
527 $ot = !is_null( $oldContent ) ? $oldContent->getRedirectTarget() : null;
528 $rt = !is_null( $newContent ) ? $newContent->getRedirectTarget() : null;
529
530 if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
531
532 $truncatedtext = $newContent->getTextForSummary(
533 250
534 - strlen( wfMsgForContent( 'autoredircomment' ) )
535 - strlen( $rt->getFullText() ) );
536
537 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
538 }
539
540 // New page autosummaries
541 if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
542 // If they're making a new article, give its text, truncated, in the summary.
543
544 $truncatedtext = $newContent->getTextForSummary(
545 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
546
547 return wfMsgForContent( 'autosumm-new', $truncatedtext );
548 }
549
550 // Blanking autosummaries
551 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
552 return wfMsgForContent( 'autosumm-blank' );
553 } elseif ( !empty( $oldContent ) && $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
554 // Removing more than 90% of the article
555
556 $truncatedtext = $newContent->getTextForSummary(
557 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
558
559 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
560 }
561
562 // If we reach this point, there's no applicable autosummary for our case, so our
563 // autosummary is empty.
564
565 return '';
566 }
567
568 /**
569 * Auto-generates a deletion reason
570 *
571 * @since WD.1
572 *
573 * @param $title Title: the page's title
574 * @param &$hasHistory Boolean: whether the page has a history
575 * @return mixed String containing deletion reason or empty string, or boolean false
576 * if no revision occurred
577 *
578 * @XXX &$hasHistory is extremely ugly, it's here because WikiPage::getAutoDeleteReason() and Article::getReason() have it / want it.
579 */
580 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
581 $dbw = wfGetDB( DB_MASTER );
582
583 // Get the last revision
584 $rev = Revision::newFromTitle( $title );
585
586 if ( is_null( $rev ) ) {
587 return false;
588 }
589
590 // Get the article's contents
591 $content = $rev->getContent();
592 $blank = false;
593
594 // If the page is blank, use the text from the previous revision,
595 // which can only be blank if there's a move/import/protect dummy revision involved
596 if ( $content->getSize() == 0 ) {
597 $prev = $rev->getPrevious();
598
599 if ( $prev ) {
600 $content = $rev->getContent();
601 $blank = true;
602 }
603 }
604
605 // Find out if there was only one contributor
606 // Only scan the last 20 revisions
607 $res = $dbw->select( 'revision', 'rev_user_text',
608 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
609 __METHOD__,
610 array( 'LIMIT' => 20 )
611 );
612
613 if ( $res === false ) {
614 // This page has no revisions, which is very weird
615 return false;
616 }
617
618 $hasHistory = ( $res->numRows() > 1 );
619 $row = $dbw->fetchObject( $res );
620
621 if ( $row ) { // $row is false if the only contributor is hidden
622 $onlyAuthor = $row->rev_user_text;
623 // Try to find a second contributor
624 foreach ( $res as $row ) {
625 if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
626 $onlyAuthor = false;
627 break;
628 }
629 }
630 } else {
631 $onlyAuthor = false;
632 }
633
634 // Generate the summary with a '$1' placeholder
635 if ( $blank ) {
636 // The current revision is blank and the one before is also
637 // blank. It's just not our lucky day
638 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
639 } else {
640 if ( $onlyAuthor ) {
641 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
642 } else {
643 $reason = wfMsgForContent( 'excontent', '$1' );
644 }
645 }
646
647 if ( $reason == '-' ) {
648 // Allow these UI messages to be blanked out cleanly
649 return '';
650 }
651
652 // Max content length = max comment length - length of the comment (excl. $1)
653 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
654
655 // Now replace the '$1' placeholder
656 $reason = str_replace( '$1', $text, $reason );
657
658 return $reason;
659 }
660
661 #@TODO: getSecondaryUpdatesForDeletion( Content ) returns an array of SecondaryDataUpdate objects
662 #... or do that in the Content class?
663
664 /**
665 * Get the Content object that needs to be saved in order to undo all revisions
666 * between $undo and $undoafter. Revisions must belong to the same page,
667 * must exist and must not be deleted
668 *
669 * @since WD.1
670 *
671 * @param $current Revision the current text
672 * @param $undo Revision the revision to undo
673 * @param $undoafter Revision Must be an earlier revision than $undo
674 *
675 * @return mixed string on success, false on failure
676 */
677 public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) {
678 $cur_content = $current->getContent();
679
680 if ( empty( $cur_content ) ) {
681 return false; // no page
682 }
683
684 $undo_content = $undo->getContent();
685 $undoafter_content = $undoafter->getContent();
686
687 if ( $cur_content->equals( $undo_content ) ) {
688 // No use doing a merge if it's just a straight revert.
689 return $undoafter_content;
690 }
691
692 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
693
694 return $undone_content;
695 }
696
697 /**
698 * Returns true for content models that support caching using the ParserCache mechanism.
699 * See WikiPage::isParserCacheUser().
700 *
701 * @since WD.1
702 *
703 * @return bool
704 */
705 public function isParserCacheSupported() {
706 return true;
707 }
708
709 /**
710 * @since WD.1
711 *
712 * @param $page WikiPage the page that was deleted (note: $page->getId() must still return the old page ID!)
713 *
714 * @return array a list of SecondaryDataUpdate instances that will clean up the database ofter deletion.
715 */
716 public function getDeletionUpdates( WikiPage $page ) {
717 return array(
718 new LinksDeletionUpdate( $page ),
719 );
720 }
721 }
722
723 /**
724 * @since WD.1
725 */
726 abstract class TextContentHandler extends ContentHandler {
727
728 public function __construct( $modelName, $formats ) {
729 parent::__construct( $modelName, $formats );
730 }
731
732 public function serializeContent( Content $content, $format = null ) {
733 $this->checkFormat( $format );
734 return $content->getNativeData();
735 }
736
737 /**
738 * attempts to merge differences between three versions.
739 * Returns a new Content object for a clean merge and false for failure or a conflict.
740 *
741 * All three Content objects passed as parameters must have the same content model.
742 *
743 * This text-based implementation uses wfMerge().
744 *
745 * @param $oldContent String
746 * @param $myContent String
747 * @param $yourContent String
748 * @return Content|Bool
749 */
750 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
751 $this->checkModelName( $oldContent->getModelName() );
752 $this->checkModelName( $myContent->getModelName() );
753 $this->checkModelName( $yourContent->getModelName() );
754
755 $format = $this->getDefaultFormat();
756
757 $old = $this->serializeContent( $oldContent, $format );
758 $mine = $this->serializeContent( $myContent, $format );
759 $yours = $this->serializeContent( $yourContent, $format );
760
761 $ok = wfMerge( $old, $mine, $yours, $result );
762
763 if ( !$ok ) {
764 return false;
765 }
766
767 if ( !$result ) {
768 return $this->makeEmptyContent();
769 }
770
771 $mergedContent = $this->unserializeContent( $result, $format );
772 return $mergedContent;
773 }
774
775
776 }
777
778 /**
779 * @since WD.1
780 */
781 class WikitextContentHandler extends TextContentHandler {
782
783 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
784 parent::__construct( $modelName, array( 'text/x-wiki' ) );
785 }
786
787 public function unserializeContent( $text, $format = null ) {
788 $this->checkFormat( $format );
789
790 return new WikitextContent( $text );
791 }
792
793 public function makeEmptyContent() {
794 return new WikitextContent( '' );
795 }
796
797
798 }
799
800 #XXX: make ScriptContentHandler base class with plugin interface for syntax highlighting?
801
802 /**
803 * @since WD.1
804 */
805 class JavaScriptContentHandler extends TextContentHandler {
806
807 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
808 parent::__construct( $modelName, array( 'text/javascript' ) ); #XXX: or use $wgJsMimeType? this is for internal storage, not HTTP...
809 }
810
811 public function unserializeContent( $text, $format = null ) {
812 $this->checkFormat( $format );
813
814 return new JavaScriptContent( $text );
815 }
816
817 public function makeEmptyContent() {
818 return new JavaScriptContent( '' );
819 }
820 }
821
822 /**
823 * @since WD.1
824 */
825 class CssContentHandler extends TextContentHandler {
826
827 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
828 parent::__construct( $modelName, array( 'text/css' ) );
829 }
830
831 public function unserializeContent( $text, $format = null ) {
832 $this->checkFormat( $format );
833
834 return new CssContent( $text );
835 }
836
837 public function makeEmptyContent() {
838 return new CssContent( '' );
839 }
840
841 }