Fix for compatibility with short_open_tag = Off
[lhc/web/wiklou.git] / includes / LinksUpdate.php
1 <?php
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
16 function doUpdate()
17 {
18 global $wgUseBetterLinksUpdate, $wgLinkCache, $wgDBtransactions;
19 global $wgEnablePersistentLC;
20
21 /* Update link tables with outgoing links from an updated article */
22 /* Relies on the 'link cache' to be filled out */
23
24 $fname = "LinksUpdate::doUpdate";
25 wfProfileIn( $fname );
26
27 $del = array();
28 $add = array();
29
30 if( $wgDBtransactions ) {
31 $sql = "BEGIN";
32 wfQuery( $sql, DB_WRITE, $fname );
33 }
34
35 #------------------------------------------------------------------------------
36 # Good links
37
38 if ( $wgLinkCache->incrementalSetup( LINKCACHE_GOOD, $del, $add ) ) {
39 # Delete where necessary
40 if ( count( $del ) ) {
41 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}' AND l_to IN(".
42 implode( ",", $del ) . ")";
43 wfQuery( $sql, DB_WRITE, $fname );
44 }
45 } else {
46 # Delete everything
47 $sql = "DELETE FROM links WHERE l_from='{$this->mTitleEnc}'";
48 wfQuery( $sql, DB_WRITE, $fname );
49
50 # Get the addition list
51 $add = $wgLinkCache->getGoodLinks();
52 }
53
54 # Do the insertion
55 $sql = "";
56 if ( 0 != count( $add ) ) {
57 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
58 $first = true;
59 foreach( $add as $lt => $lid ) {
60
61 if ( ! $first ) { $sql .= ","; }
62 $first = false;
63
64 $sql .= "('{$this->mTitleEnc}',{$lid})";
65 }
66 }
67 if ( "" != $sql ) {
68 wfQuery( $sql, DB_WRITE, $fname );
69 }
70
71 #------------------------------------------------------------------------------
72 # Bad links
73
74 if ( $wgLinkCache->incrementalSetup( LINKCACHE_BAD, $del, $add ) ) {
75 # Delete where necessary
76 if ( count( $del ) ) {
77 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId} AND bl_to IN('" .
78 implode( "','", array_map( "wfStrencode", $del ) ) . "')";
79 wfQuery( $sql, DB_WRITE, $fname );
80 }
81 } else {
82 # Delete all
83 $sql = "DELETE FROM brokenlinks WHERE bl_from={$this->mId}";
84 wfQuery( $sql, DB_WRITE, $fname );
85
86 # Get addition list
87 $add = $wgLinkCache->getBadLinks();
88 }
89
90 # Do additions
91 $sql = "";
92 if ( 0 != count ( $add ) ) {
93 $sql = "INSERT INTO brokenlinks (bl_from,bl_to) VALUES ";
94 $first = true;
95 foreach( $add as $blt ) {
96 $blt = wfStrencode( $blt );
97 if ( ! $first ) { $sql .= ","; }
98 $first = false;
99
100 $sql .= "({$this->mId},'{$blt}')";
101 }
102 }
103 if ( "" != $sql ) {
104 wfQuery( $sql, DB_WRITE, $fname );
105 }
106
107 #------------------------------------------------------------------------------
108 # Image links
109 $sql = "DELETE FROM imagelinks WHERE il_from='{$this->mTitleEnc}'";
110 wfQuery( $sql, DB_WRITE, $fname );
111
112 # Get addition list
113 $add = $wgLinkCache->getImageLinks();
114
115 # Do the insertion
116 $sql = "";
117 $image = Namespace::getImage();
118 if ( 0 != count ( $add ) ) {
119 $sql = "INSERT INTO imagelinks (il_from,il_to) VALUES ";
120 $first = true;
121 foreach( $add as $iname => $val ) {
122 # FIXME: Change all this to avoid unnecessary duplication
123 $nt = Title::makeTitle( $image, $iname );
124 if( !$nt ) continue;
125 $nt->invalidateCache();
126
127 $iname = wfStrencode( $iname );
128 if ( ! $first ) { $sql .= ","; }
129 $first = false;
130
131 $sql .= "('{$this->mTitleEnc}','{$iname}')";
132 }
133 }
134 if ( "" != $sql ) { wfQuery( $sql, DB_WRITE, $fname ); }
135
136 $this->fixBrokenLinks();
137
138 if( $wgDBtransactions ) {
139 $sql = "COMMIT";
140 wfQuery( $sql, DB_WRITE, $fname );
141 }
142 wfProfileOut( $fname );
143 }
144
145 function fixBrokenLinks() {
146 /* Update any brokenlinks *to* this page */
147 /* Call for a newly created page, or just to make sure state is consistent */
148
149 $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
150 $res = wfQuery( $sql, DB_READ, $fname );
151 if ( 0 == wfNumRows( $res ) ) { return; }
152
153 $sql = "INSERT INTO links (l_from,l_to) VALUES ";
154 $now = wfTimestampNow();
155 $sql2 = "UPDATE cur SET cur_touched='{$now}' WHERE cur_id IN (";
156 $first = true;
157 while ( $row = wfFetchObject( $res ) ) {
158 if ( ! $first ) { $sql .= ","; $sql2 .= ","; }
159 $first = false;
160 $nl = wfStrencode( Title::nameOf( $row->bl_from ) );
161
162 $sql .= "('{$nl}',{$this->mId})";
163 $sql2 .= $row->bl_from;
164 }
165 $sql2 .= ")";
166 wfQuery( $sql, DB_WRITE, $fname );
167 wfQuery( $sql2, DB_WRITE, $fname );
168
169 $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->mTitleEnc}'";
170 wfQuery( $sql, DB_WRITE, $fname );
171 }
172
173 }
174
175 ?>