Remove Out::transformBuffer method that does nothing
[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->clearHTML();
99
100 $linksUpdate = new LinksUpdate( $id, $wgTitle->getPrefixedDBkey() );
101 $linksUpdate->doDumbUpdate();
102 $dbw->immediateCommit();
103 }
104
105 function deleteLinksFromNonexistent( $maxLag = 0 ) {
106 $fname = 'deleteLinksFromNonexistent';
107
108 wfWaitForSlaves( $maxLag );
109
110 $dbw =& wfGetDB( DB_WRITE );
111
112 $linksTables = array(
113 'pagelinks' => 'pl_from',
114 'imagelinks' => 'il_from',
115 'categorylinks' => 'cl_from',
116 );
117
118 $page = $dbw->tableName( 'page' );
119
120
121 foreach ( $linksTables as $table => $field ) {
122 if ( !$dbw->ping() ) {
123 print "DB disconnected, reconnecting...";
124 while ( !$dbw->ping() ) {
125 print ".";
126 sleep(10);
127 }
128 print "\n";
129 }
130
131 $pTable = $dbw->tableName( $table );
132 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
133
134 print "Deleting $table from non-existent articles...";
135 $dbw->query( $sql, $fname );
136 print " fixed " .$dbw->affectedRows() . " row(s)\n";
137 }
138 }
139
140 ?>