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?
186 // FIXME: something is doing wrong here, causing the compared objects to always be the same.
187 // Hence returning false for now, so changes can actually be saved...
189 if ( $that === $this ) {
193 if ( $that->getModelName() !== $this->getModelName() ) {
197 return $this->getNativeData() === $that->getNativeData();
201 * Returns true if this content is countable as a "real" wiki page, provided
202 * that it's also in a countable location (e.g. a current revision in the main namespace).
204 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
205 * to avoid redundant parsing to find out.
207 public abstract function isCountable( $hasLinks = null ) ;
210 * @param null|Title $title
212 * @param null|ParserOptions $options
213 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
214 * the result of calling getText() on the ParserOutput object returned by
215 * this method is undefined.
217 * @return ParserOutput
219 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL, $generateHtml = true );
222 * Construct the redirect destination from this content and return an
223 * array of Titles, or null if this content doesn't represent a redirect.
224 * The last element in the array is the final destination after all redirects
225 * have been resolved (up to $wgMaxRedirects times).
227 * @return Array of Titles, with the destination last
229 public function getRedirectChain() {
234 * Construct the redirect destination from this content and return an
235 * array of Titles, or null if this content doesn't represent a redirect.
236 * This will only return the immediate redirect target, useful for
237 * the redirect table and other checks that don't need full recursion.
239 * @return Title: The corresponding Title
241 public function getRedirectTarget() {
246 * Construct the redirect destination from this content and return the
247 * Title, or null if this content doesn't represent a redirect.
248 * This will recurse down $wgMaxRedirects times or until a non-redirect target is hit
249 * in order to provide (hopefully) the Title of the final destination instead of another redirect.
253 public function getUltimateRedirectTarget() {
257 public function isRedirect() {
258 return $this->getRedirectTarget() != null;
262 * Returns the section with the given id.
264 * The default implementation returns null.
266 * @param String $sectionId the section's id
267 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
269 public function getSection( $sectionId ) {
274 * Replaces a section of the content and returns a Content object with the section replaced.
276 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
277 * @param $with Content: new content of the section
278 * @param $sectionTitle String: new section's subject, only if $section is 'new'
279 * @return string Complete article text, or null if error
281 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
286 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
288 * @param Title $title
290 * @param null|ParserOptions $popts
293 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
298 * Returns a new WikitextContent object with the given section heading prepended, if supported.
299 * The default implementation just returns this Content object unmodified, ignoring the section header.
301 * @param $header String
304 public function addSectionHeader( $header ) {
309 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
311 * @param Title $title
312 * @param null|ParserOptions $popts
315 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
319 # TODO: minimize special cases for CSS/JS; how to handle extra message for JS/CSS previews??
320 # TODO: handle ImagePage and CategoryPage
321 # TODO: hook into dump generation to serialize and record model and format!
323 # TODO: make sure we cover lucene search / wikisearch.
324 # TODO: make sure ReplaceTemplates still works
325 # TODO: nice&sane integration of GeSHi syntax highlighting
326 # [11:59] <vvv> Hooks are ugly; make CodeHighlighter interface and a config to set the class which handles syntax highlighting
327 # [12:00] <vvv> And default it to a DummyHighlighter
329 # TODO: make sure we cover the external editor interface (does anyone actually use that?!)
331 # TODO: tie into API to provide contentModel for Revisions
332 # TODO: tie into API to provide serialized version and contentFormat for Revisions
333 # TODO: tie into API edit interface
334 # TODO: make EditForm plugin for EditPage
336 # XXX: isCacheable( ) # can/should we do this here?
340 * Content object implementation for representing flat text. The
342 abstract class TextContent
extends Content
{
344 public function __construct( $text, $modelName = null ) {
345 parent
::__construct( $modelName );
347 $this->mText
= $text;
350 public function getTextForSummary( $maxlength = 250 ) {
353 $text = $this->getNativeData();
355 $truncatedtext = $wgContLang->truncate(
356 preg_replace( "/[\n\r]/", ' ', $text ),
357 max( 0, $maxlength ) );
359 return $truncatedtext;
363 * returns the content's nominal size in bogo-bytes.
365 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
366 $text = $this->getNativeData( );
367 return strlen( $text );
371 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
373 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
374 * to avoid redundant parsing to find out.
376 public function isCountable( $hasLinks = null ) {
377 global $wgArticleCountMethod;
379 if ( $this->isRedirect( ) ) {
383 if ( $wgArticleCountMethod === 'any' ) {
391 * Returns the text represented by this Content object, as a string.
393 * @return String the raw text
395 public function getNativeData( ) {
396 $text = $this->mText
;
401 * Returns the text represented by this Content object, as a string.
403 * @return String the raw text
405 public function getTextForSearchIndex( ) { #FIXME: use!
406 return $this->getNativeData();
410 * Returns the text represented by this Content object, as a string.
412 * @return String the raw text
414 public function getWikitextForTransclusion( ) { #FIXME: use!
415 return $this->getNativeData();
419 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
421 * @return ParserOutput representing the HTML form of the text
423 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
424 # generic implementation, relying on $this->getHtml()
426 if ( $generateHtml ) $html = $this->getHtml( $options );
429 $po = new ParserOutput( $html );
434 protected abstract function getHtml( );
438 class WikitextContent
extends TextContent
{
440 public function __construct( $text ) {
441 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
443 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
446 protected function getHtml( ) {
447 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
450 public function getDefaultParserOptions() {
451 global $wgUser, $wgContLang;
453 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
454 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
457 return $this->mDefaultParserOptions
;
461 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
463 * @return ParserOutput representing the HTML form of the text
465 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
469 $options = $this->getDefaultParserOptions();
472 $po = $wgParser->parse( $this->mText
, $title, $options, true, true, $revId );
478 * Returns the section with the given id.
480 * @param String $sectionId the section's id
481 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
483 public function getSection( $section ) {
486 $text = $this->getNativeData();
487 $sect = $wgParser->getSection( $text, $section, false );
489 return new WikitextContent( $sect );
493 * Replaces a section in the wikitext
495 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
496 * @param $with Content: new content of the section
497 * @param $sectionTitle String: new section's subject, only if $section is 'new'
498 * @return string Complete article text, or null if error
500 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
501 wfProfileIn( __METHOD__
);
503 $myModelName = $this->getModelName();
504 $sectionModelName = $with->getModelName();
506 if ( $sectionModelName != $myModelName ) {
507 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
510 $oldtext = $this->getNativeData();
511 $text = $with->getNativeData();
513 if ( $section == 'new' ) {
514 # Inserting a new section
515 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
516 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
517 $text = strlen( trim( $oldtext ) ) > 0
518 ?
"{$oldtext}\n\n{$subject}{$text}"
519 : "{$subject}{$text}";
522 # Replacing an existing section; roll out the big guns
525 $text = $wgParser->replaceSection( $oldtext, $section, $text );
528 $newContent = new WikitextContent( $text );
530 wfProfileOut( __METHOD__
);
535 * Returns a new WikitextContent object with the given section heading prepended.
537 * @param $header String
540 public function addSectionHeader( $header ) {
541 $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle
) . "\n\n" . $this->getNativeData();
543 return new WikitextContent( $text );
547 * Returns a Content object with pre-save transformations applied (or this object if no transformations apply).
549 * @param Title $title
551 * @param null|ParserOptions $popts
554 public function preSaveTransform( Title
$title, User
$user, ParserOptions
$popts = null ) {
557 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
559 $text = $this->getNativeData();
560 $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
562 return new WikitextContent( $pst );
566 * Returns a Content object with preload transformations applied (or this object if no transformations apply).
568 * @param Title $title
569 * @param null|ParserOptions $popts
572 public function preloadTransform( Title
$title, ParserOptions
$popts = null ) {
575 if ( $popts == null ) $popts = $this->getDefaultParserOptions();
577 $text = $this->getNativeData();
578 $plt = $wgParser->getPreloadText( $text, $title, $popts );
580 return new WikitextContent( $plt );
583 public function getRedirectChain() {
584 $text = $this->getNativeData();
585 return Title
::newFromRedirectArray( $text );
588 public function getRedirectTarget() {
589 $text = $this->getNativeData();
590 return Title
::newFromRedirect( $text );
593 public function getUltimateRedirectTarget() {
594 $text = $this->getNativeData();
595 return Title
::newFromRedirectRecurse( $text );
599 * Returns true if this content is not a redirect, and this content's text is countable according to
600 * the criteria defiend by $wgArticleCountMethod.
602 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
603 * to avoid redundant parsing to find out.
605 public function isCountable( $hasLinks = null ) {
606 global $wgArticleCountMethod;
608 if ( $this->isRedirect( ) ) {
612 $text = $this->getNativeData();
614 switch ( $wgArticleCountMethod ) {
618 if ( $text === false ) {
619 $text = $this->getRawText();
621 return strpos( $text, ',' ) !== false;
623 if ( $hasLinks === null ) { # not know, find out
624 $po = $this->getParserOutput();
625 $links = $po->getLinks();
626 $hasLinks = !empty( $links );
633 public function getTextForSummary( $maxlength = 250 ) {
634 $truncatedtext = parent
::getTextForSummary( $maxlength );
636 #clean up unfinished links
637 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
638 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
640 return $truncatedtext;
645 class MessageContent
extends TextContent
{
646 public function __construct( $msg_key, $params = null, $options = null ) {
647 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
); #XXX: messages may be wikitext, html or plain text! and maybe even something else entirely.
649 $this->mMessageKey
= $msg_key;
651 $this->mParameters
= $params;
653 if ( is_null( $options ) ) {
656 elseif ( is_string( $options ) ) {
657 $options = array( $options );
660 $this->mOptions
= $options;
662 $this->mHtmlOptions
= null;
666 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
668 protected function getHtml( ) {
669 $opt = array_merge( $this->mOptions
, array('parse') );
671 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
676 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
678 public function getNativeData( ) {
679 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
681 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
687 class JavaScriptContent
extends TextContent
{
688 public function __construct( $text ) {
689 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
692 protected function getHtml( ) {
694 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
695 $html .= htmlspecialchars( $this->getNativeData() );
696 $html .= "\n</pre>\n";
703 class CssContent
extends TextContent
{
704 public function __construct( $text ) {
705 parent
::__construct($text, CONTENT_MODEL_CSS
);
708 protected function getHtml( ) {
710 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
711 $html .= htmlspecialchars( $this->getNativeData() );
712 $html .= "\n</pre>\n";
718 #FUTURE: special type for redirects?!
719 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
720 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
721 #EXAMPLE: CoordinatesContent
722 #EXAMPLE: WikidataContent