content validation, global consistency check
[lhc/web/wiklou.git] / includes / Content.php
1 <?php
2
3 /**
4 * A content object represents page content, e.g. the text to show on a page.
5 * Content objects have no knowledge about how they relate to Wiki pages.
6 *
7 * @since 1.WD
8 */
9 abstract class Content {
10
11 /**
12 * Name of the content model this Content object represents.
13 * Use with CONTENT_MODEL_XXX constants
14 *
15 * @var String $model_id
16 */
17 protected $model_id;
18
19 /**
20 * @since WD.1
21 *
22 * @return String a string representing the content in a way useful for building a full text search index.
23 * If no useful representation exists, this method returns an empty string.
24 */
25 public abstract function getTextForSearchIndex( );
26
27 /**
28 * @since WD.1
29 *
30 * @return String the wikitext to include when another page includes this content, or false if the content is not
31 * includable in a wikitext page.
32 *
33 * @TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
34 * @TODO: use in parser, etc!
35 */
36 public abstract function getWikitextForTransclusion( );
37
38 /**
39 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
40 *
41 * @since WD.1
42 *
43 * @param int $maxlength maximum length of the summary text
44 * @return String the summary text
45 */
46 public abstract function getTextForSummary( $maxlength = 250 );
47
48 /**
49 * Returns native represenation of the data. Interpretation depends on the data model used,
50 * as given by getDataModel().
51 *
52 * @since WD.1
53 *
54 * @return mixed the native representation of the content. Could be a string, a nested array
55 * structure, an object, a binary blob... anything, really.
56 *
57 * @NOTE: review all calls carefully, caller must be aware of content model!
58 */
59 public abstract function getNativeData( );
60
61 /**
62 * returns the content's nominal size in bogo-bytes.
63 *
64 * @return int
65 */
66 public abstract function getSize( );
67
68 /**
69 * @param int $model_id
70 */
71 public function __construct( $model_id = null ) {
72 $this->model_id = $model_id;
73 }
74
75 /**
76 * Returns the id of the content model used by this content objects.
77 * Corresponds to the CONTENT_MODEL_XXX constants.
78 *
79 * @since WD.1
80 *
81 * @return int the model id
82 */
83 public function getModel() {
84 return $this->model_id;
85 }
86
87 /**
88 * Throws an MWException if $model_id is not the id of the content model
89 * supported by this Content object.
90 *
91 * @param int $model_id the model to check
92 */
93 protected function checkModelID( $model_id ) {
94 if ( $model_id !== $this->model_id ) {
95 $model_name = ContentHandler::getContentModelName( $model_id );
96 $own_model_name = ContentHandler::getContentModelName( $this->model_id );
97
98 throw new MWException( "Bad content model: expected {$this->model_id} ($own_model_name) but got found $model_id ($model_name)." );
99 }
100 }
101
102 /**
103 * Conveniance method that returns the ContentHandler singleton for handling the content
104 * model this Content object uses.
105 *
106 * Shorthand for ContentHandler::getForContent( $this )
107 *
108 * @since WD.1
109 *
110 * @return ContentHandler
111 */
112 public function getContentHandler() {
113 return ContentHandler::getForContent( $this );
114 }
115
116 /**
117 * Conveniance method that returns the default serialization format for the content model
118 * model this Content object uses.
119 *
120 * Shorthand for $this->getContentHandler()->getDefaultFormat()
121 *
122 * @since WD.1
123 *
124 * @return ContentHandler
125 */
126 public function getDefaultFormat() {
127 return $this->getContentHandler()->getDefaultFormat();
128 }
129
130 /**
131 * Conveniance method that returns the list of serialization formats supported
132 * for the content model model this Content object uses.
133 *
134 * Shorthand for $this->getContentHandler()->getSupportedFormats()
135 *
136 * @since WD.1
137 *
138 * @return array of supported serialization formats
139 */
140 public function getSupportedFormats() {
141 return $this->getContentHandler()->getSupportedFormats();
142 }
143
144 /**
145 * Returns true if $format is a supported serialization format for this Content object,
146 * false if it isn't.
147 *
148 * Note that this will always return true if $format is null, because null stands for the
149 * default serialization.
150 *
151 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
152 *
153 * @since WD.1
154 *
155 * @param String $format the format to check
156 * @return bool whether the format is supported
157 */
158 public function isSupportedFormat( $format ) {
159 if ( !$format ) {
160 return true; // this means "use the default"
161 }
162
163 return $this->getContentHandler()->isSupportedFormat( $format );
164 }
165
166 /**
167 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
168 *
169 * @param $format
170 * @throws MWException
171 */
172 protected function checkFormat( $format ) {
173 if ( !$this->isSupportedFormat( $format ) ) {
174 throw new MWException( "Format $format is not supported for content model " . $this->getModel() );
175 }
176 }
177
178 /**
179 * Conveniance method for serializing this Content object.
180 *
181 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
182 *
183 * @since WD.1
184 *
185 * @param null|String $format the desired serialization format (or null for the default format).
186 * @return String serialized form of this Content object
187 */
188 public function serialize( $format = null ) {
189 return $this->getContentHandler()->serializeContent( $this, $format );
190 }
191
192 /**
193 * Returns true if this Content object represents empty content.
194 *
195 * @since WD.1
196 *
197 * @return bool whether this Content object is empty
198 */
199 public function isEmpty() {
200 return $this->getSize() == 0;
201 }
202
203 /**
204 * Returns if the content is valid. This is intended for local validity checks, not considering global consistency.
205 * It needs to be valid before it can be saved.
206 *
207 * This default implementation always returns true.
208 *
209 * @since WD.1
210 *
211 * @return boolean
212 */
213 public function isValid() {
214 return true;
215 }
216
217 /**
218 * Diff this content object with another content object..
219 *
220 * @since WD.diff
221 *
222 * @return DiffResult
223 */
224 public abstract function diff( Content $that );
225
226 /**
227 * Returns true if this Content objects is conceptually equivalent to the given Content object.
228 *
229 * Will returns false if $that is null.
230 * Will return true if $that === $this.
231 * Will return false if $that->getModleName() != $this->getModel().
232 * Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
233 * where the meaning of "equal" depends on the actual data model.
234 *
235 * Implementations should be careful to make equals() transitive and reflexive:
236 *
237 * * $a->equals( $b ) <=> $b->equals( $b )
238 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
239 *
240 * @since WD.1
241 *
242 * @param Content $that the Content object to compare to
243 * @return bool true if this Content object is euqual to $that, false otherwise.
244 */
245 public function equals( Content $that = null ) {
246 if ( is_null( $that ) ){
247 return false;
248 }
249
250 if ( $that === $this ) {
251 return true;
252 }
253
254 if ( $that->getModel() !== $this->getModel() ) {
255 return false;
256 }
257
258 return $this->getNativeData() === $that->getNativeData();
259 }
260
261 /**
262 * Return a copy of this Content object. The following must be true for the object returned
263 * if $copy = $original->copy()
264 *
265 * * get_class($original) === get_class($copy)
266 * * $original->getModel() === $copy->getModel()
267 * * $original->equals( $copy )
268 *
269 * If and only if the Content object is imutable, the copy() method can and should
270 * return $this. That is, $copy === $original may be true, but only for imutable content
271 * objects.
272 *
273 * @since WD.1
274 *
275 * @return Content. A copy of this object
276 */
277 public abstract function copy( );
278
279 /**
280 * Returns true if this content is countable as a "real" wiki page, provided
281 * that it's also in a countable location (e.g. a current revision in the main namespace).
282 *
283 * @since WD.1
284 *
285 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
286 * to avoid redundant parsing to find out.
287 * @return boolean
288 */
289 public abstract function isCountable( $hasLinks = null ) ;
290
291 /**
292 * @param Title $title
293 * @param null $revId
294 * @param null|ParserOptions $options
295 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
296 * the result of calling getText() on the ParserOutput object returned by
297 * this method is undefined.
298 *
299 * @since WD.1
300 *
301 * @return ParserOutput
302 */
303 public abstract function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true );
304
305 /**
306 * Returns a list of DataUpdate objects for recording information about this Content in some secondary
307 * data store. If the optional second argument, $old, is given, the updates may model only the changes that
308 * need to be made to replace information about the old content with infomration about the new content.
309 *
310 * This default implementation calls $this->getParserOutput( $title, null, null, false ), and then
311 * calls getSecondaryDataUpdates( $title, $recursive ) on the resulting ParserOutput object.
312 *
313 * Subclasses may implement this to determine the necessary updates more efficiently, or make use of information
314 * about the old content.
315 *
316 * @param Title $title the context for determining the necessary updates
317 * @param Content|null $old a Content object representing the previous content, i.e. the content being
318 * replaced by this Content object.
319 * @param bool $recursive whether to include recursive updates (default: false).
320 *
321 * @return Array. A list of DataUpdate objects for putting information about this content object somewhere.
322 *
323 * @since WD.1
324 */
325 public function getSecondaryDataUpdates( Title $title, Content $old = null, $recursive = false ) {
326 $po = $this->getParserOutput( $title, null, null, false );
327 return $po->getSecondaryDataUpdates( $title, $recursive );
328 }
329
330 /**
331 * Construct the redirect destination from this content and return an
332 * array of Titles, or null if this content doesn't represent a redirect.
333 * The last element in the array is the final destination after all redirects
334 * have been resolved (up to $wgMaxRedirects times).
335 *
336 * @since WD.1
337 *
338 * @return Array of Titles, with the destination last
339 */
340 public function getRedirectChain() {
341 return null;
342 }
343
344 /**
345 * Construct the redirect destination from this content and return an
346 * array of Titles, or null if this content doesn't represent a redirect.
347 * This will only return the immediate redirect target, useful for
348 * the redirect table and other checks that don't need full recursion.
349 *
350 * @since WD.1
351 *
352 * @return Title: The corresponding Title
353 */
354 public function getRedirectTarget() {
355 return null;
356 }
357
358 /**
359 * Construct the redirect destination from this content and return the
360 * Title, or null if this content doesn't represent a redirect.
361 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
362 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
363 *
364 * @since WD.1
365 *
366 * @return Title
367 */
368 public function getUltimateRedirectTarget() {
369 return null;
370 }
371
372 /**
373 * @since WD.1
374 *
375 * @return bool
376 */
377 public function isRedirect() {
378 return $this->getRedirectTarget() !== null;
379 }
380
381 /**
382 * Returns the section with the given id.
383 *
384 * The default implementation returns null.
385 *
386 * @since WD.1
387 *
388 * @param String $sectionId the section's id, given as a numeric string. The id "0" retrieves the section before
389 * the first heading, "1" the text between the first heading (inluded) and the second heading (excluded), etc.
390 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
391 */
392 public function getSection( $sectionId ) {
393 return null;
394 }
395
396 /**
397 * Replaces a section of the content and returns a Content object with the section replaced.
398 *
399 * @since WD.1
400 *
401 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
402 * @param $with Content: new content of the section
403 * @param $sectionTitle String: new section's subject, only if $section is 'new'
404 * @return string Complete article text, or null if error
405 */
406 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
407 return null;
408 }
409
410 /**
411 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
412 *
413 * @since WD.1
414 *
415 * @param Title $title
416 * @param User $user
417 * @param null|ParserOptions $popts
418 * @return Content
419 */
420 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
421 return $this;
422 }
423
424 /**
425 * Returns a new WikitextContent object with the given section heading prepended, if supported.
426 * The default implementation just returns this Content object unmodified, ignoring the section header.
427 *
428 * @since WD.1
429 *
430 * @param $header String
431 * @return Content
432 */
433 public function addSectionHeader( $header ) {
434 return $this;
435 }
436
437 /**
438 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
439 *
440 * @since WD.1
441 *
442 * @param Title $title
443 * @param null|ParserOptions $popts
444 * @return Content
445 */
446 public function preloadTransform( Title $title, ParserOptions $popts ) {
447 return $this;
448 }
449
450 # TODO: handle ImagePage and CategoryPage
451 # TODO: make sure we cover lucene search / wikisearch.
452 # TODO: make sure ReplaceTemplates still works
453 # FUTURE: nice&sane integration of GeSHi syntax highlighting
454 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
455 # [12:00] <vvv> And default it to a DummyHighlighter
456
457 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
458
459 # TODO: tie into API to provide contentModel for Revisions
460 # TODO: tie into API to provide serialized version and contentFormat for Revisions
461 # TODO: tie into API edit interface
462 # FUTURE: make EditForm plugin for EditPage
463 }
464 # FUTURE: special type for redirects?!
465 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
466 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
467
468 /**
469 * Content object implementation for representing flat text.
470 *
471 * TextContent instances are imutable
472 *
473 * @since WD.1
474 */
475 abstract class TextContent extends Content {
476
477 public function __construct( $text, $model_id = null ) {
478 parent::__construct( $model_id );
479
480 $this->mText = $text;
481 }
482
483 public function copy() {
484 return $this; #NOTE: this is ok since TextContent are imutable.
485 }
486
487 public function getTextForSummary( $maxlength = 250 ) {
488 global $wgContLang;
489
490 $text = $this->getNativeData();
491
492 $truncatedtext = $wgContLang->truncate(
493 preg_replace( "/[\n\r]/", ' ', $text ),
494 max( 0, $maxlength ) );
495
496 return $truncatedtext;
497 }
498
499 /**
500 * returns the text's size in bytes.
501 *
502 * @return int the size
503 */
504 public function getSize( ) {
505 $text = $this->getNativeData( );
506 return strlen( $text );
507 }
508
509 /**
510 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
511 *
512 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
513 * to avoid redundant parsing to find out.
514 *
515 * @return bool true if the content is countable
516 */
517 public function isCountable( $hasLinks = null ) {
518 global $wgArticleCountMethod;
519
520 if ( $this->isRedirect( ) ) {
521 return false;
522 }
523
524 if ( $wgArticleCountMethod === 'any' ) {
525 return true;
526 }
527
528 return false;
529 }
530
531 /**
532 * Returns the text represented by this Content object, as a string.
533 *
534 * @return String the raw text
535 */
536 public function getNativeData( ) {
537 $text = $this->mText;
538 return $text;
539 }
540
541 /**
542 * Returns the text represented by this Content object, as a string.
543 *
544 * @return String the raw text
545 */
546 public function getTextForSearchIndex( ) {
547 return $this->getNativeData();
548 }
549
550 /**
551 * Returns the text represented by this Content object, as a string.
552 *
553 * @return String the raw text
554 */
555 public function getWikitextForTransclusion( ) {
556 return $this->getNativeData();
557 }
558
559 /**
560 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
561 *
562 * @return ParserOutput representing the HTML form of the text
563 */
564 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
565 # generic implementation, relying on $this->getHtml()
566
567 if ( $generateHtml ) $html = $this->getHtml( $options );
568 else $html = '';
569
570 $po = new ParserOutput( $html );
571
572 return $po;
573 }
574
575 protected abstract function getHtml( );
576
577 /**
578 * Diff this content object with another content object..
579 *
580 * @since WD.diff
581 *
582 * @param Content $that the other content object to compare this content object to
583 * @param Language $lang the language object to use for text segmentation. If not given, $wgContentLang is used.
584 *
585 * @return DiffResult a diff representing the changes that would have to be made to this content object
586 * to make it equal to $that.
587 */
588 public function diff( Content $that, Language $lang = null ) {
589 global $wgContLang;
590
591 $this->checkModelID( $that->getModel() );
592
593 #@todo: could implement this in DifferenceEngine and just delegate here?
594
595 if ( !$lang ) $lang = $wgContLang;
596
597 $otext = $this->getNativeData();
598 $ntext = $this->getNativeData();
599
600 # Note: Use native PHP diff, external engines don't give us abstract output
601 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
602 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
603
604 $diffs = new Diff( $ota, $nta );
605 return $diff;
606 }
607
608
609 }
610
611 /**
612 * @since WD.1
613 */
614 class WikitextContent extends TextContent {
615
616 public function __construct( $text ) {
617 parent::__construct($text, CONTENT_MODEL_WIKITEXT);
618 }
619
620 protected function getHtml( ) {
621 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
622 }
623
624 /**
625 * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
626 *
627 * @since WikiData1
628 *
629 * @param IContextSource|null $context
630 * @param null $revId
631 * @param null|ParserOptions $options
632 * @param bool $generateHtml
633 *
634 * @return ParserOutput representing the HTML form of the text
635 */
636 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
637 global $wgParser;
638
639 if ( !$options ) {
640 $options = new ParserOptions();
641 }
642
643 $po = $wgParser->parse( $this->mText, $title, $options, true, true, $revId );
644
645 return $po;
646 }
647
648 /**
649 * Returns the section with the given id.
650 *
651 * @param String $sectionId the section's id
652 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
653 */
654 public function getSection( $section ) {
655 global $wgParser;
656
657 $text = $this->getNativeData();
658 $sect = $wgParser->getSection( $text, $section, false );
659
660 return new WikitextContent( $sect );
661 }
662
663 /**
664 * Replaces a section in the wikitext
665 *
666 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
667 * @param $with Content: new content of the section
668 * @param $sectionTitle String: new section's subject, only if $section is 'new'
669 * @return Content Complete article content, or null if error
670 */
671 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
672 wfProfileIn( __METHOD__ );
673
674 $myModelId = $this->getModel();
675 $sectionModelId = $with->getModel();
676
677 if ( $sectionModelId != $myModelId ) {
678 $myModelName = ContentHandler::getContentModelName( $myModelId );
679 $sectionModelName = ContentHandler::getContentModelName( $sectionModelId );
680
681 throw new MWException( "Incompatible content model for section: document uses $myModelId ($myModelName), "
682 . "section uses $sectionModelId ($sectionModelName)." );
683 }
684
685 $oldtext = $this->getNativeData();
686 $text = $with->getNativeData();
687
688 if ( $section === '' ) {
689 return $with; #XXX: copy first?
690 } if ( $section == 'new' ) {
691 # Inserting a new section
692 $subject = $sectionTitle ? wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
693 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
694 $text = strlen( trim( $oldtext ) ) > 0
695 ? "{$oldtext}\n\n{$subject}{$text}"
696 : "{$subject}{$text}";
697 }
698 } else {
699 # Replacing an existing section; roll out the big guns
700 global $wgParser;
701
702 $text = $wgParser->replaceSection( $oldtext, $section, $text );
703 }
704
705 $newContent = new WikitextContent( $text );
706
707 wfProfileOut( __METHOD__ );
708 return $newContent;
709 }
710
711 /**
712 * Returns a new WikitextContent object with the given section heading prepended.
713 *
714 * @param $header String
715 * @return Content
716 */
717 public function addSectionHeader( $header ) {
718 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . $this->getNativeData();
719
720 return new WikitextContent( $text );
721 }
722
723 /**
724 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
725 *
726 * @param Title $title
727 * @param User $user
728 * @param ParserOptions $popts
729 * @return Content
730 */
731 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
732 global $wgParser, $wgConteLang;
733
734 $text = $this->getNativeData();
735 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
736
737 return new WikitextContent( $pst );
738 }
739
740 /**
741 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
742 *
743 * @param Title $title
744 * @param ParserOptions $popts
745 * @return Content
746 */
747 public function preloadTransform( Title $title, ParserOptions $popts ) {
748 global $wgParser, $wgConteLang;
749
750 $text = $this->getNativeData();
751 $plt = $wgParser->getPreloadText( $text, $title, $popts );
752
753 return new WikitextContent( $plt );
754 }
755
756 public function getRedirectChain() {
757 $text = $this->getNativeData();
758 return Title::newFromRedirectArray( $text );
759 }
760
761 public function getRedirectTarget() {
762 $text = $this->getNativeData();
763 return Title::newFromRedirect( $text );
764 }
765
766 public function getUltimateRedirectTarget() {
767 $text = $this->getNativeData();
768 return Title::newFromRedirectRecurse( $text );
769 }
770
771 /**
772 * Returns true if this content is not a redirect, and this content's text is countable according to
773 * the criteria defiend by $wgArticleCountMethod.
774 *
775 * @param Bool $hasLinks if it is known whether this content contains links, provide this information here,
776 * to avoid redundant parsing to find out.
777 * @param IContextSource $context context for parsing if necessary
778 *
779 * @return bool true if the content is countable
780 */
781 public function isCountable( $hasLinks = null, Title $title = null ) {
782 global $wgArticleCountMethod, $wgRequest;
783
784 if ( $this->isRedirect( ) ) {
785 return false;
786 }
787
788 $text = $this->getNativeData();
789
790 switch ( $wgArticleCountMethod ) {
791 case 'any':
792 return true;
793 case 'comma':
794 return strpos( $text, ',' ) !== false;
795 case 'link':
796 if ( $hasLinks === null ) { # not known, find out
797 if ( !$title ) {
798 $context = RequestContext::getMain();
799 $title = $context->getTitle();
800 }
801
802 $po = $this->getParserOutput( $title, null, null, false );
803 $links = $po->getLinks();
804 $hasLinks = !empty( $links );
805 }
806
807 return $hasLinks;
808 }
809 }
810
811 public function getTextForSummary( $maxlength = 250 ) {
812 $truncatedtext = parent::getTextForSummary( $maxlength );
813
814 #clean up unfinished links
815 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
816 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
817
818 return $truncatedtext;
819 }
820
821 }
822
823 /**
824 * @since WD.1
825 */
826 class MessageContent extends TextContent {
827 public function __construct( $msg_key, $params = null, $options = null ) {
828 parent::__construct(null, CONTENT_MODEL_WIKITEXT); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
829
830 $this->mMessageKey = $msg_key;
831
832 $this->mParameters = $params;
833
834 if ( is_null( $options ) ) {
835 $options = array();
836 }
837 elseif ( is_string( $options ) ) {
838 $options = array( $options );
839 }
840
841 $this->mOptions = $options;
842
843 $this->mHtmlOptions = null;
844 }
845
846 /**
847 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
848 */
849 protected function getHtml( ) {
850 $opt = array_merge( $this->mOptions, array('parse') );
851
852 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
853 }
854
855
856 /**
857 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
858 */
859 public function getNativeData( ) {
860 $opt = array_diff( $this->mOptions, array('parse', 'parseinline') );
861
862 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
863 }
864
865 }
866
867 /**
868 * @since WD.1
869 */
870 class JavaScriptContent extends TextContent {
871 public function __construct( $text ) {
872 parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
873 }
874
875 protected function getHtml( ) {
876 $html = "";
877 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
878 $html .= htmlspecialchars( $this->getNativeData() );
879 $html .= "\n</pre>\n";
880
881 return $html;
882 }
883
884 }
885
886 /**
887 * @since WD.1
888 */
889 class CssContent extends TextContent {
890 public function __construct( $text ) {
891 parent::__construct($text, CONTENT_MODEL_CSS);
892 }
893
894 protected function getHtml( ) {
895 $html = "";
896 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
897 $html .= htmlspecialchars( $this->getNativeData() );
898 $html .= "\n</pre>\n";
899
900 return $html;
901 }
902 }