MediaWiki REL1_33
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' ];
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 = [
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
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,
123 $category,
124 $prefix,
125 $use_regex = false
126 ) {
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 = [
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}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
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:40
Relational database abstraction object.
Definition Database.php:49
static configuration should be added through ResourceLoaderGetConfigVars instead & $vars
Definition hooks.txt:2228
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:925
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:1999
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:996
Result wrapper for grabbing data queried from an IDatabase object.
$sort
const DB_REPLICA
Definition defines.php:25