Topic on Extension talk:Replace Text

How do I replace non regular wikitext?

3
Spas.Z.Spasov (talkcontribs)

Hello, recently we've migrated to MediaWiki 1.35. We are using this nice exension for a long time. Yesterday I've pached it as it is decribed in the topic below.


Now I found when I'm tryng to replace text that is located within the MediaWiki NameSpace, let's say in Common.css, the web interface returns successiful replacement, but replacement is not actually done.

For example I've placed "Replace_Test" as comment into Common.css. Then I've used the web interface of Extension:ReplaceText in otder to replace "Replace_Test" with "Replace_Success". At this point the extension returns:

"Replace_Test" will be replaced with "Replace_Success" in one page.

But the replacement is not done, and when I inspected the outpot of the background job I found the following messageg:

2020-11-03 05:41:43 replaceText MediaWiki:Common.css user_id=4 target_str=Replace_Test replacement_str=Replace_Success use_regex= edit_summary=Text replacement - "Replace_Test" to "Replace_Success" create_redirect= watch_page= doAnnounce=1 namespace=8 title=Common.css requestId=X6Dtk94IEEHzBCOoVGYh3wAAAAA (id=2151,timestamp=20201103054139) STARTING
2020-11-03 05:41:43 replaceText MediaWiki:Common.css user_id=4 target_str=Replace_Test replacement_str=Replace_Success use_regex= edit_summary=Text replacement - "Replace_Test" to "Replace_Success" create_redirect= watch_page= doAnnounce=1 namespace=8 title=Common.css requestId=X6Dtk94IEEHzBCOoVGYh3wAAAAA (id=2151,timestamp=20201103054139) t=3 error=replaceText: Wiki page "MediaWiki:Common.css" does not hold regular wikitext.

Is there any workaround of this issue?

Yaron Koren (talkcontribs)

Are you sure that this affects all pages in the MediaWiki: namespace, and not just specific pages like Common.css (and probably Common.js)? And did this work before?

Spas.Z.Spasov (talkcontribs)

Hi, Yaron Koren, I found this behavior when I was taring to replace a file system path (like `resources-local/` to `images/resources-local/`) which appears in several css/js pages in our wiki. I made an additional test. For its purpose, I've placed the string Replace_Test in three pages as follow:

  1. MediaWiki:Common.css -> with content model: CSS
  2. MediaWiki:InternalWhitelist -> with content model: wikitext
  3. Test (NS=0) -> with content model: Javascript

At this point, the web interface of Replace Test returns: "Replace_Test" will be replaced with "Replace_Success" in 3 pages.

Actually the string was replaced only in the page with with content model wikitext. Here is the full output of the job queue: https://pastebin.com/L1gpyrf4


I've found the code that causes this behavior and the possible reason of it, but can't propose an elegant solution:

src/ReplaceTextJob.php:84:      if ( $this->title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
src/ReplaceTextJob.php:128:	$new_content = new WikitextContent( $new_text );

Probably the content model must be mater of choice within the Replace Text's interface, based on $wgContentHandlers.


Here is my current workaround:

/**
 * Around line 84 in src/ReplaceTextJob.php
 */
global $wgContentHandlers;
//if ( $this->title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
if ( ! in_array( $this->title->getContentModel(), array_keys($wgContentHandlers) ) ) {
	$this->error = 'replaceText: Wiki page "' .
		//$this->title->getPrefixedDBkey() . '" does not hold regular wikitext.';
		$this->title->getPrefixedDBkey() . '" does not hold any known content model, see $wgContentHandlers.';
	return false;
}


/**
 * Around line 128 in src/ReplaceTextJob.php
 */
//$new_content = new WikitextContent( $new_text );
switch ($this->title->getContentModel()) {
	case CONTENT_MODEL_WIKITEXT:
		$new_content = new WikitextContent( $new_text );
		break;
	case CONTENT_MODEL_JAVASCRIPT:
		$new_content = new JavaScriptContent( $new_text );
		break;
	case CONTENT_MODEL_JSON:
		$new_content = new JsonContent( $new_text );
		break;
	case CONTENT_MODEL_CSS:
		$new_content = new CssContent( $new_text );
		break;
	case CONTENT_MODEL_TEXT:
		$new_content = new TextContent( $new_text );
		break;
	default: // CONTENT_MODEL_UNKNOWN
		$new_content = new FallbackContent( $new_text );
}
Reply to "How do I replace non regular wikitext?"