4 * A content object represents page content, e.g. the text to show on a page.
5 * Content objects have no knowledge about how they relate to Wiki pages.
9 abstract class Content
{
12 * Name of the content model this Content object represents.
13 * Use with CONTENT_MODEL_XXX constants
15 * @var String $model_id
22 * @return String a string representing the content in a way useful for building a full text search index.
23 * If no useful representation exists, this method returns an empty string.
25 public abstract function getTextForSearchIndex( );
30 * @return String the wikitext to include when another page includes this content, or false if the content is not
31 * includable in a wikitext page.
33 * @TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
34 * @TODO: use in parser, etc!
36 public abstract function getWikitextForTransclusion( );
39 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
43 * @param int $maxlength maximum length of the summary text
44 * @return String the summary text
46 public abstract function getTextForSummary( $maxlength = 250 );
49 * Returns native represenation of the data. Interpretation depends on the data model used,
50 * as given by getDataModel().
54 * @return mixed the native representation of the content. Could be a string, a nested array
55 * structure, an object, a binary blob... anything, really.
57 * @NOTE: review all calls carefully, caller must be aware of content model!
59 public abstract function getNativeData( );
62 * returns the content's nominal size in bogo-bytes.
66 public abstract function getSize( );
69 * @param int $model_id
71 public function __construct( $model_id = null ) {
72 $this->model_id
= $model_id;
76 * Returns the id of the content model used by this content objects.
77 * Corresponds to the CONTENT_MODEL_XXX constants.
81 * @return int the model id
83 public function getModel() {
84 return $this->model_id
;
88 * Throws an MWException if $model_id is not the id of the content model
89 * supported by this Content object.
91 * @param int $model_id the model to check
93 protected function checkModelID( $model_id ) {
94 if ( $model_id !== $this->model_id
) {
95 $model_name = ContentHandler
::getContentModelName( $model_id );
96 $own_model_name = ContentHandler
::getContentModelName( $this->model_id
);
98 throw new MWException( "Bad content model: expected {$this->model_id} ($own_model_name) but got found $model_id ($model_name)." );
103 * Conveniance method that returns the ContentHandler singleton for handling the content
104 * model this Content object uses.
106 * Shorthand for ContentHandler::getForContent( $this )
110 * @return ContentHandler
112 public function getContentHandler() {
113 return ContentHandler
::getForContent( $this );
117 * Conveniance method that returns the default serialization format for the content model
118 * model this Content object uses.
120 * Shorthand for $this->getContentHandler()->getDefaultFormat()
124 * @return ContentHandler
126 public function getDefaultFormat() {
127 return $this->getContentHandler()->getDefaultFormat();
131 * Conveniance method that returns the list of serialization formats supported
132 * for the content model model this Content object uses.
134 * Shorthand for $this->getContentHandler()->getSupportedFormats()
138 * @return array of supported serialization formats
140 public function getSupportedFormats() {
141 return $this->getContentHandler()->getSupportedFormats();
145 * Returns true if $format is a supported serialization format for this Content object,
148 * Note that this will always return true if $format is null, because null stands for the
149 * default serialization.
151 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
155 * @param String $format the format to check
156 * @return bool whether the format is supported
158 public function isSupportedFormat( $format ) {
160 return true; // this means "use the default"
163 return $this->getContentHandler()->isSupportedFormat( $format );
167 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
170 * @throws MWException
172 protected function checkFormat( $format ) {
173 if ( !$this->isSupportedFormat( $format ) ) {
174 throw new MWException( "Format $format is not supported for content model " . $this->getModel() );
179 * Conveniance method for serializing this Content object.
181 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
185 * @param null|String $format the desired serialization format (or null for the default format).
186 * @return String serialized form of this Content object
188 public function serialize( $format = null ) {
189 return $this->getContentHandler()->serializeContent( $this, $format );
193 * Returns true if this Content object represents empty content.
197 * @return bool whether this Content object is empty
199 public function isEmpty() {
200 return $this->getSize() == 0;
204 * Returns if the content is valid.
205 * It needs to be valid before it can be saved.
211 public function isValid() {
217 * Diff the content object with what is currently stored in the database.
218 * If it is not currently stored, it will be diffed with an empty object.
222 * @return ContentDiff
224 public function diffToDatabase() {
229 * Returns true if this Content objects is conceptually equivalent to the given Content object.
231 * Will returns false if $that is null.
232 * Will return true if $that === $this.
233 * Will return false if $that->getModleName() != $this->getModel().
234 * Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
235 * where the meaning of "equal" depends on the actual data model.
237 * Implementations should be careful to make equals() transitive and reflexive:
239 * * $a->equals( $b ) <=> $b->equals( $b )
240 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
244 * @param Content $that the Content object to compare to
245 * @return bool true if this Content object is euqual to $that, false otherwise.
247 public function equals( Content
$that = null ) {
248 if ( is_null( $that ) ){
252 if ( $that === $this ) {
256 if ( $that->getModel() !== $this->getModel() ) {
260 return $this->getNativeData() === $that->getNativeData();
264 * Return a copy of this Content object. The following must be true for the object returned
265 * if $copy = $original->copy()
267 * * get_class($original) === get_class($copy)
268 * * $original->getModel() === $copy->getModel()
269 * * $original->equals( $copy )
271 * If and only if the Content object is imutable, the copy() method can and should
272 * return $this. That is, $copy === $original may be true, but only for imutable content
277 * @return Content. A copy of this object
279 public abstract function copy( );
282 * Returns true if this content is countable as a "real" wiki page, provided
283 * that it's also in a countable location (e.g. a current revision in the main namespace).
287 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
288 * to avoid redundant parsing to find out.
291 public abstract function isCountable( $hasLinks = null ) ;
294 * @param Title $title
296 * @param null|ParserOptions $options
297 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
298 * the result of calling getText() on the ParserOutput object returned by
299 * this method is undefined.
303 * @return ParserOutput
305 public abstract function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true );
308 * Returns a list of DataUpdate objects for recording information about this Content in some secondary
309 * data store. If the optional second argument, $old, is given, the updates may model only the changes that
310 * need to be made to replace information about the old content with infomration about the new content.
312 * This default implementation calls $this->getParserOutput( $title, null, null, false ), and then
313 * calls getSecondaryDataUpdates( $title, $recursive ) on the resulting ParserOutput object.
315 * Subclasses may implement this to determine the necessary updates more efficiently, or make use of information
316 * about the old content.
318 * @param Title $title the context for determining the necessary updates
319 * @param Content|null $old a Content object representing the previous content, i.e. the content being
320 * replaced by this Content object.
321 * @param bool $recursive whether to include recursive updates (default: false).
323 * @return Array. A list of DataUpdate objects for putting information about this content object somewhere.
327 public function getSecondaryDataUpdates( Title
$title, Content
$old = null, $recursive = false ) {
328 $po = $this->getParserOutput( $title, null, null, false );
329 return $po->getSecondaryDataUpdates( $title, $recursive );
333 * Construct the redirect destination from this content and return an
334 * array of Titles, or null if this content doesn't represent a redirect.
335 * The last element in the array is the final destination after all redirects
336 * have been resolved (up to $wgMaxRedirects times).
340 * @return Array of Titles, with the destination last
342 public function getRedirectChain() {
347 * Construct the redirect destination from this content and return an
348 * array of Titles, or null if this content doesn't represent a redirect.
349 * This will only return the immediate redirect target, useful for
350 * the redirect table and other checks that don't need full recursion.
354 * @return Title: The corresponding Title
356 public function getRedirectTarget() {
361 * Construct the redirect destination from this content and return the
362 * Title, or null if this content doesn't represent a redirect.
363 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
364 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
370 public function getUltimateRedirectTarget() {
379 public function isRedirect() {
380 return $this->getRedirectTarget() !== null;
384 * Returns the section with the given id.
386 * The default implementation returns null.
390 * @param String $sectionId the section's id, given as a numeric string. The id "0" retrieves the section before
391 * the first heading, "1" the text between the first heading (inluded) and the second heading (excluded), etc.
392 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
394 public function getSection( $sectionId ) {
399 * Replaces a section of the content and returns a Content object with the section replaced.
403 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
404 * @param $with Content: new content of the section
405 * @param $sectionTitle String: new section's subject, only if $section is 'new'
406 * @return string Complete article text, or null if error
408 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
413 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
417 * @param Title $title
419 * @param null|ParserOptions $popts
422 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
427 * Returns a new WikitextContent object with the given section heading prepended, if supported.
428 * The default implementation just returns this Content object unmodified, ignoring the section header.
432 * @param $header String
435 public function addSectionHeader( $header ) {
440 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
444 * @param Title $title
445 * @param null|ParserOptions $popts
448 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
452 # TODO: handle ImagePage and CategoryPage
453 # TODO: make sure we cover lucene search / wikisearch.
454 # TODO: make sure ReplaceTemplates still works
455 # FUTURE: nice&sane integration of GeSHi syntax highlighting
456 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
457 # [12:00] <vvv> And default it to a DummyHighlighter
459 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
461 # TODO: tie into API to provide contentModel for Revisions
462 # TODO: tie into API to provide serialized version and contentFormat for Revisions
463 # TODO: tie into API edit interface
464 # FUTURE: make EditForm plugin for EditPage
466 # FUTURE: special type for redirects?!
467 # FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
468 # FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
471 * Content object implementation for representing flat text.
473 * TextContent instances are imutable
477 abstract class TextContent
extends Content
{
479 public function __construct( $text, $model_id = null ) {
480 parent
::__construct( $model_id );
482 $this->mText
= $text;
485 public function copy() {
486 return $this; #NOTE: this is ok since TextContent are imutable.
489 public function getTextForSummary( $maxlength = 250 ) {
492 $text = $this->getNativeData();
494 $truncatedtext = $wgContLang->truncate(
495 preg_replace( "/[\n\r]/", ' ', $text ),
496 max( 0, $maxlength ) );
498 return $truncatedtext;
502 * returns the text's size in bytes.
504 * @return int the size
506 public function getSize( ) {
507 $text = $this->getNativeData( );
508 return strlen( $text );
512 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
514 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
515 * to avoid redundant parsing to find out.
517 * @return bool true if the content is countable
519 public function isCountable( $hasLinks = null ) {
520 global $wgArticleCountMethod;
522 if ( $this->isRedirect( ) ) {
526 if ( $wgArticleCountMethod === 'any' ) {
534 * Returns the text represented by this Content object, as a string.
536 * @return String the raw text
538 public function getNativeData( ) {
539 $text = $this->mText
;
544 * Returns the text represented by this Content object, as a string.
546 * @return String the raw text
548 public function getTextForSearchIndex( ) {
549 return $this->getNativeData();
553 * Returns the text represented by this Content object, as a string.
555 * @return String the raw text
557 public function getWikitextForTransclusion( ) {
558 return $this->getNativeData();
562 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
564 * @return ParserOutput representing the HTML form of the text
566 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
567 # generic implementation, relying on $this->getHtml()
569 if ( $generateHtml ) $html = $this->getHtml( $options );
572 $po = new ParserOutput( $html );
577 protected abstract function getHtml( );
584 class WikitextContent
extends TextContent
{
586 public function __construct( $text ) {
587 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
590 protected function getHtml( ) {
591 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
595 * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
599 * @param IContextSource|null $context
601 * @param null|ParserOptions $options
602 * @param bool $generateHtml
604 * @return ParserOutput representing the HTML form of the text
606 public function getParserOutput( Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
610 $options = new ParserOptions();
613 $po = $wgParser->parse( $this->mText
, $title, $options, true, true, $revId );
619 * Returns the section with the given id.
621 * @param String $sectionId the section's id
622 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
624 public function getSection( $section ) {
627 $text = $this->getNativeData();
628 $sect = $wgParser->getSection( $text, $section, false );
630 return new WikitextContent( $sect );
634 * Replaces a section in the wikitext
636 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
637 * @param $with Content: new content of the section
638 * @param $sectionTitle String: new section's subject, only if $section is 'new'
639 * @return Content Complete article content, or null if error
641 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
642 wfProfileIn( __METHOD__
);
644 $myModelId = $this->getModel();
645 $sectionModelId = $with->getModel();
647 if ( $sectionModelId != $myModelId ) {
648 $myModelName = ContentHandler
::getContentModelName( $myModelId );
649 $sectionModelName = ContentHandler
::getContentModelName( $sectionModelId );
651 throw new MWException( "Incompatible content model for section: document uses $myModelId ($myModelName), "
652 . "section uses $sectionModelId ($sectionModelName)." );
655 $oldtext = $this->getNativeData();
656 $text = $with->getNativeData();
658 if ( $section === '' ) {
659 return $with; #XXX: copy first?
660 } if ( $section == 'new' ) {
661 # Inserting a new section
662 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
663 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
664 $text = strlen( trim( $oldtext ) ) > 0
665 ?
"{$oldtext}\n\n{$subject}{$text}"
666 : "{$subject}{$text}";
669 # Replacing an existing section; roll out the big guns
672 $text = $wgParser->replaceSection( $oldtext, $section, $text );
675 $newContent = new WikitextContent( $text );
677 wfProfileOut( __METHOD__
);
682 * Returns a new WikitextContent object with the given section heading prepended.
684 * @param $header String
687 public function addSectionHeader( $header ) {
688 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $header ) . "\n\n" . $this->getNativeData();
690 return new WikitextContent( $text );
694 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
696 * @param Title $title
698 * @param ParserOptions $popts
701 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts ) {
702 global $wgParser, $wgConteLang;
704 $text = $this->getNativeData();
705 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
707 return new WikitextContent( $pst );
711 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
713 * @param Title $title
714 * @param ParserOptions $popts
717 public function preloadTransform( Title
$title, ParserOptions
$popts ) {
718 global $wgParser, $wgConteLang;
720 $text = $this->getNativeData();
721 $plt = $wgParser->getPreloadText( $text, $title, $popts );
723 return new WikitextContent( $plt );
726 public function getRedirectChain() {
727 $text = $this->getNativeData();
728 return Title
::newFromRedirectArray( $text );
731 public function getRedirectTarget() {
732 $text = $this->getNativeData();
733 return Title
::newFromRedirect( $text );
736 public function getUltimateRedirectTarget() {
737 $text = $this->getNativeData();
738 return Title
::newFromRedirectRecurse( $text );
742 * Returns true if this content is not a redirect, and this content's text is countable according to
743 * the criteria defiend by $wgArticleCountMethod.
745 * @param Bool $hasLinks if it is known whether this content contains links, provide this information here,
746 * to avoid redundant parsing to find out.
747 * @param IContextSource $context context for parsing if necessary
749 * @return bool true if the content is countable
751 public function isCountable( $hasLinks = null, Title
$title = null ) {
752 global $wgArticleCountMethod, $wgRequest;
754 if ( $this->isRedirect( ) ) {
758 $text = $this->getNativeData();
760 switch ( $wgArticleCountMethod ) {
764 return strpos( $text, ',' ) !== false;
766 if ( $hasLinks === null ) { # not known, find out
768 $context = RequestContext
::getMain();
769 $title = $context->getTitle();
772 $po = $this->getParserOutput( $title, null, null, false );
773 $links = $po->getLinks();
774 $hasLinks = !empty( $links );
781 public function getTextForSummary( $maxlength = 250 ) {
782 $truncatedtext = parent
::getTextForSummary( $maxlength );
784 #clean up unfinished links
785 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
786 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
788 return $truncatedtext;
796 class MessageContent
extends TextContent
{
797 public function __construct( $msg_key, $params = null, $options = null ) {
798 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
800 $this->mMessageKey
= $msg_key;
802 $this->mParameters
= $params;
804 if ( is_null( $options ) ) {
807 elseif ( is_string( $options ) ) {
808 $options = array( $options );
811 $this->mOptions
= $options;
813 $this->mHtmlOptions
= null;
817 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
819 protected function getHtml( ) {
820 $opt = array_merge( $this->mOptions
, array('parse') );
822 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
827 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
829 public function getNativeData( ) {
830 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
832 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
840 class JavaScriptContent
extends TextContent
{
841 public function __construct( $text ) {
842 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
845 protected function getHtml( ) {
847 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
848 $html .= htmlspecialchars( $this->getNativeData() );
849 $html .= "\n</pre>\n";
859 class CssContent
extends TextContent
{
860 public function __construct( $text ) {
861 parent
::__construct($text, CONTENT_MODEL_CSS
);
864 protected function getHtml( ) {
866 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
867 $html .= htmlspecialchars( $this->getNativeData() );
868 $html .= "\n</pre>\n";