1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
<?php
/**
* See deferred.txt
* @package MediaWiki
*/
/**
* @todo document
* @package MediaWiki
*/
class LinksUpdate {
/**#@+
* @access private
*/
var $mId, $mTitle;
/**#@-*/
/**
* Constructor
* Initialize private variables
* @param integer $id
* @param string $title
*/
function LinksUpdate( $id, $title ) {
$this->mId = $id;
$this->mTitle = $title;
}
/**
* Update link tables with outgoing links from an updated article
* Relies on the 'link cache' to be filled out.
*/
function doUpdate() {
global $wgUseDumbLinkUpdate, $wgLinkCache, $wgDBtransactions;
global $wgUseCategoryMagic;
if ( $wgUseDumbLinkUpdate ) {
$this->doDumbUpdate();
return;
}
$fname = 'LinksUpdate::doUpdate';
wfProfileIn( $fname );
$del = array();
$add = array();
$dbw =& wfGetDB( DB_MASTER );
$pagelinks = $dbw->tableName( 'pagelinks' );
$imagelinks = $dbw->tableName( 'imagelinks' );
$categorylinks = $dbw->tableName( 'categorylinks' );
#------------------------------------------------------------------------------
# Good links
if ( $wgLinkCache->incrementalSetup( LINKCACHE_PAGE, $del, $add ) ) {
# Delete where necessary
if ( count( $del ) ) {
$batch = new LinkBatch( $del );
$set = $batch->constructSet( 'pl', $dbw );
if ( $set ) {
$sql = "DELETE FROM $pagelinks WHERE pl_from={$this->mId} AND ($set)";
$dbw->query( $sql, $fname );
}
}
} else {
# Delete everything
$dbw->delete( 'pagelinks', array( 'pl_from' => $this->mId ), $fname );
# Get the addition list
$add = $wgLinkCache->getPageLinks();
}
# Do the insertion
if( 0 != count( $add ) ) {
$arr = array();
foreach( $add as $lt => $target ) {
array_push( $arr, array(
'pl_from' => $this->mId,
'pl_namespace' => $target->getNamespace(),
'pl_title' => $target->getDbKey() ) );
}
# The link cache was constructed without FOR UPDATE, so there may be collisions
# Ignoring for now, I'm not sure if that causes problems or not, but I'm fairly
# sure it's better than without IGNORE
$dbw->insert( 'pagelinks', $arr, $fname, array( 'IGNORE' ) );
}
#------------------------------------------------------------------------------
# Image links
$dbw->delete('imagelinks',array('il_from'=>$this->mId),$fname);
# Get addition list
$add = $wgLinkCache->getImageLinks();
# Do the insertion
$sql = '';
$image = NS_IMAGE;
if ( 0 != count ( $add ) ) {
$arr = array();
foreach ($add as $iname => $val ) {
$nt = Title::makeTitle( $image, $iname );
if( !$nt ) continue;
$nt->invalidateCache();
array_push( $arr, array(
'il_from' => $this->mId,
'il_to' => $iname ) );
}
$dbw->insert('imagelinks', $arr, $fname, array('IGNORE'));
}
#------------------------------------------------------------------------------
# Category links
if( $wgUseCategoryMagic ) {
global $messageMemc, $wgDBname;
# Get addition list
$add = $wgLinkCache->getCategoryLinks();
# select existing catlinks for this page
$res = $dbw->select( 'categorylinks',
array( 'cl_to', 'cl_sortkey' ),
array( 'cl_from' => $this->mId ),
$fname,
'FOR UPDATE' );
$del = array();
if( 0 != $dbw->numRows( $res ) ) {
while( $row = $dbw->fetchObject( $res ) ) {
if( !isset( $add[$row->cl_to] ) || $add[$row->cl_to] != $row->cl_sortkey ) {
// in the db, but no longer in the page
// or sortkey has changed -> delete
$del[] = $row->cl_to;
} else {
// remove already existing category memberships
// from the add array
unset( $add[$row->cl_to] );
}
}
}
// delete any removed categorylinks
if( count( $del ) > 0) {
// delete old ones
$dbw->delete( 'categorylinks',
array(
'cl_from' => $this->mId,
'cl_to' => $del ),
$fname );
foreach( $del as $cname ){
$nt = Title::makeTitle( NS_CATEGORY, $cname );
$nt->invalidateCache();
// update the timestamp which indicates when the last article
// was added or removed to/from this article
$key = $wgDBname . ':Category:' . md5( $nt->getDBkey() ) . ':adddeltimestamp';
$messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
}
}
// add any new category memberships
if( count( $add ) > 0 ) {
$arr = array();
foreach( $add as $cname => $sortkey ) {
$nt = Title::makeTitle( NS_CATEGORY, $cname );
$nt->invalidateCache();
// update the timestamp which indicates when the last article
// was added or removed to/from this article
$key = $wgDBname . ':Category:' . md5( $nt->getDBkey() ) . ':adddeltimestamp';
$messageMemc->set( $key , wfTimestamp( TS_MW ), 24*3600 );
array_push( $arr, array(
'cl_from' => $this->mId,
'cl_to' => $cname,
'cl_sortkey' => $sortkey ) );
}
// do the actual sql insertion
$dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
}
}
wfProfileOut( $fname );
}
/**
* Link update which clears the previous entries and inserts new ones
* May be slower or faster depending on level of lock contention and write speed of DB
* Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
*/
function doDumbUpdate() {
global $wgLinkCache, $wgDBtransactions, $wgUseCategoryMagic;
$fname = 'LinksUpdate::doDumbUpdate';
wfProfileIn( $fname );
$dbw =& wfGetDB( DB_MASTER );
$pagelinks = $dbw->tableName( 'pagelinks' );
$imagelinks = $dbw->tableName( 'imagelinks' );
$categorylinks = $dbw->tableName( 'categorylinks' );
$dbw->delete('pagelinks', array('pl_from'=>$this->mId),$fname);
$a = $wgLinkCache->getPageLinks();
if ( 0 != count( $a ) ) {
$arr = array();
foreach( $a as $lt => $target ) {
array_push( $arr, array(
'pl_from' => $this->mId,
'pl_namespace' => $target->getNamespace(),
'pl_title' => $target->getDBkey() ) );
}
$dbw->insert( 'pagelinks', $arr, $fname, array( 'IGNORE' ) );
}
$dbw->delete('imagelinks', array('il_from'=>$this->mId),$fname);
$a = $wgLinkCache->getImageLinks();
$sql = '';
if ( 0 != count ( $a ) ) {
$arr = array();
foreach( $a as $iname => $val )
array_push( $arr, array(
'il_from' => $this->mId,
'il_to' => $iname ) );
$dbw->insert( 'imagelinks', $arr, $fname, array( 'IGNORE' ) );
}
if( $wgUseCategoryMagic ) {
$dbw->delete('categorylinks', array('cl_from'=>$this->mId),$fname);
# Get addition list
$add = $wgLinkCache->getCategoryLinks();
# Do the insertion
$sql = '';
if ( 0 != count ( $add ) ) {
$arr = array();
foreach( $add as $cname => $sortkey ) {
# FIXME: Change all this to avoid unnecessary duplication
$nt = Title::makeTitle( NS_CATEGORY, $cname );
if( !$nt ) continue;
$nt->invalidateCache();
array_push( $arr, array(
'cl_from' => $this->mId,
'cl_to' => $cname,
'cl_sortkey' => $sortkey ) );
}
$dbw->insert( 'categorylinks', $arr, $fname, array( 'IGNORE' ) );
}
}
wfProfileOut( $fname );
}
}
?>
|