8 function refreshLinks( $start, $newOnly = false, $maxLag = false, $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
9 global $wgUser, $wgParser, $wgUseTidy;
11 $reportingInterval = 100;
12 $fname = 'refreshLinks';
13 $dbr = wfGetDB( DB_SLAVE );
14 $start = intval( $start );
16 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
17 $wgUser->setOption('math', MW_MATH_SOURCE);
19 # Don't generate extension images (e.g. Timeline)
20 if( method_exists( $wgParser, "clearTagHooks" ) ) {
21 $wgParser->clearTagHooks();
27 $what = $redirectsOnly ? "redirects" : "links";
29 if( $oldRedirectsOnly ) {
30 # This entire code path is cut-and-pasted from below. Hurrah.
34 "LEFT JOIN redirect ON page_id=rd_from ".
35 "WHERE page_is_redirect=1 AND rd_from IS NULL AND ".
36 ($end == 0 ? "page_id >= $start"
37 : "page_id BETWEEN $start AND $end"),
40 $num = $dbr->numRows( $res );
41 print "Refreshing $num old redirects from $start...\n";
43 while( $row = $dbr->fetchObject( $res ) ) {
44 if ( !( ++$i % $reportingInterval ) ) {
46 wfWaitForSlaves( $maxLag );
48 fixRedirect( $row->page_id );
50 } elseif( $newOnly ) {
51 print "Refreshing $what from ";
52 $res = $dbr->select( 'page',
56 "page_id >= $start" ),
59 $num = $dbr->numRows( $res );
60 print "$num new articles...\n";
63 while ( $row = $dbr->fetchObject( $res ) ) {
64 if ( !( ++$i % $reportingInterval ) ) {
66 wfWaitForSlaves( $maxLag );
69 fixRedirect( $row->page_id );
71 fixLinksFromArticle( $row->page_id );
74 print "Refreshing $what table.\n";
76 $end = $dbr->selectField( 'page', 'max(page_id)', false );
78 print("Starting from page_id $start of $end.\n");
80 for ($id = $start; $id <= $end; $id++) {
82 if ( !($id % $reportingInterval) ) {
84 wfWaitForSlaves( $maxLag );
89 fixLinksFromArticle( $id );
94 function fixRedirect( $id ){
95 global $wgTitle, $wgArticle;
97 $wgTitle = Title::newFromID( $id );
98 $dbw = wfGetDB( DB_MASTER );
100 if ( is_null( $wgTitle ) ) {
103 $wgArticle = new Article($wgTitle);
105 $rt = $wgArticle->followRedirect();
107 if($rt == false || !is_object($rt))
110 $wgArticle->updateRedirectOn($dbw,$rt);
113 function fixLinksFromArticle( $id ) {
114 global $wgTitle, $wgParser;
116 $wgTitle = Title::newFromID( $id );
117 $dbw = wfGetDB( DB_MASTER );
119 $linkCache =& LinkCache::singleton();
122 if ( is_null( $wgTitle ) ) {
127 $revision = Revision::newFromTitle( $wgTitle );
132 $options = new ParserOptions;
133 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
134 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
136 $dbw->immediateCommit();
140 * Removes non-existing links from pages from pagelinks, imagelinks,
141 * categorylinks, templatelinks and externallinks tables.
144 * @param $batchSize The size of deletion batches
146 * @author Merlijn van Deen <valhallasw@arctus.nl>
148 function deleteLinksFromNonexistent( $maxLag = 0, $batchSize = 100 ) {
149 wfWaitForSlaves( $maxLag );
151 $dbw = wfGetDB( DB_MASTER );
153 $lb = wfGetLBFactory()->newMainLB();
154 $dbr = $lb->getConnection( DB_SLAVE );
155 $dbr->bufferResults( false );
157 $linksTables = array( // table name => page_id field
158 'pagelinks' => 'pl_from',
159 'imagelinks' => 'il_from',
160 'categorylinks' => 'cl_from',
161 'templatelinks' => 'tl_from',
162 'externallinks' => 'el_from',
165 foreach ( $linksTables as $table => $field ) {
166 print "Retrieving illegal entries from $table... ";
168 // SELECT DISTINCT( $field ) FROM $table LEFT JOIN page ON $field=page_id WHERE page_id IS NULL;
169 $results = $dbr->select( array( $table, 'page' ),
171 array('page_id' => null ),
174 array( 'page' => array( 'LEFT JOIN', "$field=page_id"))
181 foreach( $results as $row ) {
183 $list[] = $row->$field;
184 if ( ( $counter % $batchSize ) == 0 ) {
186 $dbw->delete( $table, array( $field => $list ), __METHOD__ );
188 print $counter . "..";
194 if (count($list) > 0) {
195 $dbw->delete( $table, array( $field => $list ), __METHOD__ );