Restore clean, tail-friendly output
[mediawiki.git] / maintenance / refreshLinks.inc
blobfeb91ef214b231d3ee0371393ec133607ae4bd61
1 <?php
2 /**
3  * @todo document
4  * @package MediaWiki
5  * @subpackage Maintenance
6  */
8 /** */
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 );
18         
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();
24         
25         # Don't generate thumbnail images
26         $wgUseImageResize = false;
28         if ( $newOnly ) {
29                 print "Refreshing links from ";
30                 $res = $dbr->select( 'page',
31                         array( 'page_id' ), 
32                         array(
33                                 'page_is_new' => 1,
34                                 "page_id > $start" ),
35                         $fname
36                 );
37                 $num = $dbr->numRows( $res );
38                 print "$num new articles...\n";
39                 
40                 $i = 0;
41                 while ( $row = $dbr->fetchObject( $res ) ) {
42                         if ( !( ++$i % REPORTING_INTERVAL ) ) {
43                                 print "$i\n";
44                                 wfWaitForSlaves( $maxLag );
45                         }
47                         fixLinksFromArticle( $row->page_id );
48                 }
49         } else {
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++) {
55                         
56                         if ( !($id % REPORTING_INTERVAL) ) {
57                                 print "$id\n";
58                                 wfWaitForSlaves( $maxLag );
59                         }
60                         fixLinksFromArticle( $id );
61                 }
62                 
64         }
67 function fixLinksFromArticle( $id ) {
68         global $wgTitle, $wgArticle, $wgLinkCache, $wgOut;
69         
70         $wgTitle = Title::newFromID( $id );
71         $dbw =& wfGetDB( DB_MASTER );
72         
73         if ( is_null( $wgTitle ) ) {
74                 return;
75         }
76         $dbw->begin();
78         $wgArticle = new Article( $wgTitle );
79         $text = $wgArticle->getContent( true );
80         $wgLinkCache = new LinkCache;
81         $wgLinkCache->forUpdate( true );
82         
83         global $wgLinkHolders;
84         $wgLinkHolders = array(
85                 'namespaces' => array(),
86                 'dbkeys' => array(),
87                 'queries' => array(),
88                 'texts' => array(),
89                 'titles' => array()
90         );
93         # Parse the text and replace links with placeholders
94         $wgOut->addWikiText( $text );
95         
96         # Look up the links in the DB and add them to the link cache
97         $wgOut->clearHTML();
99         $linksUpdate = new LinksUpdate( $id, $wgTitle->getPrefixedDBkey() );
100         $linksUpdate->doDumbUpdate();
101         $dbw->immediateCommit();
104 function deleteLinksFromNonexistent( $maxLag = 0 ) {
105         $fname = 'deleteLinksFromNonexistent';
106         
107         wfWaitForSlaves( $maxLag );
109         $dbw =& wfGetDB( DB_WRITE );
110         
111         $linksTables = array( 
112                 'pagelinks' => 'pl_from',
113                 'imagelinks' => 'il_from',
114                 'categorylinks' => 'cl_from',
115         );
117         $page = $dbw->tableName( 'page' );
120         foreach ( $linksTables as $table => $field ) {
121                 if ( !$dbw->ping() ) {
122                         print "DB disconnected, reconnecting...";
123                         while ( !$dbw->ping() ) {
124                                 print ".";
125                                 sleep(10);
126                         }
127                         print "\n";
128                 }
130                 $pTable = $dbw->tableName( $table );
131                 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
132                 
133                 print "Deleting $table from non-existent articles...";
134                 $dbw->query( $sql, $fname );
135                 print " fixed " .$dbw->affectedRows() . " row(s)\n";
136         }