34ea6294d09b121d1bdf56b9f6875e9d8dddab4b
[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 $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);
21
22 # Don't generate extension images (e.g. Timeline)
23 $wgParser->mTagHooks = array();
24
25 # Don't generate thumbnail images
26 $wgUseImageResize = false;
27 $wgUseTidy = false;
28
29 if ( $newOnly ) {
30 print "Refreshing links from ";
31 $res = $dbr->select( 'page',
32 array( 'page_id' ),
33 array(
34 'page_is_new' => 1,
35 "page_id > $start" ),
36 $fname
37 );
38 $num = $dbr->numRows( $res );
39 print "$num new articles...\n";
40
41 $i = 0;
42 while ( $row = $dbr->fetchObject( $res ) ) {
43 if ( !( ++$i % REPORTING_INTERVAL ) ) {
44 print "$i\n";
45 wfWaitForSlaves( $maxLag );
46 }
47
48 fixLinksFromArticle( $row->page_id );
49 }
50 } else {
51 print "Refreshing link table.\n";
52 if ( !$end ) {
53 $end = $dbr->selectField( 'page', 'max(page_id)', false );
54 }
55 print("Starting from page_id $start of $end.\n");
56
57 for ($id = $start; $id <= $end; $id++) {
58
59 if ( !($id % REPORTING_INTERVAL) ) {
60 print "$id\n";
61 wfWaitForSlaves( $maxLag );
62 }
63 fixLinksFromArticle( $id );
64 }
65 }
66 }
67
68 function fixLinksFromArticle( $id ) {
69 global $wgTitle, $wgParser;
70
71 $wgTitle = Title::newFromID( $id );
72 $dbw =& wfGetDB( DB_MASTER );
73
74 $linkCache =& LinkCache::singleton();
75 $linkCache->clear();
76
77 if ( is_null( $wgTitle ) ) {
78 return;
79 }
80 $dbw->begin();
81
82 $revision = Revision::newFromTitle( $wgTitle );
83 if ( !$revision ) {
84 return;
85 }
86
87 $options = new ParserOptions;
88 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
89 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
90 $update->doUpdate();
91 $dbw->immediateCommit();
92 }
93
94 function deleteLinksFromNonexistent( $maxLag = 0 ) {
95 $fname = 'deleteLinksFromNonexistent';
96
97 wfWaitForSlaves( $maxLag );
98
99 $dbw =& wfGetDB( DB_WRITE );
100
101 $linksTables = array(
102 'pagelinks' => 'pl_from',
103 'imagelinks' => 'il_from',
104 'categorylinks' => 'cl_from',
105 'templatelinks' => 'tl_from',
106 'externallinks' => 'el_from',
107 );
108
109 $page = $dbw->tableName( 'page' );
110
111
112 foreach ( $linksTables as $table => $field ) {
113 if ( !$dbw->ping() ) {
114 print "DB disconnected, reconnecting...";
115 while ( !$dbw->ping() ) {
116 print ".";
117 sleep(10);
118 }
119 print "\n";
120 }
121
122 $pTable = $dbw->tableName( $table );
123 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
124
125 print "Deleting $table from non-existent articles...";
126 $dbw->query( $sql, $fname );
127 print " fixed " .$dbw->affectedRows() . " row(s)\n";
128 }
129 }
130
131 ?>