5 * @subpackage Maintenance
9 define( "REPORTING_INTERVAL", 100 );
11 function refreshLinks( $start, $newOnly = false, $maxLag = false ) {
12 global $wgUser, $wgParser, $wgUseImageResize;
14 $fname = 'refreshLinks';
15 $dbr =& wfGetDB( DB_SLAVE );
16 $dbw =& wfGetDB( DB_MASTER );
17 $start = intval( $start );
19 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
20 $wgUser->setOption('math', MW_MATH_SOURCE);
22 # Don't generate extension images (e.g. Timeline)
23 $wgParser->mTagHooks = array();
25 # Don't generate thumbnail images
26 $wgUseImageResize = false;
29 print "Refreshing links from ";
30 $res = $dbr->select( 'page',
37 $num = $dbr->numRows( $res );
38 print "$num new articles...\n";
41 while ( $row = $dbr->fetchObject( $res ) ) {
42 if ( !( ++$i % REPORTING_INTERVAL ) ) {
44 wfWaitForSlaves( $maxLag );
47 fixLinksFromArticle( $row->page_id );
50 print "Refreshing link table.\n";
51 $end = $dbr->selectField( 'page', 'max(page_id)', false );
52 print("Starting from page_id $start of $end.\n");
54 for ($id = $start; $id <= $end; $id++) {
56 if ( !($id % REPORTING_INTERVAL) ) {
58 wfWaitForSlaves( $maxLag );
60 fixLinksFromArticle( $id );
67 function fixLinksFromArticle( $id ) {
68 global $wgTitle, $wgArticle, $wgLinkCache, $wgOut;
70 $wgTitle = Title::newFromID( $id );
71 $dbw =& wfGetDB( DB_MASTER );
73 if ( is_null( $wgTitle ) ) {
78 $wgArticle = new Article( $wgTitle );
79 $text = $wgArticle->getContent( true );
80 $wgLinkCache = new LinkCache;
81 $wgLinkCache->forUpdate( true );
83 global $wgLinkHolders;
84 $wgLinkHolders = array(
85 'namespaces' => array(),
93 # Parse the text and replace links with placeholders
94 $wgOut->addWikiText( $text );
96 # Look up the links in the DB and add them to the link cache
99 $linksUpdate = new LinksUpdate( $id, $wgTitle->getPrefixedDBkey() );
100 $linksUpdate->doDumbUpdate();
101 $dbw->immediateCommit();
104 function deleteLinksFromNonexistent( $maxLag = 0 ) {
105 $fname = 'deleteLinksFromNonexistent';
107 wfWaitForSlaves( $maxLag );
109 $dbw =& wfGetDB( DB_WRITE );
111 $linksTables = array(
112 'pagelinks' => 'pl_from',
113 'imagelinks' => 'il_from',
114 'categorylinks' => 'cl_from',
117 $page = $dbw->tableName( 'page' );
120 foreach ( $linksTables as $table => $field ) {
121 if ( !$dbw->ping() ) {
122 print "DB disconnected, reconnecting...";
123 while ( !$dbw->ping() ) {
130 $pTable = $dbw->tableName( $table );
131 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
133 print "Deleting $table from non-existent articles...";
134 $dbw->query( $sql, $fname );
135 print " fixed " .$dbw->affectedRows() . " row(s)\n";