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.
6 * Content objects are imutable.
9 abstract class Content
{
11 // TODO: create actual fields and document them
14 * @return String a string representing the content in a way useful for building a full text search index.
15 * If no useful representation exists, this method returns an empty string.
17 public abstract function getTextForSearchIndex( );
20 * @return String the wikitext to include when another page includes this content, or false if the content is not
21 * includable in a wikitext page.
23 #TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
24 public abstract function getWikitextForTransclusion( ); #FIXME: use in parser, etc!
27 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
29 * @param int $maxlength maximum length of the summary text
30 * @return String the summary text
32 public abstract function getTextForSummary( $maxlength = 250 );
35 * Returns native represenation of the data. Interpretation depends on the data model used,
36 * as given by getDataModel().
38 * @return mixed the native representation of the content. Could be a string, a nested array
39 * structure, an object, a binary blob... anything, really.
41 public abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
44 * returns the content's nominal size in bogo-bytes.
48 public abstract function getSize( );
51 * TODO: do we really need to pass a $modelName here?
52 * Seems odd and makes lots of stuff hard (ie having a newEmpty static method in TextContent)
56 public function __construct( $modelName = null ) {
57 $this->mModelName
= $modelName;
61 * Returns the name of the content model used by this content objects.
62 * Corresponds to the CONTENT_MODEL_XXX constants.
64 * @return String the model name
66 public function getModelName() {
67 return $this->mModelName
;
71 * Throws an MWException if $modelName is not the name of the content model
72 * supported by this Content object.
74 protected function checkModelName( $modelName ) {
75 if ( $modelName !== $this->mModelName
) {
76 throw new MWException( "Bad content model: expected " . $this->mModelName
. " but got found " . $modelName );
81 * Conveniance method that returns the ContentHandler singleton for handling the content
82 * model this Content object uses.
84 * Shorthand for ContentHandler::getForContent( $this )
86 * @return ContentHandler
88 public function getContentHandler() {
89 return ContentHandler
::getForContent( $this );
93 * Conveniance method that returns the default serialization format for the content model
94 * model this Content object uses.
96 * Shorthand for $this->getContentHandler()->getDefaultFormat()
98 * @return ContentHandler
100 public function getDefaultFormat() {
101 return $this->getContentHandler()->getDefaultFormat();
105 * Conveniance method that returns the list of serialization formats supported
106 * for the content model model this Content object uses.
108 * Shorthand for $this->getContentHandler()->getSupportedFormats()
110 * @return array of supported serialization formats
112 public function getSupportedFormats() {
113 return $this->getContentHandler()->getSupportedFormats();
117 * Returns true if $format is a supported serialization format for this Content object,
120 * Note that this will always return true if $format is null, because null stands for the
121 * default serialization.
123 * Shorthand for $this->getContentHandler()->isSupportedFormat( $format )
125 * @param String $format the format to check
126 * @return bool whether the format is supported
128 public function isSupportedFormat( $format ) {
130 return true; // this means "use the default"
133 return $this->getContentHandler()->isSupportedFormat( $format );
137 * Throws an MWException if $this->isSupportedFormat( $format ) doesn't return true.
140 * @throws MWException
142 protected function checkFormat( $format ) {
143 if ( !$this->isSupportedFormat( $format ) ) {
144 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
149 * Conveniance method for serializing this Content object.
151 * Shorthand for $this->getContentHandler()->serialize( $this, $format )
153 * @param null|String $format the desired serialization format (or null for the default format).
154 * @return String serialized form of this Content object
156 public function serialize( $format = null ) {
157 return $this->getContentHandler()->serialize( $this, $format );
161 * Returns true if this Content object represents empty content.
163 * @return bool whether this Content object is empty
165 public function isEmpty() {
166 return $this->getSize() == 0;
170 * Returns true if this Content objects is conceptually equivalent to the given Content object.
172 * Will returns false if $that is null.
173 * Will return true if $that === $this.
175 * Returns false if this Content object uses a different content model than the
177 * @param Content $that the Content object to compare to
178 * @return bool true if this Content object is euzqla to $that, false otherwise.
180 public function equals( Content
$that = null ) {
181 if ( empty( $that ) ){ // FIXME: empty on an object?
185 if ( $that === $this ) {
189 if ( $that->getModelName() !== $this->getModelName() ) {
193 return $this->getNativeData() === $that->getNativeData();
197 * Returns true if this content is countable as a "real" wiki page, provided
198 * that it's also in a countable location (e.g. a current revision in the main namespace).
200 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
201 * to avoid redundant parsing to find out.
203 public abstract function isCountable( $hasLinks = null ) ;
206 * @param null|Title $title
208 * @param null|ParserOptions $options
209 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
210 * the result of calling getText() on the ParserOutput object returned by
211 * this method is undefined.
213 * @return ParserOutput
215 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL, $generateHtml = true );
218 * Construct the redirect destination from this content and return an
219 * array of Titles, or null if this content doesn't represent a redirect.
220 * The last element in the array is the final destination after all redirects
221 * have been resolved (up to $wgMaxRedirects times).
223 * @return Array of Titles, with the destination last
225 public function getRedirectChain() {
230 * Construct the redirect destination from this content and return an
231 * array of Titles, or null if this content doesn't represent a redirect.
232 * This will only return the immediate redirect target, useful for
233 * the redirect table and other checks that don't need full recursion.
235 * @return Title: The corresponding Title
237 public function getRedirectTarget() {
242 * Construct the redirect destination from this content and return the
243 * Title, or null if this content doesn't represent a redirect.
244 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
245 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
249 public function getUltimateRedirectTarget() {
253 public function isRedirect() {
254 return $this->getRedirectTarget() != null;
258 * Returns the section with the given id.
260 * The default implementation returns null.
262 * @param String $sectionId the section's id
263 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
265 public function getSection( $sectionId ) {
270 * Replaces a section of the content and returns a Content object with the section replaced.
272 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
273 * @param $with Content: new content of the section
274 * @param $sectionTitle String: new section's subject, only if $section is 'new'
275 * @return string Complete article text, or null if error
277 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
282 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
284 * @param Title $title
286 * @param null|ParserOptions $popts
289 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
294 * Returns a new WikitextContent object with the given section heading prepended, if supported.
295 * The default implementation just returns this Content object unmodified, ignoring the section header.
297 * @param $header String
300 public function addSectionHeader( $header ) {
305 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
307 * @param Title $title
308 * @param null|ParserOptions $popts
311 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
315 # TODO: minimize special cases for CSS/JS; how to handle extra message for JS/CSS previews??
316 # TODO: handle ImagePage and CategoryPage
317 # TODO: hook into dump generation to serialize and record model and format!
319 # TODO: make sure we cover lucene search / wikisearch.
320 # TODO: make sure ReplaceTemplates still works
321 # TODO: nice&sane integration of GeSHi syntax highlighting
322 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
323 # [12:00] <vvv> And default it to a DummyHighlighter
325 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
327 # TODO: tie into API to provide contentModel for Revisions
328 # TODO: tie into API to provide serialized version and contentFormat for Revisions
329 # TODO: tie into API edit interface
330 # TODO: make EditForm plugin for EditPage
332 # XXX: isCacheable( ) # can/should we do this here?
336 * Content object implementation for representing flat text. The
338 abstract class TextContent
extends Content
{
340 public function __construct( $text, $modelName = null ) {
341 parent
::__construct( $modelName );
343 $this->mText
= $text;
346 public function getTextForSummary( $maxlength = 250 ) {
349 $text = $this->getNativeData();
351 $truncatedtext = $wgContLang->truncate(
352 preg_replace( "/[\n\r]/", ' ', $text ),
353 max( 0, $maxlength ) );
355 return $truncatedtext;
359 * returns the content's nominal size in bogo-bytes.
361 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
362 $text = $this->getNativeData( );
363 return strlen( $text );
367 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
369 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
370 * to avoid redundant parsing to find out.
372 public function isCountable( $hasLinks = null ) {
373 global $wgArticleCountMethod;
375 if ( $this->isRedirect( ) ) {
379 if ( $wgArticleCountMethod === 'any' ) {
387 * Returns the text represented by this Content object, as a string.
389 * @return String the raw text
391 public function getNativeData( ) {
392 $text = $this->mText
;
397 * Returns the text represented by this Content object, as a string.
399 * @return String the raw text
401 public function getTextForSearchIndex( ) { #FIXME: use!
402 return $this->getNativeData();
406 * Returns the text represented by this Content object, as a string.
408 * @return String the raw text
410 public function getWikitextForTransclusion( ) { #FIXME: use!
411 return $this->getNativeData();
415 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
417 * @return ParserOutput representing the HTML form of the text
419 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
420 # generic implementation, relying on $this->getHtml()
422 if ( $generateHtml ) $html = $this->getHtml( $options );
425 $po = new ParserOutput( $html );
430 protected abstract function getHtml( );
434 class WikitextContent
extends TextContent
{
436 public function __construct( $text ) {
437 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
439 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
442 protected function getHtml( ) {
443 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
446 public function getDefaultParserOptions() {
447 global $wgUser, $wgContLang;
449 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
450 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
453 return $this->mDefaultParserOptions
;
457 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
459 * @return ParserOutput representing the HTML form of the text
461 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
465 $options = $this->getDefaultParserOptions();
468 $po = $wgParser->parse( $this->mText
, $title, $options, true, true, $revId );
474 * Returns the section with the given id.
476 * @param String $sectionId the section's id
477 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
479 public function getSection( $section ) {
482 $text = $this->getNativeData();
483 $sect = $wgParser->getSection( $text, $section, false );
485 return new WikitextContent( $sect );
489 * Replaces a section in the wikitext
491 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
492 * @param $with Content: new content of the section
493 * @param $sectionTitle String: new section's subject, only if $section is 'new'
494 * @return string Complete article text, or null if error
496 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
497 wfProfileIn( __METHOD__
);
499 $myModelName = $this->getModelName();
500 $sectionModelName = $with->getModelName();
502 if ( $sectionModelName != $myModelName ) {
503 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
506 $oldtext = $this->getNativeData();
507 $text = $with->getNativeData();
509 if ( $section == 'new' ) {
510 # Inserting a new section
511 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
512 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
513 $text = strlen( trim( $oldtext ) ) > 0
514 ?
"{$oldtext}\n\n{$subject}{$text}"
515 : "{$subject}{$text}";
518 # Replacing an existing section; roll out the big guns
521 $text = $wgParser->replaceSection( $oldtext, $section, $text );
524 $newContent = new WikitextContent( $text );
526 wfProfileOut( __METHOD__
);
531 * Returns a new WikitextContent object with the given section heading prepended.
533 * @param $header String
536 public function addSectionHeader( $header ) {
537 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle
) . "\n\n" . $this->getNativeData();
539 return new WikitextContent( $text );
543 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
545 * @param Title $title
547 * @param null|ParserOptions $popts
550 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
553 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
555 $text = $this->getNativeData();
556 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
558 return new WikitextContent( $pst );
562 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
564 * @param Title $title
565 * @param null|ParserOptions $popts
568 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
571 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
573 $text = $this->getNativeData();
574 $plt = $wgParser->getPreloadText( $text, $title, $popts );
576 return new WikitextContent( $plt );
579 public function getRedirectChain() {
580 $text = $this->getNativeData();
581 return Title
::newFromRedirectArray( $text );
584 public function getRedirectTarget() {
585 $text = $this->getNativeData();
586 return Title
::newFromRedirect( $text );
589 public function getUltimateRedirectTarget() {
590 $text = $this->getNativeData();
591 return Title
::newFromRedirectRecurse( $text );
595 * Returns true if this content is not a redirect, and this content's text is countable according to
596 * the criteria defiend by $wgArticleCountMethod.
598 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
599 * to avoid redundant parsing to find out.
601 public function isCountable( $hasLinks = null ) {
602 global $wgArticleCountMethod;
604 if ( $this->isRedirect( ) ) {
608 $text = $this->getNativeData();
610 switch ( $wgArticleCountMethod ) {
614 if ( $text === false ) {
615 $text = $this->getRawText();
617 return strpos( $text, ',' ) !== false;
619 if ( $hasLinks === null ) { # not know, find out
620 $po = $this->getParserOutput();
621 $links = $po->getLinks();
622 $hasLinks = !empty( $links );
629 public function getTextForSummary( $maxlength = 250 ) {
630 $truncatedtext = parent
::getTextForSummary( $maxlength );
632 #clean up unfinished links
633 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
634 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
636 return $truncatedtext;
641 class MessageContent
extends TextContent
{
642 public function __construct( $msg_key, $params = null, $options = null ) {
643 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
645 $this->mMessageKey
= $msg_key;
647 $this->mParameters
= $params;
649 if ( is_null( $options ) ) {
652 elseif ( is_string( $options ) ) {
653 $options = array( $options );
656 $this->mOptions
= $options;
658 $this->mHtmlOptions
= null;
662 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
664 protected function getHtml( ) {
665 $opt = array_merge( $this->mOptions
, array('parse') );
667 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
672 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
674 public function getNativeData( ) {
675 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
677 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
683 class JavaScriptContent
extends TextContent
{
684 public function __construct( $text ) {
685 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
688 protected function getHtml( ) {
690 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
691 $html .= htmlspecialchars( $this->getNativeData() );
692 $html .= "\n</pre>\n";
699 class CssContent
extends TextContent
{
700 public function __construct( $text ) {
701 parent
::__construct($text, CONTENT_MODEL_CSS
);
704 protected function getHtml( ) {
706 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
707 $html .= htmlspecialchars( $this->getNativeData() );
708 $html .= "\n</pre>\n";
714 #FUTURE: special type for redirects?!
715 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
716 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
717 #EXAMPLE: CoordinatesContent
718 #EXAMPLE: WikidataContent