MediaWiki  1.34.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', 'slots', 'content' ];
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_id = slot_revision_id',
51  'slot_content_id = content_id',
52  'SUBSTRING(content_address, 4) = old_id'
53  ];
54 
55  self::categoryCondition( $category, $tables, $conds );
56  self::prefixCondition( $prefix, $conds );
57  $options = [
58  'ORDER BY' => 'page_namespace, page_title',
59  // 250 seems like a reasonable limit for one screen.
60  // @TODO - should probably be a setting.
61  'LIMIT' => 250
62  ];
63 
64  return $dbr->select( $tables, $vars, $conds, __METHOD__, $options );
65  }
66 
72  public static function categoryCondition( $category, &$tables, &$conds ) {
73  if ( strval( $category ) !== '' ) {
74  $category = Title::newFromText( $category )->getDbKey();
75  $tables[] = 'categorylinks';
76  $conds[] = 'page_id = cl_from';
77  $conds['cl_to'] = $category;
78  }
79  }
80 
85  public static function prefixCondition( $prefix, &$conds ) {
86  if ( strval( $prefix ) === '' ) {
87  return;
88  }
89 
90  $dbr = wfGetDB( DB_REPLICA );
91  $title = Title::newFromText( $prefix );
92  if ( !is_null( $title ) ) {
93  $prefix = $title->getDbKey();
94  }
95  $any = $dbr->anyString();
96  $conds[] = 'page_title ' . $dbr->buildLike( $prefix, $any );
97  }
98 
105  public static function regexCond( $dbr, $column, $regex ) {
106  if ( $dbr->getType() == 'postgres' ) {
107  $op = '~';
108  } else {
109  $op = 'REGEXP';
110  }
111  return "$column $op " . $dbr->addQuotes( $regex );
112  }
113 
122  public static function getMatchingTitles(
123  $str,
124  $namespaces,
125  $category,
126  $prefix,
127  $use_regex = false
128  ) {
129  $dbr = wfGetDB( DB_REPLICA );
130 
131  $tables = [ 'page' ];
132  $vars = [ 'page_title', 'page_namespace' ];
133 
134  $str = str_replace( ' ', '_', $str );
135  if ( $use_regex ) {
136  $comparisonCond = self::regexCond( $dbr, 'page_title', $str );
137  } else {
138  $any = $dbr->anyString();
139  $comparisonCond = 'page_title ' . $dbr->buildLike( $any, $str, $any );
140  }
141  $conds = [
142  $comparisonCond,
143  'page_namespace' => $namespaces,
144  ];
145 
146  self::categoryCondition( $category, $tables, $conds );
147  self::prefixCondition( $prefix, $conds );
148  $sort = [ 'ORDER BY' => 'page_namespace, page_title' ];
149 
150  return $dbr->select( $tables, $vars, $conds, __METHOD__, $sort );
151  }
152 
161  public static function getReplacedText( $text, $search, $replacement, $regex ) {
162  if ( $regex ) {
163  $escapedSearch = addcslashes( $search, '/' );
164  return preg_replace( "/$escapedSearch/Uu", $replacement, $text );
165  } else {
166  return str_replace( $search, $replacement, $text );
167  }
168  }
169 
178  public static function getReplacedTitle( Title $title, $search, $replacement, $regex ) {
179  $oldTitleText = $title->getText();
180  $newTitleText = self::getReplacedText( $oldTitleText, $search, $replacement, $regex );
181  return Title::makeTitleSafe( $title->getNamespace(), $newTitleText );
182  }
183 }
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:316
ReplaceTextSearch\prefixCondition
static prefixCondition( $prefix, &$conds)
Definition: ReplaceTextSearch.php:85
ReplaceTextSearch
Definition: ReplaceTextSearch.php:24
ReplaceTextSearch\regexCond
static regexCond( $dbr, $column, $regex)
Definition: ReplaceTextSearch.php:105
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:50
ReplaceTextSearch\categoryCondition
static categoryCondition( $category, &$tables, &$conds)
Definition: ReplaceTextSearch.php:72
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:2575
$title
$title
Definition: testCompression.php:34
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
$sort
$sort
Definition: profileinfo.php:331
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
ReplaceTextSearch\getReplacedText
static getReplacedText( $text, $search, $replacement, $regex)
Do a replacement on a string.
Definition: ReplaceTextSearch.php:161
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ReplaceTextSearch\getReplacedTitle
static getReplacedTitle(Title $title, $search, $replacement, $regex)
Do a replacement on a title.
Definition: ReplaceTextSearch.php:178
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:122