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.
8 abstract class Content
{
11 * Name of the content model this Content object represents.
12 * Use with CONTENT_MODEL_XXX constants
14 * @var String $model_id
21 * @return String a string representing the content in a way useful for building a full text search index.
22 * If no useful representation exists, this method returns an empty string.
24 * @todo: test that this actually works
25 * @todo: make sure this also works with LuceneSearch / WikiSearch
27 public abstract function getTextForSearchIndex( );
32 * @return String the wikitext to include when another page includes this content, or false if the content is not
33 * includable in a wikitext page.
35 * @TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
36 * @TODO: use in parser, etc!
38 public abstract function getWikitextForTransclusion( );
41 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
45 * @param int $maxlength maximum length of the summary text
46 * @return String the summary text
48 public abstract function getTextForSummary( $maxlength = 250 );
51 * Returns native representation of the data. Interpretation depends on the data model used,
52 * as given by getDataModel().
56 * @return mixed the native representation of the content. Could be a string, a nested array
57 * structure, an object, a binary blob... anything, really.
59 * @NOTE: review all calls carefully, caller must be aware of content model!
61 public abstract function getNativeData( );
64 * returns the content's nominal size in bogo-bytes.
68 public abstract function getSize( );
71 * @param int $model_id
73 public function __construct( $model_id = null ) {
74 $this->model_id
= $model_id;
78 * Returns the id of the content model used by this content objects.
79 * Corresponds to the CONTENT_MODEL_XXX constants.
83 * @return int the model id
85 public function getModel() {
86 return $this->model_id
;
90 * Throws an MWException if $model_id is not the id of the content model
91 * supported by this Content object.
93 * @param int $model_id the model to check
97 protected function checkModelID( $model_id ) {
98 if ( $model_id !== $this->model_id
) {
99 $model_name = ContentHandler
::getContentModelName( $model_id );
100 $own_model_name = ContentHandler
::getContentModelName( $this->model_id
);
102 throw new MWException( "Bad content model: expected {$this->model_id} ($own_model_name) but got found $model_id ($model_name)." );
107 * Convenience method that returns the ContentHandler singleton for handling the content
108 * model this Content object uses.
110 * Shorthand for ContentHandler::getForContent( $this )
114 * @return ContentHandler
116 public function getContentHandler() {
117 return ContentHandler
::getForContent( $this );
121 * Convenience method that returns the default serialization format for the content model
122 * model this Content object uses.
124 * Shorthand for $this->getContentHandler()->getDefaultFormat()
128 * @return ContentHandler
130 public function getDefaultFormat() {
131 return $this->getContentHandler()->getDefaultFormat();
135 * Convenience method that returns the list of serialization formats supported
136 * for the content model model this Content object uses.
138 * Shorthand for $this->getContentHandler()->getSupportedFormats()
142 * @return array of supported serialization formats
144 public function getSupportedFormats() {
145 return $this->getContentHandler()->getSupportedFormats();
149 * Returns true if $format is a supported serialization format for this Content object,
152 * Note that this will always return true if $format is null, because null stands for the
153 * default serialization.
155 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
159 * @param String $format the format to check
160 * @return bool whether the format is supported
162 public function isSupportedFormat( $format ) {
164 return true; // this means "use the default"
167 return $this->getContentHandler()->isSupportedFormat( $format );
171 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
174 * @throws MWException
176 protected function checkFormat( $format ) {
177 if ( !$this->isSupportedFormat( $format ) ) {
178 throw new MWException( "Format $format is not supported for content model " . $this->getModel() );
183 * Convenience method for serializing this Content object.
185 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
189 * @param null|String $format the desired serialization format (or null for the default format).
190 * @return String serialized form of this Content object
192 public function serialize( $format = null ) {
193 return $this->getContentHandler()->serializeContent( $this, $format );
197 * Returns true if this Content object represents empty content.
201 * @return bool whether this Content object is empty
203 public function isEmpty() {
204 return $this->getSize() == 0;
208 * Returns if the content is valid. This is intended for local validity checks, not considering global consistency.
209 * It needs to be valid before it can be saved.
211 * This default implementation always returns true.
217 public function isValid() {
222 * Returns true if this Content objects is conceptually equivalent to the given Content object.
224 * Will returns false if $that is null.
225 * Will return true if $that === $this.
226 * Will return false if $that->getModelName() != $this->getModel().
227 * Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
228 * where the meaning of "equal" depends on the actual data model.
230 * Implementations should be careful to make equals() transitive and reflexive:
232 * * $a->equals( $b ) <=> $b->equals( $a )
233 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
237 * @param Content $that the Content object to compare to
238 * @return bool true if this Content object is euqual to $that, false otherwise.
240 public function equals( Content
$that = null ) {
241 if ( is_null( $that ) ){
245 if ( $that === $this ) {
249 if ( $that->getModel() !== $this->getModel() ) {
253 return $this->getNativeData() === $that->getNativeData();
257 * Return a copy of this Content object. The following must be true for the object returned
258 * if $copy = $original->copy()
260 * * get_class($original) === get_class($copy)
261 * * $original->getModel() === $copy->getModel()
262 * * $original->equals( $copy )
264 * If and only if the Content object is immutable, the copy() method can and should
265 * return $this. That is, $copy === $original may be true, but only for immutable content
270 * @return Content. A copy of this object
272 public abstract function copy( );
275 * Returns true if this content is countable as a "real" wiki page, provided
276 * that it's also in a countable location (e.g. a current revision in the main namespace).
280 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
281 * to avoid redundant parsing to find out.
284 public abstract function isCountable( $hasLinks = null ) ;
287 * Convenience method, shorthand for
288 * $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml )
290 * @param Title $title
292 * @param null|ParserOptions $options
293 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
294 * the result of calling getText() on the ParserOutput object returned by
295 * this method is undefined.
299 * @return ParserOutput
301 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
302 return $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml );
306 * Convenience method, shorthand for
307 * $this->getContentHandler()->getSecondaryDataUpdates( $this, $title, $old, $recursive )
309 * @param Title $title the context for determining the necessary updates
310 * @param Content|null $old a Content object representing the previous content, i.e. the content being
311 * replaced by this Content object.
312 * @param bool $recursive whether to include recursive updates (default: false).
314 * @return Array. A list of DataUpdate objects for putting information about this content object somewhere.
318 public function getSecondaryDataUpdates( Title
$title, Content
$old = null, $recursive = false ) { #TODO: remove!
319 return $this->getContentHandler()->getSecondaryDataUpdates( $this, $title, $old, $recursive );
323 * Construct the redirect destination from this content and return an
324 * array of Titles, or null if this content doesn't represent a redirect.
325 * The last element in the array is the final destination after all redirects
326 * have been resolved (up to $wgMaxRedirects times).
330 * @return Array of Titles, with the destination last
332 public function getRedirectChain() {
337 * Construct the redirect destination from this content and return a Title,
338 * or null if this content doesn't represent a redirect.
339 * This will only return the immediate redirect target, useful for
340 * the redirect table and other checks that don't need full recursion.
344 * @return Title: The corresponding Title
346 public function getRedirectTarget() {
351 * Construct the redirect destination from this content and return the
352 * Title, or null if this content doesn't represent a redirect.
353 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
354 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
360 public function getUltimateRedirectTarget() {
369 public function isRedirect() {
370 return $this->getRedirectTarget() !== null;
374 * Returns the section with the given id.
376 * The default implementation returns null.
380 * @param String $sectionId the section's id, given as a numeric string. The id "0" retrieves the section before
381 * the first heading, "1" the text between the first heading (included) and the second heading (excluded), etc.
382 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
384 public function getSection( $sectionId ) {
389 * Replaces a section of the content and returns a Content object with the section replaced.
393 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
394 * @param $with Content: new content of the section
395 * @param $sectionTitle String: new section's subject, only if $section is 'new'
396 * @return string Complete article text, or null if error
398 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
403 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
407 * @param Title $title
409 * @param null|ParserOptions $popts
412 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
417 * Returns a new WikitextContent object with the given section heading prepended, if supported.
418 * The default implementation just returns this Content object unmodified, ignoring the section header.
422 * @param $header String
425 public function addSectionHeader( $header ) {
430 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
434 * @param Title $title
435 * @param null|ParserOptions $popts
438 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
442 # TODO: handle ImagePage and CategoryPage
443 # TODO: make sure we cover lucene search / wikisearch.
444 # TODO: make sure ReplaceTemplates still works
445 # FUTURE: nice&sane integration of GeSHi syntax highlighting
446 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
447 # [12:00] <vvv> And default it to a DummyHighlighter
449 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
451 # TODO: tie into API to provide contentModel for Revisions
452 # TODO: tie into API to provide serialized version and contentFormat for Revisions
453 # TODO: tie into API edit interface
454 # FUTURE: make EditForm plugin for EditPage
456 # FUTURE: special type for redirects?!
457 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
458 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
461 * Content object implementation for representing flat text.
463 * TextContent instances are immutable
467 abstract class TextContent
extends Content
{
469 public function __construct( $text, $model_id = null ) {
470 parent
::__construct( $model_id );
472 $this->mText
= $text;
475 public function copy() {
476 return $this; #NOTE: this is ok since TextContent are immutable.
479 public function getTextForSummary( $maxlength = 250 ) {
482 $text = $this->getNativeData();
484 $truncatedtext = $wgContLang->truncate(
485 preg_replace( "/[\n\r]/", ' ', $text ),
486 max( 0, $maxlength ) );
488 return $truncatedtext;
492 * returns the text's size in bytes.
494 * @return int the size
496 public function getSize( ) {
497 $text = $this->getNativeData( );
498 return strlen( $text );
502 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
504 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
505 * to avoid redundant parsing to find out.
507 * @return bool true if the content is countable
509 public function isCountable( $hasLinks = null ) {
510 global $wgArticleCountMethod;
512 if ( $this->isRedirect( ) ) {
516 if ( $wgArticleCountMethod === 'any' ) {
524 * Returns the text represented by this Content object, as a string.
526 * @return String the raw text
528 public function getNativeData( ) {
529 $text = $this->mText
;
534 * Returns the text represented by this Content object, as a string.
536 * @return String the raw text
538 public function getTextForSearchIndex( ) {
539 return $this->getNativeData();
543 * Returns the text represented by this Content object, as a string.
545 * @return String the raw text
547 public function getWikitextForTransclusion( ) {
548 return $this->getNativeData();
552 * Diff this content object with another content object..
556 * @param Content $that the other content object to compare this content object to
557 * @param Language $lang the language object to use for text segmentation. If not given, $wgContentLang is used.
559 * @return DiffResult a diff representing the changes that would have to be made to this content object
560 * to make it equal to $that.
562 public function diff( Content
$that, Language
$lang = null ) {
565 $this->checkModelID( $that->getModel() );
567 #@todo: could implement this in DifferenceEngine and just delegate here?
569 if ( !$lang ) $lang = $wgContLang;
571 $otext = $this->getNativeData();
572 $ntext = $this->getNativeData();
574 # Note: Use native PHP diff, external engines don't give us abstract output
575 $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
576 $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
578 $diff = new Diff( $ota, $nta );
588 class WikitextContent
extends TextContent
{
590 public function __construct( $text ) {
591 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
595 * Returns the section with the given id.
597 * @param String $section
599 * @internal param String $sectionId the section's id
600 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
602 public function getSection( $section ) {
605 $text = $this->getNativeData();
606 $sect = $wgParser->getSection( $text, $section, false );
608 return new WikitextContent( $sect );
612 * Replaces a section in the wikitext
614 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
615 * @param $with Content: new content of the section
616 * @param $sectionTitle String: new section's subject, only if $section is 'new'
618 * @throws MWException
619 * @return Content Complete article content, or null if error
621 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
622 wfProfileIn( __METHOD__
);
624 $myModelId = $this->getModel();
625 $sectionModelId = $with->getModel();
627 if ( $sectionModelId != $myModelId ) {
628 $myModelName = ContentHandler
::getContentModelName( $myModelId );
629 $sectionModelName = ContentHandler
::getContentModelName( $sectionModelId );
631 throw new MWException( "Incompatible content model for section: document uses $myModelId ($myModelName), "
632 . "section uses $sectionModelId ($sectionModelName)." );
635 $oldtext = $this->getNativeData();
636 $text = $with->getNativeData();
638 if ( $section === '' ) {
639 return $with; #XXX: copy first?
640 } if ( $section == 'new' ) {
641 # Inserting a new section
642 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
643 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
644 $text = strlen( trim( $oldtext ) ) > 0
645 ?
"{$oldtext}\n\n{$subject}{$text}"
646 : "{$subject}{$text}";
649 # Replacing an existing section; roll out the big guns
652 $text = $wgParser->replaceSection( $oldtext, $section, $text );
655 $newContent = new WikitextContent( $text );
657 wfProfileOut( __METHOD__
);
662 * Returns a new WikitextContent object with the given section heading prepended.
664 * @param $header String
667 public function addSectionHeader( $header ) {
668 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . $this->getNativeData();
670 return new WikitextContent( $text );
674 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
676 * @param Title $title
678 * @param ParserOptions $popts
681 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) { #FIXME: also needed for JS/CSS!
682 global $wgParser, $wgConteLang;
684 $text = $this->getNativeData();
685 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
687 return new WikitextContent( $pst );
691 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
693 * @param Title $title
694 * @param ParserOptions $popts
697 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
698 global $wgParser, $wgConteLang;
700 $text = $this->getNativeData();
701 $plt = $wgParser->getPreloadText( $text, $title, $popts );
703 return new WikitextContent( $plt );
706 public function getRedirectChain() {
707 $text = $this->getNativeData();
708 return Title
::newFromRedirectArray( $text );
711 public function getRedirectTarget() {
712 $text = $this->getNativeData();
713 return Title
::newFromRedirect( $text );
716 public function getUltimateRedirectTarget() {
717 $text = $this->getNativeData();
718 return Title
::newFromRedirectRecurse( $text );
722 * Returns true if this content is not a redirect, and this content's text is countable according to
723 * the criteria defined by $wgArticleCountMethod.
725 * @param Bool $hasLinks if it is known whether this content contains links, provide this information here,
726 * to avoid redundant parsing to find out.
727 * @param null|\Title $title
729 * @internal param \IContextSource $context context for parsing if necessary
731 * @return bool true if the content is countable
733 public function isCountable( $hasLinks = null, Title
$title = null ) {
734 global $wgArticleCountMethod, $wgRequest;
736 if ( $this->isRedirect( ) ) {
740 $text = $this->getNativeData();
742 switch ( $wgArticleCountMethod ) {
746 return strpos( $text, ',' ) !== false;
748 if ( $hasLinks === null ) { # not known, find out
750 $context = RequestContext
::getMain();
751 $title = $context->getTitle();
754 $po = $this->getParserOutput( $title, null, null, false );
755 $links = $po->getLinks();
756 $hasLinks = !empty( $links );
765 public function getTextForSummary( $maxlength = 250 ) {
766 $truncatedtext = parent
::getTextForSummary( $maxlength );
768 #clean up unfinished links
769 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
770 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
772 return $truncatedtext;
780 class MessageContent
extends TextContent
{
781 public function __construct( $msg_key, $params = null, $options = null ) {
782 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
784 $this->mMessageKey
= $msg_key;
786 $this->mParameters
= $params;
788 if ( is_null( $options ) ) {
791 elseif ( is_string( $options ) ) {
792 $options = array( $options );
795 $this->mOptions
= $options;
799 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
800 * @return String the message text, parsed
802 public function getHtml( ) {
803 $opt = array_merge( $this->mOptions
, array('parse') );
805 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
810 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
812 * @return String the message text, unparsed.
814 public function getNativeData( ) {
815 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
817 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
825 class JavaScriptContent
extends TextContent
{
826 public function __construct( $text ) {
827 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
835 class CssContent
extends TextContent
{
836 public function __construct( $text ) {
837 parent
::__construct($text, CONTENT_MODEL_CSS
);