$wgContentHandlerTextFallback
[lhc/web/wiklou.git] / includes / ContentHandler.php
1 <?php
2
3 /**
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).
7 *
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.
12 *
13 */
14 abstract class ContentHandler {
15
16 public static function getContentText( Content $content = null ) {
17 global $wgContentHandlerTextFallback;
18
19 if ( !$content ) return '';
20
21 if ( $content instanceof TextContent ) {
22 return $content->getNativeData();
23 }
24
25 if ( $wgContentHandlerTextFallback == 'fail' ) throw new MWException( "Attempt to get text from Content with model " . $content->getModelName() );
26 if ( $wgContentHandlerTextFallback == 'serialize' ) return $content->serialize();
27
28 return null;
29 }
30
31 public static function makeContent( $text, Title $title, $modelName = null, $format = null ) {
32 if ( !$modelName ) {
33 $modelName = $title->getContentModelName();
34 }
35
36 $handler = ContentHandler::getForModelName( $modelName );
37 return $handler->unserialize( $text, $format );
38 }
39
40 public static function getDefaultModelFor( Title $title ) {
41 global $wgNamespaceContentModels;
42
43 # NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
44 # because it is used to initialized the mContentModelName memebr.
45
46 $ns = $title->getNamespace();
47
48 $ext = false;
49 $m = null;
50 $model = null;
51
52 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
53 $model = $wgNamespaceContentModels[ $ns ];
54 }
55
56 # hook can determin default model
57 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
58 if ( $model ) return $model;
59 }
60
61 # Could this page contain custom CSS or JavaScript, based on the title?
62 $isCssOrJsPage = ( NS_MEDIAWIKI == $ns && preg_match( "!\.(css|js)$!u", $title->getText(), $m ) );
63 if ( $isCssOrJsPage ) $ext = $m[1];
64
65 # hook can force js/css
66 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage, &$ext ) ); #FIXME: add $ext to hook interface spec
67
68 # Is this a .css subpage of a user page?
69 $isJsCssSubpage = ( NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m ) );
70 if ( $isJsCssSubpage ) $ext = $m[1];
71
72 # is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
73 $isWikitext = ( $model == CONTENT_MODEL_WIKITEXT || $model === null );
74 $isWikitext = ( $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage );
75
76 # hook can override $isWikitext
77 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
78
79 if ( !$isWikitext ) {
80
81 if ( $ext == 'js' )
82 return CONTENT_MODEL_JAVASCRIPT;
83 else if ( $ext == 'css' )
84 return CONTENT_MODEL_CSS;
85
86 if ( $model )
87 return $model;
88 else
89 return CONTENT_MODEL_TEXT;
90 }
91
92 # we established that is must be wikitext
93 return CONTENT_MODEL_WIKITEXT;
94 }
95
96 public static function getForTitle( Title $title ) {
97 $modelName = $title->getContentModelName();
98 return ContentHandler::getForModelName( $modelName );
99 }
100
101 public static function getForContent( Content $content ) {
102 $modelName = $content->getModelName();
103 return ContentHandler::getForModelName( $modelName );
104 }
105
106 /**
107 * @static
108 * @param $modelName String the name of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
109 * @return ContentHandler
110 * @throws MWException
111 */
112 public static function getForModelName( $modelName ) {
113 global $wgContentHandlers;
114
115 if ( empty( $wgContentHandlers[$modelName] ) ) {
116 #FIXME: hook here!
117 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
118 }
119
120 if ( is_string( $wgContentHandlers[$modelName] ) ) {
121 $class = $wgContentHandlers[$modelName];
122 $wgContentHandlers[$modelName] = new $class( $modelName );
123 }
124
125 return $wgContentHandlers[$modelName];
126 }
127
128 # ----------------------------------------------------------------------------------------------------------
129 public function __construct( $modelName, $formats ) {
130 $this->mModelName = $modelName;
131 $this->mSupportedFormats = $formats;
132 }
133
134 public function getModelName() {
135 # for wikitext: wikitext; in the future: wikiast, wikidom?
136 # for wikidata: wikidata
137 return $this->mModelName;
138 }
139
140
141 public function getSupportedFormats() {
142 # for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
143 # for wikidata: "application/json", "application/x-php", etc
144 return $this->mSupportedFormats;
145 }
146
147 public function getDefaultFormat() {
148 return $this->mSupportedFormats[0];
149 }
150
151 /**
152 * @abstract
153 * @param Content $content
154 * @param null $format
155 * @return String
156 */
157 public abstract function serialize( Content $content, $format = null );
158
159 /**
160 * @abstract
161 * @param $blob String
162 * @param null $format
163 * @return Content
164 */
165 public abstract function unserialize( $blob, $format = null );
166
167 public abstract function emptyContent();
168
169 /**
170 * Return an Article object suitable for viewing the given object
171 *
172 * NOTE: does *not* do special handling for Image and Category pages!
173 * Use Article::newFromTitle() for that!
174 *
175 * @param type $title
176 * @return \Article
177 * @todo Article is being refactored into an action class, keep track of that
178 */
179 public function createArticle( Title $title ) {
180 #XXX: assert that $title->getContentModelName() == $this->getModelname()?
181 $article = new Article($title);
182 return $article;
183 }
184
185 /**
186 * Return an EditPage object suitable for editing the given object
187 *
188 * @param type $article
189 * @return \EditPage
190 */
191 public function createEditPage( Article $article ) {
192 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
193 $editPage = new EditPage( $article );
194 return $editPage;
195 }
196
197 /**
198 * Return an ExternalEdit object suitable for editing the given object
199 *
200 * @param type $article
201 * @return \ExternalEdit
202 */
203 public function createExternalEdit( IContextSource $context ) {
204 #XXX: assert that $article->getContentObject()->getModelName() == $this->getModelname()?
205 $externalEdit = new ExternalEdit( $context );
206 return $externalEdit;
207 }
208
209 /**
210 * Factory
211 * @param $context IContextSource context to use, anything else will be ignored
212 * @param $old Integer old ID we want to show and diff with.
213 * @param $new String either 'prev' or 'next'.
214 * @param $rcid Integer ??? FIXME (default 0)
215 * @param $refreshCache boolean If set, refreshes the diff cache
216 * @param $unhide boolean If set, allow viewing deleted revs
217 */
218 public function getDifferenceEngine( IContextSource $context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
219 $refreshCache = false, $unhide = false ) {
220
221 $de = new DifferenceEngine( $context, $old, $new, $rcid, $refreshCache, $unhide );
222
223 return $de;
224 }
225
226 /**
227 * attempts to merge differences between three versions.
228 * Returns a new Content object for a clean merge and false for failure or a conflict.
229 *
230 * This default implementation always returns false.
231 *
232 * @param $oldContent String
233 * @param $myContent String
234 * @param $yourContent String
235 * @return Content|Bool
236 */
237 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
238 return false;
239 }
240
241 /**
242 * Return an applicable autosummary if one exists for the given edit.
243 *
244 * @param $oldContent Content: the previous text of the page.
245 * @param $newContent Content: The submitted text of the page.
246 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
247 *
248 * @return string An appropriate autosummary, or an empty string.
249 */
250 public function getAutosummary( Content $oldContent, Content $newContent, $flags ) {
251 global $wgContLang;
252
253 # Decide what kind of autosummary is needed.
254
255 # Redirect autosummaries
256 $ot = $oldContent->getRedirectTarget();
257 $rt = $newContent->getRedirectTarget();
258
259 if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
260
261 $truncatedtext = $newContent->getTextForSummary(
262 250
263 - strlen( wfMsgForContent( 'autoredircomment' ) )
264 - strlen( $rt->getFullText() ) );
265
266 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
267 }
268
269 # New page autosummaries
270 if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
271 # If they're making a new article, give its text, truncated, in the summary.
272
273 $truncatedtext = $newContent->getTextForSummary(
274 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
275
276 return wfMsgForContent( 'autosumm-new', $truncatedtext );
277 }
278
279 # Blanking autosummaries
280 if ( $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
281 return wfMsgForContent( 'autosumm-blank' );
282 } elseif ( $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
283 # Removing more than 90% of the article
284
285 $truncatedtext = $newContent->getTextForSummary(
286 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
287
288 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
289 }
290
291 # If we reach this point, there's no applicable autosummary for our case, so our
292 # autosummary is empty.
293 return '';
294 }
295
296 /**
297 * Auto-generates a deletion reason
298 *
299 * @param $title Title: the page's title
300 * @param &$hasHistory Boolean: whether the page has a history
301 * @return mixed String containing deletion reason or empty string, or boolean false
302 * if no revision occurred
303 */
304 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
305 global $wgContLang;
306
307 $dbw = wfGetDB( DB_MASTER );
308
309 // Get the last revision
310 $rev = Revision::newFromTitle( $title );
311
312 if ( is_null( $rev ) ) {
313 return false;
314 }
315
316 // Get the article's contents
317 $content = $rev->getContent();
318 $blank = false;
319
320 // If the page is blank, use the text from the previous revision,
321 // which can only be blank if there's a move/import/protect dummy revision involved
322 if ( $content->getSize() == 0 ) {
323 $prev = $rev->getPrevious();
324
325 if ( $prev ) {
326 $content = $rev->getContent();
327 $blank = true;
328 }
329 }
330
331 // Find out if there was only one contributor
332 // Only scan the last 20 revisions
333 $res = $dbw->select( 'revision', 'rev_user_text',
334 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
335 __METHOD__,
336 array( 'LIMIT' => 20 )
337 );
338
339 if ( $res === false ) {
340 // This page has no revisions, which is very weird
341 return false;
342 }
343
344 $hasHistory = ( $res->numRows() > 1 );
345 $row = $dbw->fetchObject( $res );
346
347 if ( $row ) { // $row is false if the only contributor is hidden
348 $onlyAuthor = $row->rev_user_text;
349 // Try to find a second contributor
350 foreach ( $res as $row ) {
351 if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
352 $onlyAuthor = false;
353 break;
354 }
355 }
356 } else {
357 $onlyAuthor = false;
358 }
359
360 // Generate the summary with a '$1' placeholder
361 if ( $blank ) {
362 // The current revision is blank and the one before is also
363 // blank. It's just not our lucky day
364 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
365 } else {
366 if ( $onlyAuthor ) {
367 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
368 } else {
369 $reason = wfMsgForContent( 'excontent', '$1' );
370 }
371 }
372
373 if ( $reason == '-' ) {
374 // Allow these UI messages to be blanked out cleanly
375 return '';
376 }
377
378 // Max content length = max comment length - length of the comment (excl. $1)
379 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
380
381 // Now replace the '$1' placeholder
382 $reason = str_replace( '$1', $text, $reason );
383
384 return $reason;
385 }
386
387 /**
388 * Get the Content object that needs to be saved in order to undo all revisions
389 * between $undo and $undoafter. Revisions must belong to the same page,
390 * must exist and must not be deleted
391 * @param $undo Revision
392 * @param $undoafter null|Revision Must be an earlier revision than $undo
393 * @return mixed string on success, false on failure
394 */
395 public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter = null ) {
396 $cur_content = $current->getContent();
397
398 if ( empty( $cur_content ) ) {
399 return false; // no page
400 }
401
402 $undo_content = $undo->getContent();
403 $undoafter_content = $undoafter->getContent();
404
405 if ( $cur_content->equals( $undo_content ) ) {
406 # No use doing a merge if it's just a straight revert.
407 return $undoafter_content;
408 }
409
410 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
411
412 return $undone_content;
413 }
414
415 #TODO: how to handle extra message for JS/CSS previews??
416 #TODO: Article::showCssOrJsPage ---> specialized classes!
417
418 #XXX: ImagePage and CategoryPage... wrappers that use ContentHandler? or ContentHandler creates wrappers?
419 }
420
421
422 abstract class TextContentHandler extends ContentHandler {
423
424 public function __construct( $modelName, $formats ) {
425 parent::__construct( $modelName, $formats );
426 }
427
428 public function serialize( Content $content, $format = null ) {
429 #FIXME: assert format
430 return $content->getNativeData();
431 }
432
433 /**
434 * attempts to merge differences between three versions.
435 * Returns a new Content object for a clean merge and false for failure or a conflict.
436 *
437 * This text-based implementation uses wfMerge().
438 *
439 * @param $oldContent String
440 * @param $myContent String
441 * @param $yourContent String
442 * @return Content|Bool
443 */
444 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
445 $format = $this->getDefaultFormat();
446
447 $old = $this->serialize( $oldContent, $format );
448 $mine = $this->serialize( $myContent, $format );
449 $yours = $this->serialize( $yourContent, $format );
450
451 $ok = wfMerge( $old, $mine, $yours, $result );
452
453 if ( !$ok ) return false;
454 if ( !$result ) return $this->emptyContent();
455
456 $mergedContent = $this->unserialize( $result, $format );
457 return $mergedContent;
458 }
459
460
461 }
462 class WikitextContentHandler extends TextContentHandler {
463
464 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
465 parent::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
466 }
467
468 public function unserialize( $text, $format = null ) {
469 #FIXME: assert format
470 return new WikitextContent($text);
471 }
472
473 public function emptyContent() {
474 return new WikitextContent("");
475 }
476
477
478 }
479
480 class JavaScriptContentHandler extends TextContentHandler {
481
482 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
483 parent::__construct( $modelName, array( 'text/javascript' ) );
484 }
485
486 public function unserialize( $text, $format = null ) {
487 return new JavaScriptContent($text);
488 }
489
490 public function emptyContent() {
491 return new JavaScriptContent("");
492 }
493 }
494
495 class CssContentHandler extends TextContentHandler {
496
497 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT ) {
498 parent::__construct( $modelName, array( 'text/css' ) );
499 }
500
501 public function unserialize( $text, $format = null ) {
502 return new CssContent($text);
503 }
504
505 public function emptyContent() {
506 return new CssContent("");
507 }
508
509 }