MediaWiki  master
generateSchemaSql.php
Go to the documentation of this file.
1 <?php
2 
25 use Doctrine\SqlFormatter\NullHighlighter;
26 use Doctrine\SqlFormatter\SqlFormatter;
28 
29 require_once __DIR__ . '/Maintenance.php';
30 
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Build SQL files from abstract JSON files' );
40 
41  $this->addOption(
42  'json',
43  'Path to the json file. Default: tables.json',
44  false,
45  true
46  );
47  $this->addOption(
48  'sql',
49  'Path to output. Default: tables-generated.sql',
50  false,
51  true
52  );
53  $this->addOption(
54  'type',
55  'Can be either \'mysql\', \'sqlite\', or \'postgres\'. Default: mysql',
56  false,
57  true
58  );
59  }
60 
61  public function execute() {
62  global $IP;
63  $jsonPath = $this->getOption( 'json', __DIR__ . '/tables.json' );
64  $relativeJsonPath = str_replace( "$IP/", '', $jsonPath );
65  $sqlPath = $this->getOption( 'sql', __DIR__ . '/tables-generated.sql' );
66  $abstractSchema = json_decode( file_get_contents( $jsonPath ), true );
67  $schemaBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaBuilder(
68  $this->getOption( 'type', 'mysql' )
69  );
70  foreach ( $abstractSchema as $table ) {
71  $schemaBuilder->addTable( $table );
72  }
73  $sql = "-- This file is automatically generated using maintenance/generateSchemaSql.php.\n" .
74  "-- Source: $relativeJsonPath\n" .
75  "-- Do not modify this file directly.\n" .
76  "-- See https://www.mediawiki.org/wiki/Manual:Schema_changes\n";
77 
78  $tables = $schemaBuilder->getSql();
79  if ( $tables !== [] ) {
80  // Temporary
81  $sql = $sql . implode( ";\n\n", $tables ) . ';';
82  $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
83  }
84  // Until the linting issue is resolved
85  // https://github.com/doctrine/sql-formatter/issues/53
86  $sql = str_replace( "\n/*_*/\n", " /*_*/", $sql );
87  $sql = str_replace( "; CREATE ", ";\nCREATE ", $sql );
88  $sql = str_replace(
89  "\n" . '/*$wgDBTableOptions*/' . ";",
90  ' /*$wgDBTableOptions*/;' . "\n",
91  $sql
92  );
93  $sql = str_replace(
94  "\n" . '/*$wgDBTableOptions*/' . "\n;",
95  ' /*$wgDBTableOptions*/;' . "\n",
96  $sql
97  );
98 
99  file_put_contents( $sqlPath, $sql );
100  }
101 
102 }
103 
104 $maintClass = GenerateSchemaSql::class;
105 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
$maintClass
$maintClass
Definition: generateSchemaSql.php:104
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:323
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:53
GenerateSchemaSql\__construct
__construct()
Default constructor.
Definition: generateSchemaSql.php:37
GenerateSchemaSql
Maintenance script to generate schema from abstract json files.
Definition: generateSchemaSql.php:36
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:241
GenerateSchemaSql\execute
execute()
Do the actual work.
Definition: generateSchemaSql.php:61
Wikimedia\Rdbms\DoctrineSchemaBuilderFactory
@experimental @unstable
Definition: DoctrineSchemaBuilderFactory.php:14
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:277
$IP
$IP
Definition: WebStart.php:49