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