Ensure users are able to edit the page after changing the content model
[lhc/web/wiklou.git] / includes / utils / AutoloadGenerator.php
index 916e2f8..395ce37 100644 (file)
  *     $gen = new AutoloadGenerator( __DIR__ );
  *     $gen->readDir( __DIR__ . '/includes' );
  *     $gen->readFile( __DIR__ . '/foo.php' )
- *     $gen->generateAutoload();
+ *     $gen->getAutoload();
  */
 class AutoloadGenerator {
+       const FILETYPE_JSON = 'json';
+       const FILETYPE_PHP = 'php';
+
        /**
         * @var string Root path of the project being scanned for classes
         */
@@ -122,11 +125,11 @@ class AutoloadGenerator {
         * Updates the AutoloadClasses field at the given
         * filename.
         *
-        * @param {string} $filename Filename of JSON
+        * @param string $filename Filename of JSON
         *  extension/skin registration file
+        * @return string Updated Json of the file given as the $filename parameter
         */
        protected function generateJsonAutoload( $filename ) {
-               require_once __DIR__ . '/../../includes/json/FormatJson.php';
                $key = 'AutoloadClasses';
                $json = FormatJson::decode( file_get_contents( $filename ), true );
                unset( $json[$key] );
@@ -148,10 +151,8 @@ class AutoloadGenerator {
                // Sorting the list of autoload classes.
                ksort( $json[$key] );
 
-               // Update file, using constants for the required
-               // formatting.
-               file_put_contents( $filename,
-                       FormatJson::encode( $json, true ) . "\n" );
+               // Return the whole JSON file
+               return FormatJson::encode( $json, true ) . "\n";
        }
 
        /**
@@ -198,8 +199,7 @@ class AutoloadGenerator {
                }
 
                $output = implode( "\n\t", $content );
-               file_put_contents(
-                       $filename,
+               return
                        <<<EOD
 <?php
 // This file is generated by $commandName, do not adjust manually
@@ -210,36 +210,60 @@ global \${$this->variableName};
        {$output}
 ];
 
-EOD
-               );
+EOD;
 
        }
 
        /**
-        * Write out all known classes to autoload.php, extension.json, or skin.json in
-        * the provided basedir
+        * Returns all known classes as a string, which can be used to put into a target
+        * file (e.g. extension.json, skin.json or autoload.php)
         *
         * @param string $commandName Value used in file comment to direct
         *  developers towards the appropriate way to update the autoload.
+        * @return string
         */
-       public function generateAutoload( $commandName = 'AutoloadGenerator' ) {
+       public function getAutoload( $commandName = 'AutoloadGenerator' ) {
 
                // We need to check whether an extenson.json or skin.json exists or not, and
                // incase it doesn't, update the autoload.php file.
 
-               $jsonFilename = null;
-               if ( file_exists( $this->basepath . "/extension.json" ) ) {
-                       $jsonFilename = $this->basepath . "/extension.json";
-               } elseif ( file_exists( $this->basepath . "/skin.json" ) ) {
-                       $jsonFilename = $this->basepath . "/skin.json";
-               }
+               $fileinfo = $this->getTargetFileinfo();
 
-               if ( $jsonFilename !== null ) {
-                       $this->generateJsonAutoload( $jsonFilename );
+               if ( $fileinfo['type'] === self::FILETYPE_JSON ) {
+                       return $this->generateJsonAutoload( $fileinfo['filename'] );
                } else {
-                       $this->generatePHPAutoload( $commandName, $this->basepath . '/autoload.php' );
+                       return $this->generatePHPAutoload( $commandName, $fileinfo['filename'] );
+               }
+       }
+
+       /**
+        * Returns the filename of the extension.json of skin.json, if there's any, or
+        * otherwise the path to the autoload.php file in an array as the "filename"
+        * key and with the type (AutoloadGenerator::FILETYPE_JSON or AutoloadGenerator::FILETYPE_PHP)
+        * of the file as the "type" key.
+        *
+        * @return array
+        */
+       public function getTargetFileinfo() {
+               $fileinfo = [
+                       'filename' => $this->basepath . '/autoload.php',
+                       'type' => self::FILETYPE_PHP
+               ];
+               if ( file_exists( $this->basepath . '/extension.json' ) ) {
+                       $fileinfo = [
+                               'filename' => $this->basepath . '/extension.json',
+                               'type' => self::FILETYPE_JSON
+                       ];
+               } elseif ( file_exists( $this->basepath . '/skin.json' ) ) {
+                       $fileinfo = [
+                               'filename' => $this->basepath . '/skin.json',
+                               'type' => self::FILETYPE_JSON
+                       ];
                }
+
+               return $fileinfo;
        }
+
        /**
         * Ensure that Unix-style path separators ("/") are used in the path.
         *
@@ -249,6 +273,24 @@ EOD
        protected static function normalizePathSeparator( $path ) {
                return str_replace( '\\', '/', $path );
        }
+
+       /**
+        * Initialize the source files and directories which are used for the MediaWiki default
+        * autoloader in {mw-base-dir}/autoload.php including:
+        *  * includes/
+        *  * languages/
+        *  * maintenance/
+        *  * mw-config/
+        *  * /*.php
+        */
+       public function initMediaWikiDefault() {
+               foreach ( [ 'includes', 'languages', 'maintenance', 'mw-config' ] as $dir ) {
+                       $this->readDir( $this->basepath . '/' . $dir );
+               }
+               foreach ( glob( $this->basepath . '/*.php' ) as $file ) {
+                       $this->readFile( $file );
+               }
+       }
 }
 
 /**