4 * A content handler knows how do deal with a specific type of content on a wiki page.
5 * Content is stored in the database in a serialized form (using a serialization format aka mime type)
6 * and is be unserialized into it's native PHP represenation (the content model).
8 * Some content types have a flat model, that is, their native represenation is the
9 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
10 * this also applies to wikitext (mediawiki's default content type), but wikitext
11 * content may be represented by a DOM or AST structure in the future.
14 abstract class ContentHandler
{
16 public static function getContentText( Content
$content = null ) {
17 if ( !$content ) return '';
19 if ( $content instanceof TextContent
) {
20 #XXX: or check by model name?
21 #XXX: or define $content->allowRawData()?
22 #XXX: or define $content->getDefaultWikiText()?
23 return $content->getNativeData();
26 #XXX: this must not be used for editing, otherwise we may loose data:
27 #XXX: e.g. if this returns the "main" text from a multipart page, all attachments would be lost
29 #TODO: log this incident!
33 public static function makeContent( $text, Title
$title, $modelName = null, $format = null ) {
35 $modelName = $title->getContentModelName();
38 $handler = ContentHandler
::getForModelName( $modelName );
39 return $handler->unserialize( $text, $format );
42 public static function getDefaultModelFor( Title
$title ) {
43 global $wgNamespaceContentModels;
45 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
46 # because it is used to initialized the mContentModelName memebr.
48 $ns = $title->getNamespace();
54 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
55 $model = $wgNamespaceContentModels[ $ns ];
58 # hook can determin default model
59 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
60 if ( $model ) return $model;
63 # Could this page contain custom CSS or JavaScript, based on the title?
64 $isCssOrJsPage = ( NS_MEDIAWIKI
== $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
65 if ( $isCssOrJsPage ) $ext = $m[1];
67 # hook can force js/css
68 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
70 # Is this a .css subpage of a user page?
71 $isJsCssSubpage = ( NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
72 if ( $isJsCssSubpage ) $ext = $m[1];
74 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
75 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT ||
$model === null );
76 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
78 # hook can override $isWikitext
79 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
84 return CONTENT_MODEL_JAVASCRIPT
;
85 else if ( $ext == 'css' )
86 return CONTENT_MODEL_CSS
;
91 return CONTENT_MODEL_TEXT
;
94 # we established that is must be wikitext
95 return CONTENT_MODEL_WIKITEXT
;
98 public static function getForTitle( Title
$title ) {
99 $modelName = $title->getContentModelName();
100 return ContentHandler
::getForModelName( $modelName );
103 public static function getForContent( Content
$content ) {
104 $modelName = $content->getModelName();
105 return ContentHandler
::getForModelName( $modelName );
110 * @param $modelName String the name of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
111 * @return ContentHandler
112 * @throws MWException
114 public static function getForModelName( $modelName ) {
115 global $wgContentHandlers;
117 if ( empty( $wgContentHandlers[$modelName] ) ) {
119 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
122 if ( is_string( $wgContentHandlers[$modelName] ) ) {
123 $class = $wgContentHandlers[$modelName];
124 $wgContentHandlers[$modelName] = new $class( $modelName );
127 return $wgContentHandlers[$modelName];
130 # ----------------------------------------------------------------------------------------------------------
131 public function __construct( $modelName, $formats ) {
132 $this->mModelName
= $modelName;
133 $this->mSupportedFormats
= $formats;
136 public function getModelName() {
137 # for wikitext: wikitext; in the future: wikiast, wikidom?
138 # for wikidata: wikidata
139 return $this->mModelName
;
143 public function getSupportedFormats() {
144 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
145 # for wikidata: "application/json", "application/x-php", etc
146 return $this->mSupportedFormats
;
149 public function getDefaultFormat() {
150 return $this->mSupportedFormats
[0];
155 * @param Content $content
156 * @param null $format
159 public abstract function serialize( Content
$content, $format = null );
163 * @param $blob String
164 * @param null $format
167 public abstract function unserialize( $blob, $format = null );
169 public abstract function emptyContent();
172 * Return an Article object suitable for viewing the given object
174 * NOTE: does *not* do special handling for Image and Category pages!
175 * Use Article::newFromTitle() for that!
179 * @todo Article is being refactored into an action class, keep track of that
181 public function createArticle( Title
$title ) {
182 #XXX: assert that $title->getContentModelName() == $this->getModelname()?
183 $article = new Article($title);
188 * Return an EditPage object suitable for editing the given object
190 * @param type $article
193 public function createEditPage( Article
$article ) {
194 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
195 $editPage = new EditPage( $article );
200 * Return an ExternalEdit object suitable for editing the given object
202 * @param type $article
203 * @return \ExternalEdit
205 public function createExternalEdit( IContextSource
$context ) {
206 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
207 $externalEdit = new ExternalEdit( $context );
208 return $externalEdit;
212 public function updatePage( $title, $obj ) {
216 public function getDiffEngine( Article
$article ) {
217 $de = new DifferenceEngine( $article->getContext() );
222 * attempts to merge differences between three versions.
223 * Returns a new Content object for a clean merge and false for failure or a conflict.
225 * This default implementation always returns false.
227 * @param $oldContent String
228 * @param $myContent String
229 * @param $yourContent String
230 * @return Content|Bool
232 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
237 * Return an applicable autosummary if one exists for the given edit.
239 * @param $oldContent Content: the previous text of the page.
240 * @param $newContent Content: The submitted text of the page.
241 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
243 * @return string An appropriate autosummary, or an empty string.
245 public function getAutosummary( Content
$oldContent, Content
$newContent, $flags ) {
248 # Decide what kind of autosummary is needed.
250 # Redirect autosummaries
251 $ot = $oldContent->getRedirectTarget();
252 $rt = $newContent->getRedirectTarget();
254 if ( is_object( $rt ) && ( !is_object( $ot ) ||
!$rt->equals( $ot ) ||
$ot->getFragment() != $rt->getFragment() ) ) {
256 $truncatedtext = $newContent->getTextForSummary(
258 - strlen( wfMsgForContent( 'autoredircomment' ) )
259 - strlen( $rt->getFullText() ) );
261 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
264 # New page autosummaries
265 if ( $flags & EDIT_NEW
&& $newContent->getSize() > 0 ) {
266 # If they're making a new article, give its text, truncated, in the summary.
268 $truncatedtext = $newContent->getTextForSummary(
269 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
271 return wfMsgForContent( 'autosumm-new', $truncatedtext );
274 # Blanking autosummaries
275 if ( $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
276 return wfMsgForContent( 'autosumm-blank' );
277 } elseif ( $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
278 # Removing more than 90% of the article
280 $truncatedtext = $newContent->getTextForSummary(
281 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
283 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
286 # If we reach this point, there's no applicable autosummary for our case, so our
287 # autosummary is empty.
292 * Auto-generates a deletion reason
294 * @param $title Title: the page's title
295 * @param &$hasHistory Boolean: whether the page has a history
296 * @return mixed String containing deletion reason or empty string, or boolean false
297 * if no revision occurred
299 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
302 $dbw = wfGetDB( DB_MASTER
);
304 // Get the last revision
305 $rev = Revision
::newFromTitle( $title );
307 if ( is_null( $rev ) ) {
311 // Get the article's contents
312 $content = $rev->getContent();
315 // If the page is blank, use the text from the previous revision,
316 // which can only be blank if there's a move/import/protect dummy revision involved
317 if ( $content->getSize() == 0 ) {
318 $prev = $rev->getPrevious();
321 $content = $rev->getContent();
326 // Find out if there was only one contributor
327 // Only scan the last 20 revisions
328 $res = $dbw->select( 'revision', 'rev_user_text',
329 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' ),
331 array( 'LIMIT' => 20 )
334 if ( $res === false ) {
335 // This page has no revisions, which is very weird
339 $hasHistory = ( $res->numRows() > 1 );
340 $row = $dbw->fetchObject( $res );
342 if ( $row ) { // $row is false if the only contributor is hidden
343 $onlyAuthor = $row->rev_user_text
;
344 // Try to find a second contributor
345 foreach ( $res as $row ) {
346 if ( $row->rev_user_text
!= $onlyAuthor ) { // Bug 22999
355 // Generate the summary with a '$1' placeholder
357 // The current revision is blank and the one before is also
358 // blank. It's just not our lucky day
359 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
362 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
364 $reason = wfMsgForContent( 'excontent', '$1' );
368 if ( $reason == '-' ) {
369 // Allow these UI messages to be blanked out cleanly
373 // Max content length = max comment length - length of the comment (excl. $1)
374 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
376 // Now replace the '$1' placeholder
377 $reason = str_replace( '$1', $text, $reason );
383 * Get the Content object that needs to be saved in order to undo all revisions
384 * between $undo and $undoafter. Revisions must belong to the same page,
385 * must exist and must not be deleted
386 * @param $undo Revision
387 * @param $undoafter null|Revision Must be an earlier revision than $undo
388 * @return mixed string on success, false on failure
390 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter = null ) {
391 $cur_content = $current->getContent();
393 if ( empty( $cur_content ) ) {
394 return false; // no page
397 $undo_content = $undo->getContent();
398 $undoafter_content = $undoafter->getContent();
400 if ( $cur_content->equals( $undo_content ) ) {
401 # No use doing a merge if it's just a straight revert.
402 return $undoafter_content;
405 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
407 return $undone_content;
410 #TODO: how to handle extra message for JS/CSS previews??
411 #TODO: Article::showCssOrJsPage ---> specialized classes!
413 #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
417 abstract class TextContentHandler
extends ContentHandler
{
419 public function __construct( $modelName, $formats ) {
420 parent
::__construct( $modelName, $formats );
423 public function serialize( Content
$content, $format = null ) {
424 #FIXME: assert format
425 return $content->getNativeData();
429 * attempts to merge differences between three versions.
430 * Returns a new Content object for a clean merge and false for failure or a conflict.
432 * This text-based implementation uses wfMerge().
434 * @param $oldContent String
435 * @param $myContent String
436 * @param $yourContent String
437 * @return Content|Bool
439 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
440 $format = $this->getDefaultFormat();
442 $old = $this->serialize( $oldContent, $format );
443 $mine = $this->serialize( $myContent, $format );
444 $yours = $this->serialize( $yourContent, $format );
446 $ok = wfMerge( $old, $mine, $yours, $result );
448 if ( !$ok ) return false;
449 if ( !$result ) return $this->emptyContent();
451 $mergedContent = $this->unserialize( $result, $format );
452 return $mergedContent;
457 class WikitextContentHandler
extends TextContentHandler
{
459 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
460 parent
::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
463 public function unserialize( $text, $format = null ) {
464 #FIXME: assert format
465 return new WikitextContent($text);
468 public function emptyContent() {
469 return new WikitextContent("");
475 class JavaScriptContentHandler
extends TextContentHandler
{
477 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
478 parent
::__construct( $modelName, array( 'text/javascript' ) );
481 public function unserialize( $text, $format = null ) {
482 return new JavaScriptContent($text);
485 public function emptyContent() {
486 return new JavaScriptContent("");
490 class CssContentHandler
extends TextContentHandler
{
492 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
493 parent
::__construct( $modelName, array( 'text/css' ) );
496 public function unserialize( $text, $format = null ) {
497 return new CssContent($text);
500 public function emptyContent() {
501 return new CssContent("");