Debian Codegraph @

maintenance/CheckSignatures.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22 use MediaWiki\Parser\ParserOptions;
23
24 /**
25 * Maintenance script to list users with invalid signatures.
26 *
27 * @ingroup Maintenance
28 */
29 class CheckSignatures extends Maintenance {
30
31 public function __construct() {
32 parent::__construct();
33 $this->addDescription( 'List users with invalid signatures' );
34 $this->setBatchSize( 1000 );
35 }
36
37 public function execute() {
38 $dbr = $this->getReplicaDB();
39 $userFactory = $this->getServiceContainer()->getUserIdentityLookup();
40 $userOptions = $this->getServiceContainer()->getUserOptionsLookup();
41 $signatureValidatorFactory = $this->getServiceContainer()->getSignatureValidatorFactory();
42 $contentLanguage = $this->getServiceContainer()->getContentLanguage();
43
44 $count = 0;
45 $maxUserId = 0;
46 do {
47 // List users who may have a signature that needs validation
48 $res = $dbr->newSelectQueryBuilder()
49 ->from( 'user_properties' )
50 ->select( 'up_user' )
51 ->where( [ 'up_property' => 'fancysig' ] )
52 ->andWhere( $dbr->expr( 'up_user', '>', $maxUserId ) )
53 ->orderBy( [ 'up_property', 'up_user' ] )
54 ->limit( $this->getBatchSize() )
55 ->caller( __METHOD__ )
56 ->fetchResultSet();
57
58 foreach ( $res as $row ) {
59 // Double-check effective preferences and check validation
60 $user = $userFactory->getUserIdentityByUserId( $row->up_user );
61 if ( !$user ) {
62 continue;
63 }
64 $signature = $userOptions->getOption( $user, 'nickname' );
65 $useFancySig = $userOptions->getBoolOption( $user, 'fancysig' );
66 if ( $useFancySig && $signature !== '' ) {
67 $parserOpts = new ParserOptions( $user, $contentLanguage );
68 $validator = $signatureValidatorFactory->newSignatureValidator( $user, null, $parserOpts );
69 $signatureErrors = $validator->validateSignature( $signature );
70 if ( $signatureErrors ) {
71 $count++;
72 $this->output( $user->getName() . "\n" );
73 }
74 }
75
76 $maxUserId = $row->up_user;
77 }
78 } while ( $res->numRows() );
79
80 $this->output( "-- $count invalid signatures --\n" );
81 }
82 }
83
84 // @codeCoverageIgnoreStart
85 $maintClass = CheckSignatures::class;
86 // @codeCoverageIgnoreEnd