r110185 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110184‎ | r110185 | r110186 >
Date:23:18, 27 January 2012
Author:yaron
Status:deferred
Tags:
Comment:
Added additional handling of $sfgAutocompleteOnAllChars, and streamlined some SQL querying for autocompletion, based on patch from Ilmars Poikans
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
@@ -163,10 +163,7 @@
164164 }
165165
166166 if ( !is_null( $substring ) ) {
167 - $substring = str_replace( "'", "\'", strtolower( $substring ) );
168 - // UTF-8 conversion is needed in case MediaWiki is using
169 - // binary data storage.
170 - $conditions[] = "REPLACE(LOWER(CONVERT($value_field USING utf8)),'_',' ') LIKE '" . $substring . "%' OR REPLACE(LOWER(CONVERT($value_field USING utf8)),'_',' ') LIKE '% " . $substring . "%'";
 167+ $conditions[] = SFUtils::getSQLConditionForAutocompleteInColumn( $value_field, $substring );
171168 }
172169
173170 $sql_options['ORDER BY'] = $value_field;
Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -462,18 +462,16 @@
463463 for ( $level = $num_levels; $level > 0; $level-- ) {
464464 $newcategories = array();
465465 foreach ( $checkcategories as $category ) {
 466+ $conditions = array();
 467+ $conditions[] = 'cl_from = page_id';
 468+ $conditions['cl_to'] = $category;
466469 if ( $substring != null ) {
467 - $substring = str_replace( ' ', '_', strtolower( $substring ) );
468 - $substring = str_replace( '_', '\_', $substring );
469 - $substring = str_replace( "'", "\'", $substring );
470 - $conditions = 'cl_to = ' . $db->addQuotes( $category ) . " AND (LOWER(CONVERT(`page_title` USING utf8)) LIKE '" . $substring . "%' OR LOWER(CONVERT(`page_title` USING utf8)) LIKE '%\_" . $substring . "%' OR page_namespace = " . NS_CATEGORY . ")";
471 - } else {
472 - $conditions = 'cl_to = ' . $db->addQuotes( $category );
 470+ $conditions[] = self::getSQLConditionForAutocompleteInColumn( 'page_title', $substring ) . ' OR page_namespace = ' . NS_CATEGORY;
473471 }
474472 $res = $db->select( // make the query
475473 array( 'categorylinks', 'page' ),
476474 array( 'page_title', 'page_namespace' ),
477 - array( 'cl_from = page_id', $conditions ),
 475+ $conditions,
478476 __METHOD__,
479477 'SORT BY cl_sortkey' );
480478 if ( $res ) {
@@ -521,7 +519,7 @@
522520 }
523521
524522 public static function getAllPagesForConcept( $concept_name, $substring = null ) {
525 - global $sfgMaxAutocompleteValues;
 523+ global $sfgMaxAutocompleteValues, $sfgAutocompleteOnAllChars;
526524
527525 $store = smwfGetStore();
528526
@@ -557,10 +555,16 @@
558556 // original SMW query, but that doesn't seem
559557 // possible yet.
560558 $lowercasePageName = strtolower( $pageName );
561 - if ( strpos( $lowercasePageName, $substring ) === 0 ||
562 - strpos( $lowercasePageName, ' ' . $substring ) > 0 ) {
 559+ if ( $sfgAutocompleteOnAllChars ) {
 560+ if ( strpos( $lowercasePageName, $substring ) >= 0 ) {
563561 $pages[] = $pageName;
564562 }
 563+ } else {
 564+ if ( strpos( $lowercasePageName, $substring ) === 0 ||
 565+ strpos( $lowercasePageName, ' ' . $substring ) > 0 ) {
 566+ $pages[] = $pageName;
 567+ }
 568+ }
565569 }
566570 }
567571 sort( $pages );
@@ -820,15 +824,39 @@
821825 }
822826
823827 /**
 828+ * Returns a SQL condition for autocompletion substring value in a column.
 829+ * @param string $value_column Value column name
 830+ * @param string $substring Substring to look for
 831+ * @return SQL condition for use in WHERE clause
 832+ *
 833+ * @author Ilmars Poikans
 834+ */
 835+ public static function getSQLConditionForAutocompleteInColumn( $column, $substring ) {
 836+ global $sfgAutocompleteOnAllChars;
 837+
 838+ $column_value = "LOWER(CONVERT($column USING utf8))";
 839+ $substring = str_replace( ' ', '_', strtolower( $substring ) );
 840+ $substring = str_replace( "'", "\'", $substring );
 841+ $substring = str_replace( '_', '\_', $substring );
 842+ $substring = str_replace( '%', '\%', $substring );
 843+
 844+ if ( $sfgAutocompleteOnAllChars ) {
 845+ return "$column_value LIKE '%$substring%'";
 846+ } else {
 847+ return "$column_value LIKE '$substring%' OR $column_value LIKE '%\_$substring%'";
 848+ }
 849+ }
 850+
 851+ /**
824852 * Appends a preview of the actual form, when a page in the "Form"
825853 * namespace is previewed.
826854 *
827855 * @author Solitarius
828856 * @since 2.4
829 - *
 857+ *
830858 * @param EditPage $editpage
831859 * @param WebRequest $request
832 - *
 860+ *
833861 * @return true
834862 */
835863 public static function showFormPreview( EditPage $editpage, WebRequest $request ) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r110199Follow-up to r110185 - missed one section of the patchyaron00:45, 28 January 2012