One more password bug fix, DisableCounters flag.
[lhc/web/wiklou.git] / maintenance / rebuildLinks.inc
1 <?
2
3 # Functions for rebuilding the link tracking tables; must
4 # be included within a script that also includes the Setup.
5 # See convertdb.php, for example.
6 #
7
8 function rebuildLinkTablesPass1()
9 {
10 global $wgLang;
11 $count = 0;
12 print "Rebuilding link tables (pass 1).\n";
13
14 $sql = "DROP TABLE IF EXISTS rebuildlinks";
15 wfQuery( $sql );
16
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 );
23
24 $sql = "LOCK TABLES cur READ, rebuildlinks WRITE";
25 wfQuery( $sql );
26
27 $sql = "DELETE FROM rebuildlinks";
28 wfQuery( $sql );
29
30 $sql = "SELECT cur_id,cur_namespace,cur_title,cur_text FROM cur";
31 $res = wfQuery( $sql );
32 $total = wfNumRows( $res );
33
34 $tc = Title::legalChars();
35 while ( $row = wfFetchObject( $res ) ) {
36 $id = $row->cur_id;
37 $ns = $wgLang->getNsText( $row->cur_namespace );
38 if ( "" == $ns ) {
39 $title = addslashes( $row->cur_title );
40 } else {
41 $title = addslashes( "$ns:{$row->cur_title}" );
42 }
43 $text = $row->cur_text;
44 $numlinks = preg_match_all( "/\\[\\[([{$tc}]+)(]|\\|)/", $text,
45 $m, PREG_PATTERN_ORDER );
46
47 if ( 0 != $numlinks ) {
48 $sql = "INSERT INTO rebuildlinks (rl_f_id,rl_f_title,rl_to) VALUES ";
49 for ( $i = 0; $i < $numlinks; ++$i ) {
50 $nt = Title::newFromText( $m[1][$i] );
51 $dest = addslashes( $nt->getPrefixedDBkey() );
52
53 if ( 0 != $i ) { $sql .= ","; }
54 $sql .= "({$id},'{$title}','{$dest}')";
55 }
56 wfQuery( $sql );
57 }
58 if ( ( ++$count % 1000 ) == 0 ) {
59 print "$count of $total articles scanned.\n";
60 }
61 }
62 print "$count articles scanned.\n";
63 mysql_free_result( $res );
64
65 $sql = "UNLOCK TABLES";
66 wfQuery( $sql );
67 }
68
69 function rebuildLinkTablesPass2()
70 {
71 global $wgLang;
72 $count = 0;
73 print "Rebuilding link tables (pass 2).\n";
74
75 $sql = "LOCK TABLES cur READ, rebuildlinks READ, " .
76 "links WRITE, brokenlinks WRITE, imagelinks WRITE";
77 wfQuery( $sql );
78
79 $sql = "DELETE FROM links";
80 wfQuery( $sql );
81
82 $sql = "DELETE FROM brokenlinks";
83 wfQuery( $sql );
84
85 $sql = "DELETE FROM links";
86 wfQuery( $sql );
87
88 $ins = $wgLang->getNsText( Namespace::getImage() );
89 $inslen = strlen($ins)+1;
90 $sql = "SELECT rl_f_title,rl_to FROM rebuildlinks " .
91 "WHERE rl_to LIKE '$ins:%'";
92 $res = wfQuery( $sql );
93
94 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
95 $sqlX = "";
96 $first = true;
97 while ( $row = wfFetchObject( $res ) ) {
98 $iname = addslashes( substr( $row->rl_to, $inslen ) );
99 $pname = addslashes( $row->rl_f_title );
100
101 if ( ! $first ) { $sqlX .= ","; }
102 $first = false;
103
104 $sqlX .= "('{$pname}','{$iname}')";
105 }
106 if ($sqlX != "") {
107 $sql .= $sqlX;
108 wfFreeResult( $res );
109 wfQuery( $sql );
110 }
111
112 $sql = "SELECT DISTINCT rl_to FROM rebuildlinks " .
113 "ORDER BY rl_to";
114 $res = wfQuery( $sql );
115 $count = 0;
116 $total = wfNumRows( $res );
117
118 while ( $row = wfFetchObject( $res ) ) {
119 if ( 0 == strncmp( "$ins:", $row->rl_to, $inslen ) ) { continue; }
120
121 $nt = Title::newFromDBkey( $row->rl_to );
122 $id = $nt->getArticleID();
123 $to = addslashes( $row->rl_to );
124
125 if ( 0 == $id ) {
126 $sql = "SELECT rl_f_id FROM rebuildlinks WHERE rl_to='{$to}'";
127 $res2 = wfQuery( $sql );
128
129 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
130 $first = true;
131 while ( $row2 = wfFetchObject( $res2 ) ) {
132 $from = $row2->rl_f_id;
133 if ( ! $first ) { $sql .= ","; }
134 $first = false;
135 $sql .= "({$from},'{$to}')";
136 }
137 wfFreeResult( $res2 );
138 if ( ! $first ) { wfQuery( $sql ); }
139 } else {
140 $sql = "SELECT rl_f_title FROM rebuildlinks WHERE rl_to='{$to}'";
141 $res2 = wfQuery( $sql );
142
143 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
144 $first = true;
145 while ( $row2 = wfFetchObject( $res2 ) ) {
146 $from = addslashes( $row2->rl_f_title );
147 if ( ! $first ) { $sql .= ","; }
148 $first = false;
149 $sql .= "('{$from}',{$id})";
150 }
151 wfFreeResult( $res2 );
152 if ( ! $first ) { wfQuery( $sql ); }
153 }
154 if ( ( ++$count % 1000 ) == 0 ) {
155 print "$count of $total titles processed.\n";
156 }
157 }
158 wfFreeResult( $res );
159
160 $sql = "UNLOCK TABLES";
161 wfQuery( $sql );
162
163 $sql = "DROP TABLE rebuildlinks";
164 wfQuery( $sql );
165 }
166 ?>