Per Raymond, follow-up r95321: LinkSearch is in core too
[lhc/web/wiklou.git] / maintenance / addwiki.php
1 <?php
2 /**
3 * @defgroup Wikimedia Wikimedia
4 */
5
6 /**
7 * Add a new wiki
8 * Wikimedia specific!
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup Maintenance
27 * @ingroup Wikimedia
28 */
29
30 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
31
32 class AddWiki extends Maintenance {
33 public function __construct() {
34 global $wgNoDBParam;
35
36 parent::__construct();
37 $this->mDescription = "Add a new wiki to the family. Wikimedia specific!";
38 $this->addArg( 'language', 'Language code of new site, e.g. en' );
39 $this->addArg( 'site', 'Type of site, e.g. wikipedia' );
40 $this->addArg( 'dbname', 'Name of database to create, e.g. enwiki' );
41 $this->addArg( 'domain', 'Domain name of the wiki, e.g. en.wikipedia.org' );
42
43 $wgNoDBParam = true;
44 }
45
46 public function getDbType() {
47 return Maintenance::DB_ADMIN;
48 }
49
50 public function execute() {
51 global $IP, $wgDefaultExternalStore, $wmfVersionNumber;
52 if ( !$wmfVersionNumber ) { // set in CommonSettings.php
53 $this->error( '$wmfVersionNumber is not set, please use MWScript.php wrapper.', true );
54 }
55
56 $lang = $this->getArg( 0 );
57 $site = $this->getArg( 1 );
58 $dbName = $this->getArg( 2 );
59 $domain = $this->getArg( 3 );
60 $languageNames = Language::getLanguageNames();
61
62 if ( !isset( $languageNames[$lang] ) ) {
63 $this->error( "Language $lang not found in Names.php", true );
64 }
65 $name = $languageNames[$lang];
66
67 $dbw = wfGetDB( DB_MASTER );
68 $common = "/home/wikipedia/common";
69
70 $this->output( "Creating database $dbName for $lang.$site ($name)\n" );
71
72 # Set up the database
73 $dbw->query( "SET table_type=Innodb" );
74 $dbw->query( "CREATE DATABASE $dbName" );
75 $dbw->selectDB( $dbName );
76
77 $this->output( "Initialising tables\n" );
78 $dbw->sourceFile( $this->getDir() . '/tables.sql' );
79 $dbw->sourceFile( "$IP/extensions/OAI/update_table.sql" );
80 $dbw->sourceFile( "$IP/extensions/AntiSpoof/sql/patch-antispoof.mysql.sql" );
81 $dbw->sourceFile( "$IP/extensions/CheckUser/cu_changes.sql" );
82 $dbw->sourceFile( "$IP/extensions/CheckUser/cu_log.sql" );
83 $dbw->sourceFile( "$IP/extensions/TitleKey/titlekey.sql" );
84 $dbw->sourceFile( "$IP/extensions/Oversight/hidden.sql" );
85 $dbw->sourceFile( "$IP/extensions/GlobalBlocking/localdb_patches/setup-global_block_whitelist.sql" );
86 $dbw->sourceFile( "$IP/extensions/AbuseFilter/abusefilter.tables.sql" );
87 $dbw->sourceFile( "$IP/extensions/PrefStats/patches/PrefStats.sql" );
88 $dbw->sourceFile( "$IP/extensions/ProofreadPage/ProofreadPage.sql" );
89 $dbw->sourceFile( "$IP/extensions/ClickTracking/patches/ClickTrackingEvents.sql" );
90 $dbw->sourceFile( "$IP/extensions/ClickTracking/patches/ClickTracking.sql" );
91 $dbw->sourceFile( "$IP/extensions/UserDailyContribs/patches/UserDailyContribs.sql" );
92
93 $dbw->query( "INSERT INTO site_stats(ss_row_id) VALUES (1)" );
94
95 # Initialise external storage
96 if ( is_array( $wgDefaultExternalStore ) ) {
97 $stores = $wgDefaultExternalStore;
98 } elseif ( $wgDefaultExternalStore ) {
99 $stores = array( $wgDefaultExternalStore );
100 } else {
101 $stores = array();
102 }
103 if ( count( $stores ) ) {
104 global $wgDBuser, $wgDBpassword, $wgExternalServers;
105 foreach ( $stores as $storeURL ) {
106 $m = array();
107 if ( !preg_match( '!^DB://(.*)$!', $storeURL, $m ) ) {
108 continue;
109 }
110
111 $cluster = $m[1];
112 $this->output( "Initialising external storage $cluster...\n" );
113
114 # Hack
115 $wgExternalServers[$cluster][0]['user'] = $wgDBuser;
116 $wgExternalServers[$cluster][0]['password'] = $wgDBpassword;
117
118 $store = new ExternalStoreDB;
119 $extdb = $store->getMaster( $cluster );
120 $extdb->query( "SET table_type=InnoDB" );
121 $extdb->query( "CREATE DATABASE $dbName" );
122 $extdb->selectDB( $dbName );
123
124 # Hack x2
125 $blobsTable = $store->getTable( $extdb );
126 $sedCmd = "sed s/blobs\\\\\\>/$blobsTable/ " . $this->getDir() . "/storage/blobs.sql";
127 $blobsFile = popen( $sedCmd, 'r' );
128 $extdb->sourceStream( $blobsFile );
129 pclose( $blobsFile );
130 $extdb->commit();
131 }
132 }
133
134 $title = Title::newFromText( wfMessage( 'mainpage' )->inLanguage( $lang )->useDatabase( false )->plain() );
135 $this->output( "Writing main page to " . $title->getPrefixedDBkey() . "\n" );
136 $article = new Article( $title );
137 $ucsite = ucfirst( $site );
138
139 $article->doEdit( $this->getFirstArticle( $ucsite, $name ), '', EDIT_NEW | EDIT_AUTOSUMMARY );
140
141 $this->output( "Adding to dblists\n" );
142
143 # Add to dblist
144 $file = fopen( "$common/all.dblist", "a" );
145 fwrite( $file, "$dbName\n" );
146 fclose( $file );
147
148 # Update the sublists
149 shell_exec( "cd $common && ./refresh-dblist" );
150
151 # Add to wikiversions.dat
152 $file = fopen( "$common/wikiversions.dat", "a" );
153 fwrite( $file, "$dbName php-$wmfVersionNumber\n" );
154 fclose( $file );
155 # Rebuild wikiversions.cdb
156 shell_exec( "cd $common/multiversion && ./refreshWikiversionsCDB" );
157
158 # print "Constructing interwiki SQL\n";
159 # Rebuild interwiki tables
160 # passthru( '/home/wikipedia/conf/interwiki/update' );
161
162 $time = wfTimestamp( TS_RFC2822 );
163 // These arguments need to be escaped twice: once for echo and once for at
164 $escDbName = wfEscapeShellArg( wfEscapeShellArg( $dbName ) );
165 $escTime = wfEscapeShellArg( wfEscapeShellArg( $time ) );
166 $escUcsite = wfEscapeShellArg( wfEscapeShellArg( $ucsite ) );
167 $escName = wfEscapeShellArg( wfEscapeShellArg( $name ) );
168 $escLang = wfEscapeShellArg( wfEscapeShellArg( $lang ) );
169 $escDomain = wfEscapeShellArg( wfEscapeShellArg( $domain ) );
170 shell_exec( "echo notifyNewProjects $escDbName $escTime $escUcsite $escName $escLang $escDomain | at now + 15 minutes" );
171
172 $this->output( "Script ended. You still have to:
173 * Add any required settings in InitialiseSettings.php
174 * Run sync-common-all
175 * Run /home/wikipedia/conf/interwiki/update
176 " );
177 }
178
179 private function getFirstArticle( $ucsite, $name ) {
180 return <<<EOT
181 ==This subdomain is reserved for the creation of a [[wikimedia:Our projects|$ucsite]] in '''[[w:en:{$name}|{$name}]]''' language==
182
183 * Please '''do not start editing''' this new site. This site has a test project on the [[incubator:|Wikimedia Incubator]] (or on the [[betawikiversity:|BetaWikiversity]] or on the [[oldwikisource:|Old Wikisource]]) and it will be imported to here.
184
185 * If you would like to help translating the interface to this language, please do not translate here, but go to [[translatewiki:|translatewiki]], a special wiki for translating the interface. That way everyone can use it on every wiki using the [[mw:|same software]].
186
187 * For information about how to edit and for other general help, see [[m:Help:Contents|Help on Wikimedia's Meta-Wiki]] or [[mw:Help:Contents|Help on MediaWiki.org]].
188
189 == Sister projects ==
190 <span class="plainlinks">
191 [http://www.wikipedia.org Wikipedia] |
192 [http://www.wiktionary.org Wiktonary] |
193 [http://www.wikibooks.org Wikibooks] |
194 [http://www.wikinews.org Wikinews] |
195 [http://www.wikiquote.org Wikiquote] |
196 [http://www.wikisource.org Wikisource]
197 [http://www.wikiversity.org Wikiversity]
198 </span>
199
200 See Wikimedia's [[m:|Meta-Wiki]] for the coordination of these projects.
201
202 EOT;
203 }
204 }
205
206 $maintClass = "AddWiki";
207 require_once( RUN_MAINTENANCE_IF_MAIN );