Consolidate a bit of the link update code from movepage into linksupdate
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?
2 # See deferred.doc
3
4 class LinksUpdate {
5
6 /* private */ var $mId, $mTitle;
7
8 function LinksUpdate( $id, $title )
9 {
10 $this->mId = $id;
11 $this->mTitle = $title;
12 $this->mTitleEnc = wfStrencode( $title );
13 }
14
15 function doUpdate()
16 {
17 /* Update link tables with outgoing links from an updated article */
18 /* Currently this is 'dumb', removing all links and putting them back. */
19
20 /* Relies on the 'link cache' to be filled out */
21 global $wgLinkCache, $wgDBtransactions;
22 $fname = "LinksUpdate::doUpdate";
23 wfProfileIn( $fname );
24
25 if( $wgDBtransactions ) {
26 $sql = "BEGIN";
27 wfQuery( $sql, $fname );
28 }
29
30 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
31 wfQuery( $sql, $fname );
32
33 $a = $wgLinkCache->getGoodLinks();
34 $sql = "";
35 if ( 0 != count( $a ) ) {
36 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
37 $first = true;
38 foreach( $a as $lt => $lid ) {
39 if ( ! $first ) { $sql .= ","; }
40 $first = false;
41
42 $sql .= "('{$this->mTitleEnc}',{$lid})";
43 }
44 }
45 if ( "" != $sql ) { wfQuery( $sql, $fname ); }
46
47 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
48 wfQuery( $sql, $fname );
49
50 $a = $wgLinkCache->getBadLinks();
51 $sql = "";
52 if ( 0 != count ( $a ) ) {
53 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
54 $first = true;
55 foreach( $a as $blt ) {
56 $blt = wfStrencode( $blt );
57 if ( ! $first ) { $sql .= ","; }
58 $first = false;
59
60 $sql .= "({$this->mId},'{$blt}')";
61 }
62 }
63 if ( "" != $sql ) { wfQuery( $sql, $fname ); }
64
65 $sql = "DELETE FROM imagelinks WHERE il_from='{$t}'";
66 wfQuery( $sql, $fname );
67
68 $a = $wgLinkCache->getImageLinks();
69 $sql = "";
70 if ( 0 != count ( $a ) ) {
71 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
72 $first = true;
73 foreach( $a as $iname => $val ) {
74 $iname = wfStrencode( $iname );
75 if ( ! $first ) { $sql .= ","; }
76 $first = false;
77
78 $sql .= "('{$t}','{$iname}')";
79 }
80 }
81 if ( "" != $sql ) { wfQuery( $sql, $fname ); }
82
83 $this->fixBrokenLinks();
84
85 if( $wgDBtransactions ) {
86 $sql = "COMMIT";
87 wfQuery( $sql, $fname );
88 }
89 wfProfileOut();
90 }
91
92 function fixBrokenLinks() {
93 /* Update any brokenlinks *to* this page */
94 /* Call for a newly created page, or just to make sure state is consistent */
95
96 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
97 $res = wfQuery( $sql, $fname );
98 if ( 0 == wfNumRows( $res ) ) { return; }
99
100 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
101 $now = wfTimestampNow();
102 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
103 $first = true;
104 while ( $row = wfFetchObject( $res ) ) {
105 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
106 $first = false;
107 $nl = wfStrencode( Article::nameOf( $row->bl_from ) );
108
109 $sql .= "('{$nl}',{$this->mId})";
110 $sql2 .= $row->bl_from;
111 }
112 $sql2 .= ")";
113 wfQuery( $sql, $fname );
114 wfQuery( $sql2, $fname );
115
116 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
117 wfQuery( $sql, $fname );
118 }
119
120 }
121
122 ?>