r105446 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105445‎ | r105446 | r105447 >
Date:17:59, 7 December 2011
Author:yaron
Status:ok (Comments)
Tags:
Comment:
Improved 'values from namespace' handling to always check correctly for the values 'Main' and 'main', and to use the English-language namespace names as a fallback for non-English languages
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_AutocompleteAPI.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_AutocompleteAPI.php
@@ -42,10 +42,6 @@
4343 } elseif ( !is_null( $concept ) ) {
4444 $data = SFUtils::getAllPagesForConcept( $concept, $substr );
4545 } elseif ( !is_null( $namespace ) ) {
46 - // Special handling for main (blank) namespace.
47 - if ( $namespace == 'main' ) {
48 - $namespace = '';
49 - }
5046 $data = SFUtils::getAllPagesForNamespace( $namespace, $substr );
5147 } elseif ( !is_null( $external_url ) ) {
5248 $data = SFUtils::getValuesFromExternalURL( $external_url, $substr );
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -584,32 +584,59 @@
585585 }
586586
587587 public static function getAllPagesForNamespace( $namespace_name, $substring = null ) {
588 - // cycle through all the namespace names for this language, and
589 - // if one matches the namespace specified in the form, add the
590 - // names of all the pages in that namespace to $names_array
591 - global $wgContLang;
 588+ global $wgContLang, $wgLanguageCode;
 589+
 590+ // Cycle through all the namespace names for this language, and
 591+ // if one matches the namespace specified in the form, get the
 592+ // names of all the pages in that namespace.
 593+
 594+ // Switch to blank for the string 'Main'.
 595+ if ( $namespace_name == 'Main' || $namespace_name == 'main' ) {
 596+ $namespace_name = '';
 597+ }
 598+ $matchingNamespaceCode = null;
592599 $namespaces = $wgContLang->getNamespaces();
593 - $db = wfGetDB( DB_SLAVE );
594 - $pages = array();
595 - foreach ( $namespaces as $ns_code => $ns_name ) {
596 - if ( $ns_name == $namespace_name ) {
597 - $conditions = "page_namespace = $ns_code";
598 - if ( $substring != null ) {
599 - $substring = str_replace( ' ', '_', strtolower( $substring ) );
600 - $substring = str_replace( '_', '\_', $substring );
601 - $substring = str_replace( "'", "\'", $substring );
602 - $conditions .= " AND (LOWER(CONVERT(`page_title` USING utf8)) LIKE '$substring%' OR LOWER(CONVERT(`page_title` USING utf8)) LIKE '%\_$substring%')";
 600+ foreach ( $namespaces as $curNSCode => $curNSName ) {
 601+ if ( $curNSName == $namespace_name ) {
 602+ $matchingNamespaceCode = $curNSCode;
 603+ }
 604+ }
 605+
 606+ // If that didn't find anything, and we're in a language
 607+ // other than English, check English as well.
 608+ if ( is_null( $matchingNamespaceCode ) && $wgLanguageCode != 'en' ) {
 609+ $englishLang = Language::factory( 'en' );
 610+ $namespaces = $englishLang->getNamespaces();
 611+ foreach ( $namespaces as $curNSCode => $curNSName ) {
 612+ if ( $curNSName == $namespace_name ) {
 613+ $matchingNamespaceCode = $curNSCode;
603614 }
604 - $res = $db->select( 'page',
605 - 'page_title',
606 - $conditions, __METHOD__,
607 - array( 'ORDER BY' => 'page_title' ) );
608 - while ( $row = $db->fetchRow( $res ) ) {
609 - $pages[] = str_replace( '_', ' ', $row[0] );
610 - }
611 - $db->freeResult( $res );
612615 }
613616 }
 617+
 618+ if ( is_null( $matchingNamespaceCode ) ) {
 619+ return array();
 620+ }
 621+
 622+ $db = wfGetDB( DB_SLAVE );
 623+ $conditions = "page_namespace = $matchingNamespaceCode";
 624+ if ( $substring != null ) {
 625+ $substring = str_replace( ' ', '_', strtolower( $substring ) );
 626+ $substring = str_replace( '_', '\_', $substring );
 627+ $substring = str_replace( "'", "\'", $substring );
 628+ $conditions .= " AND (LOWER(CONVERT(`page_title` USING utf8)) LIKE '$substring%' OR LOWER(CONVERT(`page_title` USING utf8)) LIKE '%\_$substring%')";
 629+ }
 630+ $res = $db->select( 'page',
 631+ 'page_title',
 632+ $conditions, __METHOD__,
 633+ array( 'ORDER BY' => 'page_title' ) );
 634+
 635+ $pages = array();
 636+ while ( $row = $db->fetchRow( $res ) ) {
 637+ $pages[] = str_replace( '_', ' ', $row[0] );
 638+ }
 639+ $db->freeResult( $res );
 640+
614641 return $pages;
615642 }
616643
@@ -633,9 +660,6 @@
634661 } elseif ( $source_type == 'concept' ) {
635662 $names_array = self::getAllPagesForConcept( $source_name );
636663 } else { // i.e., $source_type == 'namespace'
637 - // switch back to blank for main namespace
638 - if ( $source_name == "Main" )
639 - $source_name = "";
640664 $names_array = self::getAllPagesForNamespace( $source_name );
641665 }
642666 return $names_array;

Comments

#Comment by Nikerabbit (talk | contribs)   07:07, 8 December 2011

That code could use some cleanup, but not introduced here.

Status & tagging log