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

Change subject: Add searching of file data to Special:Drilldown
......................................................................


Add searching of file data to Special:Drilldown

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

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



diff --git a/drilldown/CargoFilter.php b/drilldown/CargoFilter.php
index 638cc03..9d00922 100644
--- a/drilldown/CargoFilter.php
+++ b/drilldown/CargoFilter.php
@@ -13,14 +13,16 @@
        public $tableName;
        public $fieldType;
        public $fieldDescription;
+       public $searchableFiles;
        public $allowed_values;
        public $required_filters = array();
        public $possible_applied_filters = array();
 
-       function __construct( $name, $tableName, $fieldDescription ) {
+       function __construct( $name, $tableName, $fieldDescription, 
$searchableFiles ) {
                $this->name = $name;
                $this->tableName = $tableName;
                $this->fieldDescription = $fieldDescription;
+               $this->searchableFiles = $searchableFiles;
        }
 
        public function addRequiredFilter( $filterName ) {
@@ -136,12 +138,17 @@
 
                list( $tableNames, $conds, $joinConds ) = $this->getQueryParts( 
$fullTextSearchTerm, $appliedFilters );
                $selectOptions = array( 'GROUP BY' => $fields, 'ORDER BY' => 
$fields );
+               if ( $this->searchableFiles ) {
+                       $countClause = "COUNT(DISTINCT 
cargo__{$this->tableName}._pageID)";
+               } else {
+                       $countClause = "COUNT(*)";
+               }
                $cdb = CargoUtils::getDB();
-               $res = $cdb->select( $tableNames, array( $fields, 'COUNT(*)' ), 
$conds, null, $selectOptions,
+               $res = $cdb->select( $tableNames, array( $fields, $countClause 
), $conds, null, $selectOptions,
                        $joinConds );
                while ( $row = $cdb->fetchRow( $res ) ) {
                        if ( $row[0] == null ) {
-                               $possible_dates['_none'] = $row['COUNT(*)'];
+                               $possible_dates['_none'] = $row[$countClause];
                        } elseif ( $timePeriod == 'day' ) {
                                $date_string = 
CargoDrilldownUtils::monthToString( $row[1] ) . ' ' . $row[2] . ', ' . $row[0];
                                $possible_dates[$date_string] = $row[3];
@@ -191,7 +198,13 @@
                        $fieldName = $this->name;
                }
 
-               $res = $cdb->select( $tableNames, array( $fieldName, 'COUNT(*)' 
), $conds, null,
+               if ( $this->searchableFiles ) {
+                       $countClause = "COUNT(DISTINCT 
cargo__{$this->tableName}._pageID)";
+               } else {
+                       $countClause = "COUNT(*)";
+               }
+
+               $res = $cdb->select( $tableNames, array( $fieldName, 
$countClause ), $conds, null,
                        array( 'GROUP BY' => $fieldName ), $joinConds );
                $possible_values = array();
                while ( $row = $cdb->fetchRow( $res ) ) {
diff --git a/drilldown/CargoSpecialDrilldown.php 
b/drilldown/CargoSpecialDrilldown.php
index 0c51958..673798f 100644
--- a/drilldown/CargoSpecialDrilldown.php
+++ b/drilldown/CargoSpecialDrilldown.php
@@ -19,6 +19,7 @@
 
        function execute( $query ) {
                global $cgScriptPath, $wgCargoPageDataColumns;
+               global $wgCargoFileDataColumns;
 
                $request = $this->getRequest();
                $out = $this->getOutput();
@@ -64,11 +65,18 @@
                $tableSchemas = CargoUtils::getTableSchemas( array( $tableName 
) );
                $all_filters = array();
                $fullTextSearchTerm = null;
+               $searchableFiles = false;
 
                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 ) {
+                       if ( !$fieldDescription->mIsHidden && 
$fieldDescription->mType == 'File' && in_array( 'fullText', 
$wgCargoFileDataColumns ) ) {
+                               $searchableFiles = true;
                        }
                }
 
@@ -83,7 +91,7 @@
                                continue;
                        }
 
-                       $all_filters[] = new CargoFilter( $fieldName, 
$tableName, $fieldDescription );
+                       $all_filters[] = new CargoFilter( $fieldName, 
$tableName, $fieldDescription, $searchableFiles );
                }
 
                $filter_used = array();
@@ -135,7 +143,7 @@
 
                $out->addHTML( "\n\t\t\t\t<div class=\"drilldown-results\">\n" 
);
                $rep = new CargoDrilldownPage(
-                       $tableName, $all_filters, $applied_filters, 
$remaining_filters, $fullTextSearchTerm, $offset, $limit );
+                       $tableName, $all_filters, $applied_filters, 
$remaining_filters, $fullTextSearchTerm, $searchableFiles, $offset, $limit );
                $num = $rep->execute( $query );
                $out->addHTML( "\n\t\t\t</div> <!-- drilldown-results -->\n" );
 
@@ -157,6 +165,7 @@
        public $applied_filters = array();
        public $remaining_filters = array();
        public $fullTextSearchTerm;
+       public $searchableFiles;
        public $showSingleTable = false;
 
        /**
@@ -166,10 +175,11 @@
         * @param array $applied_filters
         * @param array $remaining_filters
         * @param string $fullTextSearchTerm;
+        * @param boolean $searchableFiles
         * @param int $offset
         * @param int $limit
         */
-       function __construct( $tableName, $all_filters, $applied_filters, 
$remaining_filters, $fullTextSearchTerm, $offset, $limit ) {
+       function __construct( $tableName, $all_filters, $applied_filters, 
$remaining_filters, $fullTextSearchTerm, $searchableFiles, $offset, $limit ) {
                parent::__construct( 'Drilldown' );
 
                $this->tableName = $tableName;
@@ -177,6 +187,7 @@
                $this->applied_filters = $applied_filters;
                $this->remaining_filters = $remaining_filters;
                $this->fullTextSearchTerm = $fullTextSearchTerm;
+               $this->searchableFiles = $searchableFiles;
                $this->offset = $offset;
                $this->limit = $limit;
        }
@@ -1096,11 +1107,28 @@
                }
 
                $aliasedFieldNames = array(
-                       'title' => '_pageName',
-                       'value' => '_pageName',
-                       'namespace' => '_pageNamespace',
-                       'ID' => '_pageID'
+                       'title' => 'cargo__' . $this->tableName . '._pageName',
+                       'value' => 'cargo__' . $this->tableName . '._pageName',
+                       'namespace' => 'cargo__' . $this->tableName . 
'._pageNamespace',
+                       'ID' => 'cargo__' . $this->tableName . '._pageID'
                );
+
+               if ( $this->fullTextSearchTerm != null ) {
+                       $aliasedFieldNames['pageText'] = 
'cargo___pageData._fullText';
+                       if ( $this->searchableFiles ) {
+                               $aliasedFieldNames['fileName'] = 
'cargo___fileData._pageName';
+                               $aliasedFieldNames['fileText'] = 
'cargo___fileData._fullText';
+                               // @HACK -  the result set may contain both
+                               // pages and files that match the search term.
+                               // So how do we know, for each result row,
+                               // whether it's for a page or a file? We add
+                               // the "match on file" clause as a (boolean)
+                               // query field. There may be a more efficient
+                               // way to do this, using SQL variables or
+                               // something.
+                               $aliasedFieldNames['foundFileMatch'] = 
CargoUtils::fullTextMatchSQL( $cdb, '_fileData', '_fullText', 
$this->fullTextSearchTerm );
+                       }
+               }
 
                $queryInfo = array(
                        'tables' => $tableNames,
@@ -1113,7 +1141,7 @@
                return $queryInfo;
        }
 
-       static function getFullTextSearchQueryParts( $searchTerm, 
$mainTableName ) {
+       static function getFullTextSearchQueryParts( $searchTerm, 
$mainTableName, $searchableFiles ) {
                $cdb = CargoUtils::getDB();
 
                $tableNames = array();
@@ -1127,13 +1155,34 @@
                        ' = ' .
                        CargoUtils::escapedFieldName( $cdb, '_pageData', 
'_pageID' )
                );
-               $conds[] = CargoUtils::fullTextMatchSQL( $cdb, '_pageData', 
'_fullText', $searchTerm );
+          
+               if ( $searchableFiles ) {
+                       $fileTableName = $mainTableName . '___files';
+                       $tableNames[] = $fileTableName;
+                       $joinConds[$fileTableName] = array(
+                               'LEFT OUTER JOIN',
+                               CargoUtils::escapedFieldName( $cdb, 
$mainTableName, '_pageID' ) .
+                               ' = ' .
+                               CargoUtils::escapedFieldName( $cdb, 
$fileTableName, '_pageID' )
+                       );
+                       $tableNames[] = '_fileData';
+                       $joinConds['_fileData'] = array(
+                               'JOIN',
+                               CargoUtils::escapedFieldName( $cdb, 
$fileTableName, '_fileName' ) .
+                               ' = ' .
+                               CargoUtils::escapedFieldName( $cdb, 
'_fileData', '_pageTitle' )
+                       );
+                       $conds[] = CargoUtils::fullTextMatchSQL( $cdb, 
'_fileData', '_fullText', $searchTerm ) . ' OR ' .
+                               CargoUtils::fullTextMatchSQL( $cdb, 
'_pageData', '_fullText', $searchTerm );
+               } else {
+                       $conds[] = CargoUtils::fullTextMatchSQL( $cdb, 
'_pageData', '_fullText', $searchTerm );
+               }
 
                return array( $tableNames, $conds, $joinConds );
        }
 
        function getOrderFields() {
-               return array( '_pageName' );
+               return array( 'cargo__' . $this->tableName . '._pageName' );
        }
 
        function sortDescending() {
@@ -1160,29 +1209,51 @@
                $valuesTable = array();
                $cdb = CargoUtils::getDB();
                $pageTextStr = $this->msg( 'cargo-drilldown-pagetext' )->text();
+               $fileNameStr = $this->msg( 'cargo-drilldown-filename' )->text();
+               $fileTextStr = $this->msg( 'cargo-drilldown-filetext' )->text();
 
+               // @HACK - the current SQL query may return the same page as a
+               // result more than once. So keep an array of pages that have
+               // been returned so that we show each page only once.
+               $matchingPages = array();
                while ( $row = $cdb->fetchRow( $res ) ) {
-                       $curValue = array( 'title' => $row['title'] );
-                       if ( array_key_exists( 'pageText', $row ) ) {
-                               $curValue[$pageTextStr] = $row['pageText'];
+                       $pageName = $row['title'];
+                       $curValue = array( 'title' => $pageName );
+                       if ( array_key_exists( 'foundFileMatch', $row ) && 
$row['foundFileMatch'] ) {
+                               $curValue[$fileNameStr] = $row['fileName'];
+                               $curValue[$fileTextStr] = $row['fileText'];
+                               $valuesTable[] = $curValue;
+                       } elseif ( array_key_exists( 'pageText', $row ) ) {
+                               if ( !in_array( $pageName, $matchingPages ) ) {
+                                       $curValue[$pageTextStr] = 
$row['pageText'];
+                                       $valuesTable[] = $curValue;
+                                       $matchingPages[] = $pageName;
+                               }
                        }
-                       $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
+                               $pageTextStr => $searchTerms,
+                               $fileTextStr => $searchTerms
                        );
                        $queryDisplayer->mSQLQuery = $dummySQLQuery;
                        $fullTextFieldDescription = new CargoFieldDescription();
                        $fullTextFieldDescription->mType = 'Searchtext';
+                       $fileFieldDescription = new CargoFieldDescription();
+                       $fileFieldDescription->mType = 'Page';
+                       $stringFieldDescription = new CargoFieldDescription();
+                       $stringFieldDescription->mType = 'String';
                        $queryDisplayer->mFieldDescriptions[$pageTextStr] = 
$fullTextFieldDescription;
+                       $queryDisplayer->mFieldDescriptions[$fileNameStr] = 
$fileFieldDescription;
+                       $queryDisplayer->mFieldDescriptions[$fileTextStr] = 
$fullTextFieldDescription;
                }
 
                $queryDisplayer->mFormat = 'category';
diff --git a/i18n/en.json b/i18n/en.json
index 9b1f16d..c929003 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -66,6 +66,8 @@
        "cargo-drilldown-novalues": "There are no values for this filter",
        "cargo-drilldown-fulltext": "Full text",
        "cargo-drilldown-pagetext": "Page text",
+       "cargo-drilldown-filename": "File name",
+       "cargo-drilldown-filetext": "File text",
        "cargo-viewcsv": "View CSV",
        "cargo-viewxls": "View XLS",
        "cargo-viewjson": "View JSON",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 5436547..9469084 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -71,6 +71,8 @@
        "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-drilldown-filename": "The label for displaying the name of a 
file uploaded to the wiki.",
+       "cargo-drilldown-filetext": "The label for searching on all the text 
within a file.",
        "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/316594
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I88e4f1dc5370b4a6377e129ff535b145c0320177
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