Making the new message ignored.
[mediawiki.git] / maintenance / refreshLinks.inc
blob70e2a5dcb0c13a817db79fac2d859e8cddf60157
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, $oldRedirectsOnly = 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->clearTagHooks();
24         # Don't generate thumbnail images
25         $wgUseImageResize = false;
26         $wgUseTidy = false;
28         $what = $redirectsOnly ? "redirects" : "links";
30         if( $oldRedirectsOnly ) {
31                 # This entire code path is cut-and-pasted from below.  Hurrah.
32                 $res = $dbr->query(
33                         "SELECT page_id ".
34                         "FROM page ".
35                         "LEFT JOIN redirect ON page_id=rd_from ".
36                         "WHERE page_is_redirect=1 AND rd_from IS NULL AND ".
37                         ($end == 0 ? "page_id >= $start"
38                                    : "page_id BETWEEN $start AND $end"),
39                         $fname
40                 );
41                 $num = $dbr->numRows( $res );
42                 print "Refreshing $num old redirects from $start...\n";
44                 while( $row = $dbr->fetchObject( $res ) ) {
45                         if ( !( ++$i % REPORTING_INTERVAL ) ) {
46                                 print "$i\n";
47                                 wfWaitForSlaves( $maxLag );
48                         }
49                         fixRedirect( $row->page_id );
50                 }
51         } elseif( $newOnly ) {
52                 print "Refreshing $what from ";
53                 $res = $dbr->select( 'page',
54                         array( 'page_id' ),
55                         array(
56                                 'page_is_new' => 1,
57                                 "page_id >= $start" ),
58                         $fname
59                 );
60                 $num = $dbr->numRows( $res );
61                 print "$num new articles...\n";
63                 $i = 0;
64                 while ( $row = $dbr->fetchObject( $res ) ) {
65                         if ( !( ++$i % REPORTING_INTERVAL ) ) {
66                                 print "$i\n";
67                                 wfWaitForSlaves( $maxLag );
68                         }
69                         if($redirectsOnly)
70                                 fixRedirect( $row->page_id );
71                         else
72                                 fixLinksFromArticle( $row->page_id );
73                 }
74         } else {
75                 print "Refreshing $what table.\n";
76                 if ( !$end ) {
77                         $end = $dbr->selectField( 'page', 'max(page_id)', false );
78                 }
79                 print("Starting from page_id $start of $end.\n");
81                 for ($id = $start; $id <= $end; $id++) {
83                         if ( !($id % REPORTING_INTERVAL) ) {
84                                 print "$id\n";
85                                 wfWaitForSlaves( $maxLag );
86                         }
87                         if($redirectsOnly)
88                                 fixRedirect( $id );
89                         else
90                                 fixLinksFromArticle( $id );
91                 }
92         }
95 function fixRedirect( $id ){
96         global $wgTitle, $wgArticle;
98         $wgTitle = Title::newFromID( $id );
99         $dbw = wfGetDB( DB_MASTER );
101         if ( is_null( $wgTitle ) ) {
102                 return;
103         }
104         $wgArticle = new Article($wgTitle);
106         $rt = $wgArticle->followRedirect();
108         if($rt == false || !is_object($rt))
109                 return;
111         $wgArticle->updateRedirectOn($dbw,$rt);
114 function fixLinksFromArticle( $id ) {
115         global $wgTitle, $wgParser;
116         
117         $wgTitle = Title::newFromID( $id );
118         $dbw = wfGetDB( DB_MASTER );
120         $linkCache =& LinkCache::singleton();
121         $linkCache->clear();
122         
123         if ( is_null( $wgTitle ) ) {
124                 return;
125         }
126         $dbw->begin();
128         $revision = Revision::newFromTitle( $wgTitle );
129         if ( !$revision ) {
130                 return;
131         }
133         $options = new ParserOptions;
134         $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
135         $update = new LinksUpdate( $wgTitle, $parserOutput, false );
136         $update->doUpdate();
137         $dbw->immediateCommit();
140 function deleteLinksFromNonexistent( $maxLag = 0 ) {
141         $fname = 'deleteLinksFromNonexistent';
143         wfWaitForSlaves( $maxLag );
145         $dbw = wfGetDB( DB_MASTER );
147         $linksTables = array(
148                 'pagelinks' => 'pl_from',
149                 'imagelinks' => 'il_from',
150                 'categorylinks' => 'cl_from',
151                 'templatelinks' => 'tl_from',
152                 'externallinks' => 'el_from',
153         );
155         $page = $dbw->tableName( 'page' );
158         foreach ( $linksTables as $table => $field ) {
159                 if ( !$dbw->ping() ) {
160                         print "DB disconnected, reconnecting...";
161                         while ( !$dbw->ping() ) {
162                                 print ".";
163                                 sleep(10);
164                         }
165                         print "\n";
166                 }
168                 $pTable = $dbw->tableName( $table );
169                 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
171                 print "Deleting $table from non-existent articles...";
172                 $dbw->query( $sql, $fname );
173                 print " fixed " .$dbw->affectedRows() . " row(s)\n";
174         }