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