Mass conversion of $wgContLang to service
[lhc/web/wiklou.git] / includes / diff / DifferenceEngine.php
index ff07fe7..5138655 100644 (file)
@@ -28,6 +28,9 @@ use MediaWiki\Shell\Shell;
  * @ingroup DifferenceEngine
  */
 class DifferenceEngine extends ContextSource {
+
+       use DeprecationHelper;
+
        /**
         * Constant to indicate diff cache compatibility.
         * Bump this when changing the diff formatting in a way that
@@ -36,47 +39,54 @@ class DifferenceEngine extends ContextSource {
         */
        const DIFF_VERSION = '1.12';
 
-       /** @var int */
-       public $mOldid;
+       /** @var int Revision ID or 0 for current */
+       protected $mOldid;
 
-       /** @var int */
-       public $mNewid;
+       /** @var int|string Revision ID or null for current or an alias such as 'next' */
+       protected $mNewid;
 
        private $mOldTags;
        private $mNewTags;
 
-       /** @var Content */
-       public $mOldContent;
+       /** @var Content|null */
+       protected $mOldContent;
 
-       /** @var Content */
-       public $mNewContent;
+       /** @var Content|null */
+       protected $mNewContent;
 
        /** @var Language */
        protected $mDiffLang;
 
        /** @var Title */
-       public $mOldPage;
+       protected $mOldPage;
 
        /** @var Title */
-       public $mNewPage;
+       protected $mNewPage;
 
-       /** @var Revision */
+       /** @var Revision|null */
        public $mOldRev;
 
-       /** @var Revision */
+       /** @var Revision|null */
        public $mNewRev;
 
        /** @var bool Have the revisions IDs been loaded */
        private $mRevisionsIdsLoaded = false;
 
        /** @var bool Have the revisions been loaded */
-       public $mRevisionsLoaded = false;
+       protected $mRevisionsLoaded = false;
 
        /** @var int How many text blobs have been loaded, 0, 1 or 2? */
-       public $mTextLoaded = 0;
+       protected $mTextLoaded = 0;
+
+       /**
+        * Was the content overridden via setContent()?
+        * If the content was overridden, most internal state (e.g. mOldid or mOldRev) should be ignored.
+        * @var bool
+        */
+       protected $isContentOverridden = false;
 
        /** @var bool Was the diff fetched from cache? */
-       public $mCacheHit = false;
+       protected $mCacheHit = false;
 
        /**
         * Set this to true to add debug info to the HTML output.
@@ -102,7 +112,7 @@ class DifferenceEngine extends ContextSource {
        /**#@-*/
 
        /**
-        * @param IContextSource $context Context to use, anything else will be ignored
+        * @param IContextSource|null $context Context to use, anything else will be ignored
         * @param int $old Old ID we want to show and diff with.
         * @param string|int $new Either revision ID or 'prev' or 'next'. Default: 0.
         * @param int $rcid Deprecated, no longer used!
@@ -112,6 +122,16 @@ class DifferenceEngine extends ContextSource {
        public function __construct( $context = null, $old = 0, $new = 0, $rcid = 0,
                $refreshCache = false, $unhide = false
        ) {
+               $this->deprecatePublicProperty( 'mOldid', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mNewid', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mOldPage', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mNewPage', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mOldContent', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mNewContent', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mRevisionsLoaded', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mTextLoaded', '1.32', __CLASS__ );
+               $this->deprecatePublicProperty( 'mCacheHit', '1.32', __CLASS__ );
+
                if ( $context instanceof IContextSource ) {
                        $this->setContext( $context );
                }
@@ -132,6 +152,8 @@ class DifferenceEngine extends ContextSource {
        }
 
        /**
+        * Get the language of the difference engine, defaults to page content language
+        *
         * @return Language
         */
        public function getDiffLang() {
@@ -944,7 +966,7 @@ class DifferenceEngine extends ContextSource {
                        wfDeprecated( "\$wgExternalDiffEngine = '{$wgExternalDiffEngine}'", '1.27' );
                        $wgExternalDiffEngine = false;
                } elseif ( $wgExternalDiffEngine == 'wikidiff2' ) {
-                       // Same as above, but with no deprecation warnings
+                       wfDeprecated( "\$wgExternalDiffEngine = '{$wgExternalDiffEngine}'", '1.32' );
                        $wgExternalDiffEngine = false;
                } elseif ( !is_string( $wgExternalDiffEngine ) && $wgExternalDiffEngine !== false ) {
                        // And prevent people from shooting themselves in the foot...
@@ -970,8 +992,6 @@ class DifferenceEngine extends ContextSource {
         * @return bool|string
         */
        protected function textDiff( $otext, $ntext ) {
-               global $wgContLang;
-
                $otext = str_replace( "\r\n", "\n", $otext );
                $ntext = str_replace( "\r\n", "\n", $ntext );
 
@@ -1045,11 +1065,12 @@ class DifferenceEngine extends ContextSource {
                }
 
                # Native PHP diff
-               $ota = explode( "\n", $wgContLang->segmentForDiff( $otext ) );
-               $nta = explode( "\n", $wgContLang->segmentForDiff( $ntext ) );
+               $contLang = MediaWikiServices::getInstance()->getContentLanguage();
+               $ota = explode( "\n", $contLang->segmentForDiff( $otext ) );
+               $nta = explode( "\n", $contLang->segmentForDiff( $ntext ) );
                $diffs = new Diff( $ota, $nta );
                $formatter = new TableDiffFormatter();
-               $difftext = $wgContLang->unsegmentForDiff( $formatter->format( $diffs ) );
+               $difftext = $contLang->unsegmentForDiff( $formatter->format( $diffs ) );
                $difftext .= $this->debug( 'native PHP' );
 
                return $difftext;
@@ -1339,15 +1360,19 @@ class DifferenceEngine extends ContextSource {
 
                $this->mTextLoaded = 2;
                $this->mRevisionsLoaded = true;
+               $this->isContentOverridden = true;
        }
 
        /**
         * Set the language in which the diff text is written
-        * (Defaults to page content language).
-        * @param Language|string $lang
+        *
+        * @param Language $lang
         * @since 1.19
         */
        public function setTextLanguage( $lang ) {
+               if ( !$lang instanceof Language ) {
+                       wfDeprecated( __METHOD__ . ' with other type than Language for $lang', '1.32' );
+               }
                $this->mDiffLang = wfGetLangObj( $lang );
        }
 
@@ -1415,11 +1440,11 @@ class DifferenceEngine extends ContextSource {
         * to false. This is impossible via ordinary user input, and is provided for
         * API convenience.
         *
-        * @return bool
+        * @return bool Whether both revisions were loaded successfully.
         */
        public function loadRevisionData() {
                if ( $this->mRevisionsLoaded ) {
-                       return true;
+                       return $this->isContentOverridden || $this->mNewRev && $this->mOldRev;
                }
 
                // Whether it succeeds or fails, we don't want to try again
@@ -1495,11 +1520,11 @@ class DifferenceEngine extends ContextSource {
        /**
         * Load the text of the revisions, as well as revision data.
         *
-        * @return bool
+        * @return bool Whether the content of both revisions could be loaded successfully.
         */
        public function loadText() {
                if ( $this->mTextLoaded == 2 ) {
-                       return true;
+                       return $this->loadRevisionData() && $this->mOldContent && $this->mNewContent;
                }
 
                // Whether it succeeds or fails, we don't want to try again
@@ -1530,11 +1555,11 @@ class DifferenceEngine extends ContextSource {
        /**
         * Load the text of the new revision, not the old one
         *
-        * @return bool
+        * @return bool Whether the content of the new revision could be loaded successfully.
         */
        public function loadNewText() {
                if ( $this->mTextLoaded >= 1 ) {
-                       return true;
+                       return $this->loadRevisionData();
                }
 
                $this->mTextLoaded = 1;