MediaWiki  1.33.0
ReplaceTextSearch.php
Go to the documentation of this file.
1 <?php
23 
25 
34  public static function doSearchQuery(
35  $search, $namespaces, $category, $prefix, $use_regex = false
36  ) {
37  $dbr = wfGetDB( DB_REPLICA );
38  $tables = [ 'page', 'revision', 'text' ];
39  $vars = [ 'page_id', 'page_namespace', 'page_title', 'old_text' ];
40  if ( $use_regex ) {
41  $comparisonCond = self::regexCond( $dbr, 'old_text', $search );
42  } else {
43  $any = $dbr->anyString();
44  $comparisonCond = 'old_text ' . $dbr->buildLike( $any, $search, $any );
45  }
46  $conds = [
47  $comparisonCond,
48  'page_namespace' => $namespaces,
49  'rev_id = page_latest',
50  'rev_text_id = old_id'
51  ];
52 
53  self::categoryCondition( $category, $tables, $conds );
54  self::prefixCondition( $prefix, $conds );
55  $options = [
56  'ORDER BY' => 'page_namespace, page_title',
57  // 250 seems like a reasonable limit for one screen.
58  // @TODO - should probably be a setting.
59  'LIMIT' => 250
60  ];
61 
62  return $dbr->select( $tables, $vars, $conds, __METHOD__, $options );
63  }
64 
70  public static function categoryCondition( $category, &$tables, &$conds ) {
71  if ( strval( $category ) !== '' ) {
72  $category = Title::newFromText( $category )->getDbKey();
73  $tables[] = 'categorylinks';
74  $conds[] = 'page_id = cl_from';
75  $conds['cl_to'] = $category;
76  }
77  }
78 
83  public static function prefixCondition( $prefix, &$conds ) {
84  if ( strval( $prefix ) === '' ) {
85  return;
86  }
87 
88  $dbr = wfGetDB( DB_REPLICA );
89  $title = Title::newFromText( $prefix );
90  if ( !is_null( $title ) ) {
91  $prefix = $title->getDbKey();
92  }
93  $any = $dbr->anyString();
94  $conds[] = 'page_title ' . $dbr->buildLike( $prefix, $any );
95  }
96 
103  public static function regexCond( $dbr, $column, $regex ) {
104  if ( $dbr->getType() == 'postgres' ) {
105  $op = '~';
106  } else {
107  $op = 'REGEXP';
108  }
109  return "$column $op " . $dbr->addQuotes( $regex );
110  }
111 
120  public static function getMatchingTitles(
121  $str,
122  $namespaces,
123  $category,
124  $prefix,
125  $use_regex = false
126  ) {
127  $dbr = wfGetDB( DB_REPLICA );
128 
129  $tables = [ 'page' ];
130  $vars = [ 'page_title', 'page_namespace' ];
131 
132  $str = str_replace( ' ', '_', $str );
133  if ( $use_regex ) {
134  $comparisonCond = self::regexCond( $dbr, 'page_title', $str );
135  } else {
136  $any = $dbr->anyString();
137  $comparisonCond = 'page_title ' . $dbr->buildLike( $any, $str, $any );
138  }
139  $conds = [
140  $comparisonCond,
141  'page_namespace' => $namespaces,
142  ];
143 
144  self::categoryCondition( $category, $tables, $conds );
145  self::prefixCondition( $prefix, $conds );
146  $sort = [ 'ORDER BY' => 'page_namespace, page_title' ];
147 
148  return $dbr->select( $tables, $vars, $conds, __METHOD__, $sort );
149  }
150 
159  public static function getReplacedText( $text, $search, $replacement, $regex ) {
160  if ( $regex ) {
161  $escapedSearch = addcslashes( $search, '/' );
162  return preg_replace( "/$escapedSearch/Uu", $replacement, $text );
163  } else {
164  return str_replace( $search, $replacement, $text );
165  }
166  }
167 
176  public static function getReplacedTitle( Title $title, $search, $replacement, $regex ) {
177  $oldTitleText = $title->getText();
178  $newTitleText = self::getReplacedText( $oldTitleText, $search, $replacement, $regex );
179  return Title::makeTitleSafe( $title->getNamespace(), $newTitleText );
180  }
181 }
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:48
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
ReplaceTextSearch\prefixCondition
static prefixCondition( $prefix, &$conds)
Definition: ReplaceTextSearch.php:83
$namespaces
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:925
$tables
this hook is for auditing only RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist & $tables
Definition: hooks.txt:979
ReplaceTextSearch
Definition: ReplaceTextSearch.php:24
ReplaceTextSearch\regexCond
static regexCond( $dbr, $column, $regex)
Definition: ReplaceTextSearch.php:103
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$dbr
$dbr
Definition: testCompression.php:50
ReplaceTextSearch\categoryCondition
static categoryCondition( $category, &$tables, &$conds)
Definition: ReplaceTextSearch.php:70
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$vars
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition: hooks.txt:2220
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
$sort
$sort
Definition: profileinfo.php:328
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:604
ReplaceTextSearch\getReplacedText
static getReplacedText( $text, $search, $replacement, $regex)
Do a replacement on a string.
Definition: ReplaceTextSearch.php:159
Title
Represents a title within MediaWiki.
Definition: Title.php:40
ReplaceTextSearch\getReplacedTitle
static getReplacedTitle(Title $title, $search, $replacement, $regex)
Do a replacement on a title.
Definition: ReplaceTextSearch.php:176
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1985
ReplaceTextSearch\doSearchQuery
static doSearchQuery( $search, $namespaces, $category, $prefix, $use_regex=false)
Definition: ReplaceTextSearch.php:34
ReplaceTextSearch\getMatchingTitles
static getMatchingTitles( $str, $namespaces, $category, $prefix, $use_regex=false)
Definition: ReplaceTextSearch.php:120