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;
60 public function getModelName() {
61 return $this->mModelName
;
64 protected function checkModelName( $modelName ) {
65 if ( $modelName !== $this->mModelName
) {
66 throw new MWException( "Bad content model: expected " . $this->mModelName
. " but got found " . $modelName );
70 public function getContentHandler() {
71 return ContentHandler
::getForContent( $this );
74 public function getDefaultFormat() {
75 return $this->getContentHandler()->getDefaultFormat();
78 public function getSupportedFormats() {
79 return $this->getContentHandler()->getSupportedFormats();
82 public function isSupportedFormat( $format ) {
83 if ( !$format ) return true; # this means "use the default"
85 return $this->getContentHandler()->isSupportedFormat( $format );
88 protected function checkFormat( $format ) {
89 if ( !$this->isSupportedFormat( $format ) ) {
90 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
94 public function serialize( $format = null ) {
95 return $this->getContentHandler()->serialize( $this, $format );
98 public function isEmpty() {
99 return $this->getSize() == 0;
102 public function equals( Content
$that ) {
103 if ( empty( $that ) ) return false;
104 if ( $that === $this ) return true;
105 if ( $that->getModelName() !== $this->getModelName() ) return false;
107 return $this->getNativeData() == $that->getNativeData();
111 * Returns true if this content is countable as a "real" wiki page, provided
112 * that it's also in a countable location (e.g. a current revision in the main namespace).
114 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
115 * to avoid redundant parsing to find out.
117 public abstract function isCountable( $hasLinks = null ) ;
120 * @param null|Title $title
122 * @param null|ParserOptions $options
123 * @return ParserOutput
125 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL );
128 * Construct the redirect destination from this content and return an
129 * array of Titles, or null if this content doesn't represent a redirect.
130 * The last element in the array is the final destination after all redirects
131 * have been resolved (up to $wgMaxRedirects times).
133 * @return Array of Titles, with the destination last
135 public function getRedirectChain() {
140 * Construct the redirect destination from this content and return an
141 * array of Titles, or null if this content doesn't represent a redirect.
142 * This will only return the immediate redirect target, useful for
143 * the redirect table and other checks that don't need full recursion.
145 * @return Title: The corresponding Title
147 public function getRedirectTarget() {
152 * Construct the redirect destination from this content and return the
153 * Title, or null if this content doesn't represent a redirect.
154 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
155 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
159 public function getUltimateRedirectTarget() {
163 public function isRedirect() {
164 return $this->getRedirectTarget() != null;
168 * Returns the section with the given id.
170 * The default implementation returns null.
172 * @param String $sectionId the section's id
173 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
175 public function getSection( $sectionId ) {
180 * Replaces a section of the content and returns a Content object with the section replaced.
182 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
183 * @param $with Content: new content of the section
184 * @param $sectionTitle String: new section's subject, only if $section is 'new'
185 * @return string Complete article text, or null if error
187 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
192 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
194 * @param Title $title
196 * @param null|ParserOptions $popts
199 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
204 * Returns a new WikitextContent object with the given section heading prepended, if supported.
205 * The default implementation just returns this Content object unmodified, ignoring the section header.
207 * @param $header String
210 public function addSectionHeader( $header ) {
215 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
217 * @param Title $title
218 * @param null|ParserOptions $popts
221 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
225 # TODO: minimize special cases for CSS/JS; how to handle extra message for JS/CSS previews??
226 # TODO: handle ImagePage and CategoryPage
227 # TODO: hook into dump generation to serialize and record model and format!
229 # TODO: make sure we cover lucene search / wikisearch.
230 # TODO: make sure ReplaceTemplates still works
231 # TODO: nice&sane integration of GeSHi syntax highlighting
232 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
233 # [12:00] <vvv> And default it to a DummyHighlighter
235 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
237 # TODO: tie into API to provide contentModel for Revisions
238 # TODO: tie into API to provide serialized version and contentFormat for Revisions
239 # TODO: tie into API edit interface
240 # TODO: make EditForm plugin for EditPage
242 # XXX: isCacheable( ) # can/should we do this here?
246 * Content object implementation for representing flat text. The
248 abstract class TextContent
extends Content
{
250 public function __construct( $text, $modelName = null ) {
251 parent
::__construct( $modelName );
253 $this->mText
= $text;
256 public function getTextForSummary( $maxlength = 250 ) {
259 $text = $this->getNativeData();
261 $truncatedtext = $wgContLang->truncate(
262 preg_replace( "/[\n\r]/", ' ', $text ),
263 max( 0, $maxlength ) );
265 return $truncatedtext;
269 * returns the content's nominal size in bogo-bytes.
271 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
272 $text = $this->getNativeData( );
273 return strlen( $text );
277 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
279 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
280 * to avoid redundant parsing to find out.
282 public function isCountable( $hasLinks = null ) {
283 global $wgArticleCountMethod;
285 if ( $this->isRedirect( ) ) {
289 if ( $wgArticleCountMethod === 'any' ) {
297 * Returns the text represented by this Content object, as a string.
299 * @return String the raw text
301 public function getNativeData( ) {
302 $text = $this->mText
;
307 * Returns the text represented by this Content object, as a string.
309 * @return String the raw text
311 public function getTextForSearchIndex( ) { #FIXME: use!
312 return $this->getNativeData();
316 * Returns the text represented by this Content object, as a string.
318 * @return String the raw text
320 public function getWikitextForTransclusion( ) { #FIXME: use!
321 return $this->getNativeData();
325 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
327 * @return ParserOutput representing the HTML form of the text
329 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
330 # generic implementation, relying on $this->getHtml()
332 $html = $this->getHtml( $options );
333 $po = new ParserOutput( $html );
338 protected abstract function getHtml( );
342 class WikitextContent
extends TextContent
{
344 public function __construct( $text ) {
345 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
347 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
350 protected function getHtml( ) {
351 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
354 public function getDefaultParserOptions() {
355 global $wgUser, $wgContLang;
357 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
358 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
361 return $this->mDefaultParserOptions
;
365 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
367 * @return ParserOutput representing the HTML form of the text
369 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
373 $options = $this->getDefaultParserOptions();
376 $po = $wgParser->parse( $this->mText
, $title, $options, true, true, $revId );
382 * Returns the section with the given id.
384 * @param String $sectionId the section's id
385 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
387 public function getSection( $section ) {
390 $text = $this->getNativeData();
391 $sect = $wgParser->getSection( $text, $section, false );
393 return new WikitextContent( $sect );
397 * Replaces a section in the wikitext
399 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
400 * @param $with Content: new content of the section
401 * @param $sectionTitle String: new section's subject, only if $section is 'new'
402 * @return string Complete article text, or null if error
404 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
405 wfProfileIn( __METHOD__
);
407 $myModelName = $this->getModelName();
408 $sectionModelName = $with->getModelName();
410 if ( $sectionModelName != $myModelName ) {
411 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
414 $oldtext = $this->getNativeData();
415 $text = $with->getNativeData();
417 if ( $section == 'new' ) {
418 # Inserting a new section
419 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
420 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
421 $text = strlen( trim( $oldtext ) ) > 0
422 ?
"{$oldtext}\n\n{$subject}{$text}"
423 : "{$subject}{$text}";
426 # Replacing an existing section; roll out the big guns
429 $text = $wgParser->replaceSection( $oldtext, $section, $text );
432 $newContent = new WikitextContent( $text );
434 wfProfileOut( __METHOD__
);
439 * Returns a new WikitextContent object with the given section heading prepended.
441 * @param $header String
444 public function addSectionHeader( $header ) {
445 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle
) . "\n\n" . $this->getNativeData();
447 return new WikitextContent( $text );
451 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
453 * @param Title $title
455 * @param null|ParserOptions $popts
458 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
461 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
463 $text = $this->getNativeData();
464 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
466 return new WikitextContent( $pst );
470 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
472 * @param Title $title
473 * @param null|ParserOptions $popts
476 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
479 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
481 $text = $this->getNativeData();
482 $plt = $wgParser->getPreloadText( $text, $title, $popts );
484 return new WikitextContent( $plt );
487 public function getRedirectChain() {
488 $text = $this->getNativeData();
489 return Title
::newFromRedirectArray( $text );
492 public function getRedirectTarget() {
493 $text = $this->getNativeData();
494 return Title
::newFromRedirect( $text );
497 public function getUltimateRedirectTarget() {
498 $text = $this->getNativeData();
499 return Title
::newFromRedirectRecurse( $text );
503 * Returns true if this content is not a redirect, and this content's text is countable according to
504 * the criteria defiend by $wgArticleCountMethod.
506 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
507 * to avoid redundant parsing to find out.
509 public function isCountable( $hasLinks = null ) {
510 global $wgArticleCountMethod;
512 if ( $this->isRedirect( ) ) {
516 $text = $this->getNativeData();
518 switch ( $wgArticleCountMethod ) {
522 if ( $text === false ) {
523 $text = $this->getRawText();
525 return strpos( $text, ',' ) !== false;
527 if ( $hasLinks === null ) { # not know, find out
528 $po = $this->getParserOutput();
529 $links = $po->getLinks();
530 $hasLinks = !empty( $links );
537 public function getTextForSummary( $maxlength = 250 ) {
538 $truncatedtext = parent
::getTextForSummary( $maxlength );
540 #clean up unfinished links
541 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
542 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
544 return $truncatedtext;
549 class MessageContent
extends TextContent
{
550 public function __construct( $msg_key, $params = null, $options = null ) {
551 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
553 $this->mMessageKey
= $msg_key;
555 $this->mParameters
= $params;
557 if ( !$options ) $options = array();
558 $this->mOptions
= $options;
560 $this->mHtmlOptions
= null;
564 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
566 protected function getHtml( ) {
567 $opt = array_merge( $this->mOptions
, array('parse') );
569 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
574 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
576 public function getNativeData( ) {
577 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
579 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
585 class JavaScriptContent
extends TextContent
{
586 public function __construct( $text ) {
587 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
590 protected function getHtml( ) {
592 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
593 $html .= htmlspecialchars( $this->getNativeData() );
594 $html .= "\n</pre>\n";
601 class CssContent
extends TextContent
{
602 public function __construct( $text ) {
603 parent
::__construct($text, CONTENT_MODEL_CSS
);
606 protected function getHtml( ) {
608 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
609 $html .= htmlspecialchars( $this->getNativeData() );
610 $html .= "\n</pre>\n";
616 #FUTURE: special type for redirects?!
617 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
618 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
619 #EXAMPLE: CoordinatesContent
620 #EXAMPLE: WikidataContent