|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + set_notification_timestamp.php |
| 5 | +
|
| 6 | + MediaWiki API Demos |
| 7 | + Demo of `Setnotificationtimestamp` module: Reset the notification status for the entire watchlist. |
| 8 | +
|
| 9 | + MIT license |
| 10 | +*/ |
| 11 | +$endPoint ="https://test.wikipedia.org/w/api.php"; |
| 12 | + |
| 13 | +$login_Token =getLoginToken();// Step 1 |
| 14 | +loginRequest($login_Token );// Step 2 |
| 15 | +$csrf_Token =getCSRFToken();// Step 3 |
| 16 | +setNotificationTimestamp($csrf_Token );// Step 4 |
| 17 | + |
| 18 | +// Step 1: GET request to fetch login token |
| 19 | +functiongetLoginToken() { |
| 20 | +global$endPoint; |
| 21 | + |
| 22 | +$params1 = [ |
| 23 | +"action" =>"query", |
| 24 | +"meta" =>"tokens", |
| 25 | +"type" =>"login", |
| 26 | +"format" =>"json" |
| 27 | +]; |
| 28 | + |
| 29 | +$url =$endPoint ."?" .http_build_query($params1 ); |
| 30 | + |
| 31 | +$ch =curl_init($url ); |
| 32 | +curl_setopt($ch,CURLOPT_RETURNTRANSFER,true ); |
| 33 | +curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt" ); |
| 34 | +curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt" ); |
| 35 | + |
| 36 | +$output =curl_exec($ch ); |
| 37 | +curl_close($ch ); |
| 38 | + |
| 39 | +$result =json_decode($output,true ); |
| 40 | +return$result["query"]["tokens"]["logintoken"]; |
| 41 | +} |
| 42 | + |
| 43 | +// Step 2: POST request to log in. Use of main account for login is not |
| 44 | +// supported. Obtain credentials via Special:BotPasswords |
| 45 | +// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword |
| 46 | +functionloginRequest($logintoken ) { |
| 47 | +global$endPoint; |
| 48 | + |
| 49 | +$params2 = [ |
| 50 | +"action" =>"login", |
| 51 | +"lgname" =>"bot_user_name", |
| 52 | +"lgpassword" =>"bot_password", |
| 53 | +"lgtoken" =>$logintoken, |
| 54 | +"format" =>"json" |
| 55 | +]; |
| 56 | + |
| 57 | +$ch =curl_init(); |
| 58 | + |
| 59 | +curl_setopt($ch,CURLOPT_URL,$endPoint ); |
| 60 | +curl_setopt($ch,CURLOPT_POST,true ); |
| 61 | +curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($params2 ) ); |
| 62 | +curl_setopt($ch,CURLOPT_RETURNTRANSFER,true ); |
| 63 | +curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt" ); |
| 64 | +curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt" ); |
| 65 | + |
| 66 | +$output =curl_exec($ch ); |
| 67 | +curl_close($ch ); |
| 68 | +} |
| 69 | + |
| 70 | +// Step 3: GET request to fetch CSRF token |
| 71 | +functiongetCSRFToken() { |
| 72 | +global$endPoint; |
| 73 | + |
| 74 | +$params3 = [ |
| 75 | +"action" =>"query", |
| 76 | +"meta" =>"tokens", |
| 77 | +"format" =>"json" |
| 78 | +]; |
| 79 | + |
| 80 | +$url =$endPoint ."?" .http_build_query($params3 ); |
| 81 | + |
| 82 | +$ch =curl_init($url ); |
| 83 | + |
| 84 | +curl_setopt($ch,CURLOPT_RETURNTRANSFER,true ); |
| 85 | +curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt" ); |
| 86 | +curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt" ); |
| 87 | + |
| 88 | +$output =curl_exec($ch ); |
| 89 | +curl_close($ch ); |
| 90 | + |
| 91 | +$result =json_decode($output,true ); |
| 92 | +return$result["query"]["tokens"]["csrftoken"]; |
| 93 | +} |
| 94 | + |
| 95 | +# Step 4: Send a POST request to reset the notification status for the entire watchlist. |
| 96 | +functionsetNotificationTimestamp($csrftoken ) { |
| 97 | +global$endPoint; |
| 98 | + |
| 99 | +$params4 = [ |
| 100 | +"action" =>"setnotificationtimestamp", |
| 101 | +"entirewatchlist" =>"", |
| 102 | +"format" =>"json", |
| 103 | +"token" =>$csrftoken |
| 104 | +]; |
| 105 | + |
| 106 | +$ch =curl_init(); |
| 107 | + |
| 108 | +curl_setopt($ch,CURLOPT_URL,$endPoint ); |
| 109 | +curl_setopt($ch,CURLOPT_POST,true ); |
| 110 | +curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($params4 ) ); |
| 111 | +curl_setopt($ch,CURLOPT_RETURNTRANSFER,true ); |
| 112 | +curl_setopt($ch,CURLOPT_COOKIEJAR,"cookie.txt" ); |
| 113 | +curl_setopt($ch,CURLOPT_COOKIEFILE,"cookie.txt" ); |
| 114 | + |
| 115 | +$response =curl_exec($ch); |
| 116 | +curl_close($ch); |
| 117 | + |
| 118 | +echo ($response); |
| 119 | +} |