* (bug 1130) Reorder old title checks; use title null instead of empty
[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", 3);
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', array( 'page_id' ),
31 array( 'page_is_new' => 1, "page_id > $start" ), $fname );
32 $num = $dbr->numRows( $res );
33 print "$num new articles...\n";
34
35 $i = 0;
36 while ( $row = $dbr->fetchObject( $res ) ) {
37 if ( !( ++$i % REPORTING_INTERVAL ) ) {
38 print "$i\n";
39 wfWaitForSlaves( $maxLag );
40 }
41
42 fixLinksFromArticle( $row->page_id );
43 }
44 } else {
45 print "Refreshing link table.\n";
46 $end = $dbr->selectField( 'page', 'max(page_id)', false );
47 print("Starting from page_id $start of $end.\n");
48
49 for ($id = $start; $id <= $end; $id++) {
50
51 if ( !($id % REPORTING_INTERVAL) ) {
52 print "$id\n";
53 wfWaitForSlaves( $maxLag );
54 }
55 fixLinksFromArticle( $id );
56 }
57
58
59 }
60 }
61
62 function fixLinksFromArticle( $id ) {
63 global $wgTitle, $wgArticle, $wgLinkCache, $wgOut;
64
65 $wgTitle = Title::newFromID( $id );
66 $dbw =& wfGetDB( DB_MASTER );
67
68 if ( is_null( $wgTitle ) ) {
69 return;
70 }
71 $dbw->begin();
72
73 $wgArticle = new Article( $wgTitle );
74 $text = $wgArticle->getContent( true );
75 $wgLinkCache = new LinkCache;
76 $wgLinkCache->forUpdate( true );
77
78 global $wgLinkHolders;
79 $wgLinkHolders = array(
80 'namespaces' => array(),
81 'dbkeys' => array(),
82 'queries' => array(),
83 'texts' => array(),
84 'titles' => array()
85 );
86
87
88 # Parse the text and replace links with placeholders
89 $wgOut->addWikiText( $text );
90
91 # Look up the links in the DB and add them to the link cache
92 $wgOut->transformBuffer();
93 $wgOut->clearHTML();
94
95 $linksUpdate = new LinksUpdate( $id, $wgTitle->getPrefixedDBkey() );
96 $linksUpdate->doDumbUpdate();
97 $dbw->immediateCommit();
98 }
99
100 function deleteLinksFromNonexistent( $maxLag = 0 ) {
101 $fname = 'deleteLinksFromNonexistent';
102
103 wfWaitForSlaves( $maxLag );
104
105 $dbw =& wfGetDB( DB_WRITE );
106
107 $linksTables = array(
108 'pagelinks' => 'pl_from',
109 'imagelinks' => 'il_from',
110 'categorylinks' => 'cl_from',
111 );
112
113 $page = $dbw->tableName( 'page' );
114
115
116 foreach ( $linksTables as $table => $field ) {
117 if ( !$dbw->ping() ) {
118 print "DB disconnected, reconnecting...";
119 while ( !$dbw->ping() ) {
120 print ".";
121 sleep(10);
122 }
123 print "\n";
124 }
125
126 $pTable = $dbw->tableName( $table );
127 global $wgDBmysql4, $wgDBtype;
128 if( $wgDBmysql4 || $wgDBtype != 'mysql' ) {
129 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
130 } else {
131 # Hack-around for MySQL 3.x, which lacks support
132 # for multi-table deletes.
133
134 $sql = "SELECT DISTINCT $field AS id FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
135 echo "Looking in $table from non-existent articles...";
136 $result = $dbw->query( $sql );
137 $ids = array();
138 while( $row = $dbw->fetchObject( $result ) ) {
139 $ids[] = $row->id;
140 }
141 $dbw->freeResult( $result );
142
143 if( empty( $ids ) ) {
144 echo " none.\n";
145 continue;
146 }
147 echo " found.\n";
148 $sql = "DELETE FROM $pTable WHERE $field IN (" . implode( ",", $ids ) . ")";
149 }
150
151 print "Deleting $table from non-existent articles...";
152 $dbw->query( $sql, $fname );
153 print " fixed " .$dbw->affectedRows() . " row(s)\n";
154 }
155 }
156
157 ?>