Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
52.94% |
18 / 34 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| GuidedTourLauncher | |
52.94% |
18 / 34 |
|
20.00% |
1 / 5 |
17.44 | |
0.00% |
0 / 1 |
| getNewState | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| getNewCookie | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| launchTour | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| onMakeGlobalVariablesScript | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| launchTourByCookie | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\GuidedTour; |
| 4 | |
| 5 | use MediaWiki\Json\FormatJson; |
| 6 | use MediaWiki\Output\OutputPage; |
| 7 | |
| 8 | /** |
| 9 | * Allows server-side launching of tours (without the URL parameter). |
| 10 | */ |
| 11 | class GuidedTourLauncher { |
| 12 | /** |
| 13 | * State used to tell the client to directly launch tours using a client-side $wg |
| 14 | * |
| 15 | * @var array|null |
| 16 | */ |
| 17 | protected static $directLaunchState = null; |
| 18 | |
| 19 | // This matches the format used on the client-side (e.g. |
| 20 | // mw.guidedTour.internal.getInitialUserStateObject, |
| 21 | // mw.guidedTour.launchTourFromUserState, etc. |
| 22 | |
| 23 | /** |
| 24 | * Get new state from old state. The state describes the user's progress |
| 25 | * in the tour, and which step they are expected to see next. |
| 26 | * |
| 27 | * @param array|null $oldState Previous state |
| 28 | * @param string $tourName Tour name |
| 29 | * @param string $step Step to start at |
| 30 | * @return array New state |
| 31 | */ |
| 32 | protected static function getNewState( $oldState, $tourName, $step ) { |
| 33 | $newState = $oldState; |
| 34 | |
| 35 | if ( $newState === null ) { |
| 36 | $newState = []; |
| 37 | } |
| 38 | |
| 39 | $newState = array_replace_recursive( $newState, [ |
| 40 | 'version' => 1, |
| 41 | 'tours' => [ |
| 42 | $tourName => [ |
| 43 | 'step' => $step, |
| 44 | ], |
| 45 | ], |
| 46 | ] ); |
| 47 | |
| 48 | return $newState; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Adds a tour to the cookie |
| 53 | * |
| 54 | * @param string|null $oldCookieValue Previous value of cookie |
| 55 | * @param string $tourName Tour name |
| 56 | * @param string $step Step to start at |
| 57 | * @return string Value of new cookie |
| 58 | */ |
| 59 | public static function getNewCookie( $oldCookieValue, $tourName, $step ) { |
| 60 | if ( $oldCookieValue == null ) { |
| 61 | $oldCookieValue = '{}'; |
| 62 | } |
| 63 | |
| 64 | $oldState = FormatJson::decode( $oldCookieValue, true ); |
| 65 | if ( $oldState === null ) { |
| 66 | $oldState = []; |
| 67 | } |
| 68 | |
| 69 | $newState = self::getNewState( $oldState, $tourName, $step ); |
| 70 | |
| 71 | return FormatJson::encode( $newState ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets a tour to auto-launch on this view |
| 76 | * |
| 77 | * @param OutputPage $out Page to render the tour on |
| 78 | * @param string $tourName Name of tour to launch |
| 79 | * @param string $step Step to navigate to |
| 80 | */ |
| 81 | public static function launchTour( OutputPage $out, $tourName, $step ) { |
| 82 | self::$directLaunchState = self::getNewState( |
| 83 | self::$directLaunchState, |
| 84 | $tourName, |
| 85 | $step |
| 86 | ); |
| 87 | |
| 88 | Hooks::addTour( $out, $tourName ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Export data to client-side via mw.config (for use by ext.guidedTour.lib). |
| 93 | * |
| 94 | * @param array &$vars Array of request-specific JavaScript config variables |
| 95 | * @param OutputPage $out |
| 96 | */ |
| 97 | public static function onMakeGlobalVariablesScript( array &$vars, OutputPage $out ) { |
| 98 | if ( self::$directLaunchState !== null ) { |
| 99 | $vars['wgGuidedTourLaunchState'] = self::$directLaunchState; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets a tour to auto-launch on this view using a cookie. |
| 105 | * |
| 106 | * @param OutputPage $out Page to render the tour on |
| 107 | * @param string $tourName Name of tour to launch |
| 108 | * @param string $step Step to navigate to |
| 109 | */ |
| 110 | public static function launchTourByCookie( OutputPage $out, $tourName, $step ) { |
| 111 | $request = $out->getRequest(); |
| 112 | $oldCookie = $request->getCookie( Hooks::COOKIE_NAME ); |
| 113 | $newCookie = self::getNewCookie( $oldCookie, $tourName, $step ); |
| 114 | $request->response()->setCookie( Hooks::COOKIE_NAME, $newCookie, 0, [ |
| 115 | 'httpOnly' => false, |
| 116 | ] ); |
| 117 | |
| 118 | Hooks::addTour( $out, $tourName ); |
| 119 | } |
| 120 | } |