MediaWiki REL1_34
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 ) {
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
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 ) {
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}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
static categoryCondition( $category, &$tables, &$conds)
static regexCond( $dbr, $column, $regex)
static doSearchQuery( $search, $namespaces, $category, $prefix, $use_regex=false)
static getReplacedText( $text, $search, $replacement, $regex)
Do a replacement on a string.
static getReplacedTitle(Title $title, $search, $replacement, $regex)
Do a replacement on a title.
static prefixCondition( $prefix, &$conds)
static getMatchingTitles( $str, $namespaces, $category, $prefix, $use_regex=false)
Represents a title within MediaWiki.
Definition Title.php:42
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
Result wrapper for grabbing data queried from an IDatabase object.
$sort
const DB_REPLICA
Definition defines.php:25