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 # Compose DB key array
21 foreach ( $wgAllMessagesEn as $key => $enValue ) {
22 $title = Title::newFromText( $key );
23 $dbkeys[$title->getDBkey()] = 1;
26 $sql = "SELECT cur_id, cur_title FROM cur WHERE cur_namespace= " . NS_MEDIAWIKI;
27 $res = wfQuery( $sql, DB_READ );
29 # Compile target array
31 while ( $row = wfFetchObject( $res ) ) {
32 if ( !array_key_exists( $row->cur_title, $dbkeys ) ) {
33 $targets[$row->cur_title] = 1;
38 # Create redirects from destination to source
39 if ( $phase == 0 || $phase == 1 ) {
40 print "Creating redirects\n";
41 foreach ( $targets as $partial => $dummy ) {
43 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
44 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
46 if ( $nt->createRedirect( $ot, "" ) ) {
49 print "not redirected\n";
53 print "\nRedirects created. Update live script files now.\nPress ENTER to continue.\n\n";
59 if ( $phase == 0 || $phase == 2 ) {
60 print "\nMoving pages...\n";
61 foreach ( $targets as $partial => $dummy ) {
62 $ot = Title::makeTitle( NS_MEDIAWIKI, $partial );
63 $nt = Title::makeTitle( NS_TEMPLATE, $partial );
66 if ( $ot->moveNoAuth( $nt ) === true ) {
72 while ( count( $wgDeferredUpdateList ) ) {
73 $up = array_pop( $wgDeferredUpdateList );
80 if ( $phase == 0 || $phase == 3 ) {
81 print "\nConverting text...\n";
84 $options = ParserOptions::newFromUser( $wgUser );
85 $completedTitles = array();
86 $titleChars = Title::legalChars();
87 $mediaWiki = $wgLang->getNsText( NS_MEDIAWIKI );
88 $template = $wgLang->getNsText( NS_TEMPLATE );
89 $linkRegex = "/\[\[$mediaWiki:([$titleChars]*?)\]\]/";
90 $msgRegex = "/{{msg:([$titleChars]*?)}}/";
92 foreach ( $targets as $partial => $dummy ) {
93 $dest = Title::makeTitle( NS_MEDIAWIKI, $partial );
94 $linksTo = $dest->getLinksTo();
95 foreach( $linksTo as $source ) {
96 $pdbk = $source->getPrefixedDBkey();
97 if ( !array_key_exists( $pdbk, $completedTitles ) ) {
98 $completedTitles[$pdbk] = 1;
99 $id = $source->getArticleID();
100 $row = wfGetArray( 'cur', array( 'cur_text' ),
101 array( 'cur_id' => $source->getArticleID() ) );
102 $parser->startExternalParse( $source, $options, OT_WIKI );
103 $text = $parser->strip( $row->cur_text, $stripState, false );
105 $text = preg_replace( $msgRegex, "{{\$1}}", $text );
106 # [[MediaWiki:]] -> [[Template:]]
107 $text = preg_replace_callback( $linkRegex, "wfReplaceMediaWiki", $text );
108 $text = $parser->unstrip( $text, $stripState );
109 $text = $parser->unstripNoWiki( $text, $stripState );
110 if ( $text != $row->cur_text ) {
112 $art = new Article( $source );
113 $art->quickEdit( $text );
124 #--------------------------------------------------------------------------------------------------------------
125 function wfReplaceMediaWiki( $m ) {
126 global $targets, $template, $replaceCount;
127 $title = Title::newFromText( $m[1] );
128 $partial = $title->getDBkey();
130 if ( array_key_exists( $partial, $targets ) ) {
131 $text = "[[$template:{$m[1]}]]";