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 getSearchText( );
21 public abstract function getWikitextForTransclusion( );
24 * Returns native represenation of the data. Interpretation depends on the data model used,
25 * as given by getDataModel().
28 public abstract function getRawData( );
30 public abstract function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = NULL );
32 public function getRedirectChain() {
37 * Returns the section with the given id.
39 * The default implementation returns null.
41 * @param String $sectionId the section's id
42 * @return Content|Boolean|null the section, or false if no such section exist, or null if sections are not supported
44 public function getSection( $sectionId ) {
49 * Replaces a section of the content.
51 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
52 * @param $with Content: new content of the section
53 * @param $sectionTitle String: new section's subject, only if $section is 'new'
54 * @return string Complete article text, or null if error
56 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
60 #TODO: implement specialized ParserOutput for Wikidata model
61 #TODO: provide addToParserOutput fule Multipart... somehow.
63 # TODO: EditPage::mergeChanges( Content $a, Content $b )
64 # TODO: Wikipage::isCountable(Content $a)
65 # TODO: Title::newFromRedirectRecurse( $this->getRawText() );
67 # TODO: isCacheable( )
70 # TODO: WikiPage::getUndoText( Revision $undo, Revision $undoafter = null )
71 # TODO: WikiPage::getAutosummary( $oldtext, $text, $flags )
73 # TODO: EditPage::getPreloadedText( $preload ) // $wgParser->getPreloadText
76 # TODO: tie into API to provide contentModel for Revisions
77 # TODO: tie into API to provide serialized version and contentFormat for Revisions
78 # TODO: tie into API edit interface
83 * Content object implementation for representing flat text. The
85 abstract class TextContent
extends Content
{
86 public function __construct( $text, $modelName = null ) {
87 parent
::__construct($modelName);
93 * Returns the text represented by this Content object, as a string.
95 * @return String the raw text
97 public function getRawData( ) {
103 * Returns the text represented by this Content object, as a string.
105 * @return String the raw text
107 public function getSearchText( ) { #FIXME: use!
108 return $this->getRawData();
112 * Returns the text represented by this Content object, as a string.
114 * @return String the raw text
116 public function getWikitextForTransclusion( ) { #FIXME: use!
117 return $this->getRawData();
121 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
123 * @return ParserOutput representing the HTML form of the text
125 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
126 # generic implementation, relying on $this->getHtml()
128 $html = $this->getHtml( $options );
129 $po = new ParserOutput( $html );
131 if ( $this->mTitle
) $po->setTitleText( $this->mTitle
->getText() );
133 #TODO: cache settings, etc?
138 protected abstract function getHtml( );
142 class WikitextContent
extends TextContent
{
143 public function __construct( $text ) {
144 parent
::__construct($text, CONTENT_MODEL_WIKITEXT
);
146 $this->mDefaultParserOptions
= null; #TODO: use per-class static member?!
149 protected function getHtml( ) {
150 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
153 public function getDefaultParserOptions() {
154 global $wgUser, $wgContLang;
156 if ( !$this->mDefaultParserOptions
) { #TODO: use per-class static member?!
157 $this->mDefaultParserOptions
= ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
160 return $this->mDefaultParserOptions
;
164 * Returns a ParserOutput object reesulting from parsing the content's text using $wgParser
166 * @return ParserOutput representing the HTML form of the text
168 public function getParserOutput( Title
$title = null, $revId = null, ParserOptions
$options = null ) {
172 $options = $this->getDefaultParserOptions();
175 $po = $wgParser->parse( $this->mText
, $this->getTitle(), $options, true, true, $this->mRevId
);
181 * Returns the section with the given id.
183 * @param String $sectionId the section's id
184 * @return Content|false|null the section, or false if no such section exist, or null if sections are not supported
186 public function getSection( $section ) {
189 $text = $this->getRawData();
190 $sect = $wgParser->getSection( $text, $section, false );
192 return new WikitextContent( $sect );
196 * Replaces a section in the wikitext
198 * @param $section empty/null/false or a section number (0, 1, 2, T1, T2...), or "new"
199 * @param $with Content: new content of the section
200 * @param $sectionTitle String: new section's subject, only if $section is 'new'
201 * @return string Complete article text, or null if error
203 public function replaceSection( $section, Content
$with, $sectionTitle = '' ) {
206 wfProfileIn( __METHOD__
);
208 $myModelName = $this->getModelName();
209 $sectionModelName = $with->getModelName();
211 if ( $sectionModelName != $myModelName ) {
212 throw new MWException( "Incompatible content model for section: document uses $myModelName, section uses $sectionModelName." );
215 $oldtext = $this->getRawData();
216 $text = $with->getRawData();
218 if ( $section == 'new' ) {
219 # Inserting a new section
220 $subject = $sectionTitle ?
wfMsgForContent( 'newsectionheaderdefaultlevel', $sectionTitle ) . "\n\n" : '';
221 if ( wfRunHooks( 'PlaceNewSection', array( $this, $oldtext, $subject, &$text ) ) ) {
222 $text = strlen( trim( $oldtext ) ) > 0
223 ?
"{$oldtext}\n\n{$subject}{$text}"
224 : "{$subject}{$text}";
227 # Replacing an existing section; roll out the big guns
230 $text = $wgParser->replaceSection( $oldtext, $section, $text );
233 $newContent = new WikitextContent( $text );
235 wfProfileOut( __METHOD__
);
239 public function getRedirectChain() {
240 $text = $this->getRawData();
241 return Title
::newFromRedirectArray( $text );
246 class MessageContent
extends TextContent
{
247 public function __construct( $msg_key, $params = null, $options = null ) {
248 parent
::__construct(null, CONTENT_MODEL_WIKITEXT
);
250 $this->mMessageKey
= $msg_key;
252 $this->mParameters
= $params;
254 if ( !$options ) $options = array();
255 $this->mOptions
= $options;
257 $this->mHtmlOptions
= null;
261 * Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
263 protected function getHtml( ) {
264 $opt = array_merge( $this->mOptions
, array('parse') );
266 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
271 * Returns the message as raw text, using the options supplied to the constructor minus "parse" and "parseinline".
273 public function getRawData( ) {
274 $opt = array_diff( $this->mOptions
, array('parse', 'parseinline') );
276 return wfMsgExt( $this->mMessageKey
, $this->mParameters
, $opt );
282 class JavaScriptContent
extends TextContent
{
283 public function __construct( $text ) {
284 parent
::__construct($text, CONTENT_MODEL_JAVASCRIPT
);
287 protected function getHtml( ) {
289 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
290 $html .= htmlspecialchars( $this->getRawData() );
291 $html .= "\n</pre>\n";
298 class CssContent
extends TextContent
{
299 public function __construct( $text ) {
300 parent
::__construct($text, CONTENT_MODEL_CSS
);
303 protected function getHtml( ) {
305 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
306 $html .= htmlspecialchars( $this->getRawData() );
307 $html .= "\n</pre>\n";
313 #FUTURE: special type for redirects?!
314 #FUTURE: MultipartMultipart < WikipageContent (Main + Links + X)
315 #FUTURE: LinksContent < LanguageLinksContent, CategoriesContent
316 #EXAMPLE: CoordinatesContent
317 #EXAMPLE: WikidataContent