merged master
[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 /**
361 * Prepare Content for saving. Called before Content is saved by WikiPage::doEditContent().
362 * This may be used to store additional information in the database, or check the content's
363 * consistency with global state.
364 *
365 * Note that this method will be called inside the same transaction bracket that will be used
366 * to save the new revision.
367 *
368 * @param WikiPage $page The page to be saved.
369 * @param int $flags bitfield for use with EDIT_XXX constants, see WikiPage::doEditContent()
370 * @param int $baseRevId the ID of the current revision
371 * @param User $user
372 *
373 * @return Status A status object indicating whether the content was successfully prepared for saving.
374 * If the returned status indicates an error, a rollback will be performed and the
375 * transaction aborted.
376 *
377 * @see see WikiPage::doEditContent()
378 */
379 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user );
380
381 # TODO: handle ImagePage and CategoryPage
382 # TODO: make sure we cover lucene search / wikisearch.
383 # TODO: make sure ReplaceTemplates still works
384 # FUTURE: nice&sane integration of GeSHi syntax highlighting
385 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a
386 # config to set the class which handles syntax highlighting
387 # [12:00] <vvv> And default it to a DummyHighlighter
388
389 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
390
391 # TODO: tie into API to provide contentModel for Revisions
392 # TODO: tie into API to provide serialized version and contentFormat for Revisions
393 # TODO: tie into API edit interface
394 # FUTURE: make EditForm plugin for EditPage
395
396 # FUTURE: special type for redirects?!
397 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
398 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
399 }
400
401
402 /**
403 * A content object represents page content, e.g. the text to show on a page.
404 * Content objects have no knowledge about how they relate to Wiki pages.
405 *
406 * @since 1.WD
407 */
408 abstract class AbstractContent implements Content {
409
410 /**
411 * Name of the content model this Content object represents.
412 * Use with CONTENT_MODEL_XXX constants
413 *
414 * @var string $model_id
415 */
416 protected $model_id;
417
418 /**
419 * @param String $model_id
420 */
421 public function __construct( $model_id = null ) {
422 $this->model_id = $model_id;
423 }
424
425 /**
426 * @see Content::getModel()
427 */
428 public function getModel() {
429 return $this->model_id;
430 }
431
432 /**
433 * Throws an MWException if $model_id is not the id of the content model
434 * supported by this Content object.
435 *
436 * @param $model_id int the model to check
437 *
438 * @throws MWException
439 */
440 protected function checkModelID( $model_id ) {
441 if ( $model_id !== $this->model_id ) {
442 throw new MWException( "Bad content model: " .
443 "expected {$this->model_id} " .
444 "but got $model_id." );
445 }
446 }
447
448 /**
449 * @see Content::getContentHandler()
450 */
451 public function getContentHandler() {
452 return ContentHandler::getForContent( $this );
453 }
454
455 /**
456 * @see Content::getDefaultFormat()
457 */
458 public function getDefaultFormat() {
459 return $this->getContentHandler()->getDefaultFormat();
460 }
461
462 /**
463 * @see Content::getSupportedFormats()
464 */
465 public function getSupportedFormats() {
466 return $this->getContentHandler()->getSupportedFormats();
467 }
468
469 /**
470 * @see Content::isSupportedFormat()
471 */
472 public function isSupportedFormat( $format ) {
473 if ( !$format ) {
474 return true; // this means "use the default"
475 }
476
477 return $this->getContentHandler()->isSupportedFormat( $format );
478 }
479
480 /**
481 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
482 * return true.
483 *
484 * @param $format
485 * @throws MWException
486 */
487 protected function checkFormat( $format ) {
488 if ( !$this->isSupportedFormat( $format ) ) {
489 throw new MWException( "Format $format is not supported for content model " .
490 $this->getModel() );
491 }
492 }
493
494 /**
495 * @see Content::serialize
496 */
497 public function serialize( $format = null ) {
498 return $this->getContentHandler()->serializeContent( $this, $format );
499 }
500
501 /**
502 * @see Content::isEmpty()
503 */
504 public function isEmpty() {
505 return $this->getSize() == 0;
506 }
507
508 /**
509 * @see Content::isValid()
510 */
511 public function isValid() {
512 return true;
513 }
514
515 /**
516 * @see Content::equals()
517 */
518 public function equals( Content $that = null ) {
519 if ( is_null( $that ) ) {
520 return false;
521 }
522
523 if ( $that === $this ) {
524 return true;
525 }
526
527 if ( $that->getModel() !== $this->getModel() ) {
528 return false;
529 }
530
531 return $this->getNativeData() === $that->getNativeData();
532 }
533
534 /**
535 * @see Content::getParserOutput()
536 */
537 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null,
538 $generateHtml = true )
539 {
540 return $this->getContentHandler()->getParserOutput(
541 $this, $title, $revId, $options, $generateHtml );
542 }
543
544 /**
545 * @see Content::getRedirectChain()
546 */
547 public function getRedirectChain() {
548 global $wgMaxRedirects;
549 $title = $this->getRedirectTarget();
550 if ( is_null( $title ) ) {
551 return null;
552 }
553 // recursive check to follow double redirects
554 $recurse = $wgMaxRedirects;
555 $titles = array( $title );
556 while ( --$recurse > 0 ) {
557 if ( $title->isRedirect() ) {
558 $page = WikiPage::factory( $title );
559 $newtitle = $page->getRedirectTarget();
560 } else {
561 break;
562 }
563 // Redirects to some special pages are not permitted
564 if ( $newtitle instanceOf Title && $newtitle->isValidRedirectTarget() ) {
565 // The new title passes the checks, so make that our current
566 // title so that further recursion can be checked
567 $title = $newtitle;
568 $titles[] = $newtitle;
569 } else {
570 break;
571 }
572 }
573 return $titles;
574 }
575
576 /**
577 * @see Content::getRedirectTarget()
578 */
579 public function getRedirectTarget() {
580 return null;
581 }
582
583 /**
584 * @see Content::getUltimateRedirectTarget()
585 * @note: migrated here from Title::newFromRedirectRecurse
586 */
587 public function getUltimateRedirectTarget() {
588 $titles = $this->getRedirectChain();
589 return $titles ? array_pop( $titles ) : null;
590 }
591
592 /**
593 * @since WD.1
594 *
595 * @return bool
596 */
597 public function isRedirect() {
598 return $this->getRedirectTarget() !== null;
599 }
600
601 /**
602 * @see Content::getSection()
603 */
604 public function getSection( $sectionId ) {
605 return null;
606 }
607
608 /**
609 * @see Content::replaceSection()
610 */
611 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
612 return null;
613 }
614
615 /**
616 * @see Content::preSaveTransform()
617 */
618 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
619 return $this;
620 }
621
622 /**
623 * @see Content::addSectionHeader()
624 */
625 public function addSectionHeader( $header ) {
626 return $this;
627 }
628
629 /**
630 * @see Content::preloadTransform()
631 */
632 public function preloadTransform( Title $title, ParserOptions $popts ) {
633 return $this;
634 }
635
636 /**
637 * @see Content::prepareSave()
638 */
639 public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) {
640 if ( $this->isValid() ) {
641 return Status::newGood();
642 } else {
643 return Status::newFatal( "invalid-content-data" );
644 }
645 }
646 }
647
648 /**
649 * Content object implementation for representing flat text.
650 *
651 * TextContent instances are immutable
652 *
653 * @since WD.1
654 */
655 abstract class TextContent extends AbstractContent {
656
657 public function __construct( $text, $model_id = null ) {
658 parent::__construct( $model_id );
659
660 $this->mText = $text;
661 }
662
663 public function copy() {
664 return $this; # NOTE: this is ok since TextContent are immutable.
665 }
666
667 public function getTextForSummary( $maxlength = 250 ) {
668 global $wgContLang;
669
670 $text = $this->getNativeData();
671
672 $truncatedtext = $wgContLang->truncate(
673 preg_replace( "/[\n\r]/", ' ', $text ),
674 max( 0, $maxlength ) );
675
676 return $truncatedtext;
677 }
678
679 /**
680 * returns the text's size in bytes.
681 *
682 * @return int The size
683 */
684 public function getSize( ) {
685 $text = $this->getNativeData( );
686 return strlen( $text );
687 }
688
689 /**
690 * Returns true if this content is not a redirect, and $wgArticleCountMethod
691 * is "any".
692 *
693 * @param $hasLinks Bool: if it is known whether this content contains links,
694 * provide this information here, to avoid redundant parsing to find out.
695 *
696 * @return bool True if the content is countable
697 */
698 public function isCountable( $hasLinks = null ) {
699 global $wgArticleCountMethod;
700
701 if ( $this->isRedirect( ) ) {
702 return false;
703 }
704
705 if ( $wgArticleCountMethod === 'any' ) {
706 return true;
707 }
708
709 return false;
710 }
711
712 /**
713 * Returns the text represented by this Content object, as a string.
714 *
715 * @param the raw text
716 */
717 public function getNativeData( ) {
718 $text = $this->mText;
719 return $text;
720 }
721
722 /**
723 * Returns the text represented by this Content object, as a string.
724 *
725 * @param the raw text
726 */
727 public function getTextForSearchIndex( ) {
728 return $this->getNativeData();
729 }
730
731 /**
732 * Returns the text represented by this Content object, as a string.
733 *
734 * @param the raw text
735 */
736 public function getWikitextForTransclusion( ) {
737 return $this->getNativeData();
738 }
739
740 /**
741 * Diff this content object with another content object..
742 *
743 * @since WD.diff
744 *
745 * @param $that Content the other content object to compare this content object to
746 * @param $lang Language the language object to use for text segmentation.
747 * If not given, $wgContentLang is used.
748 *
749 * @return DiffResult a diff representing the changes that would have to be
750 * made to this content object to make it equal to $that.
751 */
752 public function diff( Content $that, Language $lang = null ) {
753 global $wgContLang;
754
755 $this->checkModelID( $that->getModel() );
756
757 # @todo: could implement this in DifferenceEngine and just delegate here?
758
759 if ( !$lang ) $lang = $wgContLang;
760
761 $otext = $this->getNativeData();
762 $ntext = $this->getNativeData();
763
764 # Note: Use native PHP diff, external engines don't give us abstract output
765 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
766 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
767
768 $diff = new Diff( $ota, $nta );
769 return $diff;
770 }
771
772
773 }
774
775 /**
776 * @since WD.1
777 */
778 class WikitextContent extends TextContent {
779
780 public function __construct( $text ) {
781 parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
782 }
783
784 /**
785 * @see Content::getSection()
786 */
787 public function getSection( $section ) {
788 global $wgParser;
789
790 $text = $this->getNativeData();
791 $sect = $wgParser->getSection( $text, $section, false );
792
793 return new WikitextContent( $sect );
794 }
795
796 /**
797 * @see Content::replaceSection()
798 */
799 public function replaceSection( $section, Content $with, $sectionTitle = '' ) {
800 wfProfileIn( __METHOD__ );
801
802 $myModelId = $this->getModel();
803 $sectionModelId = $with->getModel();
804
805 if ( $sectionModelId != $myModelId ) {
806 throw new MWException( "Incompatible content model for section: " .
807 "document uses $myModelId but " .
808 "section uses $sectionModelId." );
809 }
810
811 $oldtext = $this->getNativeData();
812 $text = $with->getNativeData();
813
814 if ( $section === '' ) {
815 return $with; # XXX: copy first?
816 } if ( $section == 'new' ) {
817 # Inserting a new section
818 if ( $sectionTitle ) {
819 $subject = wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n";
820 } else {
821 $subject = '';
822 }
823 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
824 $text = strlen( trim( $oldtext ) ) > 0
825 ? "{$oldtext}\n\n{$subject}{$text}"
826 : "{$subject}{$text}";
827 }
828 } else {
829 # Replacing an existing section; roll out the big guns
830 global $wgParser;
831
832 $text = $wgParser->replaceSection( $oldtext, $section, $text );
833 }
834
835 $newContent = new WikitextContent( $text );
836
837 wfProfileOut( __METHOD__ );
838 return $newContent;
839 }
840
841 /**
842 * Returns a new WikitextContent object with the given section heading
843 * prepended.
844 *
845 * @param $header string
846 * @return Content
847 */
848 public function addSectionHeader( $header ) {
849 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" .
850 $this->getNativeData();
851
852 return new WikitextContent( $text );
853 }
854
855 /**
856 * Returns a Content object with pre-save transformations applied using
857 * Parser::preSaveTransform().
858 *
859 * @param $title Title
860 * @param $user User
861 * @param $popts ParserOptions
862 * @return Content
863 */
864 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
865 global $wgParser;
866
867 $text = $this->getNativeData();
868 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
869
870 return new WikitextContent( $pst );
871 }
872
873 /**
874 * Returns a Content object with preload transformations applied (or this
875 * object if no transformations apply).
876 *
877 * @param $title Title
878 * @param $popts ParserOptions
879 * @return Content
880 */
881 public function preloadTransform( Title $title, ParserOptions $popts ) {
882 global $wgParser;
883
884 $text = $this->getNativeData();
885 $plt = $wgParser->getPreloadText( $text, $title, $popts );
886
887 return new WikitextContent( $plt );
888 }
889
890 /**
891 * Implement redirect extraction for wikitext.
892 *
893 * @return null|Title
894 *
895 * @note: migrated here from Title::newFromRedirectInternal()
896 *
897 * @see Content::getRedirectTarget
898 * @see AbstractContent::getRedirectTarget
899 */
900 public function getRedirectTarget() {
901 global $wgMaxRedirects;
902 if ( $wgMaxRedirects < 1 ) {
903 // redirects are disabled, so quit early
904 return null;
905 }
906 $redir = MagicWord::get( 'redirect' );
907 $text = trim( $this->getNativeData() );
908 if ( $redir->matchStartAndRemove( $text ) ) {
909 // Extract the first link and see if it's usable
910 // Ensure that it really does come directly after #REDIRECT
911 // Some older redirects included a colon, so don't freak about that!
912 $m = array();
913 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
914 // Strip preceding colon used to "escape" categories, etc.
915 // and URL-decode links
916 if ( strpos( $m[1], '%' ) !== false ) {
917 // Match behavior of inline link parsing here;
918 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
919 }
920 $title = Title::newFromText( $m[1] );
921 // If the title is a redirect to bad special pages or is invalid, return null
922 if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
923 return null;
924 }
925 return $title;
926 }
927 }
928 return null;
929 }
930
931 /**
932 * Returns true if this content is not a redirect, and this content's text
933 * is countable according to the criteria defined by $wgArticleCountMethod.
934 *
935 * @param $hasLinks Bool if it is known whether this content contains
936 * links, provide this information here, to avoid redundant parsing to
937 * find out.
938 * @param $title null|\Title
939 *
940 * @internal param \IContextSource $context context for parsing if necessary
941 *
942 * @return bool True if the content is countable
943 */
944 public function isCountable( $hasLinks = null, Title $title = null ) {
945 global $wgArticleCountMethod;
946
947 if ( $this->isRedirect( ) ) {
948 return false;
949 }
950
951 $text = $this->getNativeData();
952
953 switch ( $wgArticleCountMethod ) {
954 case 'any':
955 return true;
956 case 'comma':
957 return strpos( $text, ',' ) !== false;
958 case 'link':
959 if ( $hasLinks === null ) { # not known, find out
960 if ( !$title ) {
961 $context = RequestContext::getMain();
962 $title = $context->getTitle();
963 }
964
965 $po = $this->getParserOutput( $title, null, null, false );
966 $links = $po->getLinks();
967 $hasLinks = !empty( $links );
968 }
969
970 return $hasLinks;
971 }
972
973 return false;
974 }
975
976 public function getTextForSummary( $maxlength = 250 ) {
977 $truncatedtext = parent::getTextForSummary( $maxlength );
978
979 # clean up unfinished links
980 # XXX: make this optional? wasn't there in autosummary, but required for
981 # deletion summary.
982 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
983
984 return $truncatedtext;
985 }
986
987 }
988
989 /**
990 * @since WD.1
991 */
992 class MessageContent extends TextContent {
993 public function __construct( $msg_key, $params = null, $options = null ) {
994 # XXX: messages may be wikitext, html or plain text! and maybe even
995 # something else entirely.
996 parent::__construct( null, CONTENT_MODEL_WIKITEXT );
997
998 $this->mMessageKey = $msg_key;
999
1000 $this->mParameters = $params;
1001
1002 if ( is_null( $options ) ) {
1003 $options = array();
1004 }
1005 elseif ( is_string( $options ) ) {
1006 $options = array( $options );
1007 }
1008
1009 $this->mOptions = $options;
1010 }
1011
1012 /**
1013 * Returns the message as rendered HTML, using the options supplied to the
1014 * constructor plus "parse".
1015 * @param the message text, parsed
1016 */
1017 public function getHtml( ) {
1018 $opt = array_merge( $this->mOptions, array( 'parse' ) );
1019
1020 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
1021 }
1022
1023
1024 /**
1025 * Returns the message as raw text, using the options supplied to the
1026 * constructor minus "parse" and "parseinline".
1027 *
1028 * @param the message text, unparsed.
1029 */
1030 public function getNativeData( ) {
1031 $opt = array_diff( $this->mOptions, array( 'parse', 'parseinline' ) );
1032
1033 return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
1034 }
1035
1036 }
1037
1038 /**
1039 * @since WD.1
1040 */
1041 class JavaScriptContent extends TextContent {
1042 public function __construct( $text ) {
1043 parent::__construct( $text, CONTENT_MODEL_JAVASCRIPT );
1044 }
1045
1046 /**
1047 * Returns a Content object with pre-save transformations applied using
1048 * Parser::preSaveTransform().
1049 *
1050 * @param Title $title
1051 * @param User $user
1052 * @param ParserOptions $popts
1053 * @return Content
1054 */
1055 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
1056 global $wgParser;
1057 // @todo: make pre-save transformation optional for script pages
1058 // See bug #32858
1059
1060 $text = $this->getNativeData();
1061 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
1062
1063 return new JavaScriptContent( $pst );
1064 }
1065
1066 }
1067
1068 /**
1069 * @since WD.1
1070 */
1071 class CssContent extends TextContent {
1072 public function __construct( $text ) {
1073 parent::__construct( $text, CONTENT_MODEL_CSS );
1074 }
1075
1076 /**
1077 * Returns a Content object with pre-save transformations applied using
1078 * Parser::preSaveTransform().
1079 *
1080 * @param $title Title
1081 * @param $user User
1082 * @param $popts ParserOptions
1083 * @return Content
1084 */
1085 public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
1086 global $wgParser;
1087 // @todo: make pre-save transformation optional for script pages
1088
1089 $text = $this->getNativeData();
1090 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
1091
1092 return new CssContent( $pst );
1093 }
1094
1095 }