SearchDatabase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Database search engine
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Search
  22. */
  23. use Wikimedia\Rdbms\IDatabase;
  24. use Wikimedia\Rdbms\ILoadBalancer;
  25. /**
  26. * Base search engine base class for database-backed searches
  27. * @ingroup Search
  28. * @since 1.23
  29. */
  30. abstract class SearchDatabase extends SearchEngine {
  31. /** @var ILoadBalancer */
  32. protected $lb;
  33. /** @var IDatabase (backwards compatibility) */
  34. protected $db;
  35. /**
  36. * @var string[] search terms
  37. */
  38. protected $searchTerms = [];
  39. /**
  40. * @param ILoadBalancer $lb The load balancer for the DB cluster to search on
  41. */
  42. public function __construct( ILoadBalancer $lb ) {
  43. $this->lb = $lb;
  44. // @TODO: remove this deprecated field in 1.35
  45. $this->db = $lb->getLazyConnectionRef( DB_REPLICA ); // b/c
  46. }
  47. /**
  48. * @param string $term
  49. * @return ISearchResultSet|Status|null
  50. */
  51. final public function doSearchText( $term ) {
  52. return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
  53. }
  54. /**
  55. * Perform a full text search query and return a result set.
  56. *
  57. * @param string $term Raw search term
  58. * @return SqlSearchResultSet|null
  59. */
  60. abstract protected function doSearchTextInDB( $term );
  61. /**
  62. * @param string $term
  63. * @return ISearchResultSet|null
  64. */
  65. final public function doSearchTitle( $term ) {
  66. return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
  67. }
  68. /**
  69. * Perform a title-only search query and return a result set.
  70. *
  71. * @param string $term Raw search term
  72. * @return SqlSearchResultSet|null
  73. */
  74. abstract protected function doSearchTitleInDB( $term );
  75. /**
  76. * Return a 'cleaned up' search string
  77. *
  78. * @param string $text
  79. * @return string
  80. */
  81. protected function filter( $text ) {
  82. // List of chars allowed in the search query.
  83. // This must include chars used in the search syntax.
  84. // Usually " (phrase) or * (wildcards) if supported by the engine
  85. $lc = $this->legalSearchChars( self::CHARS_ALL );
  86. return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
  87. }
  88. /**
  89. * Extract the optional namespace prefix and set self::namespaces
  90. * accordingly and return the query string
  91. * @param string $term
  92. * @return string the query string without any namespace prefix
  93. */
  94. final protected function extractNamespacePrefix( $term ) {
  95. $queryAndNs = self::parseNamespacePrefixes( $term );
  96. if ( $queryAndNs === false ) {
  97. return $term;
  98. }
  99. $this->namespaces = $queryAndNs[1];
  100. return $queryAndNs[0];
  101. }
  102. }