LocalisationCache: Use file_get_contents instead of DOMDocument::load
[lhc/web/wiklou.git] / includes / cache / LocalisationCache.php
index ae27fba..c3e5e1d 100644 (file)
  * @file
  */
 
+use Cdb\Exception as CdbException;
+use Cdb\Reader as CdbReader;
+use Cdb\Writer as CdbWriter;
+
 /**
  * Class for caching the contents of localisation files, Messages*.php
  * and *.i18n.php.
@@ -33,7 +37,7 @@
  * as grammatical transformation, is done by the caller.
  */
 class LocalisationCache {
-       const VERSION = 2;
+       const VERSION = 3;
 
        /** Configuration associative array */
        private $conf;
@@ -652,8 +656,13 @@ class LocalisationCache {
         * @param string $fileName
         */
        protected function loadPluralFile( $fileName ) {
+               // Use file_get_contents instead of DOMDocument::load (T58439)
+               $xml = file_get_contents( $fileName );
+               if ( !$xml ) {
+                       throw new MWException( "Unable to read plurals file $fileName" );
+               }
                $doc = new DOMDocument;
-               $doc->load( $fileName );
+               $doc->loadXML( $xml );
                $rulesets = $doc->getElementsByTagName( "pluralRules" );
                foreach ( $rulesets as $ruleset ) {
                        $codes = $ruleset->getAttribute( 'locales' );
@@ -789,6 +798,22 @@ class LocalisationCache {
                return $used;
        }
 
+       /**
+        * Gets the combined list of messages dirs from
+        * core and extensions
+        *
+        * @since 1.25
+        * @return array
+        */
+       public function getMessagesDirs() {
+               global $wgMessagesDirs, $IP;
+               return array(
+                       'core' => "$IP/languages/i18n",
+                       'api' => "$IP/includes/api/i18n",
+                       'oojs-ui' => "$IP/resources/lib/oojs-ui/i18n",
+               ) + $wgMessagesDirs;
+       }
+
        /**
         * Load localisation data for a given language for both core and extensions
         * and save it to the persistent cache store and the process cache
@@ -796,7 +821,7 @@ class LocalisationCache {
         * @throws MWException
         */
        public function recache( $code ) {
-               global $wgExtensionMessagesFiles, $wgMessagesDirs;
+               global $wgExtensionMessagesFiles;
                wfProfileIn( __METHOD__ );
 
                if ( !$code ) {
@@ -843,6 +868,7 @@ class LocalisationCache {
                }
 
                $codeSequence = array_merge( array( $code ), $coreData['fallbackSequence'] );
+               $messageDirs = $this->getMessagesDirs();
 
                wfProfileIn( __METHOD__ . '-fallbacks' );
 
@@ -851,7 +877,7 @@ class LocalisationCache {
                        $codeSequence,
                        array_fill( 0, count( $codeSequence ), $initialData ) );
                foreach ( $wgExtensionMessagesFiles as $extension => $fileName ) {
-                       if ( isset( $wgMessagesDirs[$extension] ) ) {
+                       if ( isset( $messageDirs[$extension] ) ) {
                                # This extension has JSON message data; skip the PHP shim
                                continue;
                        }
@@ -879,7 +905,7 @@ class LocalisationCache {
                        $csData = $initialData;
 
                        # Load core messages and the extension localisations.
-                       foreach ( $wgMessagesDirs as $dirs ) {
+                       foreach ( $messageDirs as $dirs ) {
                                foreach ( (array)$dirs as $dir ) {
                                        $fileName = "$dir/$csCode.json";
                                        $data = $this->readJSONFile( $fileName );
@@ -924,7 +950,7 @@ class LocalisationCache {
 
                        # Allow extensions an opportunity to adjust the data for this
                        # fallback
-                       wfRunHooks( 'LocalisationCacheRecacheFallback', array( $this, $csCode, &$csData ) );
+                       Hooks::run( 'LocalisationCacheRecacheFallback', array( $this, $csCode, &$csData ) );
 
                        # Merge the data for this fallback into the final array
                        if ( $csCode === $code ) {
@@ -946,6 +972,7 @@ class LocalisationCache {
 
                # Add cache dependencies for any referenced globals
                $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
+               // $wgMessagesDirs is used in LocalisationCache::getMessagesDirs()
                $deps['wgMessagesDirs'] = new GlobalDependency( 'wgMessagesDirs' );
                $deps['version'] = new ConstantDependency( 'LocalisationCache::VERSION' );
 
@@ -981,7 +1008,7 @@ class LocalisationCache {
                }
                # Run hooks
                $purgeBlobs = true;
-               wfRunHooks( 'LocalisationCacheRecache', array( $this, $code, &$allData, &$purgeBlobs ) );
+               Hooks::run( 'LocalisationCacheRecache', array( $this, $code, &$allData, &$purgeBlobs ) );
 
                if ( is_null( $allData['namespaceNames'] ) ) {
                        wfProfileOut( __METHOD__ );