33$IP = getenv(
"MW_INSTALL_PATH" ) ? getenv(
"MW_INSTALL_PATH" ) : __DIR__ .
"/../../..";
34if ( !is_readable(
"$IP/maintenance/Maintenance.php" ) ) {
35 die(
"MW_INSTALL_PATH needs to be set to your MediaWiki installation.\n" );
37require_once (
"$IP/maintenance/Maintenance.php" );
60 parent::__construct();
61 $this->mDescription =
"CLI utility to replace text wherever it is ".
64 $this->
addArg(
"target",
"Target text to find.",
false );
65 $this->
addArg(
"replace",
"Text to replace.",
false );
67 $this->
addOption(
"dry-run",
"Only find the texts, don't replace.",
69 $this->
addOption(
"regex",
"This is a regex (false).",
71 $this->
addOption(
"user",
"The user to attribute this to (uid 1).",
73 $this->
addOption(
"yes",
"Skip all prompts with an assumed 'yes'.",
75 $this->
addOption(
"summary",
"Alternate edit summary. (%r is where to ".
76 " place the replacement text, %f the text to look for.)",
78 $this->
addOption(
"nsall",
"Search all canonical namespaces (false). " .
79 "If true, this option overrides the ns option.",
false,
false,
'a' );
80 $this->
addOption(
"ns",
"Comma separated namespaces to search in " .
81 "(Main) .",
false,
true );
82 $this->
addOption(
"replacements",
"File containing the list of " .
83 "replacements to be made. Fields in the file are tab-separated. " .
84 "See --show-file-format for more information.",
false,
true,
"f" );
85 $this->
addOption(
"show-file-format",
"Show a description of the " .
86 "file format to use with --replacements.",
false,
false );
87 $this->
addOption(
"no-announce",
"Do not announce edits on Special:RecentChanges or " .
88 "watchlists.",
false,
false,
"m" );
89 $this->
addOption(
"debug",
"Display replacements being made.",
false,
false );
90 $this->
addOption(
"listns",
"List out the namespaces on this wiki.",
94 if ( method_exists( $this,
'requireExtension' ) ) {
100 $userReplacing = $this->
getOption(
"user", 1 );
102 $user = is_numeric( $userReplacing ) ?
106 if ( get_class( $user ) !==
'User' ) {
108 "Couldn't translate '$userReplacing' to a user.",
true
118 $this->
error(
"You have to specify a target.",
true );
126 $this->
error(
"You have to specify replacement text.",
true );
132 $file = $this->
getOption(
"replacements" );
137 if ( !is_readable( $file ) ) {
138 throw new MWException(
"File does not exist or is not readable: "
142 $handle = fopen( $file,
"r" );
143 if ( $handle ===
false ) {
144 throw new MWException(
"Trouble opening file: $file\n" );
148 $this->defaultContinue =
true;
150 while ( (
$line = fgets( $handle ) ) !==
false ) {
152 $field = explode(
"\t", substr(
$line, 0, -1 ) );
153 if ( !isset( $field[1] ) ) {
157 $this->target[] = $field[0];
158 $this->replacement[] = $field[1];
165 if ( !is_bool( $this->defaultContinue ) ) {
166 $this->defaultContinue =
177 if ( $this->
getOption(
"summary" ) !==
null ) {
178 $msg = str_replace( [
'%f',
'%r' ],
179 [ $this->target, $this->replacement ],
186 echo
"Index\tNamespace\n";
187 $nsList = MWNamespace::getCanonicalNamespaces();
189 foreach ( $nsList as $int => $val ) {
193 echo
" $int\t$val\n";
200The format of the replacements file is tab separated with three fields.
201Any
line that does not have a tab is ignored and can be considered a comment.
205 1. String to search
for.
206 2. String to
replace found text with.
207 3. (optional) The presence of
this field indicates that the previous two
208 are considered a regular expression.
214regex(p*) Count the Ps; \\1
true
223 if ( !$nsall && !$ns ) {
226 $canonical = MWNamespace::getCanonicalNamespaces();
232 if ( is_numeric( $n ) ) {
233 if ( isset( $canonical[ $n ] ) ) {
242 }, explode(
",", $ns ) );
246 return $val !==
null;
268 if ( !$this->titles || count( $this->titles ) == 0 ) {
270 foreach (
$res as $row ) {
271 $this->titles[] = Title::makeTitleSafe(
272 $row->page_namespace,
295 'user_id' => $this->user->getId(),
299 echo
"Replacing on $title... ";
301 if (
$job->run() !==
true ) {
302 $this->
error(
"Trouble on the page '$title'." );
313 while ( $reply !==
"y" && $reply !==
"n" ) {
315 $reply = substr( strtolower( $reply ), 0, 1 );
317 return $reply ===
"y";
325 if ( $this->
getOption(
"show-file-format" ) ) {
329 $this->user = $this->
getUser();
348 $this->doAnnounce =
true;
351 $this->
error(
"No matching namespaces.",
true );
354 foreach ( array_keys( $this->target ) as $index ) {
355 $target = $this->target[$index];
360 echo
"Replacing '$target' with '$replacement'";
362 echo
" as regular expression.";
367 $this->
namespaces, $this->category, $this->prefix,
370 if (
$res->numRows() === 0 ) {
371 $this->
error(
"No targets found to replace.",
true );
380 "Replace instances on these pages?"
386 if ( $this->
getOption(
"user",
null ) ===
null ) {
387 $comment =
" (Use --user to override)";
389 if ( $this->
getOption(
"no-announce",
false ) ) {
390 $this->doAnnounce =
false;
393 "Attribute changes to the user '{$this->user}'?$comment"
397 if (
$res->numRows() > 0 ) {
to move a page</td >< td > &*You are moving the page across namespaces
$wgShowExceptionDetails
If set to true, uncaught exceptions will print a complete stack trace to output.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true)
Add some args that are needed.
requireExtension( $name)
Indicate that the specified extension must be loaded before the script can run.
static readconsole( $prompt='> ')
Prompt the console for input.
getArg( $argId=0, $default=null)
Get an argument.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Maintenance script that replaces text in pages.
__construct()
Default constructor.
getSummary( $target, $replacement)
shouldContinueByDefault()
replaceTitles( $res, $target, $replacement, $useRegex)
Background job to replace text in a given page.
static doSearchQuery( $search, $namespaces, $category, $prefix, $use_regex=false)
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
static newFromId( $id)
Static factory method for creation from a given user ID.
The ContentHandler facility adds support for arbitrary content types on wiki instead of relying on wikitext for everything It was introduced in MediaWiki Each kind of and so on Built in content types are
I won t presume to tell you how to I m just describing the methods I chose to use for myself If you do choose to follow these it will probably be easier for you to collaborate with others on the but if you want to contribute without by all means do which work well I also use K &R brace matching style I know that s a religious issue for so if you want to use a style that puts opening braces on the next line
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "<div ...>$1</div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
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 noclasses just before the function returns a value If you return true
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 noclasses & $ret
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place replace
require_once RUN_MAINTENANCE_IF_MAIN
if(count( $args)< 1) $job