4 * @addtogroup Maintenance
8 define( "REPORTING_INTERVAL", 100 );
9 #define( "REPORTING_INTERVAL", 1 );
11 function refreshLinks( $start, $newOnly = false, $maxLag = false, $end = 0, $redirectsOnly = false, $oldRedirectsOnly = false ) {
12 global $wgUser, $wgParser, $wgUseImageResize, $wgUseTidy;
14 $fname = 'refreshLinks';
15 $dbr = wfGetDB( DB_SLAVE );
16 $start = intval( $start );
18 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
19 $wgUser->setOption('math', MW_MATH_SOURCE);
21 # Don't generate extension images (e.g. Timeline)
22 $wgParser->clearTagHooks();
24 # Don't generate thumbnail images
25 $wgUseImageResize = false;
28 $what = $redirectsOnly ? "redirects" : "links";
30 if( $oldRedirectsOnly ) {
31 # This entire code path is cut-and-pasted from below. Hurrah.
35 "LEFT JOIN redirect ON page_id=rd_from ".
36 "WHERE page_is_redirect=1 AND rd_from IS NULL AND ".
37 ($end == 0 ? "page_id >= $start"
38 : "page_id BETWEEN $start AND $end"),
41 $num = $dbr->numRows( $res );
42 print "Refreshing $num old redirects from $start...\n";
44 while( $row = $dbr->fetchObject( $res ) ) {
45 if ( !( ++$i % REPORTING_INTERVAL ) ) {
47 wfWaitForSlaves( $maxLag );
49 fixRedirect( $row->page_id );
51 } elseif( $newOnly ) {
52 print "Refreshing $what from ";
53 $res = $dbr->select( 'page',
57 "page_id >= $start" ),
60 $num = $dbr->numRows( $res );
61 print "$num new articles...\n";
64 while ( $row = $dbr->fetchObject( $res ) ) {
65 if ( !( ++$i % REPORTING_INTERVAL ) ) {
67 wfWaitForSlaves( $maxLag );
70 fixRedirect( $row->page_id );
72 fixLinksFromArticle( $row->page_id );
75 print "Refreshing $what table.\n";
77 $end = $dbr->selectField( 'page', 'max(page_id)', false );
79 print("Starting from page_id $start of $end.\n");
81 for ($id = $start; $id <= $end; $id++) {
83 if ( !($id % REPORTING_INTERVAL) ) {
85 wfWaitForSlaves( $maxLag );
90 fixLinksFromArticle( $id );
95 function fixRedirect( $id ){
96 global $wgTitle, $wgArticle;
98 $wgTitle = Title::newFromID( $id );
99 $dbw = wfGetDB( DB_MASTER );
101 if ( is_null( $wgTitle ) ) {
104 $wgArticle = new Article($wgTitle);
106 $rt = $wgArticle->followRedirect();
108 if($rt == false || !is_object($rt))
111 $wgArticle->updateRedirectOn($dbw,$rt);
114 function fixLinksFromArticle( $id ) {
115 global $wgTitle, $wgParser;
117 $wgTitle = Title::newFromID( $id );
118 $dbw = wfGetDB( DB_MASTER );
120 $linkCache =& LinkCache::singleton();
123 if ( is_null( $wgTitle ) ) {
128 $revision = Revision::newFromTitle( $wgTitle );
133 $options = new ParserOptions;
134 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
135 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
137 $dbw->immediateCommit();
140 function deleteLinksFromNonexistent( $maxLag = 0 ) {
141 $fname = 'deleteLinksFromNonexistent';
143 wfWaitForSlaves( $maxLag );
145 $dbw = wfGetDB( DB_MASTER );
147 $linksTables = array(
148 'pagelinks' => 'pl_from',
149 'imagelinks' => 'il_from',
150 'categorylinks' => 'cl_from',
151 'templatelinks' => 'tl_from',
152 'externallinks' => 'el_from',
155 $page = $dbw->tableName( 'page' );
158 foreach ( $linksTables as $table => $field ) {
159 if ( !$dbw->ping() ) {
160 print "DB disconnected, reconnecting...";
161 while ( !$dbw->ping() ) {
168 $pTable = $dbw->tableName( $table );
169 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
171 print "Deleting $table from non-existent articles...";
172 $dbw->query( $sql, $fname );
173 print " fixed " .$dbw->affectedRows() . " row(s)\n";