Merge "SpecialMovepage: Convert form to use OOUI controls"
[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 /**
26 * Special:LinkSearch to search the external-links table.
27 * @ingroup SpecialPage
28 */
29 class LinkSearchPage extends QueryPage {
30 /** @var array|bool */
31 private $mungedQuery = false;
32
33 /**
34 * @var PageLinkRenderer
35 */
36 protected $linkRenderer = null;
37
38 function setParams( $params ) {
39 $this->mQuery = $params['query'];
40 $this->mNs = $params['namespace'];
41 $this->mProt = $params['protocol'];
42 }
43
44 function __construct( $name = 'LinkSearch' ) {
45 parent::__construct( $name );
46
47 // Since we don't control the constructor parameters, we can't inject services that way.
48 // Instead, we initialize services in the execute() method, and allow them to be overridden
49 // using the setServices() method.
50 }
51
52 /**
53 * Initialize or override the PageLinkRenderer LinkSearchPage collaborates with.
54 * Useful mainly for testing.
55 *
56 * @todo query logic and rendering logic should be split and also injected
57 *
58 * @param PageLinkRenderer $linkRenderer
59 */
60 public function setPageLinkRenderer(
61 PageLinkRenderer $linkRenderer
62 ) {
63 $this->linkRenderer = $linkRenderer;
64 }
65
66 /**
67 * Initialize any services we'll need (unless it has already been provided via a setter).
68 * This allows for dependency injection even though we don't control object creation.
69 */
70 private function initServices() {
71 global $wgLanguageCode;
72 if ( !$this->linkRenderer ) {
73 $lang = Language::factory( $wgLanguageCode );
74 $titleFormatter = new MediaWikiTitleCodec( $lang, GenderCache::singleton() );
75 $this->linkRenderer = new MediaWikiPageLinkRenderer( $titleFormatter );
76 }
77 }
78
79 function isCacheable() {
80 return false;
81 }
82
83 public function execute( $par ) {
84 $this->initServices();
85
86 $this->setHeaders();
87 $this->outputHeader();
88
89 $out = $this->getOutput();
90 $out->allowClickjacking();
91
92 $request = $this->getRequest();
93 $target = $request->getVal( 'target', $par );
94 $namespace = $request->getIntOrNull( 'namespace' );
95
96 $protocols_list = array();
97 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
98 if ( $prot !== '//' ) {
99 $protocols_list[] = $prot;
100 }
101 }
102
103 $target2 = $target;
104 // Get protocol, default is http://
105 $protocol = 'http://';
106 $bits = wfParseUrl( $target );
107 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
108 $protocol = $bits['scheme'] . $bits['delimiter'];
109 // Make sure wfParseUrl() didn't make some well-intended correction in the
110 // protocol
111 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
112 $target2 = substr( $target, strlen( $protocol ) );
113 } else {
114 // If it did, let LinkFilter::makeLikeArray() handle this
115 $protocol = '';
116 }
117 }
118
119 $out->addWikiMsg(
120 'linksearch-text',
121 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
122 count( $protocols_list )
123 );
124 $fields = array(
125 'target' => array(
126 'type' => 'text',
127 'name' => 'target',
128 'id' => 'target',
129 'size' => 50,
130 'label-message' => 'linksearch-pat',
131 'default' => $target,
132 'dir' => 'ltr',
133 )
134 );
135 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
136 $fields += array(
137 'namespace' => array(
138 'type' => 'namespaceselect',
139 'name' => 'namespace',
140 'label-message' => 'linksearch-ns',
141 'default' => $namespace,
142 'id' => 'namespace',
143 'all' => '',
144 'cssclass' => 'namespaceselector',
145 ),
146 );
147 }
148 $hiddenFields = array(
149 'title' => $this->getPageTitle()->getPrefixedDBkey(),
150 );
151 $htmlForm = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
152 $htmlForm->addHiddenFields( $hiddenFields );
153 $htmlForm->setSubmitTextMsg( 'linksearch-ok' );
154 $htmlForm->setWrapperLegendMsg( 'linksearch' );
155 $htmlForm->setAction( wfScript() );
156 $htmlForm->setMethod( 'get' );
157 $htmlForm->prepareForm()->displayForm( false );
158 $this->addHelpLink( 'Help:Linksearch' );
159
160 if ( $target != '' ) {
161 $this->setParams( array(
162 'query' => $target2,
163 'namespace' => $namespace,
164 'protocol' => $protocol ) );
165 parent::execute( $par );
166 if ( $this->mungedQuery === false ) {
167 $out->addWikiMsg( 'linksearch-error' );
168 }
169 }
170 }
171
172 /**
173 * Disable RSS/Atom feeds
174 * @return bool
175 */
176 function isSyndicated() {
177 return false;
178 }
179
180 /**
181 * Return an appropriately formatted LIKE query and the clause
182 *
183 * @param string $query Search pattern to search for
184 * @param string $prot Protocol, e.g. 'http://'
185 *
186 * @return array
187 */
188 static function mungeQuery( $query, $prot ) {
189 $field = 'el_index';
190 $dbr = wfGetDB( DB_SLAVE );
191
192 if ( $query === '*' && $prot !== '' ) {
193 // Allow queries like 'ftp://*' to find all ftp links
194 $rv = array( $prot, $dbr->anyString() );
195 } else {
196 $rv = LinkFilter::makeLikeArray( $query, $prot );
197 }
198
199 if ( $rv === false ) {
200 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
201 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
202 if ( preg_match( $pattern, $query ) ) {
203 $rv = array( $prot . rtrim( $query, " \t*" ), $dbr->anyString() );
204 $field = 'el_to';
205 }
206 }
207
208 return array( $rv, $field );
209 }
210
211 function linkParameters() {
212 $params = array();
213 $params['target'] = $this->mProt . $this->mQuery;
214 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
215 $params['namespace'] = $this->mNs;
216 }
217
218 return $params;
219 }
220
221 public function getQueryInfo() {
222 $dbr = wfGetDB( DB_SLAVE );
223 // strip everything past first wildcard, so that
224 // index-based-only lookup would be done
225 list( $this->mungedQuery, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
226 if ( $this->mungedQuery === false ) {
227 // Invalid query; return no results
228 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
229 }
230
231 $stripped = LinkFilter::keepOneWildcard( $this->mungedQuery );
232 $like = $dbr->buildLike( $stripped );
233 $retval = array(
234 'tables' => array( 'page', 'externallinks' ),
235 'fields' => array(
236 'namespace' => 'page_namespace',
237 'title' => 'page_title',
238 'value' => 'el_index',
239 'url' => 'el_to'
240 ),
241 'conds' => array(
242 'page_id = el_from',
243 "$clause $like"
244 ),
245 'options' => array( 'USE INDEX' => $clause )
246 );
247
248 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
249 $retval['conds']['page_namespace'] = $this->mNs;
250 }
251
252 return $retval;
253 }
254
255 /**
256 * Pre-fill the link cache
257 *
258 * @param IDatabase $db
259 * @param ResultWrapper $res
260 */
261 function preprocessResults( $db, $res ) {
262 if ( $res->numRows() > 0 ) {
263 $linkBatch = new LinkBatch();
264
265 foreach ( $res as $row ) {
266 $linkBatch->add( $row->namespace, $row->title );
267 }
268
269 $res->seek( 0 );
270 $linkBatch->execute();
271 }
272 }
273
274 /**
275 * @param Skin $skin
276 * @param object $result Result row
277 * @return string
278 */
279 function formatResult( $skin, $result ) {
280 $title = new TitleValue( (int)$result->namespace, $result->title );
281 $pageLink = $this->linkRenderer->renderHtmlLink( $title );
282
283 $url = $result->url;
284 $urlLink = Linker::makeExternalLink( $url, $url );
285
286 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
287 }
288
289 /**
290 * Override to squash the ORDER BY.
291 * We do a truncated index search, so the optimizer won't trust
292 * it as good enough for optimizing sort. The implicit ordering
293 * from the scan will usually do well enough for our needs.
294 * @return array
295 */
296 function getOrderFields() {
297 return array();
298 }
299
300 protected function getGroupName() {
301 return 'redirects';
302 }
303 }