Merge "Add .pipeline/ with dev image variant"
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * This is to display changes made to all articles linked in an article.
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialRecentChangesLinked extends SpecialRecentChanges {
32 /** @var bool|Title */
33 protected $rclTargetTitle;
34
35 function __construct() {
36 parent::__construct( 'Recentchangeslinked' );
37 }
38
39 public function getDefaultOptions() {
40 $opts = parent::getDefaultOptions();
41 $opts->add( 'target', '' );
42 $opts->add( 'showlinkedto', false );
43
44 return $opts;
45 }
46
47 public function parseParameters( $par, FormOptions $opts ) {
48 $opts['target'] = $par;
49 }
50
51 /**
52 * @inheritDoc
53 */
54 protected function doMainQuery( $tables, $select, $conds, $query_options,
55 $join_conds, FormOptions $opts
56 ) {
57 $target = $opts['target'];
58 $showlinkedto = $opts['showlinkedto'];
59 $limit = $opts['limit'];
60
61 if ( $target === '' ) {
62 return false;
63 }
64 $outputPage = $this->getOutput();
65 $title = Title::newFromText( $target );
66 if ( !$title || $title->isExternal() ) {
67 $outputPage->addHTML(
68 Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
69 );
70 return false;
71 }
72
73 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
74
75 /*
76 * Ordinary links are in the pagelinks table, while transclusions are
77 * in the templatelinks table, categorizations in categorylinks and
78 * image use in imagelinks. We need to somehow combine all these.
79 * Special:Whatlinkshere does this by firing multiple queries and
80 * merging the results, but the code we inherit from our parent class
81 * expects only one result set so we use UNION instead.
82 */
83
84 $dbr = wfGetDB( DB_REPLICA, 'recentchangeslinked' );
85 $id = $title->getArticleID();
86 $ns = $title->getNamespace();
87 $dbkey = $title->getDBkey();
88
89 $rcQuery = RecentChange::getQueryInfo();
90 $tables = array_merge( $tables, $rcQuery['tables'] );
91 $select = array_merge( $rcQuery['fields'], $select );
92 $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
93
94 // left join with watchlist table to highlight watched rows
95 $uid = $this->getUser()->getId();
96 if ( $uid && MediaWikiServices::getInstance()
97 ->getPermissionManager()
98 ->userHasRight( $this->getUser(), 'viewmywatchlist' )
99 ) {
100 $tables[] = 'watchlist';
101 $select[] = 'wl_user';
102 $join_conds['watchlist'] = [ 'LEFT JOIN', [
103 'wl_user' => $uid,
104 'wl_title=rc_title',
105 'wl_namespace=rc_namespace'
106 ] ];
107 }
108
109 // JOIN on page, used for 'last revision' filter highlight
110 $tables[] = 'page';
111 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
112 $select[] = 'page_latest';
113
114 $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
115 ChangeTags::modifyDisplayQuery(
116 $tables,
117 $select,
118 $conds,
119 $join_conds,
120 $query_options,
121 $tagFilter
122 );
123
124 if ( $dbr->unionSupportsOrderAndLimit() ) {
125 if ( count( $tagFilter ) > 1 ) {
126 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
127 // To prevent this from causing query performance problems, we need to add
128 // a GROUP BY, and add rc_id to the ORDER BY.
129 $order = [
130 'GROUP BY' => 'rc_timestamp, rc_id',
131 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
132 ];
133 } else {
134 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
135 }
136 } else {
137 $order = [];
138 }
139
140 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
141 $opts )
142 ) {
143 return false;
144 }
145
146 if ( $ns == NS_CATEGORY && !$showlinkedto ) {
147 // special handling for categories
148 // XXX: should try to make this less kludgy
149 $link_tables = [ 'categorylinks' ];
150 $showlinkedto = true;
151 } else {
152 // for now, always join on these tables; really should be configurable as in whatlinkshere
153 $link_tables = [ 'pagelinks', 'templatelinks' ];
154 // imagelinks only contains links to pages in NS_FILE
155 if ( $ns == NS_FILE || !$showlinkedto ) {
156 $link_tables[] = 'imagelinks';
157 }
158 }
159
160 if ( $id == 0 && !$showlinkedto ) {
161 return false; // nonexistent pages can't link to any pages
162 }
163
164 // field name prefixes for all the various tables we might want to join with
165 $prefix = [
166 'pagelinks' => 'pl',
167 'templatelinks' => 'tl',
168 'categorylinks' => 'cl',
169 'imagelinks' => 'il'
170 ];
171
172 $subsql = []; // SELECT statements to combine with UNION
173
174 foreach ( $link_tables as $link_table ) {
175 $pfx = $prefix[$link_table];
176
177 // imagelinks and categorylinks tables have no xx_namespace field,
178 // and have xx_to instead of xx_title
179 if ( $link_table == 'imagelinks' ) {
180 $link_ns = NS_FILE;
181 } elseif ( $link_table == 'categorylinks' ) {
182 $link_ns = NS_CATEGORY;
183 } else {
184 $link_ns = 0;
185 }
186
187 if ( $showlinkedto ) {
188 // find changes to pages linking to this page
189 if ( $link_ns ) {
190 if ( $ns != $link_ns ) {
191 continue;
192 } // should never happen, but check anyway
193 $subconds = [ "{$pfx}_to" => $dbkey ];
194 } else {
195 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
196 }
197 $subjoin = "rc_cur_id = {$pfx}_from";
198 } else {
199 // find changes to pages linked from this page
200 $subconds = [ "{$pfx}_from" => $id ];
201 if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
202 $subconds["rc_namespace"] = $link_ns;
203 $subjoin = "rc_title = {$pfx}_to";
204 } else {
205 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
206 }
207 }
208
209 $query = $dbr->selectSQLText(
210 array_merge( $tables, [ $link_table ] ),
211 $select,
212 $conds + $subconds,
213 __METHOD__,
214 $order + $query_options,
215 $join_conds + [ $link_table => [ 'JOIN', $subjoin ] ]
216 );
217
218 if ( $dbr->unionSupportsOrderAndLimit() ) {
219 $query = $dbr->limitResult( $query, $limit );
220 }
221
222 $subsql[] = $query;
223 }
224
225 if ( count( $subsql ) == 0 ) {
226 return false; // should never happen
227 }
228 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
229 $sql = $subsql[0];
230 } else {
231 // need to resort and relimit after union
232 $sql = $dbr->unionQueries( $subsql, $dbr::UNION_DISTINCT ) .
233 ' ORDER BY rc_timestamp DESC';
234 $sql = $dbr->limitResult( $sql, $limit, false );
235 }
236
237 return $dbr->query( $sql, __METHOD__ );
238 }
239
240 function setTopText( FormOptions $opts ) {
241 $target = $this->getTargetTitle();
242 if ( $target ) {
243 $this->getOutput()->addBacklinkSubtitle( $target );
244 $this->getSkin()->setRelevantTitle( $target );
245 }
246 }
247
248 /**
249 * Get options to be displayed in a form
250 *
251 * @param FormOptions $opts
252 * @return array
253 */
254 function getExtraOptions( $opts ) {
255 $extraOpts = parent::getExtraOptions( $opts );
256
257 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
258
259 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
260 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
261 Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
262 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
263
264 $this->addHelpLink( 'Help:Related changes' );
265 return $extraOpts;
266 }
267
268 /**
269 * @return Title
270 */
271 function getTargetTitle() {
272 if ( $this->rclTargetTitle === null ) {
273 $opts = $this->getOptions();
274 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
275 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
276 } else {
277 $this->rclTargetTitle = false;
278 }
279 }
280
281 return $this->rclTargetTitle;
282 }
283
284 /**
285 * Return an array of subpages beginning with $search that this special page will accept.
286 *
287 * @param string $search Prefix to search for
288 * @param int $limit Maximum number of results to return (usually 10)
289 * @param int $offset Number of results to skip (usually 0)
290 * @return string[] Matching subpages
291 */
292 public function prefixSearchSubpages( $search, $limit, $offset ) {
293 return $this->prefixSearchString( $search, $limit, $offset );
294 }
295
296 protected function outputNoResults() {
297 $targetTitle = $this->getTargetTitle();
298 if ( $targetTitle === false ) {
299 $this->getOutput()->addHTML(
300 Html::rawElement(
301 'div',
302 [ 'class' => 'mw-changeslist-empty mw-changeslist-notargetpage' ],
303 $this->msg( 'recentchanges-notargetpage' )->parse()
304 )
305 );
306 } elseif ( !$targetTitle || $targetTitle->isExternal() ) {
307 $this->getOutput()->addHTML(
308 Html::rawElement(
309 'div',
310 [ 'class' => 'mw-changeslist-empty mw-changeslist-invalidtargetpage' ],
311 $this->msg( 'allpagesbadtitle' )->parse()
312 )
313 );
314 } else {
315 parent::outputNoResults();
316 }
317 }
318 }