00001 <?php
00002
# See deferred.doc
00003
00004 class LinksUpdate {
00005
00006 var
$mId,
$mTitle;
00007
00008 function
LinksUpdate( $
id, $title )
00009 {
00010 $this->mId = $id;
00011 $this->mTitle =
$title;
00012 $this->mTitleEnc =
wfStrencode( $
title );
00013 }
00014
00015
00016 function
doUpdate()
00017 {
00018 global $wgUseBetterLinksUpdate,
$wgLinkCache,
$wgDBtransactions;
00019 global
$wgEnablePersistentLC,
$wgUseCategoryMagic;
00020
00021
00022
00023
00024 $fname =
"LinksUpdate::doUpdate";
00025
wfProfileIn( $fname );
00026
00027 $del = array();
00028 $add = array();
00029
00030
if(
$wgDBtransactions ) {
00031
$sql =
"BEGIN";
00032
wfQuery( $sql,
DB_WRITE, $fname );
00033 }
00034
00035
#------------------------------------------------------------------------------
00036
# Good links
00037
00038
if (
$wgLinkCache->incrementalSetup(
LINKCACHE_GOOD, $del, $add ) ) {
00039
# Delete where necessary
00040
if ( count( $del ) ) {
00041
$sql =
"DELETE FROM links WHERE l_from={$this->mId} AND l_to IN(".
00042 implode(
",", $del ) .
")";
00043
wfQuery( $sql,
DB_WRITE, $fname );
00044 }
00045 }
else {
00046
# Delete everything
00047
$sql =
"DELETE FROM links WHERE l_from={$this->mId}";
00048
wfQuery( $sql,
DB_WRITE, $fname );
00049
00050
# Get the addition list
00051
$add =
$wgLinkCache->getGoodLinks();
00052 }
00053
00054
# Do the insertion
00055
$sql =
"";
00056
if ( 0 != count( $add ) ) {
00057
$sql =
"INSERT INTO links (l_from,l_to) VALUES ";
00058
$first =
true;
00059 foreach( $add as $lt => $lid ) {
00060
00061
if ( !
$first ) {
$sql .=
","; }
00062
$first =
false;
00063
00064
$sql .=
"({$this->mId},{$lid})";
00065 }
00066 }
00067
if (
"" !=
$sql ) {
00068
wfQuery( $sql,
DB_WRITE, $fname );
00069 }
00070
00071
#------------------------------------------------------------------------------
00072
# Bad links
00073
00074
if (
$wgLinkCache->incrementalSetup(
LINKCACHE_BAD, $del, $add ) ) {
00075
# Delete where necessary
00076
if ( count( $del ) ) {
00077
$sql =
"DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
00078 implode(
"','", array_map(
"wfStrencode", $del ) ) .
"')";
00079
wfQuery( $sql,
DB_WRITE, $fname );
00080 }
00081 }
else {
00082
# Delete all
00083
$sql =
"DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
00084
wfQuery( $sql,
DB_WRITE, $fname );
00085
00086
# Get addition list
00087
$add =
$wgLinkCache->getBadLinks();
00088 }
00089
00090
# Do additions
00091
$sql =
"";
00092
if ( 0 != count ( $add ) ) {
00093
$sql =
"INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
00094
$first =
true;
00095 foreach( $add as $blt ) {
00096 $blt =
wfStrencode( $blt );
00097
if ( !
$first ) {
$sql .=
","; }
00098
$first =
false;
00099
00100
$sql .=
"({$this->mId},'{$blt}')";
00101 }
00102 }
00103
if (
"" !=
$sql ) {
00104
wfQuery( $sql,
DB_WRITE, $fname );
00105 }
00106
00107
#------------------------------------------------------------------------------
00108
# Image links
00109
$sql =
"DELETE FROM imagelinks WHERE il_from='{$this->mId}'";
00110
wfQuery( $sql,
DB_WRITE, $fname );
00111
00112
# Get addition list
00113
$add =
$wgLinkCache->getImageLinks();
00114
00115
# Do the insertion
00116
$sql =
"";
00117 $image =
Namespace::getImage();
00118
if ( 0 != count ( $add ) ) {
00119
$sql =
"INSERT INTO imagelinks (il_from,il_to) VALUES ";
00120
$first =
true;
00121 foreach( $add as $iname => $val ) {
00122
# FIXME: Change all this to avoid unnecessary duplication
00123
$nt = Title::makeTitle( $image, $iname );
00124
if( !$nt )
continue;
00125 $nt->invalidateCache();
00126
00127 $iname =
wfStrencode( $iname );
00128
if ( !
$first ) {
$sql .=
","; }
00129
$first =
false;
00130
00131
$sql .=
"({$this->mId},'{$iname}')";
00132 }
00133 }
00134
if (
"" !=
$sql ) {
wfQuery( $sql,
DB_WRITE, $fname ); }
00135
00136
#------------------------------------------------------------------------------
00137
# Category links
00138
if(
$wgUseCategoryMagic ) {
00139
$sql =
"DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
00140
wfQuery( $sql,
DB_WRITE, $fname );
00141
00142
# Get addition list
00143
$add =
$wgLinkCache->getCategoryLinks();
00144
00145
# Do the insertion
00146
$sql =
"";
00147
if ( 0 != count ( $add ) ) {
00148
$sql =
"INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
00149
$first =
true;
00150 foreach( $add as $cname => $sortkey ) {
00151
# FIXME: Change all this to avoid unnecessary duplication
00152
$nt = Title::makeTitle(
NS_CATEGORY, $cname );
00153
if( !$nt )
continue;
00154 $nt->invalidateCache();
00155
00156
if ( !
$first ) {
$sql .=
","; }
00157
$first =
false;
00158
00159
$sql .=
"({$this->mId},'" .
wfStrencode( $cname ) .
00160
"','" .
wfStrencode( $sortkey ) .
"')";
00161 }
00162 }
00163
if (
"" !=
$sql ) {
wfQuery( $sql,
DB_WRITE, $fname ); }
00164 }
00165
00166 $this->
fixBrokenLinks();
00167
00168
if(
$wgDBtransactions ) {
00169
$sql =
"COMMIT";
00170
wfQuery( $sql,
DB_WRITE, $fname );
00171 }
00172
wfProfileOut( $fname );
00173 }
00174
00175 function
doDumbUpdate()
00176 {
00177
# Old inefficient update function
00178
# Used for rebuilding the link table
00179
00180 global
$wgLinkCache,
$wgDBtransactions,
$wgUseCategoryMagic;
00181 $fname =
"LinksUpdate::doDumbUpdate";
00182
wfProfileIn( $fname );
00183
00184
if(
$wgDBtransactions ) {
00185
$sql =
"BEGIN";
00186
wfQuery( $sql,
DB_WRITE, $fname );
00187 }
00188
00189
$sql =
"DELETE FROM links WHERE l_from={$this->mId}";
00190
wfQuery( $sql,
DB_WRITE, $fname );
00191
00192 $a =
$wgLinkCache->getGoodLinks();
00193
$sql =
"";
00194
if ( 0 != count( $a ) ) {
00195
$sql =
"INSERT INTO links (l_from,l_to) VALUES ";
00196
$first =
true;
00197 foreach( $a as $lt => $lid ) {
00198
if ( !
$first ) {
$sql .=
","; }
00199
$first =
false;
00200
00201
$sql .=
"({$this->mId},{$lid})";
00202 }
00203 }
00204
if (
"" !=
$sql ) {
wfQuery( $sql,
DB_WRITE, $fname ); }
00205
00206
$sql =
"DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
00207
wfQuery( $sql,
DB_WRITE, $fname );
00208
00209 $a =
$wgLinkCache->getBadLinks();
00210
$sql =
"";
00211
if ( 0 != count ( $a ) ) {
00212
$sql =
"INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
00213
$first =
true;
00214 foreach( $a as $blt ) {
00215 $blt =
wfStrencode( $blt );
00216
if ( !
$first ) {
$sql .=
","; }
00217
$first =
false;
00218
00219
$sql .=
"({$this->mId},'{$blt}')";
00220 }
00221 }
00222
if (
"" !=
$sql ) {
wfQuery( $sql,
DB_WRITE, $fname ); }
00223
00224
$sql =
"DELETE FROM imagelinks WHERE il_from={$this->mId}";
00225
wfQuery( $sql,
DB_WRITE, $fname );
00226
00227 $a =
$wgLinkCache->getImageLinks();
00228
$sql =
"";
00229
if ( 0 != count ( $a ) ) {
00230
$sql =
"INSERT INTO imagelinks (il_from,il_to) VALUES ";
00231
$first =
true;
00232 foreach( $a as $iname => $val ) {
00233 $iname =
wfStrencode( $iname );
00234
if ( !
$first ) {
$sql .=
","; }
00235
$first =
false;
00236
00237
$sql .=
"({$this->mId},'{$iname}')";
00238 }
00239 }
00240
if (
"" !=
$sql ) {
wfQuery( $sql,
DB_WRITE, $fname ); }
00241
00242
if(
$wgUseCategoryMagic ) {
00243
$sql =
"DELETE FROM categorylinks WHERE cl_from='{$this->mId}'";
00244
wfQuery( $sql,
DB_WRITE, $fname );
00245
00246
# Get addition list
00247
$add =
$wgLinkCache->getCategoryLinks();
00248
00249
# Do the insertion
00250
$sql =
"";
00251
if ( 0 != count ( $add ) ) {
00252
$sql =
"INSERT INTO categorylinks (cl_from,cl_to,cl_sortkey) VALUES ";
00253
$first =
true;
00254 foreach( $add as $cname => $sortkey ) {
00255
# FIXME: Change all this to avoid unnecessary duplication
00256
$nt = Title::makeTitle(
NS_CATEGORY, $cname );
00257
if( !$nt )
continue;
00258 $nt->invalidateCache();
00259
00260
if ( !
$first ) {
$sql .=
","; }
00261
$first =
false;
00262
00263
$sql .=
"({$this->mId},'" .
wfStrencode( $cname ) .
00264
"','" .
wfStrencode( $sortkey ) .
"')";
00265 }
00266 }
00267
if (
"" !=
$sql ) {
wfQuery( $sql,
DB_WRITE, $fname ); }
00268 }
00269 $this->
fixBrokenLinks();
00270
00271
if(
$wgDBtransactions ) {
00272
$sql =
"COMMIT";
00273
wfQuery( $sql,
DB_WRITE, $fname );
00274 }
00275
wfProfileOut( $fname );
00276 }
00277
00278 function
fixBrokenLinks() {
00279
00280
00281 $fname =
"LinksUpdate::fixBrokenLinks";
00282
00283
$sql =
"SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
00284
$res =
wfQuery( $sql,
DB_READ, $fname );
00285
if ( 0 ==
wfNumRows( $res ) ) {
return; }
00286
00287
$sql =
"INSERT INTO links (l_from,l_to) VALUES ";
00288 $now =
wfTimestampNow();
00289
$sql2 =
"UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
00290
$first =
true;
00291
while (
$row =
wfFetchObject( $res ) ) {
00292
if ( !
$first ) {
$sql .=
",";
$sql2 .=
","; }
00293
$first =
false;
00294
00295
$sql .=
"({$row->bl_from},{$this->mId})";
00296
$sql2 .=
$row->bl_from;
00297 }
00298
$sql2 .=
")";
00299
wfQuery( $sql,
DB_WRITE, $fname );
00300
wfQuery( $sql2,
DB_WRITE, $fname );
00301
00302
$sql =
"DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
00303
wfQuery( $sql,
DB_WRITE, $fname );
00304 }
00305
00306 }
00307
00308 ?>