Commit graph

115 commits

Author SHA1 Message Date
Edward Chernenko 9debf59c9c Workaround for intermittent problems in Symfony/Process under HHVM.
Due to https://github.com/facebook/hhvm/issues/6921, $process->getOutput()
from Symfony/Process doesn't work reliably under HHVM.
This causes the result of "pygmentize" to be unexpectably truncated.

Until this is fixed in HHVM or Symfony, we can use callback of $process->run()
instead of the buggy $process->getOutput().

Bug: T151523
Change-Id: I21ef340eccca0741d7e86840f444272623675c46
2017-01-19 23:09:42 +03:00
Brian Wolff 8714354068 SECURITY: Replace strip markers before syntax highlighting
The raw strip markers can't be syntax highlighted properly anyhow.

Bug: T51256, T110143, T78456
Change-Id: I822fa2bbfde790df22c00bb87819c9027fb5dcc9
2016-05-20 10:21:06 -07:00
Reedy ce5cee30e0 Minor unindent
Change-Id: Ie62d0822e2a853164d0c37aed47704a2fe8c08a2
2016-01-31 09:43:42 +00:00
Kunal Mehta 03bbdd8a2e Use load_composer_autoloader
See Ib031bef17c8a7d708a5c7878e74967d19217bbc8

Change-Id: If7fff2476382853426fa61b5c878f0c6f6b9d0a8
2016-01-14 19:59:28 +00:00
Ori Livneh f89bc8d62c Drop dependency on kzykhys/pygments
kzykhys/pygments provides very little added value when compared to just using
symfony/process directly. Since kzykhys/pygments appears to be unmaintained and
is currently broken, depend on symfony/process directly instead.

Change-Id: I34c7e4201c2c21d3f8607ec826a4c9869e2da917
Task: T120068
2015-12-04 15:02:13 -08:00
Aaron Schulz 7d889f6368 Conversion to using newer WAN cache class
Change-Id: I7e1dd2bf67fa80af20613b9136329b83e17e83cc
2015-10-21 21:46:37 -07:00
Kunal Mehta c68dcbd75e Make SyntaxHighlight_GeSHi::highlight() public
So other extensions (e.g. EventLogging) can use it as a public API
instead of having to use the backwards-compat prepare() function.

Change-Id: Iae0cfb838018fc6111feb03c22fb910314387e38
2015-10-14 20:20:05 +00:00
Ed Sanders 30cdc51e11 Add missing linebreak
Change-Id: I92260974732643f7f14eca245ba21d94c175ac22
2015-10-13 14:21:56 +01:00
paladox ee3896a51c Add php code sniffer to SyntaxHighlight_GeSHi
Requires https://gerrit.wikimedia.org/r/#/c/225035/

Change-Id: If46b56fa8aa26b03890274ac772110eba9519f8d
2015-09-27 21:16:58 +02:00
Bartosz Dziewoński 05bae5a932 Use $parser->mUniqPrefix instead of $parser::MARKER_PREFIX for MW 1.25 compatibility
Parser::MARKER_PREFIX is a new constant introduced in MW 1.26,
previously the value was dynamic and available as
$parser->mUniqPrefix. That continues to be supported in MW 1.26+.

Follow-up to 043969f84e.

Bug: T105796
Change-Id: I9dfc9e1a424e28fc4f870dd513306e5144d1ec7c
2015-07-15 12:44:17 +00:00
Timo Tijhof 206f0fd585 Fix "MWException: Unexpected output from Pygments" in highlight fallback
Follows-up 043969f84e.

This caused a fatal MWException when saving/reading pages that contain a
<source> that couldn't be highlighted (e.g. no lang attribute, unknown
lang, or too large).

Specifically when wgWellFormedXml=false, in which case $out isn't
just from Pygments, but actually from our own Html::element.

Change-Id: Ib299a274d28021b2c7bba52d763dd1e17c1f09ec
2015-07-07 22:12:53 +01:00
Bartosz Dziewoński 043969f84e Refactor final output formatting
* Use 'nowiki' strip marker to prevent list processing (also known as
  doBlockLevels()). This resolves various issues related to using
  <syntaxhighlight/> blocks in lists and lists suddenly appearing
  inside <syntaxhighlight/> blocks. Fixes T17333, T25674, T104067.

* To prevent <p/>-wrapping resulting from the above, add our own
  wrapper <div/> around the output.

* Since we already have our own wrapper, remove Pygments' one and
  extend it with custom attributes. This resolves some regressions
  from the GeSHi migration. Fixes most of T103964.

Bug: T17333
Bug: T25674
Bug: T103964
Bug: T104067
Change-Id: I3afd1224a18549c62cd4a95fd046affa6d1d3b3f
2015-07-05 22:03:24 +00:00
Ori Livneh f41f26a85c Add a tracking category on pages that contain invalid <syntaxhighlight> attributes
Do this by having SyntaxHighlight_GeSHi::highlight() return a Status object
rather than a plain string, and by making it the highlight method's job to look
up a lexer for a language.  The actual warning text is not outputted anywhere
yet; deferring that for a follow-up patch.

Bug: T103586
Change-Id: Id839f925a56ab09a8423958327b9aefd7207ef37
2015-07-02 23:14:34 -07:00
Timo Tijhof f8b63304eb Support local composer setup
This makes installation easier for local development.
Per https://www.mediawiki.org/wiki/Manual:External_libraries

I was supicious about whether 'use KzykHys\Pygments\Pygments;'
works in a file if the namespace/class itself is only loaded
later, but it works fine.

Ref T103890.

Change-Id: Ia1a577f555cd8ab09969dec219316bc1e76769f4
2015-06-25 22:49:53 +00:00
Legoktm f1c6b66bac Revert "Add Pyglet, a web micro-service for highlighting using Pygments"
Doesn't work, shouldn't have been self-merged either.

This reverts commit 7e4efbe114.

Change-Id: I52b200d3fb0fea101317740e4ce63a7b71015303
2015-06-25 01:11:54 +00:00
Ori Livneh 7e4efbe114 Add Pyglet, a web micro-service for highlighting using Pygments
Pyglet is an internal web service that listens on port 31337 by default and
which accepts syntax highlighting request via POST. If $wgPygletURL is set to a
URL of a Pyglet instance, the extension will attempt to query the web service
instead of shelling out.

Change-Id: Ic49f4cd77585dbffc5392e80904754ff889b8a63
2015-06-24 17:57:50 -07:00
jenkins-bot ef3e89cc7c Merge "Use <code> instead of <span> for inline code snippets" 2015-06-24 18:11:32 +00:00
jenkins-bot 09cebbb56a Merge "On parser cache invalidation, purge Varnish as well." 2015-06-24 01:19:47 +00:00
Ori Livneh 32547b7657 On parser cache invalidation, purge Varnish as well.
This way, a visit by a logged-in user to a page with stale syntax highlighting
will also trigger a purge of the page for anons in Varnish.

Change-Id: I366563839c89d9546447d3e58bb9239a36bf70bd
2015-06-23 18:09:19 -07:00
jenkins-bot 100f57bd23 Merge "Support 'inline' as an alias for 'enclose="none"'" 2015-06-23 22:55:41 +00:00
Kunal Mehta 8dc96a9c0f RejectParserCacheValue may pass a WikiPage or Article
Change-Id: I03b6b00ab54037d1adf2b0abd17a88294dfc21ee
2015-06-23 14:56:33 -07:00
Kunal Mehta 01802ac3fb Use RejectParserCacheValue hook to invalidate caches that were using GeSHi
Change-Id: I47de88dc1c2f2380e41e6af0d3222b46c9c65569
2015-06-23 21:26:44 +00:00
Ori Livneh 41db8dbe38 Add cache versioning and makeCacheKey method
* Don't json_encode() the whole code block, that's stupidly expensive when the
  code is a string already. It's only the options array that needs to be
  explicitly stringified.
* Incorporate a cache version parameter input in the cache key so we can manage
  changes to the extension's HTML output.
* Only use wfGlobalCacheKey if available, to maintain 1.25 compat.

Change-Id: I8d536f9011763f47667502225ed4459be7d2ed4a
2015-06-23 09:46:58 -07:00
Ori Livneh 04293baad9 Use <code> instead of <span> for inline code snippets
Change-Id: Ibc07169fc3b91509aa2d98b8ae910c901d2f1703
2015-06-23 08:38:56 -07:00
Ori Livneh fa6e77e167 Support 'inline' as an alias for 'enclose="none"'
I think it's easier to understand and easier to remember.

Change-Id: I3c0e8bc209956b8d2ba071baa40142c731f56524
2015-06-23 08:38:56 -07:00
Bartosz Dziewoński 6a20c7f38d Correct whitespace around <syntaxhighlight> blocks
Remove trailing \n after closing </div>, as it results in multiple
newlines that confuse the MediaWiki parser into generating <p><br></p>
all over the place.

Bug: T85794
Change-Id: Iffaa73b49c29e94d8a1a659dd40b845a9f4ce0c1
2015-06-23 15:25:57 +00:00
Bartosz Dziewoński 5b7522a5fc Unbreak <syntaxhighlight enclose="none">
It was always "none" and not "span", this must've been an accidental change.

Also add a CSS tweak to better render the common pattern of
<code><syntaxhighlight enclose="none">...</syntaxhighlight></code>.

Bug: T85794
Change-Id: I3ed1b7f3c954374b49fc9a97881ea5236415cb6f
2015-06-23 15:21:29 +00:00
Bartosz Dziewoński d30a5724eb Correct behavior of <syntaxhighlight highlight="[lines]"> with more than one line
* Restore parseHighlightLines() function from before
  6484894497, which introduced a
  regression in parsing line ranges. There are no tests for this and
  I'm not going to debug the regexes.
* Use a space rather than a comma as separator when passing the list
  of line numbers to Pygments.

Bug: T85794
Change-Id: Ib86c216308a973000a4129c2c46286ec1dc988e1
2015-06-23 16:03:17 +02:00
Timo Tijhof 9815331195 Use wfGlobalCacheKey()
Change-Id: I62666265e297b5dbcccaa1a64a07bb7bd01b89c4
2015-06-23 08:08:48 +01:00
Ori Livneh f36b6c66d3 If unable to shell out to Pygments, emit a warning and fall back to plain text.
Change-Id: I8beb0367405e8ea70385134e03302e8bd3ab91ff
2015-06-22 16:19:25 -07:00
Ori Livneh 648744325c Bundle Pygments 2.0.2
Include Pygments 2.0.2 as an executable zip bundle. Also include a script to
automate the process of creating such bundles and to make it reproducible and
verifiable.

Change-Id: I67e6f804e493f065311164c610dc541a5779654e
2015-06-22 22:54:42 +00:00
Ori Livneh f1941f8a56 Only look for a compatible lexer if a direct Pygments match is unavailable
If Pygments ever adds a dedicated lexer for 'cadlisp', for example, we'd want the
extension to use that, rather than use the compatibility map.

Change-Id: Icc610695ac2826bb526f7c69e867576c660ba6ef
2015-06-22 22:47:52 +00:00
Ori Livneh 6484894497 Highlight using Pygments rather than Geshi
GeSHi is unmaintained, lacks support for many popular modern languages, and
suffers from deep architectural flaws, chief among them the inconsistent
tokenization of different languages, each of which requires a custom
stylesheet.

Pygments is a well-maintained alternative. It is, by my count, the most popular
syntax highlighting library around. It is BSD-licensed, actively maintained,
and is widely used in PHP projects.

To keep this easy to review, this change does not include update for l10n
files, and it does not delete the geshi/ directory. I will do those in a
separate patch.

The chief change between this and the previous implementation is that errors
result in the code block not being highlighted, as opposed to not being printed
at all, having been replaced by an angry red error message. I think that is the
right user experience. If you go to StackOverflow or GitHub and try to mark up
your code block as being written in some language that their highlighter
doesn't know about, you don't get an error message -- the code simply doesn't
get highlighted.

Because we don't recursively load dependencies for extensions, to test this,
you will need to create a composer.local.json in $IP and add:

    {
        "extra": {
            "merge-plugin": {
                "include": [
                    "extensions/SyntaxHighlight_GeSHi/composer.json"
                ]
            }
        }
    }

Then run `composer update`.

Bug: T85794
Change-Id: I07446ec9893fae3d1e394f435d3d95cf8be6bc33
2015-06-22 23:37:15 +01:00
Ed Sanders 28ff155a6c Use normal line numbers
'Fancy' line numbers are a fairly useless feature, not seen
in any other code highlighter. As the extension doesn't let you
choose a line number mode, default to normal.

Bug: T101602
Change-Id: Iccbd3ba6c91c58b0ea0f0c09832f1422936cd475
2015-06-09 16:02:18 +01:00
Timo Tijhof c81eb5978c Compare against '$geshi->error' instead of '$geshi->error()' in prepare()
Geshi::error() is a method that returns an HTML string representing
the error.

Geshi->$error is an integer error code.

This code clearly means to compare against the integer code given that
GESHI_ERROR_NO_SUCH_LANG is an integer.

Verified by using eval.php to instantiate "new Geshi( '', 'js' );"
and $geshi->error:
> 2
but $geshi->error():
> "<br /><strong>GeSHi Error:</strong> GeSHi could not find the language"

Change-Id: I1ca77733d4b6b5481c5db6aba9f6b7dda6803099
2015-06-08 16:05:46 +01:00
Gilles Dubuc 58c81408f5 Explicitly define module position
Style modules currently added through addModuleStyles default
to being in the head ("top" position). This is an unhealthy default,
since only critical styles that are needed at pageload should be
in the head. In order to be able to switch the default to "bottom",
existing module positions have to be defined explicitly.

Bug: T97410
Change-Id: Ie120a781ac1950abd7963d6f722aa316b5542b51
2015-05-29 15:28:06 +00:00
Kunal Mehta b4853c7633 Revert "Revert "Add extension.json, empty PHP entry point""
Try #2. Our last attempt loaded $wgGeSHiSupportedLanguages late, and
would override anything if it was already set. We still load it late, but
only if it is not already set.

This reverts commit 033ca20746.

Bug: T88063
Change-Id: Iae0806e06a95b2d8932b3d9e078e6135dd6750a3
2015-05-20 14:36:46 -07:00
Legoktm 033ca20746 Revert "Add extension.json, empty PHP entry point"
This reverts commit 72d1e92263.

Change-Id: I47eab2842f195bf302de0ae3bf15beff1b48290f
2015-05-20 21:21:11 +00:00
Derk-Jan Hartman 72d1e92263 Add extension.json, empty PHP entry point
Bug: T88063
Change-Id: I42afa0bf81f4f89b81a11bcafcb8e4c032a2d173
2015-05-19 17:05:43 -07:00
Brad Jorsch 3fe85b9313 Add api-pretty-content class when pretty-printing API output
The API has wrapped its pretty-printed output since Id9cdf102. Apply an
appropriate class to preserve this now that GeSHi is handling it.

Unfortunately GeSHi itself doesn't support adding more than one arbitrary class
to the <pre> (and we're already using that), so we have to add it in a
post-processing step.

Bug: T88742
Change-Id: I38e41db5c341fe06ff825c82d5a9cd4810b7cc24
2015-02-27 19:12:36 -05:00
Chad Horohoe c8297429a6 Remove obvious function-level profiling
Change-Id: Idd5f9178987000df892dde70c11686ddd9306f10
2015-02-10 13:29:40 -08:00
paladox 966ed89270 Set Geshi version in main php file
The version was set from the ExtensionTypes hook (which runs only on
Special:Version). WikiApiary and other API consumers were unable to
detect the version.

This is an amended resubmission of the reverted d69ae1f3ac, in which
the constant was declared twice.

Change include_once to require_once for langs file (follows-up 168e1296).

Bug: T75666
Change-Id: I836e0df942a066d80255c1b68472e7ee58124357
2015-01-05 12:39:45 +00:00
Ori Livneh 1369e4b813 Initialise in getSupportedLanguages()
Change-Id: Ic155c54ef9fe0dac3a3c09e563828ccb0dcaf481
2014-11-19 12:05:20 -08:00
Reedy a03277741a Revert "Fix tracking for version"
This reverts commit d69ae1f3ac.

Change-Id: Ic724f6fe1b1cbc969ac195762bdf517e129cd9dd
2014-11-19 16:29:52 +00:00
Ori Livneh 168e1296db Avoid file glob in SyntaxHighlight_GeSHi::getSupportedLanguages()
Store the list of supported languages in SyntaxHighlight_GeSHi.langs.php, which
is auto-generated via a maintenance script, updateLanguageList.php.

Change-Id: Ie0be7c42fa6716555c3e03e3f28734d7e0302664
2014-11-18 16:50:59 -08:00
paladox d69ae1f3ac Fix tracking for version
* wikiapiary does not detect the version so this way fixes the issue.

Note this has been tested.

Change-Id: I2ad43801cc71285cad8576657c5dc5e4668a37b3
2014-11-14 21:36:42 +00:00
Kunal Mehta 53e1b42012 Check $wgUseSiteCss inside ResourceLoaderGeSHiLocalModule
This way the module is always loaded, but will be empty or not
depending on the config setting.

Change-Id: Id4f18c58f62c530fbf7d81ff0880281684296b7d
2014-10-13 16:35:49 -07:00
Brad Jorsch 8dde10991c Implement 'ApiFormatHighlight' hook
Core change I04b1a3842 adds a hook to allow extensions to
syntax-highlight the pretty-printed output from the API.

Change-Id: If0413a1d922ff8a47afc355e0a2cc276cf54b400
2014-10-13 16:22:59 -07:00
Kunal Mehta da41c89fb7 Fix incorrect casing of "ContentHandler" class name
Change-Id: I502c4d2f401f655153976ddce5c3d08e469b050f
2014-09-14 21:50:20 -07:00
Bartosz Dziewoński 9d69259b13 Parse page content using the standard parser first for link tables
We could do this using TextContent::fillParserOutput(), but alas it is
'protected', so we have to duplicate a tiny bit of code from there.

Bug: 68757
Change-Id: I7d98fa0f97fb195d23caa3d7448a15c3bbe430ca
2014-07-30 21:09:05 +02:00