* @ to avoid undefined index notice
[mediawiki.git] / maintenance / refreshLinks.inc
blob73bfbdb59059717859981665ab08cfe3418e7b2a
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", 3);
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', array( 'page_id' ), 
31                   array( 'page_is_new' => 1, "page_id > $start" ), $fname );
32                 $num = $dbr->numRows( $res );
33                 print "$num new articles...\n";
34                 
35                 $i = 0;
36                 while ( $row = $dbr->fetchObject( $res ) ) {
37                         if ( !( ++$i % REPORTING_INTERVAL ) ) {
38                                 print "$i\n";
39                                 wfWaitForSlaves( $maxLag );
40                         }
42                         fixLinksFromArticle( $row->page_id );
43                 }
44         } else {
45                 print "Refreshing link table.\n";
46                 $end = $dbr->selectField( 'page', 'max(page_id)', false );
47                 print("Starting from page_id $start of $end.\n");
49                 for ($id = $start; $id <= $end; $id++) {
50                         
51                         if ( !($id % REPORTING_INTERVAL) ) {
52                                 print "$id\n";
53                                 wfWaitForSlaves( $maxLag );
54                         }
55                         fixLinksFromArticle( $id );
56                 }
57                 
59         }
62 function fixLinksFromArticle( $id ) {
63         global $wgTitle, $wgArticle, $wgLinkCache, $wgOut;
64         
65         $wgTitle = Title::newFromID( $id );
66         $dbw =& wfGetDB( DB_MASTER );
67         
68         if ( is_null( $wgTitle ) ) {
69                 return;
70         }
71         $dbw->begin();
73         $wgArticle = new Article( $wgTitle );
74         $text = $wgArticle->getContent( true );
75         $wgLinkCache = new LinkCache;
76         $wgLinkCache->forUpdate( true );
77         
78         global $wgLinkHolders;
79         $wgLinkHolders = array(
80                 'namespaces' => array(),
81                 'dbkeys' => array(),
82                 'queries' => array(),
83                 'texts' => array(),
84                 'titles' => array()
85         );
88         # Parse the text and replace links with placeholders
89         $wgOut->addWikiText( $text );
90         
91         # Look up the links in the DB and add them to the link cache
92         $wgOut->transformBuffer();
93         $wgOut->clearHTML();
95         $linksUpdate = new LinksUpdate( $id, $wgTitle->getPrefixedDBkey() );
96         $linksUpdate->doDumbUpdate();
97         $dbw->immediateCommit();
100 function deleteLinksFromNonexistent( $maxLag = 0 ) {
101         $fname = 'deleteLinksFromNonexistent';
102         
103         wfWaitForSlaves( $maxLag );
105         $dbw =& wfGetDB( DB_WRITE );
106         
107         $linksTables = array( 
108                 'pagelinks' => 'pl_from',
109                 'imagelinks' => 'il_from',
110                 'categorylinks' => 'cl_from',
111         );
113         $page = $dbw->tableName( 'page' );
116         foreach ( $linksTables as $table => $field ) {
117                 if ( !$dbw->ping() ) {
118                         print "DB disconnected, reconnecting...";
119                         while ( !$dbw->ping() ) {
120                                 print ".";
121                                 sleep(10);
122                         }
123                         print "\n";
124                 }
126                 $pTable = $dbw->tableName( $table );
127                 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
128                 
129                 print "Deleting $table from non-existent articles...";
130                 $dbw->query( $sql, $fname );
131                 print " fixed " .$dbw->affectedRows() . " row(s)\n";
132         }