Merge "Fix and re-enable Dumps' checkpoint tests"
[lhc/web/wiklou.git] / includes / content / ContentHandler.php
1 <?php
2 /**
3 * Base class for content handling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @since 1.21
21 *
22 * @file
23 * @ingroup Content
24 *
25 * @author Daniel Kinzler
26 */
27
28 /**
29 * Exception representing a failure to serialize or unserialize a content object.
30 *
31 * @ingroup Content
32 */
33 class MWContentSerializationException extends MWException {
34 }
35
36 /**
37 * A content handler knows how do deal with a specific type of content on a wiki
38 * page. Content is stored in the database in a serialized form (using a
39 * serialization format a.k.a. MIME type) and is unserialized into its native
40 * PHP representation (the content model), which is wrapped in an instance of
41 * the appropriate subclass of Content.
42 *
43 * ContentHandler instances are stateless singletons that serve, among other
44 * things, as a factory for Content objects. Generally, there is one subclass
45 * of ContentHandler and one subclass of Content for every type of content model.
46 *
47 * Some content types have a flat model, that is, their native representation
48 * is the same as their serialized form. Examples would be JavaScript and CSS
49 * code. As of now, this also applies to wikitext (MediaWiki's default content
50 * type), but wikitext content may be represented by a DOM or AST structure in
51 * the future.
52 *
53 * @ingroup Content
54 */
55 abstract class ContentHandler {
56 /**
57 * Switch for enabling deprecation warnings. Used by ContentHandler::deprecated()
58 * and ContentHandler::runLegacyHooks().
59 *
60 * Once the ContentHandler code has settled in a bit, this should be set to true to
61 * make extensions etc. show warnings when using deprecated functions and hooks.
62 */
63 protected static $enableDeprecationWarnings = false;
64
65 /**
66 * Convenience function for getting flat text from a Content object. This
67 * should only be used in the context of backwards compatibility with code
68 * that is not yet able to handle Content objects!
69 *
70 * If $content is null, this method returns the empty string.
71 *
72 * If $content is an instance of TextContent, this method returns the flat
73 * text as returned by $content->getNativeData().
74 *
75 * If $content is not a TextContent object, the behavior of this method
76 * depends on the global $wgContentHandlerTextFallback:
77 * - If $wgContentHandlerTextFallback is 'fail' and $content is not a
78 * TextContent object, an MWException is thrown.
79 * - If $wgContentHandlerTextFallback is 'serialize' and $content is not a
80 * TextContent object, $content->serialize() is called to get a string
81 * form of the content.
82 * - If $wgContentHandlerTextFallback is 'ignore' and $content is not a
83 * TextContent object, this method returns null.
84 * - otherwise, the behavior is undefined.
85 *
86 * @since 1.21
87 *
88 * @param Content $content
89 *
90 * @throws MWException If the content is not an instance of TextContent and
91 * wgContentHandlerTextFallback was set to 'fail'.
92 * @return string|null Textual form of the content, if available.
93 */
94 public static function getContentText( Content $content = null ) {
95 global $wgContentHandlerTextFallback;
96
97 if ( is_null( $content ) ) {
98 return '';
99 }
100
101 if ( $content instanceof TextContent ) {
102 return $content->getNativeData();
103 }
104
105 wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
106
107 if ( $wgContentHandlerTextFallback == 'fail' ) {
108 throw new MWException(
109 "Attempt to get text from Content with model " .
110 $content->getModel()
111 );
112 }
113
114 if ( $wgContentHandlerTextFallback == 'serialize' ) {
115 return $content->serialize();
116 }
117
118 return null;
119 }
120
121 /**
122 * Convenience function for creating a Content object from a given textual
123 * representation.
124 *
125 * $text will be deserialized into a Content object of the model specified
126 * by $modelId (or, if that is not given, $title->getContentModel()) using
127 * the given format.
128 *
129 * @since 1.21
130 *
131 * @param string $text The textual representation, will be
132 * unserialized to create the Content object
133 * @param Title $title The title of the page this text belongs to.
134 * Required if $modelId is not provided.
135 * @param string $modelId The model to deserialize to. If not provided,
136 * $title->getContentModel() is used.
137 * @param string $format The format to use for deserialization. If not
138 * given, the model's default format is used.
139 *
140 * @throws MWException If model ID or format is not supported or if the text can not be
141 * unserialized using the format.
142 * @return Content A Content object representing the text.
143 */
144 public static function makeContent( $text, Title $title = null,
145 $modelId = null, $format = null ) {
146 if ( is_null( $modelId ) ) {
147 if ( is_null( $title ) ) {
148 throw new MWException( "Must provide a Title object or a content model ID." );
149 }
150
151 $modelId = $title->getContentModel();
152 }
153
154 $handler = ContentHandler::getForModelID( $modelId );
155
156 return $handler->unserializeContent( $text, $format );
157 }
158
159 /**
160 * Returns the name of the default content model to be used for the page
161 * with the given title.
162 *
163 * Note: There should rarely be need to call this method directly.
164 * To determine the actual content model for a given page, use
165 * Title::getContentModel().
166 *
167 * Which model is to be used by default for the page is determined based
168 * on several factors:
169 * - The global setting $wgNamespaceContentModels specifies a content model
170 * per namespace.
171 * - The hook ContentHandlerDefaultModelFor may be used to override the page's default
172 * model.
173 * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript
174 * model if they end in .js or .css, respectively.
175 * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
176 * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS
177 * or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor
178 * hook should be used instead if possible.
179 * - The hook TitleIsWikitextPage may be used to force a page to use the
180 * wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor
181 * hook should be used instead if possible.
182 *
183 * If none of the above applies, the wikitext model is used.
184 *
185 * Note: this is used by, and may thus not use, Title::getContentModel()
186 *
187 * @since 1.21
188 *
189 * @param Title $title
190 *
191 * @return string Default model name for the page given by $title
192 */
193 public static function getDefaultModelFor( Title $title ) {
194 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
195 // because it is used to initialize the mContentModel member.
196
197 $ns = $title->getNamespace();
198
199 $ext = false;
200 $m = null;
201 $model = MWNamespace::getNamespaceContentModel( $ns );
202
203 // Hook can determine default model
204 if ( !Hooks::run( 'ContentHandlerDefaultModelFor', array( $title, &$model ) ) ) {
205 if ( !is_null( $model ) ) {
206 return $model;
207 }
208 }
209
210 // Could this page contain custom CSS or JavaScript, based on the title?
211 $isCssOrJsPage = NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
212 if ( $isCssOrJsPage ) {
213 $ext = $m[1];
214 }
215
216 // Hook can force JS/CSS
217 Hooks::run( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ), '1.25' );
218
219 // Is this a .css subpage of a user page?
220 $isJsCssSubpage = NS_USER == $ns
221 && !$isCssOrJsPage
222 && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
223 if ( $isJsCssSubpage ) {
224 $ext = $m[1];
225 }
226
227 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
228 $isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
229 $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
230
231 // Hook can override $isWikitext
232 Hooks::run( 'TitleIsWikitextPage', array( $title, &$isWikitext ), '1.25' );
233
234 if ( !$isWikitext ) {
235 switch ( $ext ) {
236 case 'js':
237 return CONTENT_MODEL_JAVASCRIPT;
238 case 'css':
239 return CONTENT_MODEL_CSS;
240 default:
241 return is_null( $model ) ? CONTENT_MODEL_TEXT : $model;
242 }
243 }
244
245 // We established that it must be wikitext
246
247 return CONTENT_MODEL_WIKITEXT;
248 }
249
250 /**
251 * Returns the appropriate ContentHandler singleton for the given title.
252 *
253 * @since 1.21
254 *
255 * @param Title $title
256 *
257 * @return ContentHandler
258 */
259 public static function getForTitle( Title $title ) {
260 $modelId = $title->getContentModel();
261
262 return ContentHandler::getForModelID( $modelId );
263 }
264
265 /**
266 * Returns the appropriate ContentHandler singleton for the given Content
267 * object.
268 *
269 * @since 1.21
270 *
271 * @param Content $content
272 *
273 * @return ContentHandler
274 */
275 public static function getForContent( Content $content ) {
276 $modelId = $content->getModel();
277
278 return ContentHandler::getForModelID( $modelId );
279 }
280
281 /**
282 * @var array A Cache of ContentHandler instances by model id
283 */
284 protected static $handlers;
285
286 /**
287 * Returns the ContentHandler singleton for the given model ID. Use the
288 * CONTENT_MODEL_XXX constants to identify the desired content model.
289 *
290 * ContentHandler singletons are taken from the global $wgContentHandlers
291 * array. Keys in that array are model names, the values are either
292 * ContentHandler singleton objects, or strings specifying the appropriate
293 * subclass of ContentHandler.
294 *
295 * If a class name is encountered when looking up the singleton for a given
296 * model name, the class is instantiated and the class name is replaced by
297 * the resulting singleton in $wgContentHandlers.
298 *
299 * If no ContentHandler is defined for the desired $modelId, the
300 * ContentHandler may be provided by the ContentHandlerForModelID hook.
301 * If no ContentHandler can be determined, an MWException is raised.
302 *
303 * @since 1.21
304 *
305 * @param string $modelId The ID of the content model for which to get a
306 * handler. Use CONTENT_MODEL_XXX constants.
307 *
308 * @throws MWException If no handler is known for the model ID.
309 * @return ContentHandler The ContentHandler singleton for handling the model given by the ID.
310 */
311 public static function getForModelID( $modelId ) {
312 global $wgContentHandlers;
313
314 if ( isset( ContentHandler::$handlers[$modelId] ) ) {
315 return ContentHandler::$handlers[$modelId];
316 }
317
318 if ( empty( $wgContentHandlers[$modelId] ) ) {
319 $handler = null;
320
321 Hooks::run( 'ContentHandlerForModelID', array( $modelId, &$handler ) );
322
323 if ( $handler === null ) {
324 throw new MWException( "No handler for model '$modelId' registered in \$wgContentHandlers" );
325 }
326
327 if ( !( $handler instanceof ContentHandler ) ) {
328 throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
329 }
330 } else {
331 $class = $wgContentHandlers[$modelId];
332 $handler = new $class( $modelId );
333
334 if ( !( $handler instanceof ContentHandler ) ) {
335 throw new MWException( "$class from \$wgContentHandlers is not " .
336 "compatible with ContentHandler" );
337 }
338 }
339
340 wfDebugLog( 'ContentHandler', 'Created handler for ' . $modelId
341 . ': ' . get_class( $handler ) );
342
343 ContentHandler::$handlers[$modelId] = $handler;
344
345 return ContentHandler::$handlers[$modelId];
346 }
347
348 /**
349 * Returns the localized name for a given content model.
350 *
351 * Model names are localized using system messages. Message keys
352 * have the form content-model-$name, where $name is getContentModelName( $id ).
353 *
354 * @param string $name The content model ID, as given by a CONTENT_MODEL_XXX
355 * constant or returned by Revision::getContentModel().
356 *
357 * @throws MWException If the model ID isn't known.
358 * @return string The content model's localized name.
359 */
360 public static function getLocalizedName( $name ) {
361 // Messages: content-model-wikitext, content-model-text,
362 // content-model-javascript, content-model-css
363 $key = "content-model-$name";
364
365 $msg = wfMessage( $key );
366
367 return $msg->exists() ? $msg->plain() : $name;
368 }
369
370 public static function getContentModels() {
371 global $wgContentHandlers;
372
373 return array_keys( $wgContentHandlers );
374 }
375
376 public static function getAllContentFormats() {
377 global $wgContentHandlers;
378
379 $formats = array();
380
381 foreach ( $wgContentHandlers as $model => $class ) {
382 $handler = ContentHandler::getForModelID( $model );
383 $formats = array_merge( $formats, $handler->getSupportedFormats() );
384 }
385
386 $formats = array_unique( $formats );
387
388 return $formats;
389 }
390
391 // ------------------------------------------------------------------------
392
393 /**
394 * @var string
395 */
396 protected $mModelID;
397
398 /**
399 * @var string[]
400 */
401 protected $mSupportedFormats;
402
403 /**
404 * Constructor, initializing the ContentHandler instance with its model ID
405 * and a list of supported formats. Values for the parameters are typically
406 * provided as literals by subclass's constructors.
407 *
408 * @param string $modelId (use CONTENT_MODEL_XXX constants).
409 * @param string[] $formats List for supported serialization formats
410 * (typically as MIME types)
411 */
412 public function __construct( $modelId, $formats ) {
413 $this->mModelID = $modelId;
414 $this->mSupportedFormats = $formats;
415
416 $this->mModelName = preg_replace( '/(Content)?Handler$/', '', get_class( $this ) );
417 $this->mModelName = preg_replace( '/[_\\\\]/', '', $this->mModelName );
418 $this->mModelName = strtolower( $this->mModelName );
419 }
420
421 /**
422 * Serializes a Content object of the type supported by this ContentHandler.
423 *
424 * @since 1.21
425 *
426 * @param Content $content The Content object to serialize
427 * @param string $format The desired serialization format
428 *
429 * @return string Serialized form of the content
430 */
431 abstract public function serializeContent( Content $content, $format = null );
432
433 /**
434 * Applies transformations on export (returns the blob unchanged per default).
435 * Subclasses may override this to perform transformations such as conversion
436 * of legacy formats or filtering of internal meta-data.
437 *
438 * @param string $blob The blob to be exported
439 * @param string|null $format The blob's serialization format
440 *
441 * @return string
442 */
443 public function exportTransform( $blob, $format = null ) {
444 return $blob;
445 }
446
447 /**
448 * Unserializes a Content object of the type supported by this ContentHandler.
449 *
450 * @since 1.21
451 *
452 * @param string $blob Serialized form of the content
453 * @param string $format The format used for serialization
454 *
455 * @return Content The Content object created by deserializing $blob
456 */
457 abstract public function unserializeContent( $blob, $format = null );
458
459 /**
460 * Apply import transformation (per default, returns $blob unchanged).
461 * This gives subclasses an opportunity to transform data blobs on import.
462 *
463 * @since 1.24
464 *
465 * @param string $blob
466 * @param string|null $format
467 *
468 * @return string
469 */
470 public function importTransform( $blob, $format = null ) {
471 return $blob;
472 }
473
474 /**
475 * Creates an empty Content object of the type supported by this
476 * ContentHandler.
477 *
478 * @since 1.21
479 *
480 * @return Content
481 */
482 abstract public function makeEmptyContent();
483
484 /**
485 * Creates a new Content object that acts as a redirect to the given page,
486 * or null if redirects are not supported by this content model.
487 *
488 * This default implementation always returns null. Subclasses supporting redirects
489 * must override this method.
490 *
491 * Note that subclasses that override this method to return a Content object
492 * should also override supportsRedirects() to return true.
493 *
494 * @since 1.21
495 *
496 * @param Title $destination The page to redirect to.
497 * @param string $text Text to include in the redirect, if possible.
498 *
499 * @return Content Always null.
500 */
501 public function makeRedirectContent( Title $destination, $text = '' ) {
502 return null;
503 }
504
505 /**
506 * Returns the model id that identifies the content model this
507 * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
508 *
509 * @since 1.21
510 *
511 * @return string The model ID
512 */
513 public function getModelID() {
514 return $this->mModelID;
515 }
516
517 /**
518 * @since 1.21
519 *
520 * @param string $model_id The model to check
521 *
522 * @throws MWException If the model ID is not the ID of the content model supported by this
523 * ContentHandler.
524 */
525 protected function checkModelID( $model_id ) {
526 if ( $model_id !== $this->mModelID ) {
527 throw new MWException( "Bad content model: " .
528 "expected {$this->mModelID} " .
529 "but got $model_id." );
530 }
531 }
532
533 /**
534 * Returns a list of serialization formats supported by the
535 * serializeContent() and unserializeContent() methods of this
536 * ContentHandler.
537 *
538 * @since 1.21
539 *
540 * @return string[] List of serialization formats as MIME type like strings
541 */
542 public function getSupportedFormats() {
543 return $this->mSupportedFormats;
544 }
545
546 /**
547 * The format used for serialization/deserialization by default by this
548 * ContentHandler.
549 *
550 * This default implementation will return the first element of the array
551 * of formats that was passed to the constructor.
552 *
553 * @since 1.21
554 *
555 * @return string The name of the default serialization format as a MIME type
556 */
557 public function getDefaultFormat() {
558 return $this->mSupportedFormats[0];
559 }
560
561 /**
562 * Returns true if $format is a serialization format supported by this
563 * ContentHandler, and false otherwise.
564 *
565 * Note that if $format is null, this method always returns true, because
566 * null means "use the default format".
567 *
568 * @since 1.21
569 *
570 * @param string $format The serialization format to check
571 *
572 * @return bool
573 */
574 public function isSupportedFormat( $format ) {
575 if ( !$format ) {
576 return true; // this means "use the default"
577 }
578
579 return in_array( $format, $this->mSupportedFormats );
580 }
581
582 /**
583 * Convenient for checking whether a format provided as a parameter is actually supported.
584 *
585 * @param string $format The serialization format to check
586 *
587 * @throws MWException If the format is not supported by this content handler.
588 */
589 protected function checkFormat( $format ) {
590 if ( !$this->isSupportedFormat( $format ) ) {
591 throw new MWException(
592 "Format $format is not supported for content model "
593 . $this->getModelID()
594 );
595 }
596 }
597
598 /**
599 * Returns overrides for action handlers.
600 * Classes listed here will be used instead of the default one when
601 * (and only when) $wgActions[$action] === true. This allows subclasses
602 * to override the default action handlers.
603 *
604 * @since 1.21
605 *
606 * @return array Always an empty array.
607 */
608 public function getActionOverrides() {
609 return array();
610 }
611
612 /**
613 * Factory for creating an appropriate DifferenceEngine for this content model.
614 *
615 * @since 1.21
616 *
617 * @param IContextSource $context Context to use, anything else will be ignored.
618 * @param int $old Revision ID we want to show and diff with.
619 * @param int|string $new Either a revision ID or one of the strings 'cur', 'prev' or 'next'.
620 * @param int $rcid FIXME: Deprecated, no longer used. Defaults to 0.
621 * @param bool $refreshCache If set, refreshes the diff cache. Defaults to false.
622 * @param bool $unhide If set, allow viewing deleted revs. Defaults to false.
623 *
624 * @return DifferenceEngine
625 */
626 public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0,
627 $rcid = 0, //FIXME: Deprecated, no longer used
628 $refreshCache = false, $unhide = false ) {
629
630 // hook: get difference engine
631 $differenceEngine = null;
632 if ( !wfRunHooks( 'GetDifferenceEngine',
633 array( $context, $old, $new, $refreshCache, $unhide, &$differenceEngine )
634 ) ) {
635 return $differenceEngine;
636 }
637 $diffEngineClass = $this->getDiffEngineClass();
638 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
639 }
640
641 /**
642 * Get the language in which the content of the given page is written.
643 *
644 * This default implementation just returns $wgContLang (except for pages
645 * in the MediaWiki namespace)
646 *
647 * Note that the pages language is not cacheable, since it may in some
648 * cases depend on user settings.
649 *
650 * Also note that the page language may or may not depend on the actual content of the page,
651 * that is, this method may load the content in order to determine the language.
652 *
653 * @since 1.21
654 *
655 * @param Title $title The page to determine the language for.
656 * @param Content $content The page's content, if you have it handy, to avoid reloading it.
657 *
658 * @return Language The page's language
659 */
660 public function getPageLanguage( Title $title, Content $content = null ) {
661 global $wgContLang, $wgLang;
662 $pageLang = $wgContLang;
663
664 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
665 // Parse mediawiki messages with correct target language
666 list( /* $unused */, $lang ) = MessageCache::singleton()->figureMessage( $title->getText() );
667 $pageLang = wfGetLangObj( $lang );
668 }
669
670 Hooks::run( 'PageContentLanguage', array( $title, &$pageLang, $wgLang ) );
671
672 return wfGetLangObj( $pageLang );
673 }
674
675 /**
676 * Get the language in which the content of this page is written when
677 * viewed by user. Defaults to $this->getPageLanguage(), but if the user
678 * specified a preferred variant, the variant will be used.
679 *
680 * This default implementation just returns $this->getPageLanguage( $title, $content ) unless
681 * the user specified a preferred variant.
682 *
683 * Note that the pages view language is not cacheable, since it depends on user settings.
684 *
685 * Also note that the page language may or may not depend on the actual content of the page,
686 * that is, this method may load the content in order to determine the language.
687 *
688 * @since 1.21
689 *
690 * @param Title $title The page to determine the language for.
691 * @param Content $content The page's content, if you have it handy, to avoid reloading it.
692 *
693 * @return Language The page's language for viewing
694 */
695 public function getPageViewLanguage( Title $title, Content $content = null ) {
696 $pageLang = $this->getPageLanguage( $title, $content );
697
698 if ( $title->getNamespace() !== NS_MEDIAWIKI ) {
699 // If the user chooses a variant, the content is actually
700 // in a language whose code is the variant code.
701 $variant = $pageLang->getPreferredVariant();
702 if ( $pageLang->getCode() !== $variant ) {
703 $pageLang = Language::factory( $variant );
704 }
705 }
706
707 return $pageLang;
708 }
709
710 /**
711 * Determines whether the content type handled by this ContentHandler
712 * can be used on the given page.
713 *
714 * This default implementation always returns true.
715 * Subclasses may override this to restrict the use of this content model to specific locations,
716 * typically based on the namespace or some other aspect of the title, such as a special suffix
717 * (e.g. ".svg" for SVG content).
718 *
719 * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which
720 * content model can be used where.
721 *
722 * @param Title $title The page's title.
723 *
724 * @return bool True if content of this kind can be used on the given page, false otherwise.
725 */
726 public function canBeUsedOn( Title $title ) {
727 $ok = true;
728
729 Hooks::run( 'ContentModelCanBeUsedOn', array( $this->getModelID(), $title, &$ok ) );
730
731 return $ok;
732 }
733
734 /**
735 * Returns the name of the diff engine to use.
736 *
737 * @since 1.21
738 *
739 * @return string
740 */
741 protected function getDiffEngineClass() {
742 return 'DifferenceEngine';
743 }
744
745 /**
746 * Attempts to merge differences between three versions. Returns a new
747 * Content object for a clean merge and false for failure or a conflict.
748 *
749 * This default implementation always returns false.
750 *
751 * @since 1.21
752 *
753 * @param Content $oldContent The page's previous content.
754 * @param Content $myContent One of the page's conflicting contents.
755 * @param Content $yourContent One of the page's conflicting contents.
756 *
757 * @return Content|bool Always false.
758 */
759 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
760 return false;
761 }
762
763 /**
764 * Return an applicable auto-summary if one exists for the given edit.
765 *
766 * @since 1.21
767 *
768 * @param Content $oldContent The previous text of the page.
769 * @param Content $newContent The submitted text of the page.
770 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
771 *
772 * @return string An appropriate auto-summary, or an empty string.
773 */
774 public function getAutosummary( Content $oldContent = null, Content $newContent = null,
775 $flags ) {
776 // Decide what kind of auto-summary is needed.
777
778 // Redirect auto-summaries
779
780 /**
781 * @var $ot Title
782 * @var $rt Title
783 */
784
785 $ot = !is_null( $oldContent ) ? $oldContent->getRedirectTarget() : null;
786 $rt = !is_null( $newContent ) ? $newContent->getRedirectTarget() : null;
787
788 if ( is_object( $rt ) ) {
789 if ( !is_object( $ot )
790 || !$rt->equals( $ot )
791 || $ot->getFragment() != $rt->getFragment()
792 ) {
793 $truncatedtext = $newContent->getTextForSummary(
794 250
795 - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
796 - strlen( $rt->getFullText() ) );
797
798 return wfMessage( 'autoredircomment', $rt->getFullText() )
799 ->rawParams( $truncatedtext )->inContentLanguage()->text();
800 }
801 }
802
803 // New page auto-summaries
804 if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
805 // If they're making a new article, give its text, truncated, in
806 // the summary.
807
808 $truncatedtext = $newContent->getTextForSummary(
809 200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
810
811 return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
812 ->inContentLanguage()->text();
813 }
814
815 // Blanking auto-summaries
816 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
817 return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
818 } elseif ( !empty( $oldContent )
819 && $oldContent->getSize() > 10 * $newContent->getSize()
820 && $newContent->getSize() < 500
821 ) {
822 // Removing more than 90% of the article
823
824 $truncatedtext = $newContent->getTextForSummary(
825 200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
826
827 return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
828 ->inContentLanguage()->text();
829 }
830
831 // New blank article auto-summary
832 if ( $flags & EDIT_NEW && $newContent->isEmpty() ) {
833 return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
834 }
835
836 // If we reach this point, there's no applicable auto-summary for our
837 // case, so our auto-summary is empty.
838 return '';
839 }
840
841 /**
842 * Auto-generates a deletion reason
843 *
844 * @since 1.21
845 *
846 * @param Title $title The page's title
847 * @param bool &$hasHistory Whether the page has a history
848 *
849 * @return mixed String containing deletion reason or empty string, or
850 * boolean false if no revision occurred
851 *
852 * @todo &$hasHistory is extremely ugly, it's here because
853 * WikiPage::getAutoDeleteReason() and Article::generateReason()
854 * have it / want it.
855 */
856 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
857 $dbw = wfGetDB( DB_MASTER );
858
859 // Get the last revision
860 $rev = Revision::newFromTitle( $title );
861
862 if ( is_null( $rev ) ) {
863 return false;
864 }
865
866 // Get the article's contents
867 $content = $rev->getContent();
868 $blank = false;
869
870 // If the page is blank, use the text from the previous revision,
871 // which can only be blank if there's a move/import/protect dummy
872 // revision involved
873 if ( !$content || $content->isEmpty() ) {
874 $prev = $rev->getPrevious();
875
876 if ( $prev ) {
877 $rev = $prev;
878 $content = $rev->getContent();
879 $blank = true;
880 }
881 }
882
883 $this->checkModelID( $rev->getContentModel() );
884
885 // Find out if there was only one contributor
886 // Only scan the last 20 revisions
887 $res = $dbw->select( 'revision', 'rev_user_text',
888 array(
889 'rev_page' => $title->getArticleID(),
890 $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0'
891 ),
892 __METHOD__,
893 array( 'LIMIT' => 20 )
894 );
895
896 if ( $res === false ) {
897 // This page has no revisions, which is very weird
898 return false;
899 }
900
901 $hasHistory = ( $res->numRows() > 1 );
902 $row = $dbw->fetchObject( $res );
903
904 if ( $row ) { // $row is false if the only contributor is hidden
905 $onlyAuthor = $row->rev_user_text;
906 // Try to find a second contributor
907 foreach ( $res as $row ) {
908 if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
909 $onlyAuthor = false;
910 break;
911 }
912 }
913 } else {
914 $onlyAuthor = false;
915 }
916
917 // Generate the summary with a '$1' placeholder
918 if ( $blank ) {
919 // The current revision is blank and the one before is also
920 // blank. It's just not our lucky day
921 $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text();
922 } else {
923 if ( $onlyAuthor ) {
924 $reason = wfMessage(
925 'excontentauthor',
926 '$1',
927 $onlyAuthor
928 )->inContentLanguage()->text();
929 } else {
930 $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text();
931 }
932 }
933
934 if ( $reason == '-' ) {
935 // Allow these UI messages to be blanked out cleanly
936 return '';
937 }
938
939 // Max content length = max comment length - length of the comment (excl. $1)
940 $text = $content ? $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) ) : '';
941
942 // Now replace the '$1' placeholder
943 $reason = str_replace( '$1', $text, $reason );
944
945 return $reason;
946 }
947
948 /**
949 * Get the Content object that needs to be saved in order to undo all revisions
950 * between $undo and $undoafter. Revisions must belong to the same page,
951 * must exist and must not be deleted.
952 *
953 * @since 1.21
954 *
955 * @param Revision $current The current text
956 * @param Revision $undo The revision to undo
957 * @param Revision $undoafter Must be an earlier revision than $undo
958 *
959 * @return mixed String on success, false on failure
960 */
961 public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) {
962 $cur_content = $current->getContent();
963
964 if ( empty( $cur_content ) ) {
965 return false; // no page
966 }
967
968 $undo_content = $undo->getContent();
969 $undoafter_content = $undoafter->getContent();
970
971 if ( !$undo_content || !$undoafter_content ) {
972 return false; // no content to undo
973 }
974
975 $this->checkModelID( $cur_content->getModel() );
976 $this->checkModelID( $undo_content->getModel() );
977 $this->checkModelID( $undoafter_content->getModel() );
978
979 if ( $cur_content->equals( $undo_content ) ) {
980 // No use doing a merge if it's just a straight revert.
981 return $undoafter_content;
982 }
983
984 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
985
986 return $undone_content;
987 }
988
989 /**
990 * Get parser options suitable for rendering and caching the article
991 *
992 * @param IContextSource|User|string $context One of the following:
993 * - IContextSource: Use the User and the Language of the provided
994 * context
995 * - User: Use the provided User object and $wgLang for the language,
996 * so use an IContextSource object if possible.
997 * - 'canonical': Canonical options (anonymous user with default
998 * preferences and content language).
999 *
1000 * @throws MWException
1001 * @return ParserOptions
1002 */
1003 public function makeParserOptions( $context ) {
1004 global $wgContLang, $wgEnableParserLimitReporting;
1005
1006 if ( $context instanceof IContextSource ) {
1007 $options = ParserOptions::newFromContext( $context );
1008 } elseif ( $context instanceof User ) { // settings per user (even anons)
1009 $options = ParserOptions::newFromUser( $context );
1010 } elseif ( $context === 'canonical' ) { // canonical settings
1011 $options = ParserOptions::newFromUserAndLang( new User, $wgContLang );
1012 } else {
1013 throw new MWException( "Bad context for parser options: $context" );
1014 }
1015
1016 $options->enableLimitReport( $wgEnableParserLimitReporting ); // show inclusion/loop reports
1017 $options->setTidy( true ); // fix bad HTML
1018
1019 return $options;
1020 }
1021
1022 /**
1023 * Returns true for content models that support caching using the
1024 * ParserCache mechanism. See WikiPage::isParserCacheUsed().
1025 *
1026 * @since 1.21
1027 *
1028 * @return bool Always false.
1029 */
1030 public function isParserCacheSupported() {
1031 return false;
1032 }
1033
1034 /**
1035 * Returns true if this content model supports sections.
1036 * This default implementation returns false.
1037 *
1038 * Content models that return true here should also implement
1039 * Content::getSection, Content::replaceSection, etc. to handle sections..
1040 *
1041 * @return bool Always false.
1042 */
1043 public function supportsSections() {
1044 return false;
1045 }
1046
1047 /**
1048 * Returns true if this content model supports redirects.
1049 * This default implementation returns false.
1050 *
1051 * Content models that return true here should also implement
1052 * ContentHandler::makeRedirectContent to return a Content object.
1053 *
1054 * @return bool Always false.
1055 */
1056 public function supportsRedirects() {
1057 return false;
1058 }
1059
1060 /**
1061 * Logs a deprecation warning, visible if $wgDevelopmentWarnings, but only if
1062 * self::$enableDeprecationWarnings is set to true.
1063 *
1064 * @param string $func The name of the deprecated function
1065 * @param string $version The version since the method is deprecated. Usually 1.21
1066 * for ContentHandler related stuff.
1067 * @param string|bool $component : Component to which the function belongs.
1068 * If false, it is assumed the function is in MediaWiki core.
1069 *
1070 * @see ContentHandler::$enableDeprecationWarnings
1071 * @see wfDeprecated
1072 */
1073 public static function deprecated( $func, $version, $component = false ) {
1074 if ( self::$enableDeprecationWarnings ) {
1075 wfDeprecated( $func, $version, $component, 3 );
1076 }
1077 }
1078
1079 /**
1080 * Call a legacy hook that uses text instead of Content objects.
1081 * Will log a warning when a matching hook function is registered.
1082 * If the textual representation of the content is changed by the
1083 * hook function, a new Content object is constructed from the new
1084 * text.
1085 *
1086 * @param string $event Event name
1087 * @param array $args Parameters passed to hook functions
1088 * @param bool $warn Whether to log a warning.
1089 * Default to self::$enableDeprecationWarnings.
1090 * May be set to false for testing.
1091 *
1092 * @return bool True if no handler aborted the hook
1093 *
1094 * @see ContentHandler::$enableDeprecationWarnings
1095 */
1096 public static function runLegacyHooks( $event, $args = array(),
1097 $warn = null
1098 ) {
1099
1100 if ( $warn === null ) {
1101 $warn = self::$enableDeprecationWarnings;
1102 }
1103
1104 if ( !Hooks::isRegistered( $event ) ) {
1105 return true; // nothing to do here
1106 }
1107
1108 if ( $warn ) {
1109 // Log information about which handlers are registered for the legacy hook,
1110 // so we can find and fix them.
1111
1112 $handlers = Hooks::getHandlers( $event );
1113 $handlerInfo = array();
1114
1115 wfSuppressWarnings();
1116
1117 foreach ( $handlers as $handler ) {
1118 if ( is_array( $handler ) ) {
1119 if ( is_object( $handler[0] ) ) {
1120 $info = get_class( $handler[0] );
1121 } else {
1122 $info = $handler[0];
1123 }
1124
1125 if ( isset( $handler[1] ) ) {
1126 $info .= '::' . $handler[1];
1127 }
1128 } elseif ( is_object( $handler ) ) {
1129 $info = get_class( $handler[0] );
1130 $info .= '::on' . $event;
1131 } else {
1132 $info = $handler;
1133 }
1134
1135 $handlerInfo[] = $info;
1136 }
1137
1138 wfRestoreWarnings();
1139
1140 wfWarn( "Using obsolete hook $event via ContentHandler::runLegacyHooks()! Handlers: " .
1141 implode( ', ', $handlerInfo ), 2 );
1142 }
1143
1144 // convert Content objects to text
1145 $contentObjects = array();
1146 $contentTexts = array();
1147
1148 foreach ( $args as $k => $v ) {
1149 if ( $v instanceof Content ) {
1150 /* @var Content $v */
1151
1152 $contentObjects[$k] = $v;
1153
1154 $v = $v->serialize();
1155 $contentTexts[$k] = $v;
1156 $args[$k] = $v;
1157 }
1158 }
1159
1160 // call the hook functions
1161 $ok = Hooks::run( $event, $args );
1162
1163 // see if the hook changed the text
1164 foreach ( $contentTexts as $k => $orig ) {
1165 /* @var Content $content */
1166
1167 $modified = $args[$k];
1168 $content = $contentObjects[$k];
1169
1170 if ( $modified !== $orig ) {
1171 // text was changed, create updated Content object
1172 $content = $content->getContentHandler()->unserializeContent( $modified );
1173 }
1174
1175 $args[$k] = $content;
1176 }
1177
1178 return $ok;
1179 }
1180 }