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 ) ;
225 * Convenience method, shorthand for
226 * $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml )
228 * @note: subclasses should NOT override this to provide custom rendering.
229 * Override ContentHandler::getParserOutput() instead!
231 * @param $title Title
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.
240 * @return ParserOutput
242 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = null,
243 $generateHtml = true );
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).
253 * @return Array of Titles, with the destination last
255 public function getRedirectChain();
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.
265 * @return Title: The corresponding Title
267 public function getRedirectTarget();
270 * Construct the redirect destination from this content and return the
271 * Title, or null if this content doesn't represent a redirect.
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.
277 * There is usually no need to override the default behaviour, subclasses that
278 * want to implement redirects should override getRedirectTarget().
284 public function getUltimateRedirectTarget();
287 * Returns whether this Content represents a redirect.
288 * Shorthand for getRedirectTarget() !== null.
294 public function isRedirect();
297 * Returns the section with the given ID.
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
305 * @return Content|Boolean|null The section, or false if no such section
306 * exist, or null if sections are not supported.
308 public function getSection( $sectionId );
311 * Replaces a section of the content and returns a Content object with the
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
321 public function replaceSection( $section, Content
$with, $sectionTitle = '' );
324 * Returns a Content object with pre-save transformations applied (or this
325 * object if no transformations apply).
329 * @param $title Title
331 * @param $popts null|ParserOptions
334 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts );
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.
343 * @param $header string
346 public function addSectionHeader( $header );
349 * Returns a Content object with preload transformations applied (or this
350 * object if no transformations apply).
354 * @param $title Title
355 * @param $popts null|ParserOptions
358 public function preloadTransform( Title
$title, ParserOptions
$popts );
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.
365 * Note that this method will be called inside the same transaction bracket that will be used
366 * to save the new revision.
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
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.
377 * @see see WikiPage::doEditContent()
379 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user );
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
389 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
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
396 # FUTURE: special type for redirects?!
397 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
398 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
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.
408 abstract class AbstractContent
implements Content
{
411 * Name of the content model this Content object represents.
412 * Use with CONTENT_MODEL_XXX constants
414 * @var string $model_id
419 * @param String $model_id
421 public function __construct( $model_id = null ) {
422 $this->model_id
= $model_id;
426 * @see Content::getModel()
428 public function getModel() {
429 return $this->model_id
;
433 * Throws an MWException if $model_id is not the id of the content model
434 * supported by this Content object.
436 * @param $model_id int the model to check
438 * @throws MWException
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." );
449 * @see Content::getContentHandler()
451 public function getContentHandler() {
452 return ContentHandler
::getForContent( $this );
456 * @see Content::getDefaultFormat()
458 public function getDefaultFormat() {
459 return $this->getContentHandler()->getDefaultFormat();
463 * @see Content::getSupportedFormats()
465 public function getSupportedFormats() {
466 return $this->getContentHandler()->getSupportedFormats();
470 * @see Content::isSupportedFormat()
472 public function isSupportedFormat( $format ) {
474 return true; // this means "use the default"
477 return $this->getContentHandler()->isSupportedFormat( $format );
481 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't
485 * @throws MWException
487 protected function checkFormat( $format ) {
488 if ( !$this->isSupportedFormat( $format ) ) {
489 throw new MWException( "Format $format is not supported for content model " .
495 * @see Content::serialize
497 public function serialize( $format = null ) {
498 return $this->getContentHandler()->serializeContent( $this, $format );
502 * @see Content::isEmpty()
504 public function isEmpty() {
505 return $this->getSize() == 0;
509 * @see Content::isValid()
511 public function isValid() {
516 * @see Content::equals()
518 public function equals( Content
$that = null ) {
519 if ( is_null( $that ) ) {
523 if ( $that === $this ) {
527 if ( $that->getModel() !== $this->getModel() ) {
531 return $this->getNativeData() === $that->getNativeData();
535 * @see Content::getParserOutput()
537 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = null,
538 $generateHtml = true )
540 return $this->getContentHandler()->getParserOutput(
541 $this, $title, $revId, $options, $generateHtml );
545 * @see Content::getRedirectChain()
547 public function getRedirectChain() {
548 global $wgMaxRedirects;
549 $title = $this->getRedirectTarget();
550 if ( is_null( $title ) ) {
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();
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
568 $titles[] = $newtitle;
577 * @see Content::getRedirectTarget()
579 public function getRedirectTarget() {
584 * @see Content::getUltimateRedirectTarget()
585 * @note: migrated here from Title::newFromRedirectRecurse
587 public function getUltimateRedirectTarget() {
588 $titles = $this->getRedirectChain();
589 return $titles ?
array_pop( $titles ) : null;
597 public function isRedirect() {
598 return $this->getRedirectTarget() !== null;
602 * @see Content::getSection()
604 public function getSection( $sectionId ) {
609 * @see Content::replaceSection()
611 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
616 * @see Content::preSaveTransform()
618 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
623 * @see Content::addSectionHeader()
625 public function addSectionHeader( $header ) {
630 * @see Content::preloadTransform()
632 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
637 * @see Content::prepareSave()
639 public function prepareSave( WikiPage
$page, $flags, $baseRevId, User
$user ) {
640 if ( $this->isValid() ) {
641 return Status
::newGood();
643 return Status
::newFatal( "invalid-content-data" );
649 * Content object implementation for representing flat text.
651 * TextContent instances are immutable
655 abstract class TextContent
extends AbstractContent
{
657 public function __construct( $text, $model_id = null ) {
658 parent
::__construct( $model_id );
660 $this->mText
= $text;
663 public function copy() {
664 return $this; # NOTE: this is ok since TextContent are immutable.
667 public function getTextForSummary( $maxlength = 250 ) {
670 $text = $this->getNativeData();
672 $truncatedtext = $wgContLang->truncate(
673 preg_replace( "/[\n\r]/", ' ', $text ),
674 max( 0, $maxlength ) );
676 return $truncatedtext;
680 * returns the text's size in bytes.
682 * @return int The size
684 public function getSize( ) {
685 $text = $this->getNativeData( );
686 return strlen( $text );
690 * Returns true if this content is not a redirect, and $wgArticleCountMethod
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.
696 * @return bool True if the content is countable
698 public function isCountable( $hasLinks = null ) {
699 global $wgArticleCountMethod;
701 if ( $this->isRedirect( ) ) {
705 if ( $wgArticleCountMethod === 'any' ) {
713 * Returns the text represented by this Content object, as a string.
715 * @param the raw text
717 public function getNativeData( ) {
718 $text = $this->mText
;
723 * Returns the text represented by this Content object, as a string.
725 * @param the raw text
727 public function getTextForSearchIndex( ) {
728 return $this->getNativeData();
732 * Returns the text represented by this Content object, as a string.
734 * @param the raw text
736 public function getWikitextForTransclusion( ) {
737 return $this->getNativeData();
741 * Diff this content object with another content object..
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.
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.
752 public function diff( Content
$that, Language
$lang = null ) {
755 $this->checkModelID( $that->getModel() );
757 # @todo: could implement this in DifferenceEngine and just delegate here?
759 if ( !$lang ) $lang = $wgContLang;
761 $otext = $this->getNativeData();
762 $ntext = $this->getNativeData();
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 ) );
768 $diff = new Diff( $ota, $nta );
778 class WikitextContent
extends TextContent
{
780 public function __construct( $text ) {
781 parent
::__construct( $text, CONTENT_MODEL_WIKITEXT
);
785 * @see Content::getSection()
787 public function getSection( $section ) {
790 $text = $this->getNativeData();
791 $sect = $wgParser->getSection( $text, $section, false );
793 return new WikitextContent( $sect );
797 * @see Content::replaceSection()
799 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
800 wfProfileIn( __METHOD__
);
802 $myModelId = $this->getModel();
803 $sectionModelId = $with->getModel();
805 if ( $sectionModelId != $myModelId ) {
806 throw new MWException( "Incompatible content model for section: " .
807 "document uses $myModelId but " .
808 "section uses $sectionModelId." );
811 $oldtext = $this->getNativeData();
812 $text = $with->getNativeData();
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";
823 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
824 $text = strlen( trim( $oldtext ) ) > 0
825 ?
"{$oldtext}\n\n{$subject}{$text}"
826 : "{$subject}{$text}";
829 # Replacing an existing section; roll out the big guns
832 $text = $wgParser->replaceSection( $oldtext, $section, $text );
835 $newContent = new WikitextContent( $text );
837 wfProfileOut( __METHOD__
);
842 * Returns a new WikitextContent object with the given section heading
845 * @param $header string
848 public function addSectionHeader( $header ) {
849 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" .
850 $this->getNativeData();
852 return new WikitextContent( $text );
856 * Returns a Content object with pre-save transformations applied using
857 * Parser::preSaveTransform().
859 * @param $title Title
861 * @param $popts ParserOptions
864 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
867 $text = $this->getNativeData();
868 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
870 return new WikitextContent( $pst );
874 * Returns a Content object with preload transformations applied (or this
875 * object if no transformations apply).
877 * @param $title Title
878 * @param $popts ParserOptions
881 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
884 $text = $this->getNativeData();
885 $plt = $wgParser->getPreloadText( $text, $title, $popts );
887 return new WikitextContent( $plt );
891 * Implement redirect extraction for wikitext.
895 * @note: migrated here from Title::newFromRedirectInternal()
897 * @see Content::getRedirectTarget
898 * @see AbstractContent::getRedirectTarget
900 public function getRedirectTarget() {
901 global $wgMaxRedirects;
902 if ( $wgMaxRedirects < 1 ) {
903 // redirects are disabled, so quit early
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!
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], ':' ) );
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() ) {
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.
935 * @param $hasLinks Bool if it is known whether this content contains
936 * links, provide this information here, to avoid redundant parsing to
938 * @param $title null|\Title
940 * @internal param \IContextSource $context context for parsing if necessary
942 * @return bool True if the content is countable
944 public function isCountable( $hasLinks = null, Title
$title = null ) {
945 global $wgArticleCountMethod;
947 if ( $this->isRedirect( ) ) {
951 $text = $this->getNativeData();
953 switch ( $wgArticleCountMethod ) {
957 return strpos( $text, ',' ) !== false;
959 if ( $hasLinks === null ) { # not known, find out
961 $context = RequestContext
::getMain();
962 $title = $context->getTitle();
965 $po = $this->getParserOutput( $title, null, null, false );
966 $links = $po->getLinks();
967 $hasLinks = !empty( $links );
976 public function getTextForSummary( $maxlength = 250 ) {
977 $truncatedtext = parent
::getTextForSummary( $maxlength );
979 # clean up unfinished links
980 # XXX: make this optional? wasn't there in autosummary, but required for
982 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
984 return $truncatedtext;
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
);
998 $this->mMessageKey
= $msg_key;
1000 $this->mParameters
= $params;
1002 if ( is_null( $options ) ) {
1005 elseif ( is_string( $options ) ) {
1006 $options = array( $options );
1009 $this->mOptions
= $options;
1013 * Returns the message as rendered HTML, using the options supplied to the
1014 * constructor plus "parse".
1015 * @param the message text, parsed
1017 public function getHtml( ) {
1018 $opt = array_merge( $this->mOptions
, array( 'parse' ) );
1020 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
1025 * Returns the message as raw text, using the options supplied to the
1026 * constructor minus "parse" and "parseinline".
1028 * @param the message text, unparsed.
1030 public function getNativeData( ) {
1031 $opt = array_diff( $this->mOptions
, array( 'parse', 'parseinline' ) );
1033 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
1041 class JavaScriptContent
extends TextContent
{
1042 public function __construct( $text ) {
1043 parent
::__construct( $text, CONTENT_MODEL_JAVASCRIPT
);
1047 * Returns a Content object with pre-save transformations applied using
1048 * Parser::preSaveTransform().
1050 * @param Title $title
1052 * @param ParserOptions $popts
1055 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
1057 // @todo: make pre-save transformation optional for script pages
1060 $text = $this->getNativeData();
1061 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
1063 return new JavaScriptContent( $pst );
1071 class CssContent
extends TextContent
{
1072 public function __construct( $text ) {
1073 parent
::__construct( $text, CONTENT_MODEL_CSS
);
1077 * Returns a Content object with pre-save transformations applied using
1078 * Parser::preSaveTransform().
1080 * @param $title Title
1082 * @param $popts ParserOptions
1085 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
1087 // @todo: make pre-save transformation optional for script pages
1089 $text = $this->getNativeData();
1090 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
1092 return new CssContent( $pst );