047476e91b0d2e81e129ca40be81dbfb8773c78a
[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 rebuildlinks.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, DB_WRITE );
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, DB_WRITE );
23
24 $sql = "LOCK TABLES cur READ, rebuildlinks WRITE, interwiki READ";
25 wfQuery( $sql, DB_WRITE );
26
27 $sql = "DELETE FROM rebuildlinks";
28 wfQuery( $sql, DB_WRITE );
29
30 $sql = "SELECT cur_id,cur_namespace,cur_title,cur_text FROM cur";
31 $res = wfQuery( $sql, DB_WRITE );
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 $first = true;
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] );
52 if (! $nt)
53 {
54 $txt = $m[1][$i];
55 print "error in '$ns:{$row->cur_title}' :\t'$txt'\n";
56 continue;
57 }
58
59 if (!$first)
60 $sql .= ",";
61 else
62 $first = false;
63
64 $dest = addslashes( $nt->getPrefixedDBkey() );
65 $sql .= "({$id},'{$title}','{$dest}')";
66 }
67
68 if (! $first) { wfQuery( $sql, DB_WRITE ); }
69 }
70 if ( ( ++$count % 1000 ) == 0 ) {
71 print "$count of $total articles scanned.\n";
72 }
73 }
74 print "$total articles scanned.\n";
75 mysql_free_result( $res );
76
77 $sql = "UNLOCK TABLES";
78 wfQuery( $sql, DB_WRITE );
79 }
80
81 function rebuildLinkTablesPass2()
82 {
83 global $wgLang;
84 $count = 0;
85 print "Rebuilding link tables (pass 2).\n";
86
87 $sql = "LOCK TABLES cur READ, rebuildlinks READ, interwiki READ, " .
88 "links WRITE, brokenlinks WRITE, imagelinks WRITE";
89 wfQuery( $sql, DB_WRITE );
90
91 $sql = "DELETE FROM links";
92 wfQuery( $sql, DB_WRITE );
93
94 $sql = "DELETE FROM brokenlinks";
95 wfQuery( $sql, DB_WRITE );
96
97 $sql = "DELETE FROM links";
98 wfQuery( $sql, DB_WRITE );
99
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 );
105
106 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
107 $first = true;
108 while ( $row = wfFetchObject( $res ) )
109 {
110 $iname = addslashes( substr( $row->rl_to, $inslen ) );
111 $pname = addslashes( $row->rl_f_title );
112
113 if ( ! $first )
114 $sql .= ",";
115 else
116 $first = false;
117
118 $sql .= "('{$pname}','{$iname}')";
119 }
120 wfFreeResult( $res );
121 if ( ! $first ) { wfQuery( $sql, DB_WRITE ); }
122
123 $sql = "SELECT DISTINCT rl_to FROM rebuildlinks ORDER BY rl_to";
124 $res = wfQuery( $sql, DB_WRITE );
125 $count = 0;
126 $total = wfNumRows( $res );
127
128 while ( $row = wfFetchObject( $res ) ) {
129 if ( 0 == strncmp( "$ins:", $row->rl_to, $inslen ) ) { continue; }
130
131 $nt = Title::newFromDBkey( $row->rl_to );
132 if (! $nt)
133 {
134 print "error pass2: '{$row->rl_to}'\n";
135 continue;
136 }
137 $id = $nt->getArticleID();
138 $to = addslashes( $row->rl_to );
139
140 if ( 0 == $id ) {
141 $sql = "SELECT rl_f_id FROM rebuildlinks WHERE rl_to='{$to}'";
142 $res2 = wfQuery( $sql, DB_WRITE );
143
144 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
145 $first = true;
146 while ( $row2 = wfFetchObject( $res2 ) )
147 {
148 if (! $first)
149 $sql .= ",";
150 else
151 $first = false;
152
153 $from = $row2->rl_f_id;
154 $sql .= "({$from},'{$to}')";
155 }
156 wfFreeResult( $res2 );
157 if ( ! $first ) { wfQuery( $sql, DB_WRITE ); }
158 } else {
159 $sql = "SELECT rl_f_title FROM rebuildlinks WHERE rl_to='{$to}'";
160 $res2 = wfQuery( $sql, DB_WRITE );
161
162 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
163 $first = true;
164 while ( $row2 = wfFetchObject( $res2 ) )
165 {
166 if (! $first)
167 $sql .= ",";
168 else
169 $first = false;
170
171 $from = addslashes( $row2->rl_f_title );
172 $sql .= "('{$from}',{$id})";
173 }
174 wfFreeResult( $res2 );
175 if ( ! $first ) { wfQuery( $sql, DB_WRITE ); }
176 }
177 if ( ( ++$count % 1000 ) == 0 ) {
178 print "$count of $total titles processed.\n";
179 }
180 }
181 wfFreeResult( $res );
182
183 $sql = "UNLOCK TABLES";
184 wfQuery( $sql, DB_WRITE );
185
186 $sql = "DROP TABLE rebuildlinks";
187 wfQuery( $sql, DB_WRITE );
188 }
189 ?>