Merge "Fix and make PHPDoc tags in FileBackend more specific"
[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 if ( !$this->linkRenderer ) {
72 $lang = $this->getContext()->getLanguage();
73 $titleFormatter = new MediaWikiTitleCodec( $lang, GenderCache::singleton() );
74 $this->linkRenderer = new MediaWikiPageLinkRenderer( $titleFormatter );
75 }
76 }
77
78 function isCacheable() {
79 return false;
80 }
81
82 function execute( $par ) {
83 $this->initServices();
84
85 $this->setHeaders();
86 $this->outputHeader();
87
88 $out = $this->getOutput();
89 $out->allowClickjacking();
90
91 $request = $this->getRequest();
92 $target = $request->getVal( 'target', $par );
93 $namespace = $request->getIntOrNull( 'namespace', null );
94
95 $protocols_list = array();
96 foreach ( $this->getConfig()->get( 'UrlProtocols' ) as $prot ) {
97 if ( $prot !== '//' ) {
98 $protocols_list[] = $prot;
99 }
100 }
101
102 $target2 = $target;
103 // Get protocol, default is http://
104 $protocol = 'http://';
105 $bits = wfParseUrl( $target );
106 if ( isset( $bits['scheme'] ) && isset( $bits['delimiter'] ) ) {
107 $protocol = $bits['scheme'] . $bits['delimiter'];
108 // Make sure wfParseUrl() didn't make some well-intended correction in the
109 // protocol
110 if ( strcasecmp( $protocol, substr( $target, 0, strlen( $protocol ) ) ) === 0 ) {
111 $target2 = substr( $target, strlen( $protocol ) );
112 } else {
113 // If it did, let LinkFilter::makeLikeArray() handle this
114 $protocol = '';
115 }
116 }
117
118 $out->addWikiMsg(
119 'linksearch-text',
120 '<nowiki>' . $this->getLanguage()->commaList( $protocols_list ) . '</nowiki>',
121 count( $protocols_list )
122 );
123 $s = Html::openElement(
124 'form',
125 array( 'id' => 'mw-linksearch-form', 'method' => 'get', 'action' => wfScript() )
126 ) . "\n" .
127 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) . "\n" .
128 Html::openElement( 'fieldset' ) . "\n" .
129 Html::element( 'legend', array(), $this->msg( 'linksearch' )->text() ) . "\n" .
130 Xml::inputLabel(
131 $this->msg( 'linksearch-pat' )->text(),
132 'target',
133 'target',
134 50,
135 $target,
136 array(
137 // URLs are always ltr
138 'dir' => 'ltr',
139 )
140 ) . "\n";
141
142 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
143 $s .= Html::namespaceSelector(
144 array(
145 'selected' => $namespace,
146 'all' => '',
147 'label' => $this->msg( 'linksearch-ns' )->text()
148 ), array(
149 'name' => 'namespace',
150 'id' => 'namespace',
151 'class' => 'namespaceselector',
152 )
153 );
154 }
155
156 $s .= Xml::submitButton( $this->msg( 'linksearch-ok' )->text() ) . "\n" .
157 Html::closeElement( 'fieldset' ) . "\n" .
158 Html::closeElement( 'form' ) . "\n";
159 $out->addHTML( $s );
160
161 if ( $target != '' ) {
162 $this->setParams( array(
163 'query' => $target2,
164 'namespace' => $namespace,
165 'protocol' => $protocol ) );
166 parent::execute( $par );
167 if ( $this->mungedQuery === false ) {
168 $out->addWikiMsg( 'linksearch-error' );
169 }
170 }
171 }
172
173 /**
174 * Disable RSS/Atom feeds
175 * @return bool
176 */
177 function isSyndicated() {
178 return false;
179 }
180
181 /**
182 * Return an appropriately formatted LIKE query and the clause
183 *
184 * @param string $query Search pattern to search for
185 * @param string $prot Protocol, e.g. 'http://'
186 *
187 * @return array
188 */
189 static function mungeQuery( $query, $prot ) {
190 $field = 'el_index';
191 $dbr = wfGetDB( DB_SLAVE );
192
193 if ( $query === '*' && $prot !== '' ) {
194 // Allow queries like 'ftp://*' to find all ftp links
195 $rv = array( $prot, $dbr->anyString() );
196 } else {
197 $rv = LinkFilter::makeLikeArray( $query, $prot );
198 }
199
200 if ( $rv === false ) {
201 // LinkFilter doesn't handle wildcard in IP, so we'll have to munge here.
202 $pattern = '/^(:?[0-9]{1,3}\.)+\*\s*$|^(:?[0-9]{1,3}\.){3}[0-9]{1,3}:[0-9]*\*\s*$/';
203 if ( preg_match( $pattern, $query ) ) {
204 $rv = array( $prot . rtrim( $query, " \t*" ), $dbr->anyString() );
205 $field = 'el_to';
206 }
207 }
208
209 return array( $rv, $field );
210 }
211
212 function linkParameters() {
213 $params = array();
214 $params['target'] = $this->mProt . $this->mQuery;
215 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
216 $params['namespace'] = $this->mNs;
217 }
218
219 return $params;
220 }
221
222 function getQueryInfo() {
223 $dbr = wfGetDB( DB_SLAVE );
224 // strip everything past first wildcard, so that
225 // index-based-only lookup would be done
226 list( $this->mungedQuery, $clause ) = self::mungeQuery( $this->mQuery, $this->mProt );
227 if ( $this->mungedQuery === false ) {
228 // Invalid query; return no results
229 return array( 'tables' => 'page', 'fields' => 'page_id', 'conds' => '0=1' );
230 }
231
232 $stripped = LinkFilter::keepOneWildcard( $this->mungedQuery );
233 $like = $dbr->buildLike( $stripped );
234 $retval = array(
235 'tables' => array( 'page', 'externallinks' ),
236 'fields' => array(
237 'namespace' => 'page_namespace',
238 'title' => 'page_title',
239 'value' => 'el_index',
240 'url' => 'el_to'
241 ),
242 'conds' => array(
243 'page_id = el_from',
244 "$clause $like"
245 ),
246 'options' => array( 'USE INDEX' => $clause )
247 );
248
249 if ( $this->mNs !== null && !$this->getConfig()->get( 'MiserMode' ) ) {
250 $retval['conds']['page_namespace'] = $this->mNs;
251 }
252
253 return $retval;
254 }
255
256 /**
257 * Pre-fill the link cache
258 *
259 * @param IDatabase $db
260 * @param ResultWrapper $res
261 */
262 function preprocessResults( $db, $res ) {
263 if ( $res->numRows() > 0 ) {
264 $linkBatch = new LinkBatch();
265
266 foreach ( $res as $row ) {
267 $linkBatch->add( $row->namespace, $row->title );
268 }
269
270 $res->seek( 0 );
271 $linkBatch->execute();
272 }
273 }
274
275 /**
276 * @param Skin $skin
277 * @param object $result Result row
278 * @return string
279 */
280 function formatResult( $skin, $result ) {
281 $title = new TitleValue( (int)$result->namespace, $result->title );
282 $pageLink = $this->linkRenderer->renderHtmlLink( $title );
283
284 $url = $result->url;
285 $urlLink = Linker::makeExternalLink( $url, $url );
286
287 return $this->msg( 'linksearch-line' )->rawParams( $urlLink, $pageLink )->escaped();
288 }
289
290 /**
291 * Override to squash the ORDER BY.
292 * We do a truncated index search, so the optimizer won't trust
293 * it as good enough for optimizing sort. The implicit ordering
294 * from the scan will usually do well enough for our needs.
295 * @return array
296 */
297 function getOrderFields() {
298 return array();
299 }
300
301 protected function getGroupName() {
302 return 'redirects';
303 }
304 }