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