r93876 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r93875‎ | r93876 | r93877 >
Date:09:59, 4 August 2011
Author:macbre
Status:resolved (Comments)
Tags:
Comment:
LinkSuggest: search in content namespaces by default (not only within NS_MAIN)
Modified paths:
  • /trunk/extensions/LinkSuggest/LinkSuggest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LinkSuggest/LinkSuggest.php
@@ -6,10 +6,11 @@
77 *
88 * @file
99 * @ingroup Extensions
10 - * @version 1.6 (r32133)
 10+ * @version 1.7 (r32133)
1111 * @author Inez Korczyński <korczynski at gmail dot com>
1212 * @author Bartek Łapiński <bartek at wikia-inc dot com>
1313 * @author Łukasz Garczewski (TOR) <tor at wikia-inc dot com>
 14+ * @author Maciej Brencz <macbre@wikia-inc.com>
1415 * @author Jesús Martínez Novo <martineznovo at gmail dot com>
1516 * @author Jack Phoenix <jack@countervandalism.net>
1617 * @copyright Copyright © 2008-2009, Wikia Inc.
@@ -25,9 +26,9 @@
2627 $wgExtensionCredits['other'][] = array(
2728 'path' => __FILE__,
2829 'name' => 'LinkSuggest',
29 - 'version' => '1.6',
 30+ 'version' => '1.7',
3031 'author' => array(
31 - 'Inez Korczyński', 'Bartek Łapiński', 'Łukasz Garczewski',
 32+ 'Inez Korczyński', 'Bartek Łapiński', 'Łukasz Garczewski', 'Maciej Brencz',
3233 'Jesús Martínez Novo', 'Jack Phoenix'
3334 ),
3435 'descriptionmsg' => 'linksuggest-desc',
@@ -126,7 +127,7 @@
127128 * @return $ar Array of link suggestions
128129 */
129130 function getLinkSuggest() {
130 - global $wgRequest, $wgContLang;
 131+ global $wgRequest, $wgContLang, $wgContentNamespaces;
131132
132133 // trim passed query and replace spaces by underscores
133134 // - this is how MediaWiki stores article titles in database
@@ -157,47 +158,46 @@
158159 }
159160 }
160161
161 - $results = array();
162 -
163 - if( empty( $namespace ) ) {
164 - // default namespace to search in
165 - $namespace = NS_MAIN;
 162+ // comma separated list of namespaces to search in
 163+ if ( empty( $namespace ) ) {
 164+ // search only within content namespaces - default behaviour
 165+ $namespaces = implode( ',', $wgContentNamespaces );
 166+ } else {
 167+ // search only within a namespace from query
 168+ $namespaces = $namespace;
166169 }
167170
168 - // get localized namespace name
169 - $namespaceName = $wgContLang->getNsText( $namespace );
170 - // and prepare it for later use...
171 - $namespacePrefix = ( !empty( $namespaceName ) ) ? $namespaceName . ':' : '';
 171+ $results = array();
172172
173173 $dbr = wfGetDB( DB_SLAVE );
174174 $query = $dbr->strencode( mb_strtolower( $query ) );
175175
176176 $res = $dbr->select(
177177 array( 'querycache', 'page' ),
178 - 'qc_title',
 178+ array( 'qc_namespace', 'qc_title' ),
179179 array(
180180 'qc_title = page_title',
181181 'qc_namespace = page_namespace',
182182 'page_is_redirect = 0',
183183 'qc_type' => 'Mostlinked',
184 - "LOWER(qc_title) LIKE LOWER('{$query}%')",
185 - 'qc_namespace' => $namespace
 184+ "LOWER(qc_title) LIKE '{$query}%'",
 185+ "qc_namespace IN ({$namespaces})"
186186 ),
187187 __METHOD__,
188188 array( 'ORDER BY' => 'qc_value DESC', 'LIMIT' => 10 )
189189 );
190190
191191 foreach( $res as $row ) {
192 - $results[] = str_replace( '_', ' ', $namespacePrefix . $row->qc_title );
 192+ $results[] = wfLinkSuggestFormatTitle( $row->qc_namespace, $row->qc_title );
193193 }
194194
195195 $res = $dbr->select(
196196 'page',
197 - 'page_title',
 197+ array( 'page_namespace', 'page_title' ),
198198 array(
199199 "LOWER(page_title) LIKE '{$query}%'",
200200 'page_is_redirect' => 0,
201 - 'page_namespace' => $namespace
 201+ "page_namespace IN ({$namespaces})"
202202 ),
203203 __METHOD__,
204204 array(
@@ -207,7 +207,7 @@
208208 );
209209
210210 foreach( $res as $row ) {
211 - $results[] = str_replace( '_', ' ', $namespacePrefix . $row->page_title );
 211+ $results[] = wfLinkSuggestFormatTitle( $row->page_namespace, $row->page_title );
212212 }
213213
214214 $results = array_unique( $results );
@@ -233,4 +233,19 @@
234234 }
235235
236236 return $ar;
 237+}
 238+
 239+/**
 240+ * Returns formatted title based on given namespace and title
 241+ *
 242+ * @param $namespace integer page namespace ID
 243+ * @param $title string page title
 244+ * @return string formatted title (prefixed with namespace)
 245+ */
 246+function wfLinkSuggestFormatTitle( $namespace, $title ) {
 247+ if ( $namespace != NS_MAIN ) {
 248+ $title = MWNamespace::getCanonicalName( $namespace ) . ':' . $title;
 249+ }
 250+
 251+ return str_replace( '_', ' ', $title );
237252 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r93881LinkSuggest: improve r93876...macbre12:40, 4 August 2011

Comments

#Comment by Reedy (talk | contribs)   11:03, 4 August 2011
-			'page_namespace' => $namespace
+			"page_namespace IN ({$namespaces})"

You could've left that line as is, as the DB wrappers will use foo = 'bar' or foo in ( 'bar', 'baz' )... as appropriate

Same for the other one...

Status & tagging log