* (bug 5161) Don't try to load template list for nonexistent pages
[lhc/web/wiklou.git] / maintenance / refreshLinks.inc
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
8 /** */
9 define( "REPORTING_INTERVAL", 100 );
10 #define( "REPORTING_INTERVAL", 1 );
11
12 function refreshLinks( $start, $newOnly = false, $maxLag = false, $end = 0 ) {
13 global $wgUser, $wgParser, $wgUseImageResize, $wgUseTidy;
14
15 $fname = 'refreshLinks';
16 $dbr =& wfGetDB( DB_SLAVE );
17 $dbw =& wfGetDB( DB_MASTER );
18 $start = intval( $start );
19
20 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
21 $wgUser->setOption('math', MW_MATH_SOURCE);
22
23 # Don't generate extension images (e.g. Timeline)
24 $wgParser->mTagHooks = array();
25
26 # Don't generate thumbnail images
27 $wgUseImageResize = false;
28 $wgUseTidy = false;
29
30 if ( $newOnly ) {
31 print "Refreshing links 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";
41
42 $i = 0;
43 while ( $row = $dbr->fetchObject( $res ) ) {
44 if ( !( ++$i % REPORTING_INTERVAL ) ) {
45 print "$i\n";
46 wfWaitForSlaves( $maxLag );
47 }
48
49 fixLinksFromArticle( $row->page_id );
50 }
51 } else {
52 print "Refreshing link table.\n";
53 if ( !$end ) {
54 $end = $dbr->selectField( 'page', 'max(page_id)', false );
55 }
56 print("Starting from page_id $start of $end.\n");
57
58 for ($id = $start; $id <= $end; $id++) {
59
60 if ( !($id % REPORTING_INTERVAL) ) {
61 print "$id\n";
62 wfWaitForSlaves( $maxLag );
63 }
64 fixLinksFromArticle( $id );
65 }
66
67
68 }
69 }
70
71 function fixLinksFromArticle( $id ) {
72 global $wgTitle, $wgArticle, $wgOut, $wgParser;
73
74 $wgTitle = Title::newFromID( $id );
75 $dbw =& wfGetDB( DB_MASTER );
76
77 $linkCache =& LinkCache::singleton();
78 $linkCache->clear();
79
80 if ( is_null( $wgTitle ) ) {
81 return;
82 }
83 $dbw->begin();
84
85 $revision = Revision::newFromTitle( $wgTitle );
86 if ( !$revision ) {
87 return;
88 }
89
90 $options = new ParserOptions;
91 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
92 $update = new LinksUpdate( $wgTitle, $parserOutput );
93 $update->doUpdate();
94 $dbw->immediateCommit();
95 }
96
97 function deleteLinksFromNonexistent( $maxLag = 0 ) {
98 $fname = 'deleteLinksFromNonexistent';
99
100 wfWaitForSlaves( $maxLag );
101
102 $dbw =& wfGetDB( DB_WRITE );
103
104 $linksTables = array(
105 'pagelinks' => 'pl_from',
106 'imagelinks' => 'il_from',
107 'categorylinks' => 'cl_from',
108 'templatelinks' => 'tl_from',
109 'externallinks' => 'el_from',
110 );
111
112 $page = $dbw->tableName( 'page' );
113
114
115 foreach ( $linksTables as $table => $field ) {
116 if ( !$dbw->ping() ) {
117 print "DB disconnected, reconnecting...";
118 while ( !$dbw->ping() ) {
119 print ".";
120 sleep(10);
121 }
122 print "\n";
123 }
124
125 $pTable = $dbw->tableName( $table );
126 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
127
128 print "Deleting $table from non-existent articles...";
129 $dbw->query( $sql, $fname );
130 print " fixed " .$dbw->affectedRows() . " row(s)\n";
131 }
132 }
133
134 ?>