80f729be15e3250a71b75bd6e7b6eb037fc4f513
[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->getModel() );
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 $modelId (or,
76 * if that is not given, $title->getContentModel()) 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 $modelId the model to deserialize to. If not provided, $title->getContentModel() 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, $modelId = null, $format = null ) {
90
91 if ( is_null( $modelId ) ) {
92 $modelId = $title->getContentModel();
93 }
94
95 $handler = ContentHandler::getForModelID( $modelId );
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::getContentModel().
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::getContentModel()
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->getContentModel() directly or indirectly,
127 // because it is used to initialized the mContentModel 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 $modelId = $title->getContentModel();
195 return ContentHandler::getForModelID( $modelId );
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 $modelId = $content->getModel();
209 return ContentHandler::getForModelID( $modelId );
210 }
211
212 /**
213 * returns the ContentHandler singleton for the given model id. 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 $modelId, the ContentHandler may be provided by the
224 * a ContentHandlerForModelID hook. if no Contenthandler can be determined, an MWException is raised.
225 *
226 * @since WD.1
227 *
228 * @static
229 * @param $modelId int the id 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 $modelId
231 * @throws MWException if no handler is known for $modelId.
232 */
233 public static function getForModelID( $modelId ) {
234 global $wgContentHandlers;
235
236 if ( empty( $wgContentHandlers[$modelId] ) ) {
237 $handler = null;
238
239 wfRunHooks( 'ContentHandlerForModelID', array( $modelId, &$handler ) );
240
241 if ( $handler ) { // NOTE: may be a string or an object, either is fine!
242 $wgContentHandlers[$modelId] = $handler;
243 } else {
244 throw new MWException( "No handler for model #$modelId registered in \$wgContentHandlers" );
245 }
246 }
247
248 if ( is_string( $wgContentHandlers[$modelId] ) ) {
249 $class = $wgContentHandlers[$modelId];
250 $wgContentHandlers[$modelId] = new $class( $modelId );
251 }
252
253 return $wgContentHandlers[$modelId];
254 }
255
256 /**
257 * Returns the appropriate mime type for a given content format,
258 * or null if no mime type is known for this format.
259 *
260 * Mime types can be registered in the global array $wgContentFormatMimeTypes.
261 *
262 * @static
263 * @param int $id the content format id, as given by a CONTENT_FORMAT_XXX constant
264 * or returned by Revision::getContentFormat().
265 *
266 * @return String|null the content format's mime type.
267 */
268 public static function getContentFormatMimeType( $id ) {
269 global $wgContentFormatMimeTypes;
270
271 if ( !isset( $wgContentFormatMimeTypes[ $id ] ) ) {
272 return null;
273 }
274
275 return $wgContentFormatMimeTypes[ $id ];
276 }
277
278 /**
279 * Returns the content format if for a given mime type,
280 * or null if no format id if known for this mime type.
281 *
282 * Mime types can be registered in the global array $wgContentFormatMimeTypes.
283 *
284 * @static
285 * @param String $mime the mime type
286 *
287 * @return int|null the format id, as defined by a CONTENT_FORMAT_XXX constant
288 */
289 public static function getContentFormatID( $mime ) {
290 global $wgContentFormatMimeTypes;
291
292 static $format_ids = null;
293
294 if ( $format_ids === null ) {
295 $format_ids = array_flip( $wgContentFormatMimeTypes );
296 }
297
298 if ( !isset( $format_ids[ $mime ] ) ) {
299 return null;
300 }
301
302 return $format_ids[ $mime ];
303 }
304
305 /**
306 * Returns the localized name for a given content model,
307 * or null of no mime type is known.
308 *
309 * Model names are localized using system messages. Message keys
310 * have the form conent-model-$id.
311 *
312 * @static
313 * @param int $id the content model id, as given by a CONTENT_MODEL_XXX constant
314 * or returned by Revision::getContentModel().
315 *
316 * @return String|null the content format's mime type.
317 */
318 public static function getContentModelName( $id ) {
319 $key = "content-model-$id";
320
321 if ( wfEmptyMsg( $key ) ) return null;
322 else return wfMsg( $key );
323 }
324
325 // ----------------------------------------------------------------------------------------------------------
326
327 protected $mModelID;
328 protected $mSupportedFormats;
329
330 /**
331 * Constructor, initializing the ContentHandler instance with it's model id and a list of supported formats.
332 * Values for the parameters are typically provided as literals by subclasses' constructors.
333 *
334 * @param int $modelId (use CONTENT_MODEL_XXX constants).
335 * @param array $formats list for supported serialization formats (typically as MIME types)
336 */
337 public function __construct( $modelId, $formats ) {
338 $this->mModelID = $modelId;
339 $this->mSupportedFormats = $formats;
340 }
341
342
343 /**
344 * Serializes Content object of the type supported by this ContentHandler.
345 *
346 * @since WD.1
347 *
348 * @abstract
349 * @param Content $content the Content object to serialize
350 * @param null $format the desired serialization format
351 * @return String serialized form of the content
352 */
353 public abstract function serializeContent( Content $content, $format = null );
354
355 /**
356 * Unserializes a Content object of the type supported by this ContentHandler.
357 *
358 * @since WD.1
359 *
360 * @abstract
361 * @param $blob String serialized form of the content
362 * @param null $format the format used for serialization
363 * @return Content the Content object created by deserializing $blob
364 */
365 public abstract function unserializeContent( $blob, $format = null );
366
367 /**
368 * Creates an empty Content object of the type supported by this ContentHandler.
369 *
370 * @since WD.1
371 *
372 * @return Content
373 */
374 public abstract function makeEmptyContent();
375
376 /**
377 * Returns the model id that identifies the content model this ContentHandler can handle.
378 * Use with the CONTENT_MODEL_XXX constants.
379 *
380 * @since WD.1
381 *
382 * @return int the model id
383 */
384 public function getModelID() {
385 return $this->mModelID;
386 }
387
388 /**
389 * Throws an MWException if $model_id is not the id of the content model
390 * supported by this ContentHandler.
391 *
392 * @since WD.1
393 *
394 * @param int $model_id the model to check
395 */
396 protected function checkModelID( $model_id ) {
397 if ( $model_id !== $this->mModelID ) {
398 $model_name = ContentHandler::getContentModelName( $model_id );
399 $own_model_name = ContentHandler::getContentModelName( $this->mModelID );
400
401 throw new MWException( "Bad content model: expected {$this->mModelID} ($own_model_name) but got found $model_id ($model_name)." );
402 }
403 }
404
405 /**
406 * Returns a list of serialization formats supported by the serializeContent() and unserializeContent() methods of
407 * this ContentHandler.
408 *
409 * @since WD.1
410 *
411 * @return array of serialization formats as MIME type like strings
412 */
413 public function getSupportedFormats() {
414 return $this->mSupportedFormats;
415 }
416
417 /**
418 * The format used for serialization/deserialization per default by this ContentHandler.
419 *
420 * This default implementation will return the first element of the array of formats
421 * that was passed to the constructor.
422 *
423 * @since WD.1
424 *
425 * @return String the name of the default serialiozation format as a MIME type
426 */
427 public function getDefaultFormat() {
428 return $this->mSupportedFormats[0];
429 }
430
431 /**
432 * Returns true if $format is a serialization format supported by this ContentHandler,
433 * and false otherwise.
434 *
435 * Note that if $format is null, this method always returns true, because null
436 * means "use the default format".
437 *
438 * @since WD.1
439 *
440 * @param String $format the serialization format to check
441 * @return bool
442 */
443 public function isSupportedFormat( $format ) {
444
445 if ( !$format ) {
446 return true; // this means "use the default"
447 }
448
449 return in_array( $format, $this->mSupportedFormats );
450 }
451
452 /**
453 * Throws an MWException if isSupportedFormat( $format ) is not true. Convenient
454 * for checking whether a format provided as a parameter is actually supported.
455 *
456 * @param String $format the serialization format to check
457 */
458 protected function checkFormat( $format ) {
459 if ( !$this->isSupportedFormat( $format ) ) {
460 throw new MWException( "Format $format is not supported for content model " . $this->getModelID() );
461 }
462 }
463
464 /**
465 * Returns overrides for action handlers.
466 * Classes listed here will be used instead of the default one when
467 * (and only when) $wgActions[$action] === true. This allows subclasses
468 * to override the default action handlers.
469 *
470 * @since WD.1
471 *
472 * @return Array
473 */
474 public function getActionOverrides() {
475 return array();
476 }
477
478 /**
479 * Factory
480 * @since WD.1
481 *
482 * @param $context IContextSource context to use, anything else will be ignored
483 * @param $old Integer old ID we want to show and diff with.
484 * @param $new String either 'prev' or 'next'.
485 * @param $rcid Integer ??? FIXME (default 0)
486 * @param $refreshCache boolean If set, refreshes the diff cache
487 * @param $unhide boolean If set, allow viewing deleted revs
488 *
489 * @return DifferenceEngine
490 */
491 public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
492 $refreshCache = false, $unhide = false ) {
493
494 $this->checkModelID( $context->getTitle()->getContentModel() );
495
496 $diffEngineClass = $this->getDiffEngineClass();
497
498 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
499 }
500
501 /**
502 * Returns the name of the diff engine to use.
503 *
504 * @since WD.1
505 *
506 * @return string
507 */
508 protected function getDiffEngineClass() {
509 return 'DifferenceEngine';
510 }
511
512 /**
513 * attempts to merge differences between three versions.
514 * Returns a new Content object for a clean merge and false for failure or a conflict.
515 *
516 * This default implementation always returns false.
517 *
518 * @since WD.1
519 *
520 * @param $oldContent String
521 * @param $myContent String
522 * @param $yourContent String
523 * @return Content|Bool
524 */
525 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
526 return false;
527 }
528
529 /**
530 * Return an applicable autosummary if one exists for the given edit.
531 *
532 * @since WD.1
533 *
534 * @param $oldContent Content|null: the previous text of the page.
535 * @param $newContent Content|null: The submitted text of the page.
536 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
537 *
538 * @return string An appropriate autosummary, or an empty string.
539 */
540 public function getAutosummary( Content $oldContent = null, Content $newContent = null, $flags ) {
541 global $wgContLang;
542
543 // Decide what kind of autosummary is needed.
544
545 // Redirect autosummaries
546
547 /**
548 * @var $ot Title
549 * @var $rt Title
550 */
551
552 $ot = !is_null( $oldContent ) ? $oldContent->getRedirectTarget() : null;
553 $rt = !is_null( $newContent ) ? $newContent->getRedirectTarget() : null;
554
555 if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
556
557 $truncatedtext = $newContent->getTextForSummary(
558 250
559 - strlen( wfMsgForContent( 'autoredircomment' ) )
560 - strlen( $rt->getFullText() ) );
561
562 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
563 }
564
565 // New page autosummaries
566 if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
567 // If they're making a new article, give its text, truncated, in the summary.
568
569 $truncatedtext = $newContent->getTextForSummary(
570 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
571
572 return wfMsgForContent( 'autosumm-new', $truncatedtext );
573 }
574
575 // Blanking autosummaries
576 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
577 return wfMsgForContent( 'autosumm-blank' );
578 } elseif ( !empty( $oldContent ) && $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
579 // Removing more than 90% of the article
580
581 $truncatedtext = $newContent->getTextForSummary(
582 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
583
584 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
585 }
586
587 // If we reach this point, there's no applicable autosummary for our case, so our
588 // autosummary is empty.
589
590 return '';
591 }
592
593 /**
594 * Auto-generates a deletion reason
595 *
596 * @since WD.1
597 *
598 * @param $title Title: the page's title
599 * @param &$hasHistory Boolean: whether the page has a history
600 * @return mixed String containing deletion reason or empty string, or boolean false
601 * if no revision occurred
602 *
603 * @XXX &$hasHistory is extremely ugly, it's here because WikiPage::getAutoDeleteReason() and Article::getReason() have it / want it.
604 */
605 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
606 $dbw = wfGetDB( DB_MASTER );
607
608 // Get the last revision
609 $rev = Revision::newFromTitle( $title );
610
611 if ( is_null( $rev ) ) {
612 return false;
613 }
614
615 // Get the article's contents
616 $content = $rev->getContent();
617 $blank = false;
618
619 // If the page is blank, use the text from the previous revision,
620 // which can only be blank if there's a move/import/protect dummy revision involved
621 if ( $content->getSize() == 0 ) {
622 $prev = $rev->getPrevious();
623
624 if ( $prev ) {
625 $content = $rev->getContent();
626 $blank = true;
627 }
628 }
629
630 // Find out if there was only one contributor
631 // Only scan the last 20 revisions
632 $res = $dbw->select( 'revision', 'rev_user_text',
633 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
634 __METHOD__,
635 array( 'LIMIT' => 20 )
636 );
637
638 if ( $res === false ) {
639 // This page has no revisions, which is very weird
640 return false;
641 }
642
643 $hasHistory = ( $res->numRows() > 1 );
644 $row = $dbw->fetchObject( $res );
645
646 if ( $row ) { // $row is false if the only contributor is hidden
647 $onlyAuthor = $row->rev_user_text;
648 // Try to find a second contributor
649 foreach ( $res as $row ) {
650 if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
651 $onlyAuthor = false;
652 break;
653 }
654 }
655 } else {
656 $onlyAuthor = false;
657 }
658
659 // Generate the summary with a '$1' placeholder
660 if ( $blank ) {
661 // The current revision is blank and the one before is also
662 // blank. It's just not our lucky day
663 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
664 } else {
665 if ( $onlyAuthor ) {
666 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
667 } else {
668 $reason = wfMsgForContent( 'excontent', '$1' );
669 }
670 }
671
672 if ( $reason == '-' ) {
673 // Allow these UI messages to be blanked out cleanly
674 return '';
675 }
676
677 // Max content length = max comment length - length of the comment (excl. $1)
678 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
679
680 // Now replace the '$1' placeholder
681 $reason = str_replace( '$1', $text, $reason );
682
683 return $reason;
684 }
685
686 #@TODO: getSecondaryUpdatesForDeletion( Content ) returns an array of DataUpdate objects
687 #... or do that in the Content class?
688
689 /**
690 * Get the Content object that needs to be saved in order to undo all revisions
691 * between $undo and $undoafter. Revisions must belong to the same page,
692 * must exist and must not be deleted
693 *
694 * @since WD.1
695 *
696 * @param $current Revision the current text
697 * @param $undo Revision the revision to undo
698 * @param $undoafter Revision Must be an earlier revision than $undo
699 *
700 * @return mixed string on success, false on failure
701 */
702 public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) {
703 $cur_content = $current->getContent();
704
705 if ( empty( $cur_content ) ) {
706 return false; // no page
707 }
708
709 $undo_content = $undo->getContent();
710 $undoafter_content = $undoafter->getContent();
711
712 if ( $cur_content->equals( $undo_content ) ) {
713 // No use doing a merge if it's just a straight revert.
714 return $undoafter_content;
715 }
716
717 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
718
719 return $undone_content;
720 }
721
722 /**
723 * Returns true for content models that support caching using the ParserCache mechanism.
724 * See WikiPage::isParserCacheUser().
725 *
726 * @since WD.1
727 *
728 * @return bool
729 */
730 public function isParserCacheSupported() {
731 return true;
732 }
733
734 /**
735 * @since WD.1
736 *
737 * @param $page WikiPage the page that was deleted (note: $page->getId() must still return the old page ID!)
738 *
739 * @return array a list of DataUpdate instances that will clean up the database ofter deletion.
740 */
741 public function getDeletionUpdates( WikiPage $page ) {
742 return array(
743 new LinksDeletionUpdate( $page ),
744 );
745 }
746 }
747
748 /**
749 * @since WD.1
750 */
751 abstract class TextContentHandler extends ContentHandler {
752
753 public function __construct( $modelId, $formats ) {
754 parent::__construct( $modelId, $formats );
755 }
756
757 /**
758 * Returns the content's text as-is.
759 *
760 * @param Content $content
761 * @param String|null $format
762 * @return mixed
763 */
764 public function serializeContent( Content $content, $format = null ) {
765 $this->checkFormat( $format );
766 return $content->getNativeData();
767 }
768
769 /**
770 * attempts to merge differences between three versions.
771 * Returns a new Content object for a clean merge and false for failure or a conflict.
772 *
773 * All three Content objects passed as parameters must have the same content model.
774 *
775 * This text-based implementation uses wfMerge().
776 *
777 * @param $oldContent String
778 * @param $myContent String
779 * @param $yourContent String
780 * @return Content|Bool
781 */
782 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
783 $this->checkModelID( $oldContent->getModel() );
784 $this->checkModelID( $myContent->getModel() );
785 $this->checkModelID( $yourContent->getModel() );
786
787 $format = $this->getDefaultFormat();
788
789 $old = $this->serializeContent( $oldContent, $format );
790 $mine = $this->serializeContent( $myContent, $format );
791 $yours = $this->serializeContent( $yourContent, $format );
792
793 $ok = wfMerge( $old, $mine, $yours, $result );
794
795 if ( !$ok ) {
796 return false;
797 }
798
799 if ( !$result ) {
800 return $this->makeEmptyContent();
801 }
802
803 $mergedContent = $this->unserializeContent( $result, $format );
804 return $mergedContent;
805 }
806
807
808 }
809
810 /**
811 * @since WD.1
812 */
813 class WikitextContentHandler extends TextContentHandler {
814
815 public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
816 parent::__construct( $modelId, array( CONTENT_FORMAT_WIKITEXT ) );
817 }
818
819 public function unserializeContent( $text, $format = null ) {
820 $this->checkFormat( $format );
821
822 return new WikitextContent( $text );
823 }
824
825 public function makeEmptyContent() {
826 return new WikitextContent( '' );
827 }
828
829
830 }
831
832 #XXX: make ScriptContentHandler base class with plugin interface for syntax highlighting?
833
834 /**
835 * @since WD.1
836 */
837 class JavaScriptContentHandler extends TextContentHandler {
838
839 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
840 parent::__construct( $modelId, array( CONTENT_FORMAT_JAVASCRIPT ) );
841 }
842
843 public function unserializeContent( $text, $format = null ) {
844 $this->checkFormat( $format );
845
846 return new JavaScriptContent( $text );
847 }
848
849 public function makeEmptyContent() {
850 return new JavaScriptContent( '' );
851 }
852 }
853
854 /**
855 * @since WD.1
856 */
857 class CssContentHandler extends TextContentHandler {
858
859 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
860 parent::__construct( $modelId, array( CONTENT_FORMAT_CSS ) );
861 }
862
863 public function unserializeContent( $text, $format = null ) {
864 $this->checkFormat( $format );
865
866 return new CssContent( $text );
867 }
868
869 public function makeEmptyContent() {
870 return new CssContent( '' );
871 }
872
873 }