r22556 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22555‎ | r22556 | r22557 >
Date:08:34, 30 May 2007
Author:kim
Status:old
Tags:
Comment:
dos2unix Create\ wikidata.php
Modified paths:
  • /trunk/extensions/Wikidata/Database+scripts/Convenience/Create+wikidata.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/Database scripts/Convenience/Create wikidata.php
@@ -1,130 +1,130 @@
22 <?php
3 -
4 -# this module create a wikidata extension for mediawiki
5 -# it generates the tables in a database (passed as parameter) with a defined prefix (passed as parameter)
6 -
7 -$wgUseMasterForMaintenance = true;
8 -require_once( "../../../../maintenance/commandLine.inc");
9 -#require_once( "../../../../LocalSettings.php");
10 -#require_once( "install-utils.inc" );
11 -#require_once( "AdminSettings.php");
12 -#require_once( "GlobalFunctions.php" );
13 -#require_once( "ProfilerStub.php");
14 -#require_once( "Exception.php" );
15 -#require_once( "Database.php" );
16 -
17 -function ReadSQLFile( $database, $pattern, $prefix, $filename ){
18 - $fp = fopen( $filename, 'r' );
19 - if ( false === $fp ) {
20 - return "Could not open \"{$filename}\".\n";
21 - }
22 -
23 - $cmd = "";
24 - $done = false;
25 -
26 - while ( ! feof( $fp ) ) {
27 - $line = trim( fgets( $fp, 1024 ) );
28 - $sl = strlen( $line ) - 1;
29 -
30 - if ( $sl < 0 ) { continue; }
31 - if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
32 -
33 - if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
34 - $done = true;
35 - $line = substr( $line, 0, $sl );
36 - }
37 -
38 - if ( '' != $cmd ) { $cmd .= ' '; }
39 - $cmd .= "$line\n";
40 -
41 - if ( $done ) {
42 - $cmd = str_replace(';;', ";", $cmd);
43 - $cmd = trim( str_replace( $pattern, $prefix, $cmd ) );
44 - $res = $database->query( $cmd );
45 -
46 - if ( false === $res ) {
47 - return "Query \"{$cmd}\" failed with error code \".\n";
48 - }
49 -
50 - $cmd = '';
51 - $done = false;
52 - }
53 - }
54 - fclose( $fp );
55 - return true;
56 -}
57 -
58 -$dbclass = 'Database' . ucfirst( $wgDBtype ) ;
59 -$comment = '';
60 -$database = $wgDBname;
61 -$user = $wgDBadminuser;
62 -$password = $wgDBadminpassword;
63 -$server = $wgDBserver;
64 -
65 -# Parse arguments
66 -for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
67 - if ( substr( $arg, 0, 7 ) == '-prefix' ) {
68 - $prefix = next( $argv );
69 - $wgWDprefix = $prefix . "_";
70 - }
71 - else if ( substr( $arg, 0, 9 ) == '-template' ) {
72 - $wgWDtemplate = next( $argv );
73 - }
74 - else if ( substr( $arg, 0, 8 ) == '-comment' ) {
75 - $comment = next( $argv );
76 - }
77 - else if ( substr( $arg, 0, 7 ) == '-server' ) {
78 - $server = next( $argv );
79 - }
80 - else if ( substr( $arg, 0, 9 ) == '-database' ) {
81 - $database = next( $argv );
82 - }
83 - else if ( substr( $arg, 0, 5 ) == '-user' ) {
84 - $user = next( $argv );
85 - }
86 - else if ( substr( $arg, 0, 9 ) == '-password' ) {
87 - $password = next( $argv );
88 - } else {
89 - $args[] = $arg;
90 - }
91 -}
92 -
93 -if ( !isset( $wgWDtemplate ) ){
94 - echo( "SQL template should be provided!");
95 - echo( "usage: create wikidata.php -prefix <prefix> -template <sql template> [-comment '<comment line>' -server <server> -database <database> -user <username> -password <password>]");
96 - exit();
97 -}
98 -
99 -if ( !isset( $wgWDprefix ) ){
100 - echo( "database prefix should be provided!");
101 - echo( "usage: create wikidata.php -prefix <prefix> -template <sql template> [-comment '<comment line>' -database <database> -user <username> -password <password>]");
102 - exit();
103 -}
104 -
105 -# Do a pre-emptive check to ensure we've got credentials supplied
106 -# We can't, at this stage, check them, but we can detect their absence,
107 -# which seems to cause most of the problems people whinge about
108 -if( !isset( $user ) || !isset( $password ) ) {
109 - echo( "No superuser credentials could be found. Please provide the details\n" );
110 - echo( "of a user with appropriate permissions to update the database. See\n" );
111 - echo( "AdminSettings.sample for more details.\n\n" );
112 - exit();
113 -}
114 -
115 -# Attempt to connect to the database as a privileged user
116 -# This will vomit up an error if there are permissions problems
117 -$wdDatabase = new $dbclass( $server, $user, $password, $database, 1 );
118 -
119 -if( !$wdDatabase->isOpen() ) {
120 - # Appears to have failed
121 - echo( "A connection to the database could not be established. Check the\n" );
122 - echo( "values of \$wgDBadminuser and \$wgDBadminpassword.\n" );
123 - exit();
124 -}
125 -
126 -ReadSQLFile( $wdDatabase, "/*\$wgWDprefix*/", $wgWDprefix, $wgWDtemplate );
127 -$wdDatabase->query( "DELETE FROM wikidata_sets WHERE set_prefix = '$prefix'" );
128 -$wdDatabase->query( "INSERT INTO wikidata_sets (set_prefix,set_fallback_name,set_dmid) VALUES ('$prefix','$comment',0)" );
129 -$wdDatabase->close();
1303
 4+# this module create a wikidata extension for mediawiki
 5+# it generates the tables in a database (passed as parameter) with a defined prefix (passed as parameter)
 6+
 7+$wgUseMasterForMaintenance = true;
 8+require_once( "../../../../maintenance/commandLine.inc");
 9+#require_once( "../../../../LocalSettings.php");
 10+#require_once( "install-utils.inc" );
 11+#require_once( "AdminSettings.php");
 12+#require_once( "GlobalFunctions.php" );
 13+#require_once( "ProfilerStub.php");
 14+#require_once( "Exception.php" );
 15+#require_once( "Database.php" );
 16+
 17+function ReadSQLFile( $database, $pattern, $prefix, $filename ){
 18+ $fp = fopen( $filename, 'r' );
 19+ if ( false === $fp ) {
 20+ return "Could not open \"{$filename}\".\n";
 21+ }
 22+
 23+ $cmd = "";
 24+ $done = false;
 25+
 26+ while ( ! feof( $fp ) ) {
 27+ $line = trim( fgets( $fp, 1024 ) );
 28+ $sl = strlen( $line ) - 1;
 29+
 30+ if ( $sl < 0 ) { continue; }
 31+ if ( '-' == $line{0} && '-' == $line{1} ) { continue; }
 32+
 33+ if ( ';' == $line{$sl} && ($sl < 2 || ';' != $line{$sl - 1})) {
 34+ $done = true;
 35+ $line = substr( $line, 0, $sl );
 36+ }
 37+
 38+ if ( '' != $cmd ) { $cmd .= ' '; }
 39+ $cmd .= "$line\n";
 40+
 41+ if ( $done ) {
 42+ $cmd = str_replace(';;', ";", $cmd);
 43+ $cmd = trim( str_replace( $pattern, $prefix, $cmd ) );
 44+ $res = $database->query( $cmd );
 45+
 46+ if ( false === $res ) {
 47+ return "Query \"{$cmd}\" failed with error code \".\n";
 48+ }
 49+
 50+ $cmd = '';
 51+ $done = false;
 52+ }
 53+ }
 54+ fclose( $fp );
 55+ return true;
 56+}
 57+
 58+$dbclass = 'Database' . ucfirst( $wgDBtype ) ;
 59+$comment = '';
 60+$database = $wgDBname;
 61+$user = $wgDBadminuser;
 62+$password = $wgDBadminpassword;
 63+$server = $wgDBserver;
 64+
 65+# Parse arguments
 66+for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
 67+ if ( substr( $arg, 0, 7 ) == '-prefix' ) {
 68+ $prefix = next( $argv );
 69+ $wgWDprefix = $prefix . "_";
 70+ }
 71+ else if ( substr( $arg, 0, 9 ) == '-template' ) {
 72+ $wgWDtemplate = next( $argv );
 73+ }
 74+ else if ( substr( $arg, 0, 8 ) == '-comment' ) {
 75+ $comment = next( $argv );
 76+ }
 77+ else if ( substr( $arg, 0, 7 ) == '-server' ) {
 78+ $server = next( $argv );
 79+ }
 80+ else if ( substr( $arg, 0, 9 ) == '-database' ) {
 81+ $database = next( $argv );
 82+ }
 83+ else if ( substr( $arg, 0, 5 ) == '-user' ) {
 84+ $user = next( $argv );
 85+ }
 86+ else if ( substr( $arg, 0, 9 ) == '-password' ) {
 87+ $password = next( $argv );
 88+ } else {
 89+ $args[] = $arg;
 90+ }
 91+}
 92+
 93+if ( !isset( $wgWDtemplate ) ){
 94+ echo( "SQL template should be provided!");
 95+ echo( "usage: create wikidata.php -prefix <prefix> -template <sql template> [-comment '<comment line>' -server <server> -database <database> -user <username> -password <password>]");
 96+ exit();
 97+}
 98+
 99+if ( !isset( $wgWDprefix ) ){
 100+ echo( "database prefix should be provided!");
 101+ echo( "usage: create wikidata.php -prefix <prefix> -template <sql template> [-comment '<comment line>' -database <database> -user <username> -password <password>]");
 102+ exit();
 103+}
 104+
 105+# Do a pre-emptive check to ensure we've got credentials supplied
 106+# We can't, at this stage, check them, but we can detect their absence,
 107+# which seems to cause most of the problems people whinge about
 108+if( !isset( $user ) || !isset( $password ) ) {
 109+ echo( "No superuser credentials could be found. Please provide the details\n" );
 110+ echo( "of a user with appropriate permissions to update the database. See\n" );
 111+ echo( "AdminSettings.sample for more details.\n\n" );
 112+ exit();
 113+}
 114+
 115+# Attempt to connect to the database as a privileged user
 116+# This will vomit up an error if there are permissions problems
 117+$wdDatabase = new $dbclass( $server, $user, $password, $database, 1 );
 118+
 119+if( !$wdDatabase->isOpen() ) {
 120+ # Appears to have failed
 121+ echo( "A connection to the database could not be established. Check the\n" );
 122+ echo( "values of \$wgDBadminuser and \$wgDBadminpassword.\n" );
 123+ exit();
 124+}
 125+
 126+ReadSQLFile( $wdDatabase, "/*\$wgWDprefix*/", $wgWDprefix, $wgWDtemplate );
 127+$wdDatabase->query( "DELETE FROM wikidata_sets WHERE set_prefix = '$prefix'" );
 128+$wdDatabase->query( "INSERT INTO wikidata_sets (set_prefix,set_fallback_name,set_dmid) VALUES ('$prefix','$comment',0)" );
 129+$wdDatabase->close();
 130+
131131 ?>
\ No newline at end of file