1ec4b0c569c88d53ece10c09119b489f9788c241
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 public function __construct( $modelName = null ) {
12 $this->mModelName
= $modelName;
15 public function getModelName() {
16 return $this->mModelName
;
19 protected function checkModelName( $modelName ) {
20 if ( $modelName !== $this->mModelName
) {
21 throw new MWException( "Bad content model: expected " . $this->mModelName
. " but got found " . $modelName );
25 public function getContentHandler() {
26 return ContentHandler
::getForContent( $this );
29 public function getDefaultFormat() {
30 return $this->getContentHandler()->getDefaultFormat();
33 public function getSupportedFormats() {
34 return $this->getContentHandler()->getSupportedFormats();
37 public function isSupportedFormat( $format ) {
38 if ( !$format ) return true; # this means "use the default"
40 return $this->getContentHandler()->isSupportedFormat( $format );
43 protected function checkFormat( $format ) {
44 if ( !$this->isSupportedFormat( $format ) ) {
45 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
49 public function serialize( $format = null ) {
50 return $this->getContentHandler()->serialize( $this, $format );
54 * @return String a string representing the content in a way useful for building a full text search index.
55 * If no useful representation exists, this method returns an empty string.
57 public abstract function getTextForSearchIndex( );
60 * @return String the wikitext to include when another page includes this content, or false if the content is not
61 * includable in a wikitext page.
63 #TODO: allow native handling, bypassing wikitext representation, like for includable special pages.
64 public abstract function getWikitextForTransclusion( ); #FIXME: use in parser, etc!
67 * Returns a textual representation of the content suitable for use in edit summaries and log messages.
69 * @param int $maxlength maximum length of the summary text
70 * @return String the summary text
72 public abstract function getTextForSummary( $maxlength = 250 );
75 * Returns native represenation of the data. Interpretation depends on the data model used,
76 * as given by getDataModel().
78 * @return mixed the native representation of the content. Could be a string, a nested array
79 * structure, an object, a binary blob... anything, really.
81 public abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
84 * returns the content's nominal size in bogo-bytes.
88 public abstract function getSize( );
90 public function isEmpty() {
91 return $this->getSize() == 0;
94 public function equals( Content
$that ) {
95 if ( empty( $that ) ) return false;
96 if ( $that === $this ) return true;
97 if ( $that->getModelName() !== $this->getModelName() ) return false;
99 return $this->getNativeData() == $that->getNativeData();
103 * Returns true if this content is countable as a "real" wiki page, provided
104 * that it's also in a countable location (e.g. a current revision in the main namespace).
106 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
107 * to avoid redundant parsing to find out.
109 public abstract function isCountable( $hasLinks = null ) ;
112 * @param null|Title $title
114 * @param null|ParserOptions $options
115 * @return ParserOutput
117 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL );
120 * Construct the redirect destination from this content and return an
121 * array of Titles, or null if this content doesn't represent a redirect.
122 * The last element in the array is the final destination after all redirects
123 * have been resolved (up to $wgMaxRedirects times).
125 * @return Array of Titles, with the destination last
127 public function getRedirectChain() {
132 * Construct the redirect destination from this content and return an
133 * array of Titles, or null if this content doesn't represent a redirect.
134 * This will only return the immediate redirect target, useful for
135 * the redirect table and other checks that don't need full recursion.
137 * @return Title: The corresponding Title
139 public function getRedirectTarget() {
144 * Construct the redirect destination from this content and return the
145 * Title, or null if this content doesn't represent a redirect.
146 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
147 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
151 public function getUltimateRedirectTarget() {
155 public function isRedirect() {
156 return $this->getRedirectTarget() != null;
160 * Returns the section with the given id.
162 * The default implementation returns null.
164 * @param String $sectionId the section's id
165 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
167 public function getSection( $sectionId ) {
172 * Replaces a section of the content and returns a Content object with the section replaced.
174 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
175 * @param $with Content: new content of the section
176 * @param $sectionTitle String: new section's subject, only if $section is 'new'
177 * @return string Complete article text, or null if error
179 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
184 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
186 * @param Title $title
188 * @param null|ParserOptions $popts
191 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
196 * Returns a new WikitextContent object with the given section heading prepended, if supported.
197 * The default implementation just returns this Content object unmodified, ignoring the section header.
199 * @param $header String
202 public function addSectionHeader( $header ) {
207 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
209 * @param Title $title
210 * @param null|ParserOptions $popts
213 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
217 #TODO: implement specialized ParserOutput for Wikidata model
218 #TODO: provide "combined" ParserOutput for Multipart... somehow.
220 # XXX: isCacheable( ) # can/should we do this here?
222 # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
223 # TODO: tie into EditPage, make it use Content-objects throughout, make edit form aware of content model and format
224 # TODO: make model-aware diff view!
225 # TODO: handle ImagePage and CategoryPage
227 # TODO: Title::newFromRedirectRecurse( $this->getRawText() );
229 # TODO: tie into API to provide contentModel for Revisions
230 # TODO: tie into API to provide serialized version and contentFormat for Revisions
231 # TODO: tie into API edit interface
236 * Content object implementation for representing flat text. The
238 abstract class TextContent
extends Content
{
239 public function __construct( $text, $modelName = null ) {
240 parent
::__construct($modelName);
242 $this->mText
= $text;
245 public function getTextForSummary( $maxlength = 250 ) {
248 $text = $this->getNativeData();
250 $truncatedtext = $wgContLang->truncate(
251 preg_replace( "/[\n\r]/", ' ', $text ),
252 max( 0, $maxlength ) );
254 return $truncatedtext;
258 * returns the content's nominal size in bogo-bytes.
260 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
261 $text = $this->getNativeData( );
262 return strlen( $text );
266 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
268 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
269 * to avoid redundant parsing to find out.
271 public function isCountable( $hasLinks = null ) {
272 global $wgArticleCountMethod;
274 if ( $this->isRedirect( ) ) {
278 if ( $wgArticleCountMethod === 'any' ) {
286 * Returns the text represented by this Content object, as a string.
288 * @return String the raw text
290 public function getNativeData( ) {
291 $text = $this->mText
;
296 * Returns the text represented by this Content object, as a string.
298 * @return String the raw text
300 public function getTextForSearchIndex( ) { #FIXME: use!
301 return $this->getNativeData();
305 * Returns the text represented by this Content object, as a string.
307 * @return String the raw text
309 public function getWikitextForTransclusion( ) { #FIXME: use!
310 return $this->getNativeData();
314 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
316 * @return ParserOutput representing the HTML form of the text
318 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
319 # generic implementation, relying on $this->getHtml()
321 $html = $this->getHtml( $options );
322 $po = new ParserOutput( $html );
327 protected abstract function getHtml( );
331 class WikitextContent
extends TextContent
{
332 public function __construct( $text ) {
333 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
335 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
338 protected function getHtml( ) {
339 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
342 public function getDefaultParserOptions() {
343 global $wgUser, $wgContLang;
345 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
346 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
349 return $this->mDefaultParserOptions
;
353 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
355 * @return ParserOutput representing the HTML form of the text
357 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
361 $options = $this->getDefaultParserOptions();
364 $po = $wgParser->parse( $this->mText
, $title, $options, true, true, $revId );
370 * Returns the section with the given id.
372 * @param String $sectionId the section's id
373 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
375 public function getSection( $section ) {
378 $text = $this->getNativeData();
379 $sect = $wgParser->getSection( $text, $section, false );
381 return new WikitextContent( $sect );
385 * Replaces a section in the wikitext
387 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
388 * @param $with Content: new content of the section
389 * @param $sectionTitle String: new section's subject, only if $section is 'new'
390 * @return string Complete article text, or null if error
392 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
395 wfProfileIn( __METHOD__
);
397 $myModelName = $this->getModelName();
398 $sectionModelName = $with->getModelName();
400 if ( $sectionModelName != $myModelName ) {
401 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
404 $oldtext = $this->getNativeData();
405 $text = $with->getNativeData();
407 if ( $section == 'new' ) {
408 # Inserting a new section
409 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
410 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
411 $text = strlen( trim( $oldtext ) ) > 0
412 ?
"{$oldtext}\n\n{$subject}{$text}"
413 : "{$subject}{$text}";
416 # Replacing an existing section; roll out the big guns
419 $text = $wgParser->replaceSection( $oldtext, $section, $text );
422 $newContent = new WikitextContent( $text );
424 wfProfileOut( __METHOD__
);
429 * Returns a new WikitextContent object with the given section heading prepended.
431 * @param $header String
434 public function addSectionHeader( $header ) {
435 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle
) . "\n\n" . $this->getNativeData();
437 return new WikitextContent( $text );
441 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
443 * @param Title $title
445 * @param null|ParserOptions $popts
448 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
451 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
453 $text = $this->getNativeData();
454 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
456 return new WikitextContent( $pst );
460 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
462 * @param Title $title
463 * @param null|ParserOptions $popts
466 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
469 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
471 $text = $this->getNativeData();
472 $plt = $wgParser->getPreloadText( $text, $title, $popts );
474 return new WikitextContent( $plt );
477 public function getRedirectChain() {
478 $text = $this->getNativeData();
479 return Title
::newFromRedirectArray( $text );
482 public function getRedirectTarget() {
483 $text = $this->getNativeData();
484 return Title
::newFromRedirect( $text );
487 public function getUltimateRedirectTarget() {
488 $text = $this->getNativeData();
489 return Title
::newFromRedirectRecurse( $text );
493 * Returns true if this content is not a redirect, and this content's text is countable according to
494 * the criteria defiend by $wgArticleCountMethod.
496 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
497 * to avoid redundant parsing to find out.
499 public function isCountable( $hasLinks = null ) {
500 global $wgArticleCountMethod;
502 if ( $this->isRedirect( ) ) {
506 $text = $this->getNativeData();
508 switch ( $wgArticleCountMethod ) {
512 if ( $text === false ) {
513 $text = $this->getRawText();
515 return strpos( $text, ',' ) !== false;
517 if ( $hasLinks === null ) { # not know, find out
518 $po = $this->getParserOutput();
519 $links = $po->getLinks();
520 $hasLinks = !empty( $links );
527 public function getTextForSummary( $maxlength = 250 ) {
528 $truncatedtext = parent
::getTextForSummary( $maxlength );
530 #clean up unfinished links
531 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
532 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
534 return $truncatedtext;
539 class MessageContent
extends TextContent
{
540 public function __construct( $msg_key, $params = null, $options = null ) {
541 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
543 $this->mMessageKey
= $msg_key;
545 $this->mParameters
= $params;
547 if ( !$options ) $options = array();
548 $this->mOptions
= $options;
550 $this->mHtmlOptions
= null;
554 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
556 protected function getHtml( ) {
557 $opt = array_merge( $this->mOptions
, array('parse') );
559 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
564 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
566 public function getNativeData( ) {
567 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
569 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
575 class JavaScriptContent
extends TextContent
{
576 public function __construct( $text ) {
577 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
580 protected function getHtml( ) {
582 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
583 $html .= htmlspecialchars( $this->getNativeData() );
584 $html .= "\n</pre>\n";
591 class CssContent
extends TextContent
{
592 public function __construct( $text ) {
593 parent
::__construct($text, CONTENT_MODEL_CSS
);
596 protected function getHtml( ) {
598 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
599 $html .= htmlspecialchars( $this->getNativeData() );
600 $html .= "\n</pre>\n";
606 #FUTURE: special type for redirects?!
607 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
608 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
609 #EXAMPLE: CoordinatesContent
610 #EXAMPLE: WikidataContent