Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / maintenance / refreshLinks.inc
1 <?php
2 /**
3 * @todo document
4 * @addtogroup Maintenance
5 */
6
7 /** */
8 define( "REPORTING_INTERVAL", 100 );
9 #define( "REPORTING_INTERVAL", 1 );
10
11 function refreshLinks( $start, $newOnly = false, $maxLag = false, $end = 0 ) {
12 global $wgUser, $wgParser, $wgUseImageResize, $wgUseTidy;
13
14 $fname = 'refreshLinks';
15 $dbr =& wfGetDB( DB_SLAVE );
16 $start = intval( $start );
17
18 # Don't generate TeX PNGs (lack of a sensible current directory causes errors anyway)
19 $wgUser->setOption('math', MW_MATH_SOURCE);
20
21 # Don't generate extension images (e.g. Timeline)
22 $wgParser->mTagHooks = array();
23
24 # Don't generate thumbnail images
25 $wgUseImageResize = false;
26 $wgUseTidy = 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 if ( !$end ) {
52 $end = $dbr->selectField( 'page', 'max(page_id)', false );
53 }
54 print("Starting from page_id $start of $end.\n");
55
56 for ($id = $start; $id <= $end; $id++) {
57
58 if ( !($id % REPORTING_INTERVAL) ) {
59 print "$id\n";
60 wfWaitForSlaves( $maxLag );
61 }
62 fixLinksFromArticle( $id );
63 }
64 }
65 }
66
67 function fixLinksFromArticle( $id ) {
68 global $wgTitle, $wgParser;
69
70 $wgTitle = Title::newFromID( $id );
71 $dbw =& wfGetDB( DB_MASTER );
72
73 $linkCache =& LinkCache::singleton();
74 $linkCache->clear();
75
76 if ( is_null( $wgTitle ) ) {
77 return;
78 }
79 $dbw->begin();
80
81 $revision = Revision::newFromTitle( $wgTitle );
82 if ( !$revision ) {
83 return;
84 }
85
86 $options = new ParserOptions;
87 $parserOutput = $wgParser->parse( $revision->getText(), $wgTitle, $options, true, true, $revision->getId() );
88 $update = new LinksUpdate( $wgTitle, $parserOutput, false );
89 $update->doUpdate();
90 $dbw->immediateCommit();
91 }
92
93 function deleteLinksFromNonexistent( $maxLag = 0 ) {
94 $fname = 'deleteLinksFromNonexistent';
95
96 wfWaitForSlaves( $maxLag );
97
98 $dbw =& wfGetDB( DB_WRITE );
99
100 $linksTables = array(
101 'pagelinks' => 'pl_from',
102 'imagelinks' => 'il_from',
103 'categorylinks' => 'cl_from',
104 'templatelinks' => 'tl_from',
105 'externallinks' => 'el_from',
106 );
107
108 $page = $dbw->tableName( 'page' );
109
110
111 foreach ( $linksTables as $table => $field ) {
112 if ( !$dbw->ping() ) {
113 print "DB disconnected, reconnecting...";
114 while ( !$dbw->ping() ) {
115 print ".";
116 sleep(10);
117 }
118 print "\n";
119 }
120
121 $pTable = $dbw->tableName( $table );
122 $sql = "DELETE $pTable FROM $pTable LEFT JOIN $page ON page_id=$field WHERE page_id IS NULL";
123
124 print "Deleting $table from non-existent articles...";
125 $dbw->query( $sql, $fname );
126 print " fixed " .$dbw->affectedRows() . " row(s)\n";
127 }
128 }
129
130 ?>