CodeMirror uses the [[ https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay | BeforePageDisplay ]] hook, and checks if the `action` is `edit` or `submit`, along with other conditions ([[ https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/CodeMirror/+/7002bc434d891edaff6c12adeecae7289ebe8cc0/includes/Hooks.php#47 | Hooks::shouldLoadCodeMirror ]]). It's unclear why this was used over the [[ https://www.mediawiki.org/wiki/Manual:Hooks/EditPage::showEditForm:initial | EditPage::showEditForm:initial ]] hook. As a result, the CodeMirror module gets predelivered even when it isn't used, such as on read-only pages.
Browsing to a page you are unable to edit…
Before [[ https://gerrit.wikimedia.org/r/c/mediawiki/extensions/CodeMirror/+/1007746 | r1007746 ]], with `$wgCodeMirrorV6 = false;`:
```
> mw.loader.getState( 'ext.CodeMirror.WikiEditor' )
"ready"
```
and afterwards:
```
> mw.loader.getState( 'ext.CodeMirror.WikiEditor' )
"registered"
```
which is what we want.
The module is also loaded pretty much anywhere `action=edit` is and #CodeEditor isn't, so it could unnecessarily be in the startup module in a variety of scenarios. To further address this, we should check the content model to make sure it is one that is supported (as of the time of writing, only `wikitext`).
**Possible risk**
* It is possible that some gadget or script out there is assuming CodeMirror is available upfront, when it won't be following this task. A [[ https://global-search.toolforge.org/?q=%22CodeMirror.fromTextArea%22&namespaces=2%2C4%2C8&title=%28Gadgets-definition%7C.*%5C.%28js%7Ccss%7Cjson%29%29 | Global Search ]] for `CodeMirror.fromTextArea` shows most usages already require the `ext.CodeMirorr.WikiEditor` module (or the CM library directly), so hopefully we won't see much breakage. Nonetheless we should monitor for an increase of JS errors.
**Acceptance criteria**
* `ext.CodeMirror.WikiEditor` (or `ext.CodeMirror.v6.WikiEditor` in the case of CM6) should only be "ready" where CodeMirror is actually usable
* Extensions that have content models needing CodeMirror should register a `CodeMirrorContentModels`. In our cluster, I *think* this only includes #proofreadpage ([[ https://gerrit.wikimedia.org/r/1007747 | r1007747 ]])