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