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 ) { #FIXME: really need revId? annoying! #FIXME: really $title? or just when parsing, every time?
12 $this->mModelName
= $modelName;
15 public function getModelName() {
16 return $this->mModelName
;
19 public abstract function getTextForSearchIndex( );
21 public abstract function getWikitextForTransclusion( );
23 public abstract function getTextForSummary( $maxlength = 250 );
26 * Returns native represenation of the data. Interpretation depends on the data model used,
27 * as given by getDataModel().
29 * @return mixed the native representation of the content. Could be a string, a nested array
30 * structure, an object, a binary blob... anything, really.
32 public abstract function getNativeData( ); #FIXME: review all calls carefully, caller must be aware of content model!
35 * returns the content's nominal size in bogo-bytes.
37 public abstract function getSize( ); #XXX: do we really need/want this here? we could just use the byte syse of the serialized form...
39 public function isEmpty() {
40 return $this->getSize() == 0;
44 * Returns true if this content is countable as a "real" wiki page, provided
45 * that it's also in a countable location (e.g. a current revision in the main namespace).
47 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
48 * to avoid redundant parsing to find out.
50 public abstract function isCountable( $hasLinks = null ) ;
52 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL );
54 public function getRedirectChain() { #TODO: document!
58 public function getRedirectTarget() {
62 public function isRedirect() {
63 return $this->getRedirectTarget() != null;
67 * Returns the section with the given id.
69 * The default implementation returns null.
71 * @param String $sectionId the section's id
72 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
74 public function getSection( $sectionId ) {
79 * Replaces a section of the content and returns a Content object with the section replaced.
81 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
82 * @param $with Content: new content of the section
83 * @param $sectionTitle String: new section's subject, only if $section is 'new'
84 * @return string Complete article text, or null if error
86 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
90 #TODO: implement specialized ParserOutput for Wikidata model
91 #TODO: provide "combined" ParserOutput for Multipart... somehow.
93 # XXX: isCacheable( ) # can/should we do this here?
95 # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
96 # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags )
98 # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
99 # TODO: tie into EditPage, make it use Content-objects throughout, make edit form aware of content model and format
100 # TODO: tie into WikiPage, make it use Content-objects throughout, especially in doEdit(), doDelete(), etc
101 # TODO: make model-aware diff view!
102 # TODO: handle ImagePage and CategoryPage
104 # TODO: Title::newFromRedirectRecurse( $this->getRawText() );
106 # TODO: tie into API to provide contentModel for Revisions
107 # TODO: tie into API to provide serialized version and contentFormat for Revisions
108 # TODO: tie into API edit interface
113 * Content object implementation for representing flat text. The
115 abstract class TextContent
extends Content
{
116 public function __construct( $text, $modelName = null ) {
117 parent
::__construct($modelName);
119 $this->mText
= $text;
122 public function getTextForSummary( $maxlength = 250 ) {
125 $text = $this->getNativeData();
127 $truncatedtext = $wgContLang->truncate(
128 preg_replace( "/[\n\r]/", ' ', $text ),
129 max( 0, $maxlength ) );
131 return $truncatedtext;
135 * returns the content's nominal size in bogo-bytes.
137 public function getSize( ) { #FIXME: use! replace strlen in WikiPage.
138 $text = $this->getNativeData( );
139 return strlen( $text );
143 * Returns true if this content is not a redirect, and $wgArticleCountMethod is "any".
145 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
146 * to avoid redundant parsing to find out.
148 public function isCountable( $hasLinks = null ) {
149 global $wgArticleCountMethod;
151 if ( $this->isRedirect( ) ) {
155 if ( $wgArticleCountMethod === 'any' ) {
163 * Returns the text represented by this Content object, as a string.
165 * @return String the raw text
167 public function getNativeData( ) {
168 $text = $this->mText
;
173 * Returns the text represented by this Content object, as a string.
175 * @return String the raw text
177 public function getTextForSearchIndex( ) { #FIXME: use!
178 return $this->getNativeData();
182 * Returns the text represented by this Content object, as a string.
184 * @return String the raw text
186 public function getWikitextForTransclusion( ) { #FIXME: use!
187 return $this->getNativeData();
191 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
193 * @return ParserOutput representing the HTML form of the text
195 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
196 # generic implementation, relying on $this->getHtml()
198 $html = $this->getHtml( $options );
199 $po = new ParserOutput( $html );
201 if ( $this->mTitle
) $po->setTitleText( $this->mTitle
->getText() );
203 #TODO: cache settings, etc?
208 protected abstract function getHtml( );
212 class WikitextContent
extends TextContent
{
213 public function __construct( $text ) {
214 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
216 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
219 protected function getHtml( ) {
220 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
223 public function getDefaultParserOptions() {
224 global $wgUser, $wgContLang;
226 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
227 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
230 return $this->mDefaultParserOptions
;
234 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
236 * @return ParserOutput representing the HTML form of the text
238 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
242 $options = $this->getDefaultParserOptions();
245 $po = $wgParser->parse( $this->mText
, $this->getTitle(), $options, true, true, $this->mRevId
);
251 * Returns the section with the given id.
253 * @param String $sectionId the section's id
254 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
256 public function getSection( $section ) {
259 $text = $this->getNativeData();
260 $sect = $wgParser->getSection( $text, $section, false );
262 return new WikitextContent( $sect );
266 * Replaces a section in the wikitext
268 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
269 * @param $with Content: new content of the section
270 * @param $sectionTitle String: new section's subject, only if $section is 'new'
271 * @return string Complete article text, or null if error
273 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
276 wfProfileIn( __METHOD__
);
278 $myModelName = $this->getModelName();
279 $sectionModelName = $with->getModelName();
281 if ( $sectionModelName != $myModelName ) {
282 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
285 $oldtext = $this->getNativeData();
286 $text = $with->getNativeData();
288 if ( $section == 'new' ) {
289 # Inserting a new section
290 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
291 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
292 $text = strlen( trim( $oldtext ) ) > 0
293 ?
"{$oldtext}\n\n{$subject}{$text}"
294 : "{$subject}{$text}";
297 # Replacing an existing section; roll out the big guns
300 $text = $wgParser->replaceSection( $oldtext, $section, $text );
303 $newContent = new WikitextContent( $text );
305 wfProfileOut( __METHOD__
);
309 public function getRedirectChain() {
310 $text = $this->getNativeData();
311 return Title
::newFromRedirectArray( $text );
314 public function getRedirectTarget() {
315 $text = $this->getNativeData();
316 return Title
::newFromRedirect( $text );
320 * Returns true if this content is not a redirect, and this content's text is countable according to
321 * the criteria defiend by $wgArticleCountMethod.
323 * @param $hasLinks Bool: if it is known whether this content contains links, provide this information here,
324 * to avoid redundant parsing to find out.
326 public function isCountable( $hasLinks = null ) {
327 global $wgArticleCountMethod;
329 if ( $this->isRedirect( ) ) {
333 $text = $this->getNativeData();
335 switch ( $wgArticleCountMethod ) {
339 if ( $text === false ) {
340 $text = $this->getRawText();
342 return strpos( $text, ',' ) !== false;
344 if ( $hasLinks === null ) { # not know, find out
345 $po = $this->getParserOutput();
346 $links = $po->getLinks();
347 $hasLinks = !empty( $links );
354 public function getTextForSummary( $maxlength = 250 ) {
355 $truncatedtext = parent
::getTextForSummary( $maxlength );
357 #clean up unfinished links
358 #XXX: make this optional? wasn't there in autosummary, but required for deletion summary.
359 $truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
361 return $truncatedtext;
366 class MessageContent
extends TextContent
{
367 public function __construct( $msg_key, $params = null, $options = null ) {
368 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
);
370 $this->mMessageKey
= $msg_key;
372 $this->mParameters
= $params;
374 if ( !$options ) $options = array();
375 $this->mOptions
= $options;
377 $this->mHtmlOptions
= null;
381 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
383 protected function getHtml( ) {
384 $opt = array_merge( $this->mOptions
, array('parse') );
386 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
391 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
393 public function getNativeData( ) {
394 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
396 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
402 class JavaScriptContent
extends TextContent
{
403 public function __construct( $text ) {
404 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
407 protected function getHtml( ) {
409 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
410 $html .= htmlspecialchars( $this->getNativeData() );
411 $html .= "\n</pre>\n";
418 class CssContent
extends TextContent
{
419 public function __construct( $text ) {
420 parent
::__construct($text, CONTENT_MODEL_CSS
);
423 protected function getHtml( ) {
425 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
426 $html .= htmlspecialchars( $this->getNativeData() );
427 $html .= "\n</pre>\n";
433 #FUTURE: special type for redirects?!
434 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
435 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
436 #EXAMPLE: CoordinatesContent
437 #EXAMPLE: WikidataContent