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