* Use the pretty count function
[lhc/web/wiklou.git] / maintenance / refreshLinks.inc
1 <?php
2 require_once 'counter.php';
3 /**
4 * @todo document
5 * @package MediaWiki
6 * @subpackage Maintenance
7 */
8
9 /** */
10 define( 'REPORTING_INTERVAL', 10 );
11
12 function refreshLinks( $start, $newOnly = false, $maxLag = false ) {
13 global $wgUser, $wgParser, $wgUseImageResize;
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
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_c( $i - 1, $i );
45 wfWaitForSlaves( $maxLag );
46 }
47
48 fixLinksFromArticle( $row->page_id );
49 }
50 } else {
51 print "Refreshing link table.\n";
52 $end = $dbr->selectField( 'page', 'max(page_id)', false );
53 print("Starting from page_id $start of $end.\n");
54
55 for ($id = $start; $id <= $end; $id++) {
56
57 if ( !($id % REPORTING_INTERVAL) ) {
58 print_c( $id - 1, $id );
59 wfWaitForSlaves( $maxLag );
60 }
61 fixLinksFromArticle( $id );
62 }
63
64
65 }
66 }
67
68 function fixLinksFromArticle( $id ) {
69 global $wgTitle, $wgArticle, $wgLinkCache, $wgOut;
70
71 $wgTitle = Title::newFromID( $id );
72 $dbw =& wfGetDB( DB_MASTER );
73
74 if ( is_null( $wgTitle ) ) {
75 return;
76 }
77 $dbw->begin();
78
79 $wgArticle = new Article( $wgTitle );
80 $text = $wgArticle->getContent( true );
81 $wgLinkCache = new LinkCache;
82 $wgLinkCache->forUpdate( true );
83
84 global $wgLinkHolders;
85 $wgLinkHolders = array(
86 'namespaces' => array(),
87 'dbkeys' => array(),
88 'queries' => array(),
89 'texts' => array(),
90 'titles' => array()
91 );
92
93
94 # Parse the text and replace links with placeholders
95 $wgOut->addWikiText( $text );
96
97 # Look up the links in the DB and add them to the link cache
98 $wgOut->transformBuffer();
99 $wgOut->clearHTML();
100
101 $linksUpdate = new LinksUpdate( $id, $wgTitle->getPrefixedDBkey() );
102 $linksUpdate->doDumbUpdate();
103 $dbw->immediateCommit();
104 }
105
106 function deleteLinksFromNonexistent( $maxLag = 0 ) {
107 $fname = 'deleteLinksFromNonexistent';
108
109 wfWaitForSlaves( $maxLag );
110
111 $dbw =& wfGetDB( DB_WRITE );
112
113 $linksTables = array(
114 'pagelinks' => 'pl_from',
115 'imagelinks' => 'il_from',
116 'categorylinks' => 'cl_from',
117 );
118
119 $page = $dbw->tableName( 'page' );
120
121
122 foreach ( $linksTables as $table => $field ) {
123 if ( !$dbw->ping() ) {
124 print "DB disconnected, reconnecting...";
125 while ( !$dbw->ping() ) {
126 print ".";
127 sleep(10);
128 }
129 print "\n";
130 }
131
132 $pTable = $dbw->tableName( $table );
133 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
134
135 print "Deleting $table from non-existent articles...";
136 $dbw->query( $sql, $fname );
137 print " fixed " .$dbw->affectedRows() . " row(s)\n";
138 }
139 }
140
141 ?>