MediaWiki  master
deleteAutoPatrolLogsTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
12 
13  public function getMaintenanceClass() {
15  }
16 
17  public function setUp() {
18  parent::setUp();
19  $this->tablesUsed = [ 'logging' ];
20 
21  $this->cleanLoggingTable();
22  $this->insertLoggingData();
23  }
24 
25  private function cleanLoggingTable() {
26  wfGetDB( DB_MASTER )->delete( 'logging', '*' );
27  }
28 
29  private function insertLoggingData() {
30  $dbw = wfGetDB( DB_MASTER );
31  $logs = [];
32 
33  $comment = \MediaWiki\MediaWikiServices::getInstance()->getCommentStore()
34  ->createComment( $dbw, '' );
35 
36  // Manual patrolling
37  $logs[] = [
38  'log_type' => 'patrol',
39  'log_action' => 'patrol',
40  'log_user' => 7251,
41  'log_params' => '',
42  'log_timestamp' => $dbw->timestamp( '20041223210426' ),
43  'log_namespace' => NS_MAIN,
44  'log_title' => 'DeleteAutoPatrolLogs',
45  'log_comment_id' => $comment->id,
46  ];
47 
48  // Autopatrol #1
49  $logs[] = [
50  'log_type' => 'patrol',
51  'log_action' => 'autopatrol',
52  'log_user' => 7252,
53  'log_params' => '',
54  'log_timestamp' => $dbw->timestamp( '20051223210426' ),
55  'log_namespace' => NS_MAIN,
56  'log_title' => 'DeleteAutoPatrolLogs',
57  'log_comment_id' => $comment->id,
58  ];
59 
60  // Block
61  $logs[] = [
62  'log_type' => 'block',
63  'log_action' => 'block',
64  'log_user' => 7253,
65  'log_params' => '',
66  'log_timestamp' => $dbw->timestamp( '20061223210426' ),
67  'log_namespace' => NS_MAIN,
68  'log_title' => 'DeleteAutoPatrolLogs',
69  'log_comment_id' => $comment->id,
70  ];
71 
72  // Very old/ invalid patrol
73  $logs[] = [
74  'log_type' => 'patrol',
75  'log_action' => 'patrol',
76  'log_user' => 7253,
77  'log_params' => 'nanana',
78  'log_timestamp' => $dbw->timestamp( '20061223210426' ),
79  'log_namespace' => NS_MAIN,
80  'log_title' => 'DeleteAutoPatrolLogs',
81  'log_comment_id' => $comment->id,
82  ];
83 
84  // Autopatrol #2
85  $logs[] = [
86  'log_type' => 'patrol',
87  'log_action' => 'autopatrol',
88  'log_user' => 7254,
89  'log_params' => '',
90  'log_timestamp' => $dbw->timestamp( '20071223210426' ),
91  'log_namespace' => NS_MAIN,
92  'log_title' => 'DeleteAutoPatrolLogs',
93  'log_comment_id' => $comment->id,
94  ];
95 
96  // Autopatrol #3 old way
97  $logs[] = [
98  'log_type' => 'patrol',
99  'log_action' => 'patrol',
100  'log_user' => 7255,
101  'log_params' => serialize( [ '6::auto' => true ] ),
102  'log_timestamp' => $dbw->timestamp( '20081223210426' ),
103  'log_namespace' => NS_MAIN,
104  'log_title' => 'DeleteAutoPatrolLogs',
105  'log_comment_id' => $comment->id,
106  ];
107 
108  // Manual patrol #2 old way
109  $logs[] = [
110  'log_type' => 'patrol',
111  'log_action' => 'patrol',
112  'log_user' => 7256,
113  'log_params' => serialize( [ '6::auto' => false ] ),
114  'log_timestamp' => $dbw->timestamp( '20091223210426' ),
115  'log_namespace' => NS_MAIN,
116  'log_title' => 'DeleteAutoPatrolLogs',
117  'log_comment_id' => $comment->id,
118  ];
119 
120  // Autopatrol #4 very old way
121  $logs[] = [
122  'log_type' => 'patrol',
123  'log_action' => 'patrol',
124  'log_user' => 7257,
125  'log_params' => "9227851\n0\n1",
126  'log_timestamp' => $dbw->timestamp( '20081223210426' ),
127  'log_namespace' => NS_MAIN,
128  'log_title' => 'DeleteAutoPatrolLogs',
129  'log_comment_id' => $comment->id,
130  ];
131 
132  // Manual patrol #3 very old way
133  $logs[] = [
134  'log_type' => 'patrol',
135  'log_action' => 'patrol',
136  'log_user' => 7258,
137  'log_params' => "9227851\n0\n0",
138  'log_timestamp' => $dbw->timestamp( '20091223210426' ),
139  'log_namespace' => NS_MAIN,
140  'log_title' => 'DeleteAutoPatrolLogs',
141  'log_comment_id' => $comment->id,
142  ];
143 
144  $dbw->insert( 'logging', $logs );
145  }
146 
147  public function runProvider() {
148  $allRows = [
149  (object)[
150  'log_type' => 'patrol',
151  'log_action' => 'patrol',
152  'log_user' => '7251',
153  ],
154  (object)[
155  'log_type' => 'patrol',
156  'log_action' => 'autopatrol',
157  'log_user' => '7252',
158  ],
159  (object)[
160  'log_type' => 'block',
161  'log_action' => 'block',
162  'log_user' => '7253',
163  ],
164  (object)[
165  'log_type' => 'patrol',
166  'log_action' => 'patrol',
167  'log_user' => '7253',
168  ],
169  (object)[
170  'log_type' => 'patrol',
171  'log_action' => 'autopatrol',
172  'log_user' => '7254',
173  ],
174  (object)[
175  'log_type' => 'patrol',
176  'log_action' => 'patrol',
177  'log_user' => '7255',
178  ],
179  (object)[
180  'log_type' => 'patrol',
181  'log_action' => 'patrol',
182  'log_user' => '7256',
183  ],
184  (object)[
185  'log_type' => 'patrol',
186  'log_action' => 'patrol',
187  'log_user' => '7257',
188  ],
189  (object)[
190  'log_type' => 'patrol',
191  'log_action' => 'patrol',
192  'log_user' => '7258',
193  ],
194  ];
195 
196  $cases = [
197  'dry run' => [
198  $allRows,
199  [ '--sleep', '0', '--dry-run', '-q' ]
200  ],
201  'basic run' => [
202  [
203  $allRows[0],
204  $allRows[2],
205  $allRows[3],
206  $allRows[5],
207  $allRows[6],
208  $allRows[7],
209  $allRows[8],
210  ],
211  [ '--sleep', '0', '-q' ]
212  ],
213  'run with before' => [
214  [
215  $allRows[0],
216  $allRows[2],
217  $allRows[3],
218  $allRows[4],
219  $allRows[5],
220  $allRows[6],
221  $allRows[7],
222  $allRows[8],
223  ],
224  [ '--sleep', '0', '--before', '20060123210426', '-q' ]
225  ],
226  'run with check-old' => [
227  [
228  $allRows[0],
229  $allRows[1],
230  $allRows[2],
231  $allRows[3],
232  $allRows[4],
233  $allRows[6],
234  $allRows[8],
235  ],
236  [ '--sleep', '0', '--check-old', '-q' ]
237  ],
238  ];
239 
240  foreach ( $cases as $key => $case ) {
241  yield $key . '-batch-size-1' => [
242  $case[0],
243  array_merge( $case[1], [ '--batch-size', '1' ] )
244  ];
245  yield $key . '-batch-size-5' => [
246  $case[0],
247  array_merge( $case[1], [ '--batch-size', '5' ] )
248  ];
249  yield $key . '-batch-size-1000' => [
250  $case[0],
251  array_merge( $case[1], [ '--batch-size', '1000' ] )
252  ];
253  }
254  }
255 
259  public function testRun( $expected, $args ) {
260  // FIXME: fails under postgres
261  $this->markTestSkippedIfDbType( 'postgres' );
262 
263  $this->maintenance->loadWithArgv( $args );
264 
265  $this->maintenance->execute();
266 
267  $remainingLogs = wfGetDB( DB_REPLICA )->select(
268  [ 'logging' ],
269  [ 'log_type', 'log_action', 'log_user' ],
270  [],
271  __METHOD__,
272  [ 'ORDER BY' => 'log_id' ]
273  );
274 
275  $this->assertEquals( $expected, iterator_to_array( $remainingLogs, false ) );
276  }
277 
278  public function testFromId() {
279  $fromId = wfGetDB( DB_REPLICA )->selectField(
280  'logging',
281  'log_id',
282  [ 'log_params' => 'nanana' ]
283  );
284 
285  $this->maintenance->loadWithArgv( [ '--sleep', '0', '--from-id', strval( $fromId ), '-q' ] );
286 
287  $this->maintenance->execute();
288 
289  $remainingLogs = wfGetDB( DB_REPLICA )->select(
290  [ 'logging' ],
291  [ 'log_type', 'log_action', 'log_user' ],
292  [],
293  __METHOD__,
294  [ 'ORDER BY' => 'log_id' ]
295  );
296 
297  $deleted = [
298  'log_type' => 'patrol',
299  'log_action' => 'autopatrol',
300  'log_user' => '7254',
301  ];
302  $notDeleted = [
303  'log_type' => 'patrol',
304  'log_action' => 'autopatrol',
305  'log_user' => '7252',
306  ];
307 
308  $remainingLogs = array_map(
309  function ( $val ) {
310  return (array)$val;
311  },
312  iterator_to_array( $remainingLogs, false )
313  );
314 
315  $this->assertNotContains( $deleted, $remainingLogs );
316  $this->assertContains( $notDeleted, $remainingLogs );
317  }
318 
319 }
const NS_MAIN
Definition: Defines.php:60
serialize()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
markTestSkippedIfDbType( $type)
Skip the test if using the specified database type.
const DB_MASTER
Definition: defines.php:26
if( $line===false) $args
Definition: cdb.php:64
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
const DB_REPLICA
Definition: defines.php:25