3 function isTemplateInitialised() {
4 $sql = "SELECT 1 FROM cur WHERE cur_namespace=" . NS_TEMPLATE . " LIMIT 1";
5 $res = wfQuery( $sql, DB_READ );
6 return wfNumRows( $res ) ? true : false;
9 function moveCustomMessages( $phase ) {
10 global $wgUser, $wgAllMessagesEn, $wgDeferredUpdateList, $wgLang;
11 global $targets, $template, $replaceCount;
14 $wgUser->setLoaded( true ); # Don't load from DB
15 $wgUser->setName( "Template namespace initialisation script" );
16 $wgUser->addRight( "bot" );
18 wfIgnoreSQLErrors( true );
20 # Compose DB key array
23 foreach ( $wgAllMessagesEn as $key => $enValue ) {
24 $title = Title::newFromText( $key );
25 $dbkeys[$title->getDBkey()] = 1;
28 $sql = "SELECT cur_id, cur_title FROM cur WHERE cur_namespace= " . NS_MEDIAWIKI;
29 $res = wfQuery( $sql, DB_READ );
31 # Compile target array
33 while ( $row = wfFetchObject( $res ) ) {
34 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
35 $targets[$row->cur_title] = 1;
40 # Create redirects from destination to source
41 if ( $phase == 0 || $phase == 1 ) {
42 print "Creating redirects\n";
43 foreach ( $targets as $partial => $dummy ) {
45 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
46 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
48 if ( $nt->createRedirect( $ot, "" ) ) {
51 print "not redirected\n";
55 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
61 if ( $phase == 0 || $phase == 2 ) {
62 print "\nMoving pages...\n";
63 foreach ( $targets as $partial => $dummy ) {
64 wfQuery( "BEGIN", DB_WRITE );
65 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
66 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
69 if ( $ot->moveNoAuth( $nt ) === true ) {
75 while ( count( $wgDeferredUpdateList ) ) {
76 $up = array_pop( $wgDeferredUpdateList );
79 wfQuery( "COMMIT", DB_WRITE );
84 if ( $phase == 0 || $phase == 3 ) {
85 print "\nConverting text...\n";
88 $options = ParserOptions::newFromUser( $wgUser );
89 $completedTitles = array();
90 $titleChars = Title::legalChars();
91 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
92 $template = $wgLang->getNsText( NS_TEMPLATE );
93 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
94 $msgRegex = "/{{msg:([$titleChars]*?)}}/";
96 foreach ( $targets as $partial => $dummy ) {
97 $dest = Title::makeTitle( NS_MEDIAWIKI, $partial );
98 $linksTo = $dest->getLinksTo();
99 foreach( $linksTo as $source ) {
100 wfQuery( "BEGIN", DB_WRITE );
101 $pdbk = $source->getPrefixedDBkey();
102 if ( !array_key_exists( $pdbk, $completedTitles ) ) {
103 $completedTitles[$pdbk] = 1;
104 $id = $source->getArticleID();
105 $row = wfGetArray( 'cur', array( 'cur_text' ),
106 array( 'cur_id' => $source->getArticleID() ) );
107 $parser->startExternalParse( $source, $options, OT_WIKI );
108 $text = $parser->strip( $row->cur_text, $stripState, false );
110 $text = preg_replace( $msgRegex, "{{\$1}}", $text );
111 # [[MediaWiki:]] -> [[Template:]]
112 $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
113 $text = $parser->unstrip( $text, $stripState );
114 $text = $parser->unstripNoWiki( $text, $stripState );
115 if ( $text != $row->cur_text ) {
117 $art = new Article( $source );
118 $art->updateArticle( $text, "", false, false );
119 # Do deferred updates
120 while ( count( $wgDeferredUpdateList ) ) {
121 $up = array_pop( $wgDeferredUpdateList );
128 wfQuery( "COMMIT", DB_WRITE );
135 #--------------------------------------------------------------------------------------------------------------
136 function wfReplaceMediaWiki( $m ) {
137 global $targets, $template, $replaceCount;
138 $title = Title::newFromText( $m[1] );
139 $partial = $title->getDBkey();
141 if ( array_key_exists( $partial, $targets ) ) {
142 $text = "[[$template:{$m[1]}]]";