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 ) {
84 return true; // this means "use the default"
87 return $this->getContentHandler()->isSupportedFormat( $format );
90 protected function checkFormat( $format ) {
91 if ( !$this->isSupportedFormat( $format ) ) {
92 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
96 public function serialize( $format = null ) {
97 return $this->getContentHandler()->serialize( $this, $format );
100 public function isEmpty() {
101 return $this->getSize() == 0;
104 public function equals( Content
$that ) {
105 if ( empty( $that ) ){ // FIXME: empty on an object?
109 if ( $that === $this ) {
113 if ( $that->getModelName() !== $this->getModelName() ) {
117 return $this->getNativeData() === $that->getNativeData();
121 * Returns true if this content is countable as a "real" wiki page, provided
122 * that it's also in a countable location (e.g. a current revision in the main namespace).
124 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
125 * to avoid redundant parsing to find out.
127 public abstract function isCountable( $hasLinks = null ) ;
130 * @param null|Title $title
132 * @param null|ParserOptions $options
133 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
134 * the result of calling getText() on the ParserOutput object returned by
135 * this method is undefined.
137 * @return ParserOutput
139 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL, $generateHtml = true );
142 * Construct the redirect destination from this content and return an
143 * array of Titles, or null if this content doesn't represent a redirect.
144 * The last element in the array is the final destination after all redirects
145 * have been resolved (up to $wgMaxRedirects times).
147 * @return Array of Titles, with the destination last
149 public function getRedirectChain() {
154 * Construct the redirect destination from this content and return an
155 * array of Titles, or null if this content doesn't represent a redirect.
156 * This will only return the immediate redirect target, useful for
157 * the redirect table and other checks that don't need full recursion.
159 * @return Title: The corresponding Title
161 public function getRedirectTarget() {
166 * Construct the redirect destination from this content and return the
167 * Title, or null if this content doesn't represent a redirect.
168 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
169 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
173 public function getUltimateRedirectTarget() {
177 public function isRedirect() {
178 return $this->getRedirectTarget() != null;
182 * Returns the section with the given id.
184 * The default implementation returns null.
186 * @param String $sectionId the section's id
187 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
189 public function getSection( $sectionId ) {
194 * Replaces a section of the content and returns a Content object with the section replaced.
196 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
197 * @param $with Content: new content of the section
198 * @param $sectionTitle String: new section's subject, only if $section is 'new'
199 * @return string Complete article text, or null if error
201 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
206 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
208 * @param Title $title
210 * @param null|ParserOptions $popts
213 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
218 * Returns a new WikitextContent object with the given section heading prepended, if supported.
219 * The default implementation just returns this Content object unmodified, ignoring the section header.
221 * @param $header String
224 public function addSectionHeader( $header ) {
229 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
231 * @param Title $title
232 * @param null|ParserOptions $popts
235 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
239 # TODO: minimize special cases for CSS/JS; how to handle extra message for JS/CSS previews??
240 # TODO: handle ImagePage and CategoryPage
241 # TODO: hook into dump generation to serialize and record model and format!
243 # TODO: make sure we cover lucene search / wikisearch.
244 # TODO: make sure ReplaceTemplates still works
245 # TODO: nice&sane integration of GeSHi syntax highlighting
246 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
247 # [12:00] <vvv> And default it to a DummyHighlighter
249 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
251 # TODO: tie into API to provide contentModel for Revisions
252 # TODO: tie into API to provide serialized version and contentFormat for Revisions
253 # TODO: tie into API edit interface
254 # TODO: make EditForm plugin for EditPage
256 # XXX: isCacheable( ) # can/should we do this here?
260 * Content object implementation for representing flat text. The
262 abstract class TextContent
extends Content
{
264 public function __construct( $text, $modelName = null ) {
265 parent
::__construct( $modelName );
267 $this->mText
= $text;
270 public function getTextForSummary( $maxlength = 250 ) {
273 $text = $this->getNativeData();
275 $truncatedtext = $wgContLang->truncate(
276 preg_replace( "/[\n\r]/", ' ', $text ),
277 max( 0, $maxlength ) );
279 return $truncatedtext;
283 * returns the content's nominal size in bogo-bytes.
285 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
286 $text = $this->getNativeData( );
287 return strlen( $text );
291 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
293 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
294 * to avoid redundant parsing to find out.
296 public function isCountable( $hasLinks = null ) {
297 global $wgArticleCountMethod;
299 if ( $this->isRedirect( ) ) {
303 if ( $wgArticleCountMethod === 'any' ) {
311 * Returns the text represented by this Content object, as a string.
313 * @return String the raw text
315 public function getNativeData( ) {
316 $text = $this->mText
;
321 * Returns the text represented by this Content object, as a string.
323 * @return String the raw text
325 public function getTextForSearchIndex( ) { #FIXME: use!
326 return $this->getNativeData();
330 * Returns the text represented by this Content object, as a string.
332 * @return String the raw text
334 public function getWikitextForTransclusion( ) { #FIXME: use!
335 return $this->getNativeData();
339 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
341 * @return ParserOutput representing the HTML form of the text
343 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
344 # generic implementation, relying on $this->getHtml()
346 if ( $generateHtml ) $html = $this->getHtml( $options );
349 $po = new ParserOutput( $html );
354 protected abstract function getHtml( );
358 class WikitextContent
extends TextContent
{
360 public function __construct( $text ) {
361 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
363 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
366 protected function getHtml( ) {
367 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
370 public function getDefaultParserOptions() {
371 global $wgUser, $wgContLang;
373 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
374 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
377 return $this->mDefaultParserOptions
;
381 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
383 * @return ParserOutput representing the HTML form of the text
385 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
389 $options = $this->getDefaultParserOptions();
392 $po = $wgParser->parse( $this->mText
, $title, $options, true, true, $revId );
398 * Returns the section with the given id.
400 * @param String $sectionId the section's id
401 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
403 public function getSection( $section ) {
406 $text = $this->getNativeData();
407 $sect = $wgParser->getSection( $text, $section, false );
409 return new WikitextContent( $sect );
413 * Replaces a section in the wikitext
415 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
416 * @param $with Content: new content of the section
417 * @param $sectionTitle String: new section's subject, only if $section is 'new'
418 * @return string Complete article text, or null if error
420 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
421 wfProfileIn( __METHOD__
);
423 $myModelName = $this->getModelName();
424 $sectionModelName = $with->getModelName();
426 if ( $sectionModelName != $myModelName ) {
427 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
430 $oldtext = $this->getNativeData();
431 $text = $with->getNativeData();
433 if ( $section == 'new' ) {
434 # Inserting a new section
435 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
436 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
437 $text = strlen( trim( $oldtext ) ) > 0
438 ?
"{$oldtext}\n\n{$subject}{$text}"
439 : "{$subject}{$text}";
442 # Replacing an existing section; roll out the big guns
445 $text = $wgParser->replaceSection( $oldtext, $section, $text );
448 $newContent = new WikitextContent( $text );
450 wfProfileOut( __METHOD__
);
455 * Returns a new WikitextContent object with the given section heading prepended.
457 * @param $header String
460 public function addSectionHeader( $header ) {
461 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle
) . "\n\n" . $this->getNativeData();
463 return new WikitextContent( $text );
467 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
469 * @param Title $title
471 * @param null|ParserOptions $popts
474 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
477 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
479 $text = $this->getNativeData();
480 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
482 return new WikitextContent( $pst );
486 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
488 * @param Title $title
489 * @param null|ParserOptions $popts
492 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
495 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
497 $text = $this->getNativeData();
498 $plt = $wgParser->getPreloadText( $text, $title, $popts );
500 return new WikitextContent( $plt );
503 public function getRedirectChain() {
504 $text = $this->getNativeData();
505 return Title
::newFromRedirectArray( $text );
508 public function getRedirectTarget() {
509 $text = $this->getNativeData();
510 return Title
::newFromRedirect( $text );
513 public function getUltimateRedirectTarget() {
514 $text = $this->getNativeData();
515 return Title
::newFromRedirectRecurse( $text );
519 * Returns true if this content is not a redirect, and this content's text is countable according to
520 * the criteria defiend by $wgArticleCountMethod.
522 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
523 * to avoid redundant parsing to find out.
525 public function isCountable( $hasLinks = null ) {
526 global $wgArticleCountMethod;
528 if ( $this->isRedirect( ) ) {
532 $text = $this->getNativeData();
534 switch ( $wgArticleCountMethod ) {
538 if ( $text === false ) {
539 $text = $this->getRawText();
541 return strpos( $text, ',' ) !== false;
543 if ( $hasLinks === null ) { # not know, find out
544 $po = $this->getParserOutput();
545 $links = $po->getLinks();
546 $hasLinks = !empty( $links );
553 public function getTextForSummary( $maxlength = 250 ) {
554 $truncatedtext = parent
::getTextForSummary( $maxlength );
556 #clean up unfinished links
557 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
558 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
560 return $truncatedtext;
565 class MessageContent
extends TextContent
{
566 public function __construct( $msg_key, $params = null, $options = null ) {
567 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
569 $this->mMessageKey
= $msg_key;
571 $this->mParameters
= $params;
573 if ( is_null( $options ) ) {
576 elseif ( is_string( $options ) ) {
577 $options = array( $options );
580 $this->mOptions
= $options;
582 $this->mHtmlOptions
= null;
586 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
588 protected function getHtml( ) {
589 $opt = array_merge( $this->mOptions
, array('parse') );
591 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
596 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
598 public function getNativeData( ) {
599 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
601 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
607 class JavaScriptContent
extends TextContent
{
608 public function __construct( $text ) {
609 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
612 protected function getHtml( ) {
614 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
615 $html .= htmlspecialchars( $this->getNativeData() );
616 $html .= "\n</pre>\n";
623 class CssContent
extends TextContent
{
624 public function __construct( $text ) {
625 parent
::__construct($text, CONTENT_MODEL_CSS
);
628 protected function getHtml( ) {
630 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
631 $html .= htmlspecialchars( $this->getNativeData() );
632 $html .= "\n</pre>\n";
638 #FUTURE: special type for redirects?!
639 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
640 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
641 #EXAMPLE: CoordinatesContent
642 #EXAMPLE: WikidataContent