3 # Functions for rebuilding the link tracking tables; must
4 # be included within a script that also includes the Setup.
5 # See rebuildlinks.php, for example.
8 function rebuildLinkTablesPass1()
12 print "Rebuilding link tables (pass 1).\n";
14 $sql = "DROP TABLE IF EXISTS rebuildlinks";
15 wfQuery( $sql, DB_WRITE );
17 $sql = "CREATE TABLE rebuildlinks (
18 rl_f_id int(8) unsigned NOT NULL default 0,
19 rl_f_title varchar(255) binary NOT NULL default '',
20 rl_to varchar(255) binary NOT NULL default '',
21 INDEX rl_to (rl_to) ) TYPE=MyISAM";
22 wfQuery( $sql, DB_WRITE );
24 $sql = "LOCK TABLES cur READ, rebuildlinks WRITE, interwiki READ";
25 wfQuery( $sql, DB_WRITE );
27 $sql = "DELETE FROM rebuildlinks";
28 wfQuery( $sql, DB_WRITE );
30 $sql = "SELECT cur_id,cur_namespace,cur_title,cur_text FROM cur";
31 $res = wfQuery( $sql, DB_WRITE );
32 $total = wfNumRows( $res );
34 $tc = Title::legalChars();
35 while ( $row = wfFetchObject( $res ) ) {
37 $ns = $wgLang->getNsText( $row->cur_namespace );
39 $title = addslashes( $row->cur_title );
41 $title = addslashes( "$ns:{$row->cur_title}" );
43 $text = $row->cur_text;
44 $numlinks = preg_match_all( "/\\[\\[([{$tc}]+)(]|\\|)/", $text,
45 $m, PREG_PATTERN_ORDER );
47 if ( 0 != $numlinks ) {
49 $sql = "INSERT INTO rebuildlinks (rl_f_id,rl_f_title,rl_to) VALUES ";
50 for ( $i = 0; $i < $numlinks; ++$i ) {
51 $nt = Title::newFromText( $m[1][$i] );
55 print "error in '$ns:{$row->cur_title}' :\t'$txt'\n";
64 $dest = addslashes( $nt->getPrefixedDBkey() );
65 $sql .= "({$id},'{$title}','{$dest}')";
68 if (! $first) { wfQuery( $sql, DB_WRITE ); }
70 if ( ( ++$count % 1000 ) == 0 ) {
71 print "$count of $total articles scanned.\n";
74 print "$total articles scanned.\n";
75 mysql_free_result( $res );
77 $sql = "UNLOCK TABLES";
78 wfQuery( $sql, DB_WRITE );
81 function rebuildLinkTablesPass2()
85 print "Rebuilding link tables (pass 2).\n";
87 $sql = "LOCK TABLES cur READ, rebuildlinks READ, interwiki READ, " .
88 "links WRITE, brokenlinks WRITE, imagelinks WRITE";
89 wfQuery( $sql, DB_WRITE );
91 $sql = "DELETE FROM links";
92 wfQuery( $sql, DB_WRITE );
94 $sql = "DELETE FROM brokenlinks";
95 wfQuery( $sql, DB_WRITE );
97 $sql = "DELETE FROM links";
98 wfQuery( $sql, DB_WRITE );
100 $ins = $wgLang->getNsText( Namespace::getImage() );
101 $inslen = strlen($ins)+1;
102 $sql = "SELECT rl_f_title,rl_to FROM rebuildlinks " .
103 "WHERE rl_to LIKE '$ins:%'";
104 $res = wfQuery( $sql, DB_WRITE );
106 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
108 while ( $row = wfFetchObject( $res ) )
110 $iname = addslashes( substr( $row->rl_to, $inslen ) );
111 $pname = addslashes( $row->rl_f_title );
118 $sql .= "('{$pname}','{$iname}')";
120 wfFreeResult( $res );
121 if ( ! $first ) { wfQuery( $sql, DB_WRITE ); }
123 $sql = "SELECT DISTINCT rl_to FROM rebuildlinks ORDER BY rl_to";
124 $res = wfQuery( $sql, DB_WRITE );
126 $total = wfNumRows( $res );
128 while ( $row = wfFetchObject( $res ) ) {
129 if ( 0 == strncmp( "$ins:", $row->rl_to, $inslen ) ) { continue; }
131 $nt = Title::newFromDBkey( $row->rl_to );
134 print "error pass2: '{$row->rl_to}'\n";
137 $id = $nt->getArticleID();
138 $to = addslashes( $row->rl_to );
141 $sql = "SELECT rl_f_id FROM rebuildlinks WHERE rl_to='{$to}'";
142 $res2 = wfQuery( $sql, DB_WRITE );
144 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
146 while ( $row2 = wfFetchObject( $res2 ) )
153 $from = $row2->rl_f_id;
154 $sql .= "({$from},'{$to}')";
156 wfFreeResult( $res2 );
157 if ( ! $first ) { wfQuery( $sql, DB_WRITE ); }
159 $sql = "SELECT rl_f_title FROM rebuildlinks WHERE rl_to='{$to}'";
160 $res2 = wfQuery( $sql, DB_WRITE );
162 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
164 while ( $row2 = wfFetchObject( $res2 ) )
171 $from = addslashes( $row2->rl_f_title );
172 $sql .= "('{$from}',{$id})";
174 wfFreeResult( $res2 );
175 if ( ! $first ) { wfQuery( $sql, DB_WRITE ); }
177 if ( ( ++$count % 1000 ) == 0 ) {
178 print "$count of $total titles processed.\n";
181 wfFreeResult( $res );
183 $sql = "UNLOCK TABLES";
184 wfQuery( $sql, DB_WRITE );
186 $sql = "DROP TABLE rebuildlinks";
187 wfQuery( $sql, DB_WRITE );