ba33a0144ad20b29c1a558dc5d41d7917aa8f7b8
[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 * Return an Article object suitable for viewing the given object
480 *
481 * NOTE: does *not* do special handling for Image and Category pages!
482 * Use Article::newFromTitle() for that!
483 *
484 * @since WD.1
485 *
486 * @param Title $title
487 * @return Article
488 * @todo Article is being refactored into an action class, keep track of that
489 * @todo Article really defines the view of the content... rename this method to createViewPage ?
490 */
491 public function createArticle( Title $title ) {
492 $this->checkModelID( $title->getContentModel() );
493
494 $article = new Article($title);
495 return $article;
496 }
497
498 /**
499 * Return an EditPage object suitable for editing the given object.
500 * This default implementation always fails with an MWException, because there is no
501 * generic edit page implementation suitable for all content models.
502 *
503 * @since WD.1
504 *
505 * @param Article $article
506 * @return EditPage
507 */
508 public function createEditPage( Article $article ) {
509 throw new MWException( "ContentHandler class " . get_classs( $this ) . " does not provide an EditPage." );
510 }
511
512 /**
513 * Return an ExternalEdit object suitable for editing the given object
514 *
515 * @since WD.1
516 *
517 * @param IContextSource $context
518 * @return ExternalEdit
519 * @todo does anyone or anythign actually use the external edit facility? Can we just deprecate and ignore it?
520 */
521 public function createExternalEdit( IContextSource $context ) {
522 $this->checkModelID( $context->getTitle()->getContentModel() );
523
524 $externalEdit = new ExternalEdit( $context );
525 return $externalEdit;
526 }
527
528 /**
529 * Factory
530 * @since WD.1
531 *
532 * @param $context IContextSource context to use, anything else will be ignored
533 * @param $old Integer old ID we want to show and diff with.
534 * @param $new String either 'prev' or 'next'.
535 * @param $rcid Integer ??? FIXME (default 0)
536 * @param $refreshCache boolean If set, refreshes the diff cache
537 * @param $unhide boolean If set, allow viewing deleted revs
538 *
539 * @return DifferenceEngine
540 */
541 public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
542 $refreshCache = false, $unhide = false ) {
543
544 $this->checkModelID( $context->getTitle()->getContentModel() );
545
546 $diffEngineClass = $this->getDiffEngineClass();
547
548 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
549 }
550
551 /**
552 * Returns the name of the diff engine to use.
553 *
554 * @since WD.1
555 *
556 * @return string
557 */
558 protected function getDiffEngineClass() {
559 return 'DifferenceEngine';
560 }
561
562 /**
563 * attempts to merge differences between three versions.
564 * Returns a new Content object for a clean merge and false for failure or a conflict.
565 *
566 * This default implementation always returns false.
567 *
568 * @since WD.1
569 *
570 * @param $oldContent String
571 * @param $myContent String
572 * @param $yourContent String
573 * @return Content|Bool
574 */
575 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
576 return false;
577 }
578
579 /**
580 * Return an applicable autosummary if one exists for the given edit.
581 *
582 * @since WD.1
583 *
584 * @param $oldContent Content|null: the previous text of the page.
585 * @param $newContent Content|null: The submitted text of the page.
586 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
587 *
588 * @return string An appropriate autosummary, or an empty string.
589 */
590 public function getAutosummary( Content $oldContent = null, Content $newContent = null, $flags ) {
591 global $wgContLang;
592
593 // Decide what kind of autosummary is needed.
594
595 // Redirect autosummaries
596
597 /**
598 * @var $ot Title
599 * @var $rt Title
600 */
601
602 $ot = !is_null( $oldContent ) ? $oldContent->getRedirectTarget() : null;
603 $rt = !is_null( $newContent ) ? $newContent->getRedirectTarget() : null;
604
605 if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
606
607 $truncatedtext = $newContent->getTextForSummary(
608 250
609 - strlen( wfMsgForContent( 'autoredircomment' ) )
610 - strlen( $rt->getFullText() ) );
611
612 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
613 }
614
615 // New page autosummaries
616 if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
617 // If they're making a new article, give its text, truncated, in the summary.
618
619 $truncatedtext = $newContent->getTextForSummary(
620 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
621
622 return wfMsgForContent( 'autosumm-new', $truncatedtext );
623 }
624
625 // Blanking autosummaries
626 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
627 return wfMsgForContent( 'autosumm-blank' );
628 } elseif ( !empty( $oldContent ) && $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
629 // Removing more than 90% of the article
630
631 $truncatedtext = $newContent->getTextForSummary(
632 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
633
634 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
635 }
636
637 // If we reach this point, there's no applicable autosummary for our case, so our
638 // autosummary is empty.
639
640 return '';
641 }
642
643 /**
644 * Auto-generates a deletion reason
645 *
646 * @since WD.1
647 *
648 * @param $title Title: the page's title
649 * @param &$hasHistory Boolean: whether the page has a history
650 * @return mixed String containing deletion reason or empty string, or boolean false
651 * if no revision occurred
652 *
653 * @XXX &$hasHistory is extremely ugly, it's here because WikiPage::getAutoDeleteReason() and Article::getReason() have it / want it.
654 */
655 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
656 $dbw = wfGetDB( DB_MASTER );
657
658 // Get the last revision
659 $rev = Revision::newFromTitle( $title );
660
661 if ( is_null( $rev ) ) {
662 return false;
663 }
664
665 // Get the article's contents
666 $content = $rev->getContent();
667 $blank = false;
668
669 // If the page is blank, use the text from the previous revision,
670 // which can only be blank if there's a move/import/protect dummy revision involved
671 if ( $content->getSize() == 0 ) {
672 $prev = $rev->getPrevious();
673
674 if ( $prev ) {
675 $content = $rev->getContent();
676 $blank = true;
677 }
678 }
679
680 // Find out if there was only one contributor
681 // Only scan the last 20 revisions
682 $res = $dbw->select( 'revision', 'rev_user_text',
683 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
684 __METHOD__,
685 array( 'LIMIT' => 20 )
686 );
687
688 if ( $res === false ) {
689 // This page has no revisions, which is very weird
690 return false;
691 }
692
693 $hasHistory = ( $res->numRows() > 1 );
694 $row = $dbw->fetchObject( $res );
695
696 if ( $row ) { // $row is false if the only contributor is hidden
697 $onlyAuthor = $row->rev_user_text;
698 // Try to find a second contributor
699 foreach ( $res as $row ) {
700 if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
701 $onlyAuthor = false;
702 break;
703 }
704 }
705 } else {
706 $onlyAuthor = false;
707 }
708
709 // Generate the summary with a '$1' placeholder
710 if ( $blank ) {
711 // The current revision is blank and the one before is also
712 // blank. It's just not our lucky day
713 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
714 } else {
715 if ( $onlyAuthor ) {
716 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
717 } else {
718 $reason = wfMsgForContent( 'excontent', '$1' );
719 }
720 }
721
722 if ( $reason == '-' ) {
723 // Allow these UI messages to be blanked out cleanly
724 return '';
725 }
726
727 // Max content length = max comment length - length of the comment (excl. $1)
728 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
729
730 // Now replace the '$1' placeholder
731 $reason = str_replace( '$1', $text, $reason );
732
733 return $reason;
734 }
735
736 #@TODO: getSecondaryUpdatesForDeletion( Content ) returns an array of DataUpdate objects
737 #... or do that in the Content class?
738
739 /**
740 * Get the Content object that needs to be saved in order to undo all revisions
741 * between $undo and $undoafter. Revisions must belong to the same page,
742 * must exist and must not be deleted
743 *
744 * @since WD.1
745 *
746 * @param $current Revision the current text
747 * @param $undo Revision the revision to undo
748 * @param $undoafter Revision Must be an earlier revision than $undo
749 *
750 * @return mixed string on success, false on failure
751 */
752 public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) {
753 $cur_content = $current->getContent();
754
755 if ( empty( $cur_content ) ) {
756 return false; // no page
757 }
758
759 $undo_content = $undo->getContent();
760 $undoafter_content = $undoafter->getContent();
761
762 if ( $cur_content->equals( $undo_content ) ) {
763 // No use doing a merge if it's just a straight revert.
764 return $undoafter_content;
765 }
766
767 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
768
769 return $undone_content;
770 }
771
772 /**
773 * Returns true for content models that support caching using the ParserCache mechanism.
774 * See WikiPage::isParserCacheUser().
775 *
776 * @since WD.1
777 *
778 * @return bool
779 */
780 public function isParserCacheSupported() {
781 return true;
782 }
783
784 /**
785 * @since WD.1
786 *
787 * @param $page WikiPage the page that was deleted (note: $page->getId() must still return the old page ID!)
788 *
789 * @return array a list of DataUpdate instances that will clean up the database ofter deletion.
790 */
791 public function getDeletionUpdates( WikiPage $page ) {
792 return array(
793 new LinksDeletionUpdate( $page ),
794 );
795 }
796 }
797
798 /**
799 * @since WD.1
800 */
801 abstract class TextContentHandler extends ContentHandler {
802
803 public function __construct( $modelId, $formats ) {
804 parent::__construct( $modelId, $formats );
805 }
806
807 /**
808 * Returns the content's text as-is.
809 *
810 * @param Content $content
811 * @param String|null $format
812 * @return mixed
813 */
814 public function serializeContent( Content $content, $format = null ) {
815 $this->checkFormat( $format );
816 return $content->getNativeData();
817 }
818
819 /**
820 * Return an EditPage object for editing the given object
821 *
822 * @since WD.1
823 *
824 * @param Article $article
825 * @return EditPage
826 */
827 public function createEditPage( Article $article ) {
828 $this->checkModelID( $article->getPage()->getContentModel() );
829
830 $editPage = new EditPage( $article );
831 return $editPage;
832 }
833
834 /**
835 * attempts to merge differences between three versions.
836 * Returns a new Content object for a clean merge and false for failure or a conflict.
837 *
838 * All three Content objects passed as parameters must have the same content model.
839 *
840 * This text-based implementation uses wfMerge().
841 *
842 * @param $oldContent String
843 * @param $myContent String
844 * @param $yourContent String
845 * @return Content|Bool
846 */
847 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
848 $this->checkModelID( $oldContent->getModel() );
849 $this->checkModelID( $myContent->getModel() );
850 $this->checkModelID( $yourContent->getModel() );
851
852 $format = $this->getDefaultFormat();
853
854 $old = $this->serializeContent( $oldContent, $format );
855 $mine = $this->serializeContent( $myContent, $format );
856 $yours = $this->serializeContent( $yourContent, $format );
857
858 $ok = wfMerge( $old, $mine, $yours, $result );
859
860 if ( !$ok ) {
861 return false;
862 }
863
864 if ( !$result ) {
865 return $this->makeEmptyContent();
866 }
867
868 $mergedContent = $this->unserializeContent( $result, $format );
869 return $mergedContent;
870 }
871
872
873 }
874
875 /**
876 * @since WD.1
877 */
878 class WikitextContentHandler extends TextContentHandler {
879
880 public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
881 parent::__construct( $modelId, array( CONTENT_FORMAT_WIKITEXT ) );
882 }
883
884 public function unserializeContent( $text, $format = null ) {
885 $this->checkFormat( $format );
886
887 return new WikitextContent( $text );
888 }
889
890 public function makeEmptyContent() {
891 return new WikitextContent( '' );
892 }
893
894
895 }
896
897 #XXX: make ScriptContentHandler base class with plugin interface for syntax highlighting?
898
899 /**
900 * @since WD.1
901 */
902 class JavaScriptContentHandler extends TextContentHandler {
903
904 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
905 parent::__construct( $modelId, array( CONTENT_FORMAT_JAVASCRIPT ) );
906 }
907
908 public function unserializeContent( $text, $format = null ) {
909 $this->checkFormat( $format );
910
911 return new JavaScriptContent( $text );
912 }
913
914 public function makeEmptyContent() {
915 return new JavaScriptContent( '' );
916 }
917 }
918
919 /**
920 * @since WD.1
921 */
922 class CssContentHandler extends TextContentHandler {
923
924 public function __construct( $modelId = CONTENT_MODEL_CSS ) {
925 parent::__construct( $modelId, array( CONTENT_FORMAT_CSS ) );
926 }
927
928 public function unserializeContent( $text, $format = null ) {
929 $this->checkFormat( $format );
930
931 return new CssContent( $text );
932 }
933
934 public function makeEmptyContent() {
935 return new CssContent( '' );
936 }
937
938 }