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