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.
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.
17 * @todo: test that this actually works
18 * @todo: make sure this also works with LuceneSearch / WikiSearch
20 public function getTextForSearchIndex( );
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.
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?!
34 public function getWikitextForTransclusion( );
37 * Returns a textual representation of the content suitable for use in edit
38 * summaries and log messages.
42 * @param $maxlength int Maximum length of the summary text
43 * @return The summary text
45 public function getTextForSummary( $maxlength = 250 );
48 * Returns native representation of the data. Interpretation depends on
49 * the data model used, as given by getDataModel().
53 * @return mixed The native representation of the content. Could be a
54 * string, a nested array structure, an object, a binary blob...
57 * @NOTE: review all calls carefully, caller must be aware of content model!
59 public function getNativeData( );
62 * Returns the content's nominal size in bogo-bytes.
66 public function getSize( );
69 * Returns the ID of the content model used by this Content object.
70 * Corresponds to the CONTENT_MODEL_XXX constants.
74 * @return String The model id
76 public function getModel();
79 * Convenience method that returns the ContentHandler singleton for handling
80 * the content model that this Content object uses.
82 * Shorthand for ContentHandler::getForContent( $this )
86 * @return ContentHandler
88 public function getContentHandler();
91 * Convenience method that returns the default serialization format for the
92 * content model that this Content object uses.
94 * Shorthand for $this->getContentHandler()->getDefaultFormat()
100 public function getDefaultFormat();
103 * Convenience method that returns the list of serialization formats
104 * supported for the content model that this Content object uses.
106 * Shorthand for $this->getContentHandler()->getSupportedFormats()
110 * @return Array of supported serialization formats
112 public function getSupportedFormats();
115 * Returns true if $format is a supported serialization format for this
116 * Content object, false if it isn't.
118 * Note that this should always return true if $format is null, because null
119 * stands for the default serialization.
121 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
125 * @param $format string The format to check
126 * @return bool Whether the format is supported
128 public function isSupportedFormat( $format );
131 * Convenience method for serializing this Content object.
133 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
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
141 public function serialize( $format = null );
144 * Returns true if this Content object represents empty content.
148 * @return bool Whether this Content object is empty
150 public function isEmpty();
153 * Returns whether the content is valid. This is intended for local validity
154 * checks, not considering global consistency.
156 * Content needs to be valid before it can be saved.
158 * This default implementation always returns true.
164 public function isValid();
167 * Returns true if this Content objects is conceptually equivalent to the
168 * given Content object.
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.
178 * Implementations should be careful to make equals() transitive and reflexive:
180 * - $a->equals( $b ) <=> $b->equals( $a )
181 * - $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
185 * @param $that Content The Content object to compare to
186 * @return bool True if this Content object is equal to $that, false otherwise.
188 public function equals( Content
$that = null );
191 * Return a copy of this Content object. The following must be true for the
194 * if $copy = $original->copy()
196 * - get_class($original) === get_class($copy)
197 * - $original->getModel() === $copy->getModel()
198 * - $original->equals( $copy )
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.
206 * @return Content. A copy of this object
208 public function copy( );
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
217 * @param $hasLinks Bool: If it is known whether this content contains
218 * links, provide this information here, to avoid redundant parsing to
222 public function isCountable( $hasLinks = null ) ;
226 * Parse the Content object and generate a ParserOutput from the result.
227 * $result->getText() can be used to obtain the generated HTML. If no HTML
228 * is needed, $generateHtml can be set to false; in that case,
229 * $result->getText() may return null.
231 * @param $title Title The page title to use as a context for rendering
232 * @param $revId null|int The revision being rendered (optional)
233 * @param $options null|ParserOptions Any parser options
234 * @param $generateHtml Boolean Whether to generate HTML (default: true). If false,
235 * the result of calling getText() on the ParserOutput object returned by
236 * this method is undefined.
240 * @return ParserOutput
242 public function getParserOutput( Title
$title,
244 ParserOptions
$options = null, $generateHtml = true );
245 # TODO: make RenderOutput and RenderOptions base classes
248 * Returns a list of DataUpdate objects for recording information about this
249 * Content in some secondary data store. If the optional second argument,
250 * $old, is given, the updates may model only the changes that need to be
251 * made to replace information about the old content with information about
254 * This default implementation calls
255 * $this->getParserOutput( $content, $title, null, null, false ),
256 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
257 * resulting ParserOutput object.
259 * Subclasses may implement this to determine the necessary updates more
260 * efficiently, or make use of information about the old content.
262 * @param $title Title The context for determining the necessary updates
263 * @param $old Content|null An optional Content object representing the
264 * previous content, i.e. the content being replaced by this Content
266 * @param $recursive boolean Whether to include recursive updates (default:
268 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
269 * Provide if you have one handy, to avoid re-parsing of the content.
271 * @return Array. A list of DataUpdate objects for putting information
272 * about this content object somewhere.
276 public function getSecondaryDataUpdates( Title
$title,
278 $recursive = true, ParserOutput
$parserOutput = null
282 * Construct the redirect destination from this content and return an
283 * array of Titles, or null if this content doesn't represent a redirect.
284 * The last element in the array is the final destination after all redirects
285 * have been resolved (up to $wgMaxRedirects times).
289 * @return Array of Titles, with the destination last
291 public function getRedirectChain();
294 * Construct the redirect destination from this content and return a Title,
295 * or null if this content doesn't represent a redirect.
296 * This will only return the immediate redirect target, useful for
297 * the redirect table and other checks that don't need full recursion.
301 * @return Title: The corresponding Title
303 public function getRedirectTarget();
306 * Construct the redirect destination from this content and return the
307 * Title, or null if this content doesn't represent a redirect.
309 * This will recurse down $wgMaxRedirects times or until a non-redirect
310 * target is hit in order to provide (hopefully) the Title of the final
311 * destination instead of another redirect.
313 * There is usually no need to override the default behaviour, subclasses that
314 * want to implement redirects should override getRedirectTarget().
320 public function getUltimateRedirectTarget();
323 * Returns whether this Content represents a redirect.
324 * Shorthand for getRedirectTarget() !== null.
330 public function isRedirect();
333 * Returns the section with the given ID.
337 * @param $sectionId string The section's ID, given as a numeric string.
338 * The ID "0" retrieves the section before the first heading, "1" the
339 * text between the first heading (included) and the second heading
341 * @return Content|Boolean|null The section, or false if no such section
342 * exist, or null if sections are not supported.
344 public function getSection( $sectionId );
347 * Replaces a section of the content and returns a Content object with the
352 * @param $section Empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
353 * @param $with Content: new content of the section
354 * @param $sectionTitle String: new section's subject, only if $section is 'new'
355 * @return string Complete article text, or null if error
357 public function replaceSection( $section, Content
$with, $sectionTitle = '' );
360 * Returns a Content object with pre-save transformations applied (or this
361 * object if no transformations apply).
365 * @param $title Title
367 * @param $popts null|ParserOptions
370 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts );
373 * Returns a new WikitextContent object with the given section heading
374 * prepended, if supported. The default implementation just returns this
375 * Content object unmodified, ignoring the section header.
379 * @param $header string
382 public function addSectionHeader( $header );
385 * Returns a Content object with preload transformations applied (or this
386 * object if no transformations apply).
390 * @param $title Title
391 * @param $popts null|ParserOptions
394 public function preloadTransform( Title
$title, ParserOptions
$popts );
397 * Prepare Content for saving. Called before Content is saved by WikiPage::doEditContent() and in
400 * This may be used to check the content's consistency with global state. This function should
401 * NOT write any information to the database.
403 * Note that this method will usually be called inside the same transaction bracket that will be used
404 * to save the new revision.
406 * Note that this method is called before any update to the page table is performed. This means that
407 * $page may not yet know a page ID.
409 * @param WikiPage $page The page to be saved.
410 * @param int $flags bitfield for use with EDIT_XXX constants, see WikiPage::doEditContent()
411 * @param int $baseRevId the ID of the current revision
414 * @return Status A status object indicating whether the content was successfully prepared for saving.
415 * If the returned status indicates an error, a rollback will be performed and the
416 * transaction aborted.
418 * @see see WikiPage::doEditContent()
420 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user );
423 * Returns a list of updates to perform when this content is deleted.
424 * The necessary updates may be taken from the Content object, or depend on
425 * the current state of the database.
429 * @param $title \Title the title of the deleted page
430 * @param $parserOutput null|\ParserOutput optional parser output object
431 * for efficient access to meta-information about the content object.
432 * Provide if you have one handy.
434 * @return array A list of DataUpdate instances that will clean up the
435 * database after deletion.
437 public function getDeletionUpdates( Title
$title,
438 ParserOutput
$parserOutput = null );
440 # TODO: handle ImagePage and CategoryPage
441 # TODO: make sure we cover lucene search / wikisearch.
442 # TODO: make sure ReplaceTemplates still works
443 # FUTURE: nice&sane integration of GeSHi syntax highlighting
444 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a
445 # config to set the class which handles syntax highlighting
446 # [12:00] <vvv> And default it to a DummyHighlighter
448 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
450 # TODO: tie into API to provide contentModel for Revisions
451 # TODO: tie into API to provide serialized version and contentFormat for Revisions
452 # TODO: tie into API edit interface
453 # FUTURE: make EditForm plugin for EditPage
455 # FUTURE: special type for redirects?!
456 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
457 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
462 * A content object represents page content, e.g. the text to show on a page.
463 * Content objects have no knowledge about how they relate to Wiki pages.
467 abstract class AbstractContent
implements Content
{
470 * Name of the content model this Content object represents.
471 * Use with CONTENT_MODEL_XXX constants
473 * @var string $model_id
478 * @param String $model_id
480 public function __construct( $model_id = null ) {
481 $this->model_id
= $model_id;
485 * @see Content::getModel()
487 public function getModel() {
488 return $this->model_id
;
492 * Throws an MWException if $model_id is not the id of the content model
493 * supported by this Content object.
495 * @param $model_id int the model to check
497 * @throws MWException
499 protected function checkModelID( $model_id ) {
500 if ( $model_id !== $this->model_id
) {
501 throw new MWException( "Bad content model: " .
502 "expected {$this->model_id} " .
503 "but got $model_id." );
508 * @see Content::getContentHandler()
510 public function getContentHandler() {
511 return ContentHandler
::getForContent( $this );
515 * @see Content::getDefaultFormat()
517 public function getDefaultFormat() {
518 return $this->getContentHandler()->getDefaultFormat();
522 * @see Content::getSupportedFormats()
524 public function getSupportedFormats() {
525 return $this->getContentHandler()->getSupportedFormats();
529 * @see Content::isSupportedFormat()
531 public function isSupportedFormat( $format ) {
533 return true; // this means "use the default"
536 return $this->getContentHandler()->isSupportedFormat( $format );
540 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
544 * @throws MWException
546 protected function checkFormat( $format ) {
547 if ( !$this->isSupportedFormat( $format ) ) {
548 throw new MWException( "Format $format is not supported for content model " .
554 * @see Content::serialize
556 public function serialize( $format = null ) {
557 return $this->getContentHandler()->serializeContent( $this, $format );
561 * @see Content::isEmpty()
563 public function isEmpty() {
564 return $this->getSize() == 0;
568 * @see Content::isValid()
570 public function isValid() {
575 * @see Content::equals()
577 public function equals( Content
$that = null ) {
578 if ( is_null( $that ) ) {
582 if ( $that === $this ) {
586 if ( $that->getModel() !== $this->getModel() ) {
590 return $this->getNativeData() === $that->getNativeData();
595 * Returns a list of DataUpdate objects for recording information about this
596 * Content in some secondary data store.
598 * This default implementation calls
599 * $this->getParserOutput( $content, $title, null, null, false ),
600 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
601 * resulting ParserOutput object.
603 * Subclasses may override this to determine the secondary data updates more
604 * efficiently, preferrably without the need to generate a parser output object.
606 * @see Content::getSecondaryDataUpdates()
608 * @param $title Title The context for determining the necessary updates
609 * @param $old Content|null An optional Content object representing the
610 * previous content, i.e. the content being replaced by this Content
612 * @param $recursive boolean Whether to include recursive updates (default:
614 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
615 * Provide if you have one handy, to avoid re-parsing of the content.
617 * @return Array. A list of DataUpdate objects for putting information
618 * about this content object somewhere.
622 public function getSecondaryDataUpdates( Title
$title,
624 $recursive = true, ParserOutput
$parserOutput = null
626 if ( !$parserOutput ) {
627 $parserOutput = $this->getParserOutput( $title, null, null, false );
630 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
635 * @see Content::getRedirectChain()
637 public function getRedirectChain() {
638 global $wgMaxRedirects;
639 $title = $this->getRedirectTarget();
640 if ( is_null( $title ) ) {
643 // recursive check to follow double redirects
644 $recurse = $wgMaxRedirects;
645 $titles = array( $title );
646 while ( --$recurse > 0 ) {
647 if ( $title->isRedirect() ) {
648 $page = WikiPage
::factory( $title );
649 $newtitle = $page->getRedirectTarget();
653 // Redirects to some special pages are not permitted
654 if ( $newtitle instanceOf Title
&& $newtitle->isValidRedirectTarget() ) {
655 // The new title passes the checks, so make that our current
656 // title so that further recursion can be checked
658 $titles[] = $newtitle;
667 * @see Content::getRedirectTarget()
669 public function getRedirectTarget() {
674 * @see Content::getUltimateRedirectTarget()
675 * @note: migrated here from Title::newFromRedirectRecurse
677 public function getUltimateRedirectTarget() {
678 $titles = $this->getRedirectChain();
679 return $titles ?
array_pop( $titles ) : null;
687 public function isRedirect() {
688 return $this->getRedirectTarget() !== null;
692 * @see Content::getSection()
694 public function getSection( $sectionId ) {
699 * @see Content::replaceSection()
701 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
706 * @see Content::preSaveTransform()
708 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
713 * @see Content::addSectionHeader()
715 public function addSectionHeader( $header ) {
720 * @see Content::preloadTransform()
722 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
727 * @see Content::prepareSave()
729 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
730 if ( $this->isValid() ) {
731 return Status
::newGood();
733 return Status
::newFatal( "invalid-content-data" );
738 * Returns a list of updates to perform when this content is deleted.
739 * The necessary updates may be taken from the Content object, or depend on
740 * the current state of the database.
744 * @param $title \Title the title of the deleted page
745 * @param $parserOutput null|\ParserOutput optional parser output object
746 * for efficient access to meta-information about the content object.
747 * Provide if you have one handy.
749 * @return array A list of DataUpdate instances that will clean up the
750 * database after deletion.
752 public function getDeletionUpdates( Title
$title,
753 ParserOutput
$parserOutput = null )
756 new LinksDeletionUpdate( $title ),
762 * Content object implementation for representing flat text.
764 * TextContent instances are immutable
768 abstract class TextContent
extends AbstractContent
{
770 public function __construct( $text, $model_id = null ) {
771 parent
::__construct( $model_id );
773 $this->mText
= $text;
776 public function copy() {
777 return $this; # NOTE: this is ok since TextContent are immutable.
780 public function getTextForSummary( $maxlength = 250 ) {
783 $text = $this->getNativeData();
785 $truncatedtext = $wgContLang->truncate(
786 preg_replace( "/[\n\r]/", ' ', $text ),
787 max( 0, $maxlength ) );
789 return $truncatedtext;
793 * returns the text's size in bytes.
795 * @return int The size
797 public function getSize( ) {
798 $text = $this->getNativeData( );
799 return strlen( $text );
803 * Returns true if this content is not a redirect, and $wgArticleCountMethod
806 * @param $hasLinks Bool: if it is known whether this content contains links,
807 * provide this information here, to avoid redundant parsing to find out.
809 * @return bool True if the content is countable
811 public function isCountable( $hasLinks = null ) {
812 global $wgArticleCountMethod;
814 if ( $this->isRedirect( ) ) {
818 if ( $wgArticleCountMethod === 'any' ) {
826 * Returns the text represented by this Content object, as a string.
828 * @param the raw text
830 public function getNativeData( ) {
831 $text = $this->mText
;
836 * Returns the text represented by this Content object, as a string.
838 * @param the raw text
840 public function getTextForSearchIndex( ) {
841 return $this->getNativeData();
845 * Returns the text represented by this Content object, as a string.
847 * @param the raw text
849 public function getWikitextForTransclusion( ) {
850 return $this->getNativeData();
854 * Diff this content object with another content object..
858 * @param $that Content the other content object to compare this content object to
859 * @param $lang Language the language object to use for text segmentation.
860 * If not given, $wgContentLang is used.
862 * @return DiffResult a diff representing the changes that would have to be
863 * made to this content object to make it equal to $that.
865 public function diff( Content
$that, Language
$lang = null ) {
868 $this->checkModelID( $that->getModel() );
870 # @todo: could implement this in DifferenceEngine and just delegate here?
872 if ( !$lang ) $lang = $wgContLang;
874 $otext = $this->getNativeData();
875 $ntext = $this->getNativeData();
877 # Note: Use native PHP diff, external engines don't give us abstract output
878 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
879 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
881 $diff = new Diff( $ota, $nta );
887 * Returns a generic ParserOutput object, wrapping the HTML returned by
890 * @param $title Title Context title for parsing
891 * @param $revId int|null Revision ID (for {{REVISIONID}})
892 * @param $options ParserOptions|null Parser options
893 * @param $generateHtml bool Whether or not to generate HTML
895 * @return ParserOutput representing the HTML form of the text
897 public function getParserOutput( Title
$title,
899 ParserOptions
$options = null, $generateHtml = true
901 # Generic implementation, relying on $this->getHtml()
903 if ( $generateHtml ) {
904 $html = $this->getHtml();
909 $po = new ParserOutput( $html );
914 * Generates an HTML version of the content, for display. Used by
915 * getParserOutput() to construct a ParserOutput object.
917 * This default implementation just calls getHighlightHtml(). Content
918 * models that have another mapping to HTML (as is the case for markup
919 * languages like wikitext) should override this method to generate the
922 * @return string An HTML representation of the content
924 protected function getHtml() {
925 return $this->getHighlightHtml();
929 * Generates a syntax-highlighted version of the content, as HTML.
930 * Used by the default implementation of getHtml().
932 * @return string an HTML representation of the content's markup
934 protected function getHighlightHtml( ) {
935 # TODO: make Highlighter interface, use highlighter here, if available
936 return htmlspecialchars( $this->getNativeData() );
944 class WikitextContent
extends TextContent
{
946 public function __construct( $text ) {
947 parent
::__construct( $text, CONTENT_MODEL_WIKITEXT
);
951 * @see Content::getSection()
953 public function getSection( $section ) {
956 $text = $this->getNativeData();
957 $sect = $wgParser->getSection( $text, $section, false );
959 return new WikitextContent( $sect );
963 * @see Content::replaceSection()
965 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
966 wfProfileIn( __METHOD__
);
968 $myModelId = $this->getModel();
969 $sectionModelId = $with->getModel();
971 if ( $sectionModelId != $myModelId ) {
972 throw new MWException( "Incompatible content model for section: " .
973 "document uses $myModelId but " .
974 "section uses $sectionModelId." );
977 $oldtext = $this->getNativeData();
978 $text = $with->getNativeData();
980 if ( $section === '' ) {
981 return $with; # XXX: copy first?
982 } if ( $section == 'new' ) {
983 # Inserting a new section
984 if ( $sectionTitle ) {
985 $subject = wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n";
989 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
990 $text = strlen( trim( $oldtext ) ) > 0
991 ?
"{$oldtext}\n\n{$subject}{$text}"
992 : "{$subject}{$text}";
995 # Replacing an existing section; roll out the big guns
998 $text = $wgParser->replaceSection( $oldtext, $section, $text );
1001 $newContent = new WikitextContent( $text );
1003 wfProfileOut( __METHOD__
);
1008 * Returns a new WikitextContent object with the given section heading
1011 * @param $header string
1014 public function addSectionHeader( $header ) {
1015 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" .
1016 $this->getNativeData();
1018 return new WikitextContent( $text );
1022 * Returns a Content object with pre-save transformations applied using
1023 * Parser::preSaveTransform().
1025 * @param $title Title
1027 * @param $popts ParserOptions
1030 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
1033 $text = $this->getNativeData();
1034 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
1036 return new WikitextContent( $pst );
1040 * Returns a Content object with preload transformations applied (or this
1041 * object if no transformations apply).
1043 * @param $title Title
1044 * @param $popts ParserOptions
1047 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
1050 $text = $this->getNativeData();
1051 $plt = $wgParser->getPreloadText( $text, $title, $popts );
1053 return new WikitextContent( $plt );
1057 * Implement redirect extraction for wikitext.
1059 * @return null|Title
1061 * @note: migrated here from Title::newFromRedirectInternal()
1063 * @see Content::getRedirectTarget
1064 * @see AbstractContent::getRedirectTarget
1066 public function getRedirectTarget() {
1067 global $wgMaxRedirects;
1068 if ( $wgMaxRedirects < 1 ) {
1069 // redirects are disabled, so quit early
1072 $redir = MagicWord
::get( 'redirect' );
1073 $text = trim( $this->getNativeData() );
1074 if ( $redir->matchStartAndRemove( $text ) ) {
1075 // Extract the first link and see if it's usable
1076 // Ensure that it really does come directly after #REDIRECT
1077 // Some older redirects included a colon, so don't freak about that!
1079 if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}!', $text, $m ) ) {
1080 // Strip preceding colon used to "escape" categories, etc.
1081 // and URL-decode links
1082 if ( strpos( $m[1], '%' ) !== false ) {
1083 // Match behavior of inline link parsing here;
1084 $m[1] = rawurldecode( ltrim( $m[1], ':' ) );
1086 $title = Title
::newFromText( $m[1] );
1087 // If the title is a redirect to bad special pages or is invalid, return null
1088 if ( !$title instanceof Title ||
!$title->isValidRedirectTarget() ) {
1098 * Returns true if this content is not a redirect, and this content's text
1099 * is countable according to the criteria defined by $wgArticleCountMethod.
1101 * @param $hasLinks Bool if it is known whether this content contains
1102 * links, provide this information here, to avoid redundant parsing to
1104 * @param $title null|\Title
1106 * @internal param \IContextSource $context context for parsing if necessary
1108 * @return bool True if the content is countable
1110 public function isCountable( $hasLinks = null, Title
$title = null ) {
1111 global $wgArticleCountMethod;
1113 if ( $this->isRedirect( ) ) {
1117 $text = $this->getNativeData();
1119 switch ( $wgArticleCountMethod ) {
1123 return strpos( $text, ',' ) !== false;
1125 if ( $hasLinks === null ) { # not known, find out
1127 $context = RequestContext
::getMain();
1128 $title = $context->getTitle();
1131 $po = $this->getParserOutput( $title, null, null, false );
1132 $links = $po->getLinks();
1133 $hasLinks = !empty( $links );
1142 public function getTextForSummary( $maxlength = 250 ) {
1143 $truncatedtext = parent
::getTextForSummary( $maxlength );
1145 # clean up unfinished links
1146 # XXX: make this optional? wasn't there in autosummary, but required for
1148 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
1150 return $truncatedtext;
1155 * Returns a ParserOutput object resulting from parsing the content's text
1160 * @param $content Content the content to render
1161 * @param $title \Title
1162 * @param $revId null
1163 * @param $options null|ParserOptions
1164 * @param $generateHtml bool
1166 * @internal param \IContextSource|null $context
1167 * @return ParserOutput representing the HTML form of the text
1169 public function getParserOutput( Title
$title,
1171 ParserOptions
$options = null, $generateHtml = true
1176 $options = new ParserOptions();
1179 $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
1183 protected function getHtml() {
1184 throw new MWException(
1185 "getHtml() not implemented for wikitext. "
1186 . "Use getParserOutput()->getText()."
1196 class MessageContent
extends TextContent
{
1197 public function __construct( $msg_key, $params = null, $options = null ) {
1198 # XXX: messages may be wikitext, html or plain text! and maybe even
1199 # something else entirely.
1200 parent
::__construct( null, CONTENT_MODEL_WIKITEXT
);
1202 $this->mMessageKey
= $msg_key;
1204 $this->mParameters
= $params;
1206 if ( is_null( $options ) ) {
1209 elseif ( is_string( $options ) ) {
1210 $options = array( $options );
1213 $this->mOptions
= $options;
1217 * Returns the message as rendered HTML, using the options supplied to the
1218 * constructor plus "parse".
1219 * @param the message text, parsed
1221 public function getHtml( ) {
1222 $opt = array_merge( $this->mOptions
, array( 'parse' ) );
1224 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
1229 * Returns the message as raw text, using the options supplied to the
1230 * constructor minus "parse" and "parseinline".
1232 * @param the message text, unparsed.
1234 public function getNativeData( ) {
1235 $opt = array_diff( $this->mOptions
, array( 'parse', 'parseinline' ) );
1237 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
1245 class JavaScriptContent
extends TextContent
{
1246 public function __construct( $text ) {
1247 parent
::__construct( $text, CONTENT_MODEL_JAVASCRIPT
);
1251 * Returns a Content object with pre-save transformations applied using
1252 * Parser::preSaveTransform().
1254 * @param Title $title
1256 * @param ParserOptions $popts
1259 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
1261 // @todo: make pre-save transformation optional for script pages
1264 $text = $this->getNativeData();
1265 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
1267 return new JavaScriptContent( $pst );
1271 protected function getHtml( ) {
1273 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
1274 $html .= $this->getHighlightHtml( );
1275 $html .= "\n</pre>\n";
1284 class CssContent
extends TextContent
{
1285 public function __construct( $text ) {
1286 parent
::__construct( $text, CONTENT_MODEL_CSS
);
1290 * Returns a Content object with pre-save transformations applied using
1291 * Parser::preSaveTransform().
1293 * @param $title Title
1295 * @param $popts ParserOptions
1298 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
1300 // @todo: make pre-save transformation optional for script pages
1302 $text = $this->getNativeData();
1303 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
1305 return new CssContent( $pst );
1309 protected function getHtml( ) {
1311 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
1312 $html .= $this->getHighlightHtml( );
1313 $html .= "\n</pre>\n";