Commit graph

42 commits

Author SHA1 Message Date
Ammarpad
1b203d0558 TemplateParser: Remove support for deprecated cache-disabling parameter
Passing EmptyBagOStuff will still have same effect, but the deprecated
way of specifying so with boolean is now removed.

Change-Id: I8ec0f54e94b71af9256813f333e615bca1bb8444
2021-09-17 19:48:54 +00:00
Timo Tijhof
e4b7341fb6 TemplateParser: Inject BagOStuff and deprecate '$forceRecompile'
* Injecting the cache object makes the class easier to test.

* The $forceRecompile parameter was not used anywhere, not tested,
  and only mentioned in one README (fixed in I13fba7314).
  Based on past experience I generally expect that when bypassing a
  cache, it is neither read from nor written to. Such feature might
  be reached out to when testing something, in which case it should
  not be stored.

  If this was intended as a way to purge it in prod via eval.php,
  we generally don't provide dedicated purging commands in code.
  We either call BagOStuff or Memc/Apcu directly, or invalidate the
  underlying data (e.g. touch the template file, or bump the cache
  version).

  To use TemplateParser without caching, one can still pass
  'new EmptyBagOStuff' instead if needed.

* Set the $ttl parameter in the set() call.
  It is generally an anti-pattern to store something without
  any TTL as that means it can needlessly compete for space
  even after further deployments have changed the key or stopped
  using a particular key. For now I used a liberal default
  of 1 week.

Change-Id: Ic84b996b46c72e9dc33eed2645204fc2d6c5240c
2020-03-23 20:09:05 +00:00
Sam Smith
d4be8b9c08 TemplateParser: Include template dir in cache key
Template names aren't expected to be globally unique. Template paths are
by construction.

Include the template directory in the cache key in order to avoid the
cache keys of ambiguosly-named templates - e.g. index.mustache -
overriding one another.

Bug: T113095
Bug: T248010
Change-Id: I3196967ec2a7a5cec409a0c7ce4471a7d8773978
2020-03-18 21:57:34 +00:00
Sam Smith
52f50cd657 TemplateParser: Invalidate cache if partial changes
Changes:

- Update TemplateParser::compile to return all files read during the
  compilation of the template and the hash of those files

- Tweak the cache invalidation logic in TemplateParser::getTemplate
  accordingly. This is made trivial due to the friendly design of the
  FileContentsHasher::getFileContentsHash.

Bug: T113095
Change-Id: I948fdaecf720d7d16c5ccabb2d7f01b5cbf27c90
2020-03-03 11:14:38 +00:00
Sam Smith
5607158ba7 TemplateParser: Make cache value include metadata
Update TemplateParser::compile to return the PHP code and metadata. The
metadata includes details about the compilation that can be used to
validate that PHP code fetched from a cache is fresh without having to
recompile the original template.

TemplateParser::getTemplate did have such an invalidation mechanism but:

- It stored the hash of the template file in the key

- It stored the hash used for checking the integrity of the cached PHP
  code in the first 64 bytes of the value

This tight coupling between the compilation result and the contents of
the cache key/value made changing either the compilation or cache
invalidation steps difficult.

After this change, arbitrary metadata can be added or removed from the
compilation result.

Changes:

- Move all filename generation and reading to TemplateParser::compile
  and align its signature with ::getTemplate

- Update TemplateParser::compile to generate the file hash with
  FileContentsHasher::getFileContentsHash, which is used by
  Resource Loader to generate file hashes, and return it as part of its
  result

- Update TemplateParser::getTemplate to store the integrity hash in the
  compilation result prior to caching it

- Bump the cache key major version as both the key and value have
  changed

Note well that TemplateParser isn't expected to be subclassed, and
::getTemplate and ::compile are protected members and are therefore out
of the scope of MediaWiki's deprecation policy.

Bug: T113095
Change-Id: Ifb5e122c6ae238fd300cd60f2b1ab33b7ece8e71
2020-03-02 21:21:43 +00:00
Sam Smith
56abd837ba TemplateParser: Document and normalize exceptions
TemplateParser::getTemplate and ::compile both throw exceptions in
certain circumstances. Document those exceptions/circumstances for other
developers.

Also, use the same language and message format in all exceptions thrown.

Bug: T113095
Change-Id: I73fedd2bd66f5f938ac95edfbd78067a776cdff8
2020-02-27 13:13:10 +00:00
Catrope
5368decf99 Revert "TemplateParser: Use LightnCandy::prepare() instead eval()"
This reverts commit 82faff75ee.

Reason for revert: Was not actually necessary to make lightncandy work

Also remove compileForEval(): stripping <?php tags is no longer needed
because the new version of lightncandy doesn't emit them.

Change-Id: I87c3f4842baaf400b532ba4843d66b22bc48e6fb
2020-01-29 23:15:01 +00:00
Roan Kattouw
821d8c8e30 TemplateParser: Change cache key and version cache
Needed because we upgraded to a newer version of lightncandy, which
contains breaking changes. Cached compilations from the old versions
won't work with the new version.

Add a cache version variable so that we can bump this more easily in the
future when upgrading lightncandy.

Follows-up 7c4e9f790a

Change-Id: I7c05019df7eb044cff7bb8e67b2835970e1f384d
2020-01-14 21:29:10 -08:00
Roan Kattouw
82faff75ee TemplateParser: Use LightnCandy::prepare() instead eval()
Follows-up 7c4e9f790a

Change-Id: I82b6ea560165aee19bb1ddb4b355a14b83e59e1c
2020-01-14 20:49:58 -08:00
Reedy
7c4e9f790a Update zordius/lightncandy v0.23 to v1.2.4
https://github.com/zordius/lightncandy/compare/v0.23...v1.2.4
https://github.com/zordius/lightncandy/releases/tag/v0.89
https://github.com/zordius/lightncandy/releases/tag/v0.90
https://github.com/zordius/lightncandy/releases/tag/v0.91
https://github.com/zordius/lightncandy/releases/tag/v0.92
https://github.com/zordius/lightncandy/releases/tag/v0.93
https://github.com/zordius/lightncandy/releases/tag/v0.94
https://github.com/zordius/lightncandy/releases/tag/v0.95
https://github.com/zordius/lightncandy/releases/tag/v1.0.0
https://github.com/zordius/lightncandy/releases/tag/v1.0.1
https://github.com/zordius/lightncandy/releases/tag/v1.0.2
https://github.com/zordius/lightncandy/releases/tag/v1.0.3
https://github.com/zordius/lightncandy/releases/tag/v1.1.0
https://github.com/zordius/lightncandy/releases/tag/v1.2.0
https://github.com/zordius/lightncandy/releases/tag/v1.2.1
https://github.com/zordius/lightncandy/releases/tag/v1.2.2
https://github.com/zordius/lightncandy/releases/tag/v1.2.3
https://github.com/zordius/lightncandy/releases/tag/v1.2.4

Bug: T121947
Change-Id: Ida9e12e759cdc4508238c32153540000485a6561
Depends-On: Id1a642f5a32dfd5655c9c29094e3f3ed6077e10a
Depends-On: If448d8155970d7cb4df2b9194dce8094eb887b86
2020-01-14 19:26:49 -08:00
Reedy
ab8b7781fe Remove class_exists( 'LightnCandy' )
We can rely on composer making sure the library exists

Change-Id: I3dd12b4b4b6abc36c700ebb8a3bc0ac2d820a3fe
2019-10-09 01:17:11 +01:00
Thiemo Kreuz
8a4e15db7f Add missing newline between <?php and namespace/use section
The rest of the codebase is using this code formatting standard.

Change-Id: I4d2ba61757a7e28d40096d9dc5915005c340d4f2
2019-06-03 13:36:48 +00:00
Derick Alangi
6778fede1b TemplateParser: Use operator short cut where necessary
Change-Id: I7f9dbe54cec876863920155ccd7cd735a3101481
2019-04-11 21:48:07 +01:00
Max Semenik
61c539126f Convert a few call_user_func*() calls to native PHP syntax
Change-Id: I54d94f4369eb4fa0b0ebe892a1d6cc57b2bdb1f9
2019-03-07 19:25:40 -08:00
Roan Kattouw
3c06d76521 TemplateParser: Pass FLAG_MUSTACHELOOKUP to enable parent context access
This allows variables defined in an outer context to be used in inner
contexts. For example:

<h2>{{foo}}</h2>
<ul>
{{#things}}
    <!-- bar is a property of each thing, foo is an outer variable -->
    <li>{{foo}} is a {{bar}}</li>
{{/things}}
</ul>

Bug: T203209
Change-Id: Ib0ae0fb0b4be6b161f548c79db6fb6f4b831f7c1
2018-09-04 12:39:34 -07:00
petarpetkovic
3f60fb5ad1 Remove duplicated article usages
Bug: T201491
Change-Id: I72b1e7609b49b9bf182c0872f8b780d9e9e08695
2018-08-09 10:29:38 +02:00
Brian Wolff
89b21847e1 Start working on phan-taint-check warnings. Fix minor escaping issues.
This fixes 26 of the phan-taint-check warnings on MW core. Some
are outright fixed, others are false positives that were suppressed.

This really only covers some of the easy ones. There are still
314 warnings to go.

Change-Id: I30463bc3a09fd4324d190de8533f51784764dd3a
2018-07-12 03:26:59 +00:00
Umherirrender
130ec2523d Fix PhanTypeMismatchDeclaredParam
Auto fix MediaWiki.Commenting.FunctionComment.DefaultNullTypeParam sniff

Change-Id: I865323fd0295aabd06f3e3c75e0e5043fb31069e
2018-07-07 00:34:30 +00:00
Gergő Tisza
49350108bf Enable recursive partials in TemplateParser
Recursive partials are the only way to handle tree-like structures
such as nested lists. Allow setting FLAG_RUNTIMEPARTIAL in LightnCandy
so they can be used.

Since this has a slight performance impact (makes partial invocations
evaluation-time functions calls instead of compilation-time transclusions)
make it optional.

Change-Id: Ie37105a9f1ff92e1a79bfcd9f8578965e3d347f0
2017-09-10 05:51:07 +00:00
Kunal Mehta
2d64890f3f TemplateParser: Improve cache integrity check failure scenario
Previously, if the cache integrity check failed then it would emit a
warning but then continue to use the code. The integrity check could
genuinely fail if the secret key was changed, if the cache was
truncated, or other edge case scenarios.

Now TemplateParser will recompile if the cache fails the integrity
check, and then update the cache with the newly compiled version.

Bug: T163154
Change-Id: I9a6c8d528f84cfbabf402cfaf6468c162fab1f15
2017-05-16 15:40:08 -07:00
Brian Wolff
69f3f4a923 rv accidental inclusion of debug code in TemplateParser
Follow up 73e08353fb

Change-Id: I59bcff4334be61977a617a4a8726ae90165530e7
2017-03-15 04:53:39 +00:00
Brian Wolff
73e08353fb Better path traversal prevention in TemplateParser.
In practise this probably doesn't matter, since template names
are not user controlled, and php isn't stupid enough to fall for
tricks with nulls (afaict). Nonetheless, the code from Title is
only meant to prevent url traversal, it is not meant to prevent
file system path traversal.

Change-Id: Id690576326d03744acc8fbbe78f4b7a4b4c04d7e
2017-03-14 18:43:11 +00:00
addshore
b12086b618 Get ConfigFactory & MainConfig from MediaWikiServices
Change-Id: Iafdd7e00747060572463ffb05aae4543f3a06163
2016-11-23 00:12:38 +00:00
Amir Sarabadani
9850c542c6 Clean up array() syntax in docs, part VII
Last part

Change-Id: I38f015e2122ef4fd2d2141718bd889794c29f06c
2016-09-27 06:53:25 +03:30
Ricordisamoa
1617e7822e Always use 'bool' instead of 'boolean' in Doxygen tags
Just like commit f86a5590aa

Change-Id: Ic9d08bca6524d6bb4baf5170c081ad0f3d738e28
2016-03-24 09:44:09 +01:00
Kunal Mehta
6e9b4f0e9c Convert all array() syntax to []
Per wikitech-l consensus:
 https://lists.wikimedia.org/pipermail/wikitech-l/2016-February/084821.html

Notes:
* Disabled CallTimePassByReference due to false positives (T127163)

Change-Id: I2c8ce713ce6600a0bb7bf67537c87044c7a45c4b
2016-02-17 01:33:00 -08:00
Aaron Schulz
17c91ad610 Replace newAccelerator() with getLocalServerInstance()
The name is clearer and more consistent, with simpler arguments.

Change-Id: I7205a99ce033e8b086a52cd02c8a721e99c84b1e
2015-11-02 21:39:08 +00:00
Timo Tijhof
f0d53506cf TemplateParser: Use context makeKey() instead of wfMemcKey()
Also:
* Use ternary shorthand.
* Remove verbose comment about APC fallback. APC always requires
  a fallback and is enforced by the method being called. Stating
  the obvious is confusing here.

Change-Id: Ie5cb3bdc60600806b01b57f1f1b352b981818b0d
2015-10-22 00:55:10 +01:00
Amir E. Aharoni
9f4c895190 Make lines short to pass phpcs in files under includes/
Bug: T102614
Change-Id: I33ac3f4bb8116020ccda5d64fdd2924e78810546
2015-09-27 16:10:59 +00:00
Timo Tijhof
79ce51d1c9 objectcache: Make first parameter of newAccelerator optional
Makes it more convenient to use.

Change-Id: I1e11f7a759bd2816e47d1c2453cbe39b8f44b2f0
2015-08-24 23:43:19 +02:00
S Page
c235e0717e Support mustache partials in server-side templates
This is sufficient to make https://gerrit.wikimedia.org/r/#/c/223165/
work.  It hardcodes .mustache as the extension, but so does
existing getTemplateFilename().

Bug: T97188
Change-Id: Id588ae9b43b13fcf35ebd285c826dd502ac424ec
2015-07-06 20:13:45 -07:00
umherirrender
2ce8768495 Avoid assignment in return in TemplateParser.php
Hard to read and may misread as boolean return

Change-Id: If6bfdbed7e647dde51db62ae6901874a83682d9e
2015-06-17 22:16:36 +02:00
Kunal Mehta
a9ae91c362 TemplateParser: Use ObjectCache::newAccelerator() with fallback set
Change-Id: I202192f6bf509068941e9490d02a11bdedecd872
2015-03-23 03:34:57 +00:00
Kunal Mehta
2f88829e1b TemplateParser: make most functions protected, only expose processTemplate()
All of the other functions expose internal implementation details, which no
external caller should ever need. In fact, no external caller does use these
functions directly.

The TemplateParser::compile() tests were removed as they're simply just
checking LightnCandy functionality, which is something the library should be
doing.

Change-Id: If9003d40315e0e5aa361c174b764b799e3b88c34
2015-03-22 20:31:59 -07:00
Timo Tijhof
cfcaa33fcc TemplateParser: Don't fatal on cache misses
Also add regression test, and coverage for more methods.

Was trying to eval the code which had the hmac integrity check in front of it,
which causes a syntax error in valid PHP code.

Follows-up db1866da4, 50c50bea2e.

Bug: T93436
Bug: T93511
Change-Id: Ie90074e4885de7340e53f59fdd479f5384b5eac6
2015-03-23 01:38:56 +00:00
kaldari
2a8992a8b9 Make sure template function is callable from processTemplate()
Change-Id: Id9df4062754b02e7217779bc6c0ba42d457e9942
2015-03-20 15:06:59 -07:00
kaldari
a2def8064d Replacing generic Exception with Exception subclasses
Improves ability to debug.

Change-Id: I21a51fc5b4f185a01ba4706bd5a853c2974057dd
2015-03-12 14:31:32 -07:00
kaldari
50c50bea2e If no secret key is available, don't try to use cache
In the unlikely event that no secret key is available, we shouldn't
rely on the cache at all in TemplateParser.

Adding new compileForEval() function and and moving eval() outside
of if statement to prevent code duplication.

Also, if the template fails integrity check, generate a notice
instead of throwing an exception in case we change the secret key.

Change-Id: Id44fdcc9533fc8a9c77e84fcebaa064f602477c6
2015-03-04 09:27:23 -08:00
kaldari
db1866da41 Make sure hash_hmac() always works even if no secret key
Otherwise a fatal error is possible.

Change-Id: Icda96bac3e75f424be068cdad30ad618b503a8e1
2015-02-27 15:36:33 -08:00
kaldari
13aff8aa7c Adding sample code to TemplateParser::processTemplate() docs
Also reverting punctuation that was accidently changed in
change I28cd13d4d.

Change-Id: I1283fbfbaf8cefbbc951599ee286d923b9dedf65
2015-02-20 16:40:31 -08:00
kaldari
7040aeffca Some clean-up of TemplateParser
* Replacing global var with Config
* Fixing spacing
* Adding @since tag

Change-Id: I8b0a35116eef6ecead16e03a3c408081ee500aa6
2015-02-20 10:50:17 -08:00
kaldari
2ec0272218 Adding TemplateParser class providing interface to Mustache templates
The TemplateParser class provides a server-side interface to cachable
dynamically-compiled Mustache templates. It currently uses the
lightncandy library to do compilation (which is already included in
the vendor repo).

Also converting NoLocalSettings.php to use it as a proof-of-concept.

Bug: T379
Change-Id: I28cd13d4d1132bd386e2ae2f4f0d1dd88ad9162b
2015-02-19 17:41:45 -08:00