jenkins-bot has submitted this change and it was merged.

Change subject: Add text search to Special:Drilldown
......................................................................


Add text search to Special:Drilldown

Change-Id: I39240980a36c631f0269d1caaad65a67ce57fd63
---
M drilldown/CargoFilter.php
M drilldown/CargoSpecialDrilldown.php
M i18n/en.json
M i18n/qqq.json
4 files changed, 123 insertions(+), 28 deletions(-)

Approvals:
  Yaron Koren: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/drilldown/CargoFilter.php b/drilldown/CargoFilter.php
index c77ff11..638cc03 100644
--- a/drilldown/CargoFilter.php
+++ b/drilldown/CargoFilter.php
@@ -36,7 +36,7 @@
         * @param array $appliedFilters
         * @return string
         */
-       function getTimePeriod( $appliedFilters ) {
+       function getTimePeriod( $fullTextSearchTerm, $appliedFilters ) {
                // If it's not a date field, return null.
                if ( $this->fieldDescription->mType != 'Date' ) {
                        return null;
@@ -44,7 +44,7 @@
 
                $cdb = CargoUtils::getDB();
                $date_field = $this->name;
-               list( $tableNames, $conds, $joinConds ) = $this->getQueryParts( 
$appliedFilters );
+               list( $tableNames, $conds, $joinConds ) = $this->getQueryParts( 
$fullTextSearchTerm, $appliedFilters );
                $res = $cdb->select( $tableNames, array( "MIN($date_field)", 
"MAX($date_field)" ), $conds, null,
                        null, $joinConds );
                $row = $cdb->fetchRow( $res );
@@ -85,12 +85,21 @@
         * @param array $appliedFilters
         * @return array
         */
-       function getQueryParts( $appliedFilters ) {
+       function getQueryParts( $fullTextSearchTerm, $appliedFilters ) {
                $cdb = CargoUtils::getDB();
 
                $tableNames = array( $this->tableName );
                $conds = array();
                $joinConds = array();
+          
+               if ( $fullTextSearchTerm != null ) {
+                       list( $curTableNames, $curConds, $curJoinConds ) =
+                               
CargoDrilldownPage::getFullTextSearchQueryParts( $fullTextSearchTerm, 
$this->tableName, $this->searchableFiles );
+                       $tableNames = array_merge( $tableNames, $curTableNames 
);
+                       $conds = array_merge( $conds, $curConds );
+                       $joinConds = array_merge( $joinConds, $curJoinConds );
+               }
+
                foreach ( $appliedFilters as $af ) {
                        $conds[] = $af->checkSQL();
                        if ( $af->filter->fieldDescription->mIsList ) {
@@ -110,7 +119,7 @@
         * @param array $appliedFilters
         * @return array
         */
-       function getTimePeriodValues( $appliedFilters ) {
+       function getTimePeriodValues( $fullTextSearchTerm, $appliedFilters ) {
                $possible_dates = array();
                $date_field = $this->name;
                $timePeriod = $this->getTimePeriod( $fullTextSearchTerm, 
$appliedFilters );
@@ -125,7 +134,7 @@
                        $fields = "YEAR($date_field)";
                }
 
-               list( $tableNames, $conds, $joinConds ) = $this->getQueryParts( 
$appliedFilters );
+               list( $tableNames, $conds, $joinConds ) = $this->getQueryParts( 
$fullTextSearchTerm, $appliedFilters );
                $selectOptions = array( 'GROUP BY' => $fields, 'ORDER BY' => 
$fields );
                $cdb = CargoUtils::getDB();
                $res = $cdb->select( $tableNames, array( $fields, 'COUNT(*)' ), 
$conds, null, $selectOptions,
@@ -169,10 +178,10 @@
         * @param array $appliedFilters
         * @return array
         */
-       function getAllValues( $appliedFilters ) {
+       function getAllValues( $fullTextSearchTerm, $appliedFilters ) {
                $cdb = CargoUtils::getDB();
 
-               list( $tableNames, $conds, $joinConds ) = $this->getQueryParts( 
$appliedFilters );
+               list( $tableNames, $conds, $joinConds ) = $this->getQueryParts( 
$fullTextSearchTerm, $appliedFilters );
                if ( $this->fieldDescription->mIsList ) {
                        $fieldTableName = $this->tableName . '__' . $this->name;
                        $tableNames[] = $fieldTableName;
diff --git a/drilldown/CargoSpecialDrilldown.php 
b/drilldown/CargoSpecialDrilldown.php
index f5c7243..0c51958 100644
--- a/drilldown/CargoSpecialDrilldown.php
+++ b/drilldown/CargoSpecialDrilldown.php
@@ -18,7 +18,7 @@
        }
 
        function execute( $query ) {
-               global $cgScriptPath;
+               global $cgScriptPath, $wgCargoPageDataColumns;
 
                $request = $this->getRequest();
                $out = $this->getOutput();
@@ -63,6 +63,15 @@
 
                $tableSchemas = CargoUtils::getTableSchemas( array( $tableName 
) );
                $all_filters = array();
+               $fullTextSearchTerm = null;
+
+               if ( in_array( 'fullText', $wgCargoPageDataColumns ) ) {
+                       $vals_array = $request->getArray( '_search' );
+                       if ( $vals_array != null ) {
+                               $fullTextSearchTerm = $vals_array[0];
+                       }
+               }
+
                foreach ( $tableSchemas[$tableName]->mFieldDescriptions as 
$fieldName => $fieldDescription ) {
                        // Skip "hidden" fields.
                        if ( $fieldDescription->mIsHidden ) {
@@ -126,7 +135,7 @@
 
                $out->addHTML( "\n\t\t\t\t<div class=\"drilldown-results\">\n" 
);
                $rep = new CargoDrilldownPage(
-                       $tableName, $all_filters, $applied_filters, 
$remaining_filters, $offset, $limit );
+                       $tableName, $all_filters, $applied_filters, 
$remaining_filters, $fullTextSearchTerm, $offset, $limit );
                $num = $rep->execute( $query );
                $out->addHTML( "\n\t\t\t</div> <!-- drilldown-results -->\n" );
 
@@ -147,6 +156,7 @@
        public $all_filters = array();
        public $applied_filters = array();
        public $remaining_filters = array();
+       public $fullTextSearchTerm;
        public $showSingleTable = false;
 
        /**
@@ -155,16 +165,18 @@
         * @param string $tableName
         * @param array $applied_filters
         * @param array $remaining_filters
+        * @param string $fullTextSearchTerm;
         * @param int $offset
         * @param int $limit
         */
-       function __construct( $tableName, $all_filters, $applied_filters, 
$remaining_filters, $offset, $limit ) {
+       function __construct( $tableName, $all_filters, $applied_filters, 
$remaining_filters, $fullTextSearchTerm, $offset, $limit ) {
                parent::__construct( 'Drilldown' );
 
                $this->tableName = $tableName;
                $this->all_filters = $all_filters;
                $this->applied_filters = $applied_filters;
                $this->remaining_filters = $remaining_filters;
+               $this->fullTextSearchTerm = $fullTextSearchTerm;
                $this->offset = $offset;
                $this->limit = $limit;
        }
@@ -176,13 +188,19 @@
         * @param string $filter_to_remove
         * @return string
         */
-       function makeBrowseURL( $tableName, $applied_filters = array(), 
$filter_to_remove = null ) {
+       function makeBrowseURL( $tableName, $searchTerm = null, 
$applied_filters = array(), $filter_to_remove = null ) {
                $dd = SpecialPage::getTitleFor( 'Drilldown' );
                $url = $dd->getLocalURL() . '/' . $tableName;
                if ( $this->showSingleTable ) {
                        $url .= ( strpos( $url, '?' ) ) ? '&' : '?';
                        $url .= "_single";
                }
+
+               if ( $searchTerm != null ) {
+                       $url .= ( strpos( $url, '?' ) ) ? '&' : '?';
+                       $url .= '_search=' . urlencode( str_replace( ' ', '_', 
$searchTerm ) );
+               }
+
                foreach ( $applied_filters as $af ) {
                        if ( $af->filter->name == $filter_to_remove ) {
                                continue;
@@ -412,7 +430,7 @@
                        if ( $found_match ) {
                                $results_line .= "\n\t\t\t\t$filter_text";
                        } else {
-                               $filter_url = $this->makeBrowseURL( 
$this->tableName, $applied_filters );
+                               $filter_url = $this->makeBrowseURL( 
$this->tableName, $this->fullTextSearchTerm, $applied_filters );
                                $results_line .= "\n\t\t\t\t\t\t" . 
Html::rawElement( 'a',
                                                array( 'href' => $filter_url,
                                                'title' => $this->msg( 
'cargo-drilldown-filterbyvalue' )->text() ), $filter_text );
@@ -644,7 +662,7 @@
                // We generate $cur_url here, instead of passing it in, because
                // if there's a previous value for this filter it may be
                // removed.
-               $cur_url = $this->makeBrowseURL( $this->tableName, 
$this->applied_filters, $filter_name );
+               $cur_url = $this->makeBrowseURL( $this->tableName, 
$this->fullTextSearchTerm, $this->applied_filters, $filter_name );
                $cur_url .= ( strpos( $cur_url, '?' ) ) ? '&' : '?';
 
                $numberArray = array();
@@ -709,7 +727,11 @@
                // of values.
                $filter_name .= '[' . $instance_num . ']';
 
-               $inputName = "_search_$filter_name";
+               if ( strpos( $filter_name, '_search' ) === 0 ) {
+                       $inputName = '_search';
+               } else {
+                       $inputName = "_search_$filter_name";
+               }
 
                $text = <<< END
 <form method="get">
@@ -844,9 +866,9 @@
 
                $fieldType = $f->fieldDescription->mType;
                if ( $fieldType == 'Date' ) {
-                       $filter_values = $f->getTimePeriodValues( 
$this->applied_filters );
+                       $filter_values = $f->getTimePeriodValues( 
$this->fullTextSearchTerm, $this->applied_filters );
                } else {
-                       $filter_values = $f->getAllValues( 
$this->applied_filters );
+                       $filter_values = $f->getAllValues( 
$this->fullTextSearchTerm, $this->applied_filters );
                }
                if ( !is_array( $filter_values ) ) {
                        return $this->printFilterLine( $f->name, false, false, 
$filter_values );
@@ -869,7 +891,7 @@
                        $normal_filter = false;
                } else {
                        // If $cur_url wasn't passed in, we have to create it.
-                       $cur_url = $this->makeBrowseURL( $this->tableName, 
$this->applied_filters, $f->name );
+                       $cur_url = $this->makeBrowseURL( $this->tableName, 
$this->fullTextSearchTerm, $this->applied_filters, $f->name );
                        $cur_url .= ( strpos( $cur_url, '?' ) ) ? '&' : '?';
                        $results_line = $this->printUnappliedFilterValues( 
$cur_url, $f, $filter_values );
                }
@@ -879,7 +901,7 @@
        }
 
        function getPageHeader() {
-               global $wgRequest;
+               global $wgRequest, $wgCargoPageDataColumns;
                global $cgScriptPath;
 
                $tables = CargoUtils::getTables();
@@ -895,14 +917,14 @@
                }
                // If there are no fields for this table,
                // escape now that we've (possibly) printed the
-               // tables list
-               if ( ( count( $this->applied_filters ) == 0 ) &&
-                       ( count( $this->remaining_filters ) == 0 ) ) {
+               // tables list.
+               if ( count( $this->all_filters ) == 0 &&
+                       !array_key_exists( 'fullText', $wgCargoPageDataColumns 
) ) {
                        return $header;
                }
 
                $appliedFiltersHTML = '                         <div 
id="drilldown-header">' . "\n";
-               if ( count( $this->applied_filters ) > 0 ) {
+               if ( count( $this->applied_filters ) > 0 || 
$this->fullTextSearchTerm != null ) {
                        $tableURL = $this->makeBrowseURL( $this->tableName );
                        $appliedFiltersHTML .= '<a href="' . $tableURL . '" 
title="' .
                                $this->msg( 'cargo-drilldown-resetfilters' 
)->text() . '">' .
@@ -910,8 +932,21 @@
                } else {
                        $appliedFiltersHTML .= str_replace( '_', ' ', 
$this->tableName );
                }
+          
+               if ( $this->fullTextSearchTerm != null ) {
+                       $appliedFiltersHTML .= " > ";
+                       $appliedFiltersHTML .= $this->msg( 
'cargo-drilldown-fulltext' )->text() . ': ';
+
+                       $remove_filter_url = $this->makeBrowseURL( 
$this->tableName, null, $this->applied_filters );
+                       $appliedFiltersHTML .= "\n\t" . '<span 
class="drilldown-header-value">~ \'' .
+                               $this->fullTextSearchTerm .
+                               '\'</span> <a href="' . $remove_filter_url . '" 
title="' .
+                               $this->msg( 'cargo-drilldown-removefilter' 
)->text() . '"><img src="' .
+                               $cgScriptPath . 
'/drilldown/resources/filter-x.png" /></a> ';
+               }
+
                foreach ( $this->applied_filters as $i => $af ) {
-                       $appliedFiltersHTML .= ( $i == 0 ) ? " > " :
+                       $appliedFiltersHTML .= ( $i == 0 && 
$this->fullTextSearchTerm == null ) ? " > " :
                                "\n\t\t\t\t\t<span 
class=\"drilldown-header-value\">&</span> ";
                        $filter_label = str_replace( '_', ' ', 
$af->filter->name );
                        // Add an "x" to remove this filter, if it has more
@@ -919,7 +954,7 @@
                        if ( count( $this->applied_filters[$i]->values ) > 1 ) {
                                $temp_filters_array = $this->applied_filters;
                                array_splice( $temp_filters_array, $i, 1 );
-                               $remove_filter_url = $this->makeBrowseURL( 
$this->tableName, $temp_filters_array );
+                               $remove_filter_url = $this->makeBrowseURL( 
$this->tableName, $this->fullTextSearchTerm, $temp_filters_array );
                                array_splice( $temp_filters_array, $i, 0 );
                                $appliedFiltersHTML .= $filter_label . ' <a 
href="' . $remove_filter_url . '" title="' .
                                        $this->msg( 
'cargo-drilldown-removefilter' )->text() .
@@ -935,7 +970,7 @@
                                $filter_text = $this->printFilterValue( 
$af->filter, $fv->text );
                                $temp_filters_array = $this->applied_filters;
                                $removed_values = array_splice( 
$temp_filters_array[$i]->values, $j, 1 );
-                               $remove_filter_url = $this->makeBrowseURL( 
$this->tableName, $temp_filters_array );
+                               $remove_filter_url = $this->makeBrowseURL( 
$this->tableName, $this->fullTextSearchTerm, $temp_filters_array );
                                array_splice( $temp_filters_array[$i]->values, 
$j, 0, $removed_values );
                                $appliedFiltersHTML .= "\n      " . '           
                <span class="drilldown-header-value">' .
                                        $filter_text . '</span> <a href="' . 
$remove_filter_url . '" title="' .
@@ -951,7 +986,7 @@
                                        }
                                        $temp_filters_array = 
$this->applied_filters;
                                        $removed_values = array_splice( 
$temp_filters_array[$i]->search_terms, $j, 1 );
-                                       $remove_filter_url = 
$this->makeBrowseURL( $this->tableName, $temp_filters_array );
+                                       $remove_filter_url = 
$this->makeBrowseURL( $this->tableName, $this->fullTextSearchTerm, 
$temp_filters_array );
                                        array_splice( 
$temp_filters_array[$i]->search_terms, $j, 0, $removed_values );
                                        $appliedFiltersHTML .= "\n\t" . '<span 
class="drilldown-header-value">~ \'' . $search_term .
                                                '\'</span> <a href="' . 
$remove_filter_url . '" title="' .
@@ -973,7 +1008,7 @@
                // contain the possible values, and, in parentheses, the
                // number of pages that match that value.
                $filtersHTML = "                                <div 
class=\"drilldown-filters\">\n";
-               $cur_url = $this->makeBrowseURL( $this->tableName, 
$this->applied_filters );
+               $cur_url = $this->makeBrowseURL( $this->tableName, 
$this->fullTextSearchTerm, $this->applied_filters );
                $cur_url .= ( strpos( $cur_url, '?' ) ) ? '&' : '?';
 
                foreach ( $this->all_filters as $f ) {
@@ -1044,6 +1079,15 @@
                $tableNames = array( $this->tableName );
                $conds = array();
                $joinConds = array();
+
+               if ( $this->fullTextSearchTerm != null ) {
+                       list( $curTableNames, $curConds, $curJoinConds ) =
+                               self::getFullTextSearchQueryParts( 
$this->fullTextSearchTerm, $this->tableName, $this->searchableFiles );
+                       $tableNames = array_merge( $tableNames, $curTableNames 
);
+                       $conds = array_merge( $conds, $curConds );
+                       $joinConds = array_merge( $joinConds, $curJoinConds );
+               }
+
                foreach ( $this->applied_filters as $i => $af ) {
                        list( $curTableNames, $curConds, $curJoinConds ) = 
$af->getQueryParts( $this->tableName );
                        $tableNames = array_merge( $tableNames, $curTableNames 
);
@@ -1067,6 +1111,25 @@
                );
 
                return $queryInfo;
+       }
+
+       static function getFullTextSearchQueryParts( $searchTerm, 
$mainTableName ) {
+               $cdb = CargoUtils::getDB();
+
+               $tableNames = array();
+               $conds = array();
+               $joinConds = array();
+
+               $tableNames[] = '_pageData';
+               $joinConds['_pageData'] = array(
+                       'LEFT OUTER JOIN',
+                       CargoUtils::escapedFieldName( $cdb, $mainTableName, 
'_pageID' ) .
+                       ' = ' .
+                       CargoUtils::escapedFieldName( $cdb, '_pageData', 
'_pageID' )
+               );
+               $conds[] = CargoUtils::fullTextMatchSQL( $cdb, '_pageData', 
'_fullText', $searchTerm );
+
+               return array( $tableNames, $conds, $joinConds );
        }
 
        function getOrderFields() {
@@ -1096,13 +1159,32 @@
        protected function outputResults( $out, $skin, $dbr, $res, $num, 
$offset ) {
                $valuesTable = array();
                $cdb = CargoUtils::getDB();
+               $pageTextStr = $this->msg( 'cargo-drilldown-pagetext' )->text();
+
                while ( $row = $cdb->fetchRow( $res ) ) {
-                       $valuesTable[] = array( 'title' => $row['title'] );
+                       $curValue = array( 'title' => $row['title'] );
+                       if ( array_key_exists( 'pageText', $row ) ) {
+                               $curValue[$pageTextStr] = $row['pageText'];
+                       }
+                       $valuesTable[] = $curValue;
                }
                $queryDisplayer = new CargoQueryDisplayer();
                $fieldDescription = new CargoFieldDescription();
                $fieldDescription->mType = 'Page';
                $queryDisplayer->mFieldDescriptions = array( 'title' => 
$fieldDescription );
+          
+               if ( $this->fullTextSearchTerm != null ) {
+                       $searchTerms = CargoUtils::smartSplit( ' ', 
$this->fullTextSearchTerm );
+                       $dummySQLQuery = new CargoSQLQuery();
+                       $dummySQLQuery->mSearchTerms = array(
+                               $pageTextStr => $searchTerms
+                       );
+                       $queryDisplayer->mSQLQuery = $dummySQLQuery;
+                       $fullTextFieldDescription = new CargoFieldDescription();
+                       $fullTextFieldDescription->mType = 'Searchtext';
+                       $queryDisplayer->mFieldDescriptions[$pageTextStr] = 
$fullTextFieldDescription;
+               }
+
                $queryDisplayer->mFormat = 'category';
                $formatter = $queryDisplayer->getFormatter( $out );
                $html = $queryDisplayer->displayQueryResults( $formatter, 
$valuesTable );
diff --git a/i18n/en.json b/i18n/en.json
index 807c321..9b1f16d 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -64,6 +64,8 @@
        "cargo-drilldown-resetfilters": "Reset filters",
        "cargo-drilldown-addanothervalue": "Click arrow to add another value",
        "cargo-drilldown-novalues": "There are no values for this filter",
+       "cargo-drilldown-fulltext": "Full text",
+       "cargo-drilldown-pagetext": "Page text",
        "cargo-viewcsv": "View CSV",
        "cargo-viewxls": "View XLS",
        "cargo-viewjson": "View JSON",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index d1d16c6..5436547 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -69,6 +69,8 @@
        "cargo-drilldown-resetfilters": "This is the content of a tooltip on 
[[Special:Drilldown]] when you hover your mouse over the name of a table.",
        "cargo-drilldown-addanothervalue": "This is an informational message 
providing help for the usage of [[Special:Drilldown]].",
        "cargo-drilldown-novalues": "This is an informational message on 
[[Special:Drildown]].",
+       "cargo-drilldown-fulltext": "The label for searching on all the text 
within a page or file.",
+       "cargo-drilldown-pagetext": "The label for searching on all the text 
within a page.",
        "cargo-viewcsv": "The text of a link.",
        "cargo-viewxls": "The text of a link.",
        "cargo-viewjson": "The text of a link.",

-- 
To view, visit https://gerrit.wikimedia.org/r/316407
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I39240980a36c631f0269d1caaad65a67ce57fd63
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Cargo
Gerrit-Branch: master
Gerrit-Owner: Yaron Koren <yaro...@gmail.com>
Gerrit-Reviewer: Yaron Koren <yaro...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to