Merge "Made SSL validation in Curl HTTP requests the default."
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchangeslinked.php
1 <?php
2 /**
3 * Implements Special:Recentchangeslinked
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * This is to display changes made to all articles linked in an article.
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialRecentchangeslinked extends SpecialRecentChanges {
30 var $rclTargetTitle;
31
32 function __construct(){
33 parent::__construct( 'Recentchangeslinked' );
34 }
35
36 public function getDefaultOptions() {
37 $opts = parent::getDefaultOptions();
38 $opts->add( 'target', '' );
39 $opts->add( 'showlinkedto', false );
40 return $opts;
41 }
42
43 public function parseParameters( $par, FormOptions $opts ) {
44 $opts['target'] = $par;
45 }
46
47 public function feedSetup() {
48 $opts = parent::feedSetup();
49 $opts['target'] = $this->getRequest()->getVal( 'target' );
50 return $opts;
51 }
52
53 public function getFeedObject( $feedFormat ){
54 $feed = new ChangesFeed( $feedFormat, false );
55 $feedObj = $feed->getFeedObject(
56 $this->msg( 'recentchangeslinked-title', $this->getTargetTitle()->getPrefixedText() )
57 ->inContentLanguage()->text(),
58 $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
59 $this->getTitle()->getFullUrl()
60 );
61 return array( $feed, $feedObj );
62 }
63
64 public function doMainQuery( $conds, $opts ) {
65 $target = $opts['target'];
66 $showlinkedto = $opts['showlinkedto'];
67 $limit = $opts['limit'];
68
69 if ( $target === '' ) {
70 return false;
71 }
72 $outputPage = $this->getOutput();
73 $title = Title::newFromURL( $target );
74 if( !$title || $title->getInterwiki() != '' ){
75 $outputPage->wrapWikiMsg( "<div class=\"errorbox\">\n$1\n</div><br style=\"clear: both\" />", 'allpagesbadtitle' );
76 return false;
77 }
78
79 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
80
81 /*
82 * Ordinary links are in the pagelinks table, while transclusions are
83 * in the templatelinks table, categorizations in categorylinks and
84 * image use in imagelinks. We need to somehow combine all these.
85 * Special:Whatlinkshere does this by firing multiple queries and
86 * merging the results, but the code we inherit from our parent class
87 * expects only one result set so we use UNION instead.
88 */
89
90 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
91 $id = $title->getArticleID();
92 $ns = $title->getNamespace();
93 $dbkey = $title->getDBkey();
94
95 $tables = array( 'recentchanges' );
96 $select = RecentChange::selectFields();
97 $join_conds = array();
98 $query_options = array();
99
100 // left join with watchlist table to highlight watched rows
101 $uid = $this->getUser()->getId();
102 if( $uid ) {
103 $tables[] = 'watchlist';
104 $select[] = 'wl_user';
105 $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
106 }
107 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
108 $tables[] = 'page';
109 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
110 $select[] = 'page_latest';
111 }
112 ChangeTags::modifyDisplayQuery(
113 $tables,
114 $select,
115 $conds,
116 $join_conds,
117 $query_options,
118 $opts['tagfilter']
119 );
120
121 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
122 return false;
123 }
124
125 if( $ns == NS_CATEGORY && !$showlinkedto ) {
126 // special handling for categories
127 // XXX: should try to make this less klugy
128 $link_tables = array( 'categorylinks' );
129 $showlinkedto = true;
130 } else {
131 // for now, always join on these tables; really should be configurable as in whatlinkshere
132 $link_tables = array( 'pagelinks', 'templatelinks' );
133 // imagelinks only contains links to pages in NS_FILE
134 if( $ns == NS_FILE || !$showlinkedto ) {
135 $link_tables[] = 'imagelinks';
136 }
137 }
138
139 if( $id == 0 && !$showlinkedto ) {
140 return false; // nonexistent pages can't link to any pages
141 }
142
143 // field name prefixes for all the various tables we might want to join with
144 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
145
146 $subsql = array(); // SELECT statements to combine with UNION
147
148 foreach( $link_tables as $link_table ) {
149 $pfx = $prefix[$link_table];
150
151 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
152 if( $link_table == 'imagelinks' ) {
153 $link_ns = NS_FILE;
154 } elseif( $link_table == 'categorylinks' ) {
155 $link_ns = NS_CATEGORY;
156 } else {
157 $link_ns = 0;
158 }
159
160 if( $showlinkedto ) {
161 // find changes to pages linking to this page
162 if( $link_ns ) {
163 if( $ns != $link_ns ) {
164 continue;
165 } // should never happen, but check anyway
166 $subconds = array( "{$pfx}_to" => $dbkey );
167 } else {
168 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
169 }
170 $subjoin = "rc_cur_id = {$pfx}_from";
171 } else {
172 // find changes to pages linked from this page
173 $subconds = array( "{$pfx}_from" => $id );
174 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
175 $subconds["rc_namespace"] = $link_ns;
176 $subjoin = "rc_title = {$pfx}_to";
177 } else {
178 $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
179 }
180 }
181
182 if( $dbr->unionSupportsOrderAndLimit()) {
183 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
184 } else {
185 $order = array();
186 }
187
188 $query = $dbr->selectSQLText(
189 array_merge( $tables, array( $link_table ) ),
190 $select,
191 $conds + $subconds,
192 __METHOD__,
193 $order + $query_options,
194 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
195 );
196
197 if( $dbr->unionSupportsOrderAndLimit())
198 $query = $dbr->limitResult( $query, $limit );
199
200 $subsql[] = $query;
201 }
202
203 if( count($subsql) == 0 ) {
204 return false; // should never happen
205 }
206 if( count($subsql) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
207 $sql = $subsql[0];
208 } else {
209 // need to resort and relimit after union
210 $sql = $dbr->unionQueries($subsql, false).' ORDER BY rc_timestamp DESC';
211 $sql = $dbr->limitResult($sql, $limit, false);
212 }
213
214 $res = $dbr->query( $sql, __METHOD__ );
215
216 if( $res->numRows() == 0 ) {
217 $this->mResultEmpty = true;
218 }
219
220 return $res;
221 }
222
223 /**
224 * @param $opts FormOptions
225 * @return array
226 */
227 function getExtraOptions( $opts ){
228 $opts->consumeValues( array( 'showlinkedto', 'target', 'tagfilter' ) );
229 $extraOpts = array();
230 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
231 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
232 Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
233 Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
234 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
235 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
236 if ($tagFilter) {
237 $extraOpts['tagfilter'] = $tagFilter;
238 }
239 return $extraOpts;
240 }
241
242 /**
243 * @return Title
244 */
245 function getTargetTitle() {
246 if ( $this->rclTargetTitle === null ) {
247 $opts = $this->getOptions();
248 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
249 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
250 } else {
251 $this->rclTargetTitle = false;
252 }
253 }
254 return $this->rclTargetTitle;
255 }
256
257 function setTopText( FormOptions $opts ) {
258 $target = $this->getTargetTitle();
259 if( $target ) {
260 $this->getOutput()->addBacklinkSubtitle( $target );
261 }
262 }
263
264 public function getFeedQuery() {
265 $target = $this->getTargetTitle();
266 if( $target ) {
267 return "target=" . urlencode( $target->getPrefixedDBkey() );
268 } else {
269 return false;
270 }
271 }
272
273 function setBottomText( FormOptions $opts ) {
274 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ) {
275 $this->getOutput()->addWikiMsg( 'recentchangeslinked-noresult' );
276 }
277 }
278 }