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 ) {
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";
31 print "Refreshing $what from ";
32 $res = $dbr->select( 'page',
39 $num = $dbr->numRows( $res );
40 print "$num new articles...\n";
43 while ( $row = $dbr->fetchObject( $res ) ) {
44 if ( !( ++$i % REPORTING_INTERVAL ) ) {
46 wfWaitForSlaves( $maxLag );
49 fixRedirect( $row->page_id );
51 fixLinksFromArticle( $row->page_id );
54 print "Refreshing $what table.\n";
56 $end = $dbr->selectField( 'page', 'max(page_id)', false );
58 print("Starting from page_id $start of $end.\n");
60 for ($id = $start; $id <= $end; $id++) {
62 if ( !($id % REPORTING_INTERVAL) ) {
64 wfWaitForSlaves( $maxLag );
69 fixLinksFromArticle( $id );
74 function fixRedirect( $id ){
75 global $wgTitle, $wgArticle;
77 $wgTitle = Title::newFromID( $id );
78 $dbw = wfGetDB( DB_MASTER );
80 if ( is_null( $wgTitle ) ) {
83 $wgArticle = new Article($wgTitle);
85 $rt = $wgArticle->followRedirect();
87 if($rt == false || !is_object($rt))
90 $wgArticle->updateRedirectOn($dbw,$rt);
93 function fixLinksFromArticle( $id ) {
94 global $wgTitle, $wgParser;
96 $wgTitle = Title::newFromID( $id );
97 $dbw = wfGetDB( DB_MASTER );
99 $linkCache =& LinkCache::singleton();
102 if ( is_null( $wgTitle ) ) {
107 $revision = Revision::newFromTitle( $wgTitle );
112 $options = new ParserOptions;
113 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
114 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
116 $dbw->immediateCommit();
119 function deleteLinksFromNonexistent( $maxLag = 0 ) {
120 $fname = 'deleteLinksFromNonexistent';
122 wfWaitForSlaves( $maxLag );
124 $dbw = wfGetDB( DB_WRITE );
126 $linksTables = array(
127 'pagelinks' => 'pl_from',
128 'imagelinks' => 'il_from',
129 'categorylinks' => 'cl_from',
130 'templatelinks' => 'tl_from',
131 'externallinks' => 'el_from',
134 $page = $dbw->tableName( 'page' );
137 foreach ( $linksTables as $table => $field ) {
138 if ( !$dbw->ping() ) {
139 print "DB disconnected, reconnecting...";
140 while ( !$dbw->ping() ) {
147 $pTable = $dbw->tableName( $table );
148 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
150 print "Deleting $table from non-existent articles...";
151 $dbw->query( $sql, $fname );
152 print " fixed " .$dbw->affectedRows() . " row(s)\n";