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