Merge "Allow partially blocked users to tag unrelated revisions"
[lhc/web/wiklou.git] / includes / specials / SpecialLinkSearch.php
1 <?php
2 /**
3 * Implements Special:LinkSearch
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 * @author Brion Vibber
23 */
24
25 use Wikimedia\Rdbms\IResultWrapper;
26 use Wikimedia\Rdbms\IDatabase;
27
28 /**
29 * Special:LinkSearch to search the external-links table.
30 * @ingroup SpecialPage
31 */
32 class SpecialLinkSearch extends QueryPage {
33 /** @var array|bool */
34 private $mungedQuery = false;
35 /** @var string|null */
36 private $mQuery;
37 /** @var int|null */
38 private $mNs;
39 /** @var string|null */
40 private $mProt;
41
42 function setParams( $params ) {
43 $this->mQuery = $params['query'];
44 $this->mNs = $params['namespace'];
45 $this->mProt = $params['protocol'];
46 }
47
48 function __construct( $name = 'LinkSearch' ) {
49 parent::__construct( $name );
50
51 // Since we don't control the constructor parameters, we can't inject services that way.
52 // Instead, we initialize services in the execute() method, and allow them to be overridden
53 // using the setServices() method.
54 }
55
56 function isCacheable() {
57 return false;
58 }
59
60 public function execute( $par ) {
61 $this->setHeaders();
62 $this->outputHeader();
63
64 $out = $this->getOutput();
65 $out->allowClickjacking();
66
67 $request = $this->getRequest();
68 $target = $request->getVal( 'target', $par );
69 $namespace = $request->getIntOrNull( 'namespace' );
70
71 $protocols_list = [];
72 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
73 if ( $prot !== '//' ) {
74 $protocols_list[] = $prot;
75 }
76 }
77
78 $target2 = Parser::normalizeLinkUrl( $target );
79 // Get protocol, default is http://
80 $protocol = 'http://';
81 $bits = wfParseUrl( $target );
82 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
83 $protocol = $bits['scheme'] . $bits['delimiter'];
84 // Make sure wfParseUrl() didn't make some well-intended correction in the
85 // protocol
86 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
87 $target2 = substr( $target, strlen( $protocol ) );
88 } else {
89 // If it did, let LinkFilter::makeLikeArray() handle this
90 $protocol = '';
91 }
92 }
93
94 $out->addWikiMsg(
95 'linksearch-text',
96 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
97 count( $protocols_list )
98 );
99 $fields = [
100 'target' => [
101 'type' => 'text',
102 'name' => 'target',
103 'id' => 'target',
104 'size' => 50,
105 'label-message' => 'linksearch-pat',
106 'default' => $target,
107 'dir' => 'ltr',
108 ]
109 ];
110 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
111 $fields += [
112 'namespace' => [
113 'type' => 'namespaceselect',
114 'name' => 'namespace',
115 'label-message' => 'linksearch-ns',
116 'default' => $namespace,
117 'id' => 'namespace',
118 'all' => '',
119 'cssclass' => 'namespaceselector',
120 ],
121 ];
122 }
123 $hiddenFields = [
124 'title' => $this->getPageTitle()->getPrefixedDBkey(),
125 ];
126 $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
127 $htmlForm->addHiddenFields( $hiddenFields );
128 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
129 $htmlForm->setWrapperLegendMsg( 'linksearch' );
130 $htmlForm->setAction( wfScript() );
131 $htmlForm->setMethod( 'get' );
132 $htmlForm->prepareForm()->displayForm( false );
133 $this->addHelpLink( 'Help:Linksearch' );
134
135 if ( $target != '' ) {
136 $this->setParams( [
137 'query' => $target2,
138 'namespace' => $namespace,
139 'protocol' => $protocol ] );
140 parent::execute( $par );
141 if ( $this->mungedQuery === false ) {
142 $out->addWikiMsg( 'linksearch-error' );
143 }
144 }
145 }
146
147 /**
148 * Disable RSS/Atom feeds
149 * @return bool
150 */
151 function isSyndicated() {
152 return false;
153 }
154
155 function linkParameters() {
156 $params = [];
157 $params['target'] = $this->mProt . $this->mQuery;
158 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
159 $params['namespace'] = $this->mNs;
160 }
161
162 return $params;
163 }
164
165 public function getQueryInfo() {
166 $dbr = wfGetDB( DB_REPLICA );
167
168 if ( $this->mQuery === '*' && $this->mProt !== '' ) {
169 $this->mungedQuery = [
170 'el_index_60' . $dbr->buildLike( $this->mProt, $dbr->anyString() ),
171 ];
172 } else {
173 $this->mungedQuery = LinkFilter::getQueryConditions( $this->mQuery, [
174 'protocol' => $this->mProt,
175 'oneWildcard' => true,
176 'db' => $dbr
177 ] );
178 }
179 if ( $this->mungedQuery === false ) {
180 // Invalid query; return no results
181 return [ 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' ];
182 }
183
184 $orderBy = [];
185 if ( !isset( $this->mungedQuery['el_index_60'] ) ) {
186 $orderBy[] = 'el_index_60';
187 }
188 $orderBy[] = 'el_id';
189
190 $retval = [
191 'tables' => [ 'page', 'externallinks' ],
192 'fields' => [
193 'namespace' => 'page_namespace',
194 'title' => 'page_title',
195 'value' => 'el_index',
196 'url' => 'el_to'
197 ],
198 'conds' => array_merge(
199 [
200 'page_id = el_from',
201 ],
202 $this->mungedQuery
203 ),
204 'options' => [ 'ORDER BY' => $orderBy ]
205 ];
206
207 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
208 $retval['conds']['page_namespace'] = $this->mNs;
209 }
210
211 return $retval;
212 }
213
214 /**
215 * Pre-fill the link cache
216 *
217 * @param IDatabase $db
218 * @param IResultWrapper $res
219 */
220 function preprocessResults( $db, $res ) {
221 $this->executeLBFromResultWrapper( $res );
222 }
223
224 /**
225 * @param Skin $skin
226 * @param object $result Result row
227 * @return string
228 */
229 function formatResult( $skin, $result ) {
230 $title = new TitleValue( (int)$result->namespace, $result->title );
231 $pageLink = $this->getLinkRenderer()->makeLink( $title );
232
233 $url = $result->url;
234 $urlLink = Linker::makeExternalLink( $url, $url );
235
236 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
237 }
238
239 /**
240 * Override to squash the ORDER BY.
241 * Not much point in descending order here.
242 * @return array
243 */
244 function getOrderFields() {
245 return [];
246 }
247
248 protected function getGroupName() {
249 return 'redirects';
250 }
251
252 /**
253 * enwiki complained about low limits on this special page
254 *
255 * @see T130058
256 * @todo FIXME This special page should not use LIMIT for paging
257 * @return int
258 */
259 protected function getMaxResults() {
260 return max( parent::getMaxResults(), 60000 );
261 }
262 }