Remove Wikipedia/Wikimedia specific translations
[lhc/web/wiklou.git] / maintenance / refreshLinks.inc
1 <?php
2 /**
3 * @todo document
4 * @addtogroup Maintenance
5 */
6
7 /** */
8 define( "REPORTING_INTERVAL", 100 );
9 #define( "REPORTING_INTERVAL", 1 );
10
11 function refreshLinks( $start, $newOnly = false, $maxLag = false, $end = 0, $redirectsOnly = false ) {
12 global $wgUser, $wgParser, $wgUseImageResize, $wgUseTidy;
13
14 $fname = 'refreshLinks';
15 $dbr = wfGetDB( DB_SLAVE );
16 $start = intval( $start );
17
18 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
19 $wgUser->setOption('math', MW_MATH_SOURCE);
20
21 # Don't generate extension images (e.g. Timeline)
22 $wgParser->clearTagHooks();
23
24 # Don't generate thumbnail images
25 $wgUseImageResize = false;
26 $wgUseTidy = false;
27
28 $what = ($redirectsOnly)? "redirects" : "links";
29
30 if ( $newOnly ) {
31 print "Refreshing $what from ";
32 $res = $dbr->select( 'page',
33 array( 'page_id' ),
34 array(
35 'page_is_new' => 1,
36 "page_id > $start" ),
37 $fname
38 );
39 $num = $dbr->numRows( $res );
40 print "$num new articles...\n";
41
42 $i = 0;
43 while ( $row = $dbr->fetchObject( $res ) ) {
44 if ( !( ++$i % REPORTING_INTERVAL ) ) {
45 print "$i\n";
46 wfWaitForSlaves( $maxLag );
47 }
48 if($redirectsOnly)
49 fixRedirect( $row->page_id );
50 else
51 fixLinksFromArticle( $row->page_id );
52 }
53 } else {
54 print "Refreshing $what table.\n";
55 if ( !$end ) {
56 $end = $dbr->selectField( 'page', 'max(page_id)', false );
57 }
58 print("Starting from page_id $start of $end.\n");
59
60 for ($id = $start; $id <= $end; $id++) {
61
62 if ( !($id % REPORTING_INTERVAL) ) {
63 print "$id\n";
64 wfWaitForSlaves( $maxLag );
65 }
66 if($redirectsOnly)
67 fixRedirect( $id );
68 else
69 fixLinksFromArticle( $id );
70 }
71 }
72 }
73
74 function fixRedirect( $id ){
75 global $wgTitle, $wgArticle;
76
77 $wgTitle = Title::newFromID( $id );
78 $dbw = wfGetDB( DB_MASTER );
79
80 if ( is_null( $wgTitle ) ) {
81 return;
82 }
83 $wgArticle = new Article($wgTitle);
84
85 $rt = $wgArticle->followRedirect();
86
87 if($rt == false || !is_object($rt))
88 return;
89
90 $wgArticle->updateRedirectOn($dbw,$rt);
91 }
92
93 function fixLinksFromArticle( $id ) {
94 global $wgTitle, $wgParser;
95
96 $wgTitle = Title::newFromID( $id );
97 $dbw = wfGetDB( DB_MASTER );
98
99 $linkCache =& LinkCache::singleton();
100 $linkCache->clear();
101
102 if ( is_null( $wgTitle ) ) {
103 return;
104 }
105 $dbw->begin();
106
107 $revision = Revision::newFromTitle( $wgTitle );
108 if ( !$revision ) {
109 return;
110 }
111
112 $options = new ParserOptions;
113 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
114 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
115 $update->doUpdate();
116 $dbw->immediateCommit();
117 }
118
119 function deleteLinksFromNonexistent( $maxLag = 0 ) {
120 $fname = 'deleteLinksFromNonexistent';
121
122 wfWaitForSlaves( $maxLag );
123
124 $dbw = wfGetDB( DB_WRITE );
125
126 $linksTables = array(
127 'pagelinks' => 'pl_from',
128 'imagelinks' => 'il_from',
129 'categorylinks' => 'cl_from',
130 'templatelinks' => 'tl_from',
131 'externallinks' => 'el_from',
132 );
133
134 $page = $dbw->tableName( 'page' );
135
136
137 foreach ( $linksTables as $table => $field ) {
138 if ( !$dbw->ping() ) {
139 print "DB disconnected, reconnecting...";
140 while ( !$dbw->ping() ) {
141 print ".";
142 sleep(10);
143 }
144 print "\n";
145 }
146
147 $pTable = $dbw->tableName( $table );
148 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
149
150 print "Deleting $table from non-existent articles...";
151 $dbw->query( $sql, $fname );
152 print " fixed " .$dbw->affectedRows() . " row(s)\n";
153 }
154 }
155
156 ?>