Index: trunk/extensions/LinkSuggest/LinkSuggest.php |
— | — | @@ -6,10 +6,11 @@ |
7 | 7 | * |
8 | 8 | * @file |
9 | 9 | * @ingroup Extensions |
10 | | - * @version 1.6 (r32133) |
| 10 | + * @version 1.7 (r32133) |
11 | 11 | * @author Inez Korczyński <korczynski at gmail dot com> |
12 | 12 | * @author Bartek Łapiński <bartek at wikia-inc dot com> |
13 | 13 | * @author Łukasz Garczewski (TOR) <tor at wikia-inc dot com> |
| 14 | + * @author Maciej Brencz <macbre@wikia-inc.com> |
14 | 15 | * @author Jesús Martínez Novo <martineznovo at gmail dot com> |
15 | 16 | * @author Jack Phoenix <jack@countervandalism.net> |
16 | 17 | * @copyright Copyright © 2008-2009, Wikia Inc. |
— | — | @@ -25,9 +26,9 @@ |
26 | 27 | $wgExtensionCredits['other'][] = array( |
27 | 28 | 'path' => __FILE__, |
28 | 29 | 'name' => 'LinkSuggest', |
29 | | - 'version' => '1.6', |
| 30 | + 'version' => '1.7', |
30 | 31 | 'author' => array( |
31 | | - 'Inez Korczyński', 'Bartek Łapiński', 'Łukasz Garczewski', |
| 32 | + 'Inez Korczyński', 'Bartek Łapiński', 'Łukasz Garczewski', 'Maciej Brencz', |
32 | 33 | 'Jesús Martínez Novo', 'Jack Phoenix' |
33 | 34 | ), |
34 | 35 | 'descriptionmsg' => 'linksuggest-desc', |
— | — | @@ -126,7 +127,7 @@ |
127 | 128 | * @return $ar Array of link suggestions |
128 | 129 | */ |
129 | 130 | function getLinkSuggest() { |
130 | | - global $wgRequest, $wgContLang; |
| 131 | + global $wgRequest, $wgContLang, $wgContentNamespaces; |
131 | 132 | |
132 | 133 | // trim passed query and replace spaces by underscores |
133 | 134 | // - this is how MediaWiki stores article titles in database |
— | — | @@ -157,47 +158,46 @@ |
158 | 159 | } |
159 | 160 | } |
160 | 161 | |
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; |
166 | 169 | } |
167 | 170 | |
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(); |
172 | 172 | |
173 | 173 | $dbr = wfGetDB( DB_SLAVE ); |
174 | 174 | $query = $dbr->strencode( mb_strtolower( $query ) ); |
175 | 175 | |
176 | 176 | $res = $dbr->select( |
177 | 177 | array( 'querycache', 'page' ), |
178 | | - 'qc_title', |
| 178 | + array( 'qc_namespace', 'qc_title' ), |
179 | 179 | array( |
180 | 180 | 'qc_title = page_title', |
181 | 181 | 'qc_namespace = page_namespace', |
182 | 182 | 'page_is_redirect = 0', |
183 | 183 | '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})" |
186 | 186 | ), |
187 | 187 | __METHOD__, |
188 | 188 | array( 'ORDER BY' => 'qc_value DESC', 'LIMIT' => 10 ) |
189 | 189 | ); |
190 | 190 | |
191 | 191 | foreach( $res as $row ) { |
192 | | - $results[] = str_replace( '_', ' ', $namespacePrefix . $row->qc_title ); |
| 192 | + $results[] = wfLinkSuggestFormatTitle( $row->qc_namespace, $row->qc_title ); |
193 | 193 | } |
194 | 194 | |
195 | 195 | $res = $dbr->select( |
196 | 196 | 'page', |
197 | | - 'page_title', |
| 197 | + array( 'page_namespace', 'page_title' ), |
198 | 198 | array( |
199 | 199 | "LOWER(page_title) LIKE '{$query}%'", |
200 | 200 | 'page_is_redirect' => 0, |
201 | | - 'page_namespace' => $namespace |
| 201 | + "page_namespace IN ({$namespaces})" |
202 | 202 | ), |
203 | 203 | __METHOD__, |
204 | 204 | array( |
— | — | @@ -207,7 +207,7 @@ |
208 | 208 | ); |
209 | 209 | |
210 | 210 | foreach( $res as $row ) { |
211 | | - $results[] = str_replace( '_', ' ', $namespacePrefix . $row->page_title ); |
| 211 | + $results[] = wfLinkSuggestFormatTitle( $row->page_namespace, $row->page_title ); |
212 | 212 | } |
213 | 213 | |
214 | 214 | $results = array_unique( $results ); |
— | — | @@ -233,4 +233,19 @@ |
234 | 234 | } |
235 | 235 | |
236 | 236 | 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 ); |
237 | 252 | } |
\ No newline at end of file |