API: Various docu and clean-up.
[mediawiki.git] / maintenance / refreshLinks.inc
blob9e4eea8d6b4071daba62c97552d172139d89750f
1 <?php
2 /**
3  * @todo document
4  * @addtogroup Maintenance
5  */
7 /** */
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->mTagHooks = array();
24         # Don't generate thumbnail images
25         $wgUseImageResize = false;
26         $wgUseTidy = false;
28         $what = ($redirectsOnly)? "redirects" : "links";
30         if ( $newOnly ) {
31                 print "Refreshing $what from ";
32                 $res = $dbr->select( 'page',
33                         array( 'page_id' ),
34                         array(
35                                 'page_is_new' => 1,
36                                 "page_id > $start" ),
37                         $fname
38                 );
39                 $num = $dbr->numRows( $res );
40                 print "$num new articles...\n";
42                 $i = 0;
43                 while ( $row = $dbr->fetchObject( $res ) ) {
44                         if ( !( ++$i % REPORTING_INTERVAL ) ) {
45                                 print "$i\n";
46                                 wfWaitForSlaves( $maxLag );
47                         }
48                         if($redirectsOnly)
49                                 fixRedirect( $row->page_id );
50                         else
51                                 fixLinksFromArticle( $row->page_id );
52                 }
53         } else {
54                 print "Refreshing $what table.\n";
55                 if ( !$end ) {
56                         $end = $dbr->selectField( 'page', 'max(page_id)', false );
57                 }
58                 print("Starting from page_id $start of $end.\n");
60                 for ($id = $start; $id <= $end; $id++) {
62                         if ( !($id % REPORTING_INTERVAL) ) {
63                                 print "$id\n";
64                                 wfWaitForSlaves( $maxLag );
65                         }
66                         if($redirectsOnly)
67                                 fixRedirect( $id );
68                         else
69                                 fixLinksFromArticle( $id );
70                 }
71         }
74 function fixRedirect( $id ){
75         global $wgTitle, $wgArticle;
77         $wgTitle = Title::newFromID( $id );
78         $dbw = wfGetDB( DB_MASTER );
80         if ( is_null( $wgTitle ) ) {
81                 return;
82         }
83         $wgArticle = new Article($wgTitle);
85         $rt = $wgArticle->followRedirect();
87         if($rt == false || !is_object($rt))
88                 return;
90         $wgArticle->updateRedirectOn($dbw,$rt);
93 function fixLinksFromArticle( $id ) {
94         global $wgTitle, $wgParser;
95         
96         $wgTitle = Title::newFromID( $id );
97         $dbw = wfGetDB( DB_MASTER );
99         $linkCache =& LinkCache::singleton();
100         $linkCache->clear();
101         
102         if ( is_null( $wgTitle ) ) {
103                 return;
104         }
105         $dbw->begin();
107         $revision = Revision::newFromTitle( $wgTitle );
108         if ( !$revision ) {
109                 return;
110         }
112         $options = new ParserOptions;
113         $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
114         $update = new LinksUpdate( $wgTitle, $parserOutput, false );
115         $update->doUpdate();
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',
132         );
134         $page = $dbw->tableName( 'page' );
137         foreach ( $linksTables as $table => $field ) {
138                 if ( !$dbw->ping() ) {
139                         print "DB disconnected, reconnecting...";
140                         while ( !$dbw->ping() ) {
141                                 print ".";
142                                 sleep(10);
143                         }
144                         print "\n";
145                 }
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";
153         }