4

I work with the CMS MediaWiki which includes numerous features that some I want to prevent from being parsed in all webpages that would otherwise include them.
Some such features (such as minor built-in menu links) I am good to prevent with CSS or JavaScript, but some others not and that's my problem:

As a user can cancel CSS and/or JS, that user might eventually use the "prevented" feature.
A good practical example to my problem is my desire to prevent parsing of edit summary boxes which I found causing more harm than good in too many MediaWiki based communities.

As I am not a PHP programmer and I don't want to study and edit the massive source code of MediaWiki CMS from scratch (and make exceptions for automatic upgrades and so forth), I prefer an indirect ("tweak") solution:

MediaWiki has the LocalSettings.php file in which one can run custom PHP.
I desire to run some PHP function that, by some criterion (CSS class maybe), will prevent the parsing of elements that are parsed earlier such as the edit summary box.
If this is even possible with the current release of PHP and if so, how could this be done?

  • 2
    upvoted as i think this deserves some attention. What i will say is that I am not sure this is possible. Any submissions from pages will go to the backend and be handled. I don't think you can stop this by putting PHP code into a different file. I will add that its been a good while since I've touched PHP, so maybe I'm wrong. – Piercy Nov 27 '19 at 17:05
  • well, if you can use PHP that means you can alter JS and ultimately CSS through JS, [have a look](https://stackoverflow.com/questions/1164130/how-to-output-javascript-with-php) – Vladan Nov 27 '19 at 21:35
  • @Vladan that doesn't make sense. What if I just deactivate JS in my browser? – Aaron3219 Nov 28 '19 at 18:37

2 Answers2

1

No, it is not possible to change the HTML code from a different file, unless you have some kind of middleware that will retrieve the code as plain text, edit it (with Regex for example) and pass it on to the actual view, which almost sounds like an MVC pattern. If that is how MediaWiki works (which I highly doubt), then this is your way.

Why doesn't it work? This is because PHP is a backend language. It can't interact with the frontend as the PHP script/file (which contains your HTML) will be executed at the server (= your backend) and is then passed to the browser.

Imagine it like this (very, very simplified):
your_file.php
This is the state of the file at your server before the script is executed:

<div>
  <?php
    echo "<a>test</a>;
  ?>
</div>

The script is then executed on your server resulting in:

<div>
  <a>test</a>
</div>

Your file hasn't even been sent to the browser yet, but it will now. Sadly, your file was already executed and you won't be able to execute any PHP functions. And no, you can't use CSS and JS at your server.


Since you probably won't be able to implement a pattern as described at the top there is one last hope. I am not familiar with MediaWiki but I think I heard that they generate the pages inside an actual PHP file. If this is the case just do the following:

<div>
  Yey, some nice features I want to keep!
</div>
<?php
/*
<div>
  Oh no! A feature I should hide!
</div>
?>
<div>
  Yey, some nice features I want to keep!
</div>

Just comment it out ;). If that also doesn't work, then... well then there is no fix in the way you want to, let alone an easy one.

Aaron3219
  • 2,168
  • 4
  • 11
  • 28
0

You can't do that via PHP, but if you have server access you could try some high-risk methods just for the sake of art.

Disclaimer: these are not practical solutions, never use them unless you know what you're doing and there's no other possible way.

So, assuming you can only edit server configs and LocalSetings.php, you could:

  1. Use a rewrite rule to direct all requests to your LocalSettings.php file, effectively changing the entry point for the CMS. You could then capture the output with ob_get_clean() and modify it as you like.
  2. With nginx, use sub_filter to filter out the HTML before it gets to the browser.

Since you don't want to mess with CMS source code I assume you want to change your server configs even less. But I'll still leave my answer here in case it helps somebody else in the future.

Pavel Lint
  • 3,252
  • 1
  • 18
  • 18
  • Hi; thanks; I am actually more convenient to do some server/bash "tricks" than PHP tricks, in general... This is a "shared" CentOS server environment and the hosting company (Siteground) uses a combination of Apache-Nginx (Nginx as "reverse proxy" which I have no idea what that means), but they do give me only limited Apache access (`.htaccess files) and some `cron` jobs and CLI console in my user partition. –  Nov 30 '19 at 07:58
  • Maybe you have something to add / edit after the above comment. –  Nov 30 '19 at 07:58
  • hey @ShadowyShade, thanks for the bounty :) Reverse proxy is just a proxy on server side ("normal" proxy is something you use on your computer). Assuming you have an `.htaccess` file in the root of your CMS next to `index.php`, you might implement a `RewriteRule` there that sends all requests to `LocalSettings.php`. Then basically copy code from index.php to `LcocalSettings.php`, wrap it in `ob_start()` and `ob_get_clean()`, and replace things that you want there. Again, there's no guarantee this will actually work, but you can give it a try :) – Pavel Lint Nov 30 '19 at 08:15
  • Thx Pavel --- I assume that even if it works; should there be a significant performance problem? BTW, you might want to make it part of the answer as comments might get deleted. –  Nov 30 '19 at 08:22
  • I basically just repeated the answer except for the ReverseProxy part :) I don't think it will bring any performance problems, but I think it may be easier for you to just turn off modules in the CMS source code. – Pavel Lint Nov 30 '19 at 08:25
  • Oh yest - I mean; clarify that this should be done through the websites (or any other) `.htaccess` and mention `ob_start()` and `ob_get_clean()` and replace anything; but of course, it's only my personal recommendation. Thank you for all your help ! –  Nov 30 '19 at 08:49