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.
8 abstract class Content
{
10 // TODO: create actual fields and document them
13 * @return String a string representing the content in a way useful for building a full text search index.
14 * If no useful representation exists, this method returns an empty string.
16 public abstract function getTextForSearchIndex( );
19 * @return String the wikitext to include when another page includes this content, or false if the content is not
20 * includable in a wikitext page.
22 #TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
23 public abstract function getWikitextForTransclusion( ); #FIXME: use in parser, etc!
26 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
28 * @param int $maxlength maximum length of the summary text
29 * @return String the summary text
31 public abstract function getTextForSummary( $maxlength = 250 );
34 * Returns native represenation of the data. Interpretation depends on the data model used,
35 * as given by getDataModel().
37 * @return mixed the native representation of the content. Could be a string, a nested array
38 * structure, an object, a binary blob... anything, really.
40 public abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
43 * returns the content's nominal size in bogo-bytes.
47 public abstract function getSize( );
50 * TODO: do we really need to pass a $modelName here?
51 * Seems odd and makes lots of stuff hard (ie having a newEmpty static method in TextContent)
55 public function __construct( $modelName = null ) {
56 $this->mModelName
= $modelName;
60 * Returns the name of the content model used by this content objects.
61 * Corresponds to the CONTENT_MODEL_XXX constants.
63 * @return String the model name
65 public function getModelName() {
66 return $this->mModelName
;
70 * Throws an MWException if $modelName is not the name of the content model
71 * supported by this Content object.
73 protected function checkModelName( $modelName ) {
74 if ( $modelName !== $this->mModelName
) {
75 throw new MWException( "Bad content model: expected " . $this->mModelName
. " but got found " . $modelName );
80 * Conveniance method that returns the ContentHandler singleton for handling the content
81 * model this Content object uses.
83 * Shorthand for ContentHandler::getForContent( $this )
85 * @return ContentHandler
87 public function getContentHandler() {
88 return ContentHandler
::getForContent( $this );
92 * Conveniance method that returns the default serialization format for the content model
93 * model this Content object uses.
95 * Shorthand for $this->getContentHandler()->getDefaultFormat()
97 * @return ContentHandler
99 public function getDefaultFormat() {
100 return $this->getContentHandler()->getDefaultFormat();
104 * Conveniance method that returns the list of serialization formats supported
105 * for the content model model this Content object uses.
107 * Shorthand for $this->getContentHandler()->getSupportedFormats()
109 * @return array of supported serialization formats
111 public function getSupportedFormats() {
112 return $this->getContentHandler()->getSupportedFormats();
116 * Returns true if $format is a supported serialization format for this Content object,
119 * Note that this will always return true if $format is null, because null stands for the
120 * default serialization.
122 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
124 * @param String $format the format to check
125 * @return bool whether the format is supported
127 public function isSupportedFormat( $format ) {
129 return true; // this means "use the default"
132 return $this->getContentHandler()->isSupportedFormat( $format );
136 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
139 * @throws MWException
141 protected function checkFormat( $format ) {
142 if ( !$this->isSupportedFormat( $format ) ) {
143 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
148 * Conveniance method for serializing this Content object.
150 * Shorthand for $this->getContentHandler()->serializeContent( $this, $format )
152 * @param null|String $format the desired serialization format (or null for the default format).
153 * @return String serialized form of this Content object
155 public function serialize( $format = null ) {
156 return $this->getContentHandler()->serializeContent( $this, $format );
160 * Returns true if this Content object represents empty content.
162 * @return bool whether this Content object is empty
164 public function isEmpty() {
165 return $this->getSize() == 0;
169 * Returns true if this Content objects is conceptually equivalent to the given Content object.
171 * Will returns false if $that is null.
172 * Will return true if $that === $this.
173 * Will return false if $that->getModleName() != $this->getModelName().
175 * Implementations should be careful to make equals() transitive and reflexive:
177 * * $a->equals( $b ) <=> $b->equals( $b )
178 * * $a->equals( $b ) && $b->equals( $c ) ==> $a->equals( $c )
180 * @param Content $that the Content object to compare to
181 * @return bool true if this Content object is euzqla to $that, false otherwise.
183 public function equals( Content
$that = null ) {
184 if ( empty( $that ) ){ // FIXME: empty on an object?
189 // FIXME: something is doing wrong here, causing the compared objects to always be the same.
190 // Hence returning false for now, so changes can actually be saved...
192 if ( $that === $this ) {
196 if ( $that->getModelName() !== $this->getModelName() ) {
200 return $this->getNativeData() === $that->getNativeData();
204 * Return a copy of this Content object. The following must be true for the object returned
205 * if $copy = $original->copy()
207 * * get_class($original) === get_class($copy)
208 * * $original->getModelName() === $copy->getModelName()
209 * * $original->equals( $copy )
211 * If and only if the Content object is imutable, the copy() method can and should
212 * return $this. That is, $copy === $original may be true, but only for imutable content
215 * @return a copy of this Content object
217 public abstract function copy( );
220 * Returns true if this content is countable as a "real" wiki page, provided
221 * that it's also in a countable location (e.g. a current revision in the main namespace).
223 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
224 * to avoid redundant parsing to find out.
226 public abstract function isCountable( $hasLinks = null ) ;
229 * @param IContextSource $context
231 * @param null|ParserOptions $options
232 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
233 * the result of calling getText() on the ParserOutput object returned by
234 * this method is undefined.
236 * @return ParserOutput
238 public abstract function getParserOutput( IContextSource
$context, $revId = null, ParserOptions
$options = NULL, $generateHtml = true );
241 * Construct the redirect destination from this content and return an
242 * array of Titles, or null if this content doesn't represent a redirect.
243 * The last element in the array is the final destination after all redirects
244 * have been resolved (up to $wgMaxRedirects times).
246 * @return Array of Titles, with the destination last
248 public function getRedirectChain() {
253 * Construct the redirect destination from this content and return an
254 * array of Titles, or null if this content doesn't represent a redirect.
255 * This will only return the immediate redirect target, useful for
256 * the redirect table and other checks that don't need full recursion.
258 * @return Title: The corresponding Title
260 public function getRedirectTarget() {
265 * Construct the redirect destination from this content and return the
266 * Title, or null if this content doesn't represent a redirect.
267 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
268 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
272 public function getUltimateRedirectTarget() {
276 public function isRedirect() {
277 return $this->getRedirectTarget() != null;
281 * Returns the section with the given id.
283 * The default implementation returns null.
285 * @param String $sectionId the section's id
286 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
288 public function getSection( $sectionId ) {
293 * Replaces a section of the content and returns a Content object with the section replaced.
295 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
296 * @param $with Content: new content of the section
297 * @param $sectionTitle String: new section's subject, only if $section is 'new'
298 * @return string Complete article text, or null if error
300 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
305 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
307 * @param Title $title
309 * @param null|ParserOptions $popts
312 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
317 * Returns a new WikitextContent object with the given section heading prepended, if supported.
318 * The default implementation just returns this Content object unmodified, ignoring the section header.
320 * @param $header String
323 public function addSectionHeader( $header ) {
328 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
330 * @param Title $title
331 * @param null|ParserOptions $popts
334 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
338 # TODO: handle ImagePage and CategoryPage
339 # TODO: hook into dump generation to serialize and record model and format!
341 # TODO: make sure we cover lucene search / wikisearch.
342 # TODO: make sure ReplaceTemplates still works
343 # TODO: nice&sane integration of GeSHi syntax highlighting
344 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
345 # [12:00] <vvv> And default it to a DummyHighlighter
347 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
349 # TODO: tie into API to provide contentModel for Revisions
350 # TODO: tie into API to provide serialized version and contentFormat for Revisions
351 # TODO: tie into API edit interface
352 # TODO: make EditForm plugin for EditPage
354 # XXX: isCacheable( ) # can/should we do this here?
358 * Content object implementation for representing flat text.
360 * TextContent instances are imutable
362 abstract class TextContent
extends Content
{
364 public function __construct( $text, $modelName = null ) {
365 parent
::__construct( $modelName );
367 $this->mText
= $text;
370 public function copy() {
371 return $this; #NOTE: this is ok since TextContent are imutable.
374 public function getTextForSummary( $maxlength = 250 ) {
377 $text = $this->getNativeData();
379 $truncatedtext = $wgContLang->truncate(
380 preg_replace( "/[\n\r]/", ' ', $text ),
381 max( 0, $maxlength ) );
383 return $truncatedtext;
387 * returns the content's nominal size in bogo-bytes.
389 public function getSize( ) {
390 $text = $this->getNativeData( );
391 return strlen( $text );
395 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
397 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
398 * to avoid redundant parsing to find out.
400 public function isCountable( $hasLinks = null ) {
401 global $wgArticleCountMethod;
403 if ( $this->isRedirect( ) ) {
407 if ( $wgArticleCountMethod === 'any' ) {
415 * Returns the text represented by this Content object, as a string.
417 * @return String the raw text
419 public function getNativeData( ) {
420 $text = $this->mText
;
425 * Returns the text represented by this Content object, as a string.
427 * @return String the raw text
429 public function getTextForSearchIndex( ) { #FIXME: use!
430 return $this->getNativeData();
434 * Returns the text represented by this Content object, as a string.
436 * @return String the raw text
438 public function getWikitextForTransclusion( ) { #FIXME: use!
439 return $this->getNativeData();
443 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
445 * @return ParserOutput representing the HTML form of the text
447 public function getParserOutput( IContextSource
$context, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
448 # generic implementation, relying on $this->getHtml()
450 if ( $generateHtml ) $html = $this->getHtml( $options );
453 $po = new ParserOutput( $html );
458 protected abstract function getHtml( );
462 class WikitextContent
extends TextContent
{
464 public function __construct( $text ) {
465 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
467 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
470 protected function getHtml( ) {
471 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
474 public function getDefaultParserOptions() {
475 global $wgUser, $wgContLang;
477 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
478 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
481 return $this->mDefaultParserOptions
;
485 * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
489 * @param IContextSource|null $context
491 * @param null|ParserOptions $options
492 * @param bool $generateHtml
494 * @return ParserOutput representing the HTML form of the text
496 public function getParserOutput( IContextSource
$context, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
500 $options = $this->getDefaultParserOptions();
503 $po = $wgParser->parse( $this->mText
, $context->getTitle(), $options, true, true, $revId );
509 * Returns the section with the given id.
511 * @param String $sectionId the section's id
512 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
514 public function getSection( $section ) {
517 $text = $this->getNativeData();
518 $sect = $wgParser->getSection( $text, $section, false );
520 return new WikitextContent( $sect );
524 * Replaces a section in the wikitext
526 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
527 * @param $with Content: new content of the section
528 * @param $sectionTitle String: new section's subject, only if $section is 'new'
529 * @return string Complete article text, or null if error
531 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
532 wfProfileIn( __METHOD__
);
534 $myModelName = $this->getModelName();
535 $sectionModelName = $with->getModelName();
537 if ( $sectionModelName != $myModelName ) {
538 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
541 $oldtext = $this->getNativeData();
542 $text = $with->getNativeData();
544 if ( $section == 'new' ) {
545 # Inserting a new section
546 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
547 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
548 $text = strlen( trim( $oldtext ) ) > 0
549 ?
"{$oldtext}\n\n{$subject}{$text}"
550 : "{$subject}{$text}";
553 # Replacing an existing section; roll out the big guns
556 $text = $wgParser->replaceSection( $oldtext, $section, $text );
559 $newContent = new WikitextContent( $text );
561 wfProfileOut( __METHOD__
);
566 * Returns a new WikitextContent object with the given section heading prepended.
568 * @param $header String
571 public function addSectionHeader( $header ) {
572 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle
) . "\n\n" . $this->getNativeData();
574 return new WikitextContent( $text );
578 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
580 * @param Title $title
582 * @param null|ParserOptions $popts
585 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
588 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
590 $text = $this->getNativeData();
591 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
593 return new WikitextContent( $pst );
597 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
599 * @param Title $title
600 * @param null|ParserOptions $popts
603 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
606 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
608 $text = $this->getNativeData();
609 $plt = $wgParser->getPreloadText( $text, $title, $popts );
611 return new WikitextContent( $plt );
614 public function getRedirectChain() {
615 $text = $this->getNativeData();
616 return Title
::newFromRedirectArray( $text );
619 public function getRedirectTarget() {
620 $text = $this->getNativeData();
621 return Title
::newFromRedirect( $text );
624 public function getUltimateRedirectTarget() {
625 $text = $this->getNativeData();
626 return Title
::newFromRedirectRecurse( $text );
630 * Returns true if this content is not a redirect, and this content's text is countable according to
631 * the criteria defiend by $wgArticleCountMethod.
633 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
634 * to avoid redundant parsing to find out.
636 public function isCountable( $hasLinks = null ) {
637 global $wgArticleCountMethod;
639 if ( $this->isRedirect( ) ) {
643 $text = $this->getNativeData();
645 switch ( $wgArticleCountMethod ) {
649 if ( $text === false ) {
650 $text = $this->getRawText();
652 return strpos( $text, ',' ) !== false;
654 if ( $hasLinks === null ) { # not know, find out
655 $po = $this->getParserOutput();
656 $links = $po->getLinks();
657 $hasLinks = !empty( $links );
664 public function getTextForSummary( $maxlength = 250 ) {
665 $truncatedtext = parent
::getTextForSummary( $maxlength );
667 #clean up unfinished links
668 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
669 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
671 return $truncatedtext;
676 class MessageContent
extends TextContent
{
677 public function __construct( $msg_key, $params = null, $options = null ) {
678 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
680 $this->mMessageKey
= $msg_key;
682 $this->mParameters
= $params;
684 if ( is_null( $options ) ) {
687 elseif ( is_string( $options ) ) {
688 $options = array( $options );
691 $this->mOptions
= $options;
693 $this->mHtmlOptions
= null;
697 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
699 protected function getHtml( ) {
700 $opt = array_merge( $this->mOptions
, array('parse') );
702 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
707 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
709 public function getNativeData( ) {
710 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
712 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
718 class JavaScriptContent
extends TextContent
{
719 public function __construct( $text ) {
720 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
723 protected function getHtml( ) {
725 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
726 $html .= htmlspecialchars( $this->getNativeData() );
727 $html .= "\n</pre>\n";
734 class CssContent
extends TextContent
{
735 public function __construct( $text ) {
736 parent
::__construct($text, CONTENT_MODEL_CSS
);
739 protected function getHtml( ) {
741 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
742 $html .= htmlspecialchars( $this->getNativeData() );
743 $html .= "\n</pre>\n";
749 #FUTURE: special type for redirects?!
750 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
751 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
752 #EXAMPLE: CoordinatesContent
753 #EXAMPLE: WikidataContent