2 require_once 'counter.php';
6 * @subpackage Maintenance
10 define( 'REPORTING_INTERVAL', 10 );
12 function refreshLinks( $start, $newOnly = false, $maxLag = false ) {
13 global $wgUser, $wgParser, $wgUseImageResize;
15 $fname = 'refreshLinks';
16 $dbr =& wfGetDB( DB_SLAVE );
17 $dbw =& wfGetDB( DB_MASTER );
18 $start = intval( $start );
20 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
21 $wgUser->setOption('math', MW_MATH_SOURCE);
23 # Don't generate extension images (e.g. Timeline)
24 $wgParser->mTagHooks = array();
26 # Don't generate thumbnail images
27 $wgUseImageResize = false;
30 print "Refreshing links from ";
31 $res = $dbr->select( 'page',
38 $num = $dbr->numRows( $res );
39 print "$num new articles...\n";
42 while ( $row = $dbr->fetchObject( $res ) ) {
43 if ( !( ++$i % REPORTING_INTERVAL ) ) {
44 print_c( $i - 1, $i );
45 wfWaitForSlaves( $maxLag );
48 fixLinksFromArticle( $row->page_id );
51 print "Refreshing link table.\n";
52 $end = $dbr->selectField( 'page', 'max(page_id)', false );
53 print("Starting from page_id $start of $end.\n");
55 for ($id = $start; $id <= $end; $id++) {
57 if ( !($id % REPORTING_INTERVAL) ) {
58 print_c( $id - 1, $id );
59 wfWaitForSlaves( $maxLag );
61 fixLinksFromArticle( $id );
68 function fixLinksFromArticle( $id ) {
69 global $wgTitle, $wgArticle, $wgLinkCache, $wgOut;
71 $wgTitle = Title::newFromID( $id );
72 $dbw =& wfGetDB( DB_MASTER );
74 if ( is_null( $wgTitle ) ) {
79 $wgArticle = new Article( $wgTitle );
80 $text = $wgArticle->getContent( true );
81 $wgLinkCache = new LinkCache;
82 $wgLinkCache->forUpdate( true );
84 global $wgLinkHolders;
85 $wgLinkHolders = array(
86 'namespaces' => array(),
94 # Parse the text and replace links with placeholders
95 $wgOut->addWikiText( $text );
97 # Look up the links in the DB and add them to the link cache
98 $wgOut->transformBuffer();
101 $linksUpdate = new LinksUpdate( $id, $wgTitle->getPrefixedDBkey() );
102 $linksUpdate->doDumbUpdate();
103 $dbw->immediateCommit();
106 function deleteLinksFromNonexistent( $maxLag = 0 ) {
107 $fname = 'deleteLinksFromNonexistent';
109 wfWaitForSlaves( $maxLag );
111 $dbw =& wfGetDB( DB_WRITE );
113 $linksTables = array(
114 'pagelinks' => 'pl_from',
115 'imagelinks' => 'il_from',
116 'categorylinks' => 'cl_from',
119 $page = $dbw->tableName( 'page' );
122 foreach ( $linksTables as $table => $field ) {
123 if ( !$dbw->ping() ) {
124 print "DB disconnected, reconnecting...";
125 while ( !$dbw->ping() ) {
132 $pTable = $dbw->tableName( $table );
133 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
135 print "Deleting $table from non-existent articles...";
136 $dbw->query( $sql, $fname );
137 print " fixed " .$dbw->affectedRows() . " row(s)\n";