Hashar has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/335374 )

Change subject: Extract hash mecanism to a standalone method
......................................................................

Extract hash mecanism to a standalone method

The hashing mecanism and cache invalidation via $wgTimelineFileBackend
is self contained. Move it to a new public method Timeline::hash().

Add tests for all cases.
Slightly document renderTimeline() while at it.

Bug: T138036
Change-Id: Ic8cb4e448323cf741eac0efd7e903e352c1a67d8
---
M Timeline.body.php
A tests/phpunit/Timeline_hash_Test.php
2 files changed, 71 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/timeline 
refs/changes/74/335374/1

diff --git a/Timeline.body.php b/Timeline.body.php
index 8cc9519..a69a744 100644
--- a/Timeline.body.php
+++ b/Timeline.body.php
@@ -14,6 +14,15 @@
        }
 
        /**
+        * Render timeline and save to file backend
+        *
+        * Files are saved to the file backend $wgTimelineFileBackend if set. 
Else
+        * default to FSFileBackend named 'timeline-backend'.
+        *
+        * The rendered timeline is saved in the file backend using @see hash() 
and
+        * will be reused if the hash match. You can invalidate the cache by
+        * setting the global variable $wgRenderHashAppend (default: '').
+        *
         * @param $timelinesrc string
         * @param $args array
         * @param Parser $parser
@@ -22,7 +31,7 @@
         * @return string HTML
         */
        public static function renderTimeline( $timelinesrc, array $args, 
$parser, $frame ) {
-               global $wgUploadDirectory, $wgUploadPath, $wgArticlePath, 
$wgTmpDirectory, $wgRenderHashAppend;
+               global $wgUploadDirectory, $wgUploadPath, $wgArticlePath, 
$wgTmpDirectory;
                global $wgTimelineFileBackend, $wgTimelineEpochTimestamp, 
$wgTimelinePerlCommand, $wgTimelineFile;
                global $wgTimelineFontFile, $wgTimelinePloticusCommand;
 
@@ -48,13 +57,7 @@
                        );
                }
 
-               // Get a hash of the plot data.
-               // $args must be checked, because the same source text may be 
used with
-               // with different args.
-               $hash = md5( $timelinesrc . implode( '', $args ) );
-               if ( $wgRenderHashAppend != '' ) {
-                       $hash = md5( $hash . $wgRenderHashAppend );
-               }
+               $hash = self::hash( $timelinesrc, $args );
 
                // Storage destination path (excluding file extension)
                $fname = 'mwstore://' . $backend->getName() . 
"/timeline-render/$hash";
@@ -203,6 +206,31 @@
        }
 
        /**
+        * Generate a hash of the plot data
+        *
+        * $args must be checked, because the same source text may be used with
+        * different arguments.
+        *
+        * Uses global $wgRenderHashAppend to salt / vary the hash. Will 
invalidate
+        * the cache as a side effect though old files will be left in the file
+        * backend.
+        *
+        * @param $timelinesrc string
+        * @param $args array
+        * @return string hash
+        */
+       public static function hash( $timelinesrc, array $args ) {
+               global $wgRenderHashAppend;
+
+               $hash = md5( $timelinesrc . implode( '', $args ) );
+               if ( $wgRenderHashAppend != '' ) {
+                       $hash = md5( $hash . $wgRenderHashAppend );
+               }
+
+               return $hash;
+       }
+
+       /**
         * Do a security check on the image map HTML
         * @param $html string
         * @return string HTML
diff --git a/tests/phpunit/Timeline_hash_Test.php 
b/tests/phpunit/Timeline_hash_Test.php
new file mode 100644
index 0000000..1f7c7c4
--- /dev/null
+++ b/tests/phpunit/Timeline_hash_Test.php
@@ -0,0 +1,35 @@
+<?php
+class TimelineHashTest extends MediaWikiTestCase {
+
+       /**
+        * @dataProvider provideHashCases
+        */
+       public function testHash( $expected, $timelinesrc, $args = [], 
$hashAppend ) {
+               $this->setMwGlobals( 'wgRenderHashAppend', $hashAppend );
+
+               $this->assertEquals(
+                       $expected,
+                       Timeline::hash( $timelinesrc, $args )
+               );
+       }
+
+       public static function provideHashCases() {
+               $NO_APPEND = '';
+
+               return [
+                       'no arguments' => [
+                               md5( 'hello' ), 'hello', [], $NO_APPEND ],
+                       'no arguments and $wgRenderHashAppend' => [
+                               md5( md5( 'hello' ) . 'World' ), 'hello', [], 
'World' ],
+                       'with two arguments' => [
+                               md5( 'helloarg1arg2' ), 'hello',
+                               [ 'arg1', 'arg2' ], $NO_APPEND
+                       ],
+                       'with two arguments and $wgRenderHashAppend' => [
+                               md5( md5( 'helloarg1arg2' ) . 'World' ), 
'hello',
+                               [ 'arg1', 'arg2' ], 'World'
+                       ],
+               ];
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/335374
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic8cb4e448323cf741eac0efd7e903e352c1a67d8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/timeline
Gerrit-Branch: master
Gerrit-Owner: Hashar <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to