MediaWiki master
AccidentalRecreationConstraint.php
Go to the documentation of this file.
1<?php
8
19
28
29 public function __construct(
30 private readonly IConnectionProvider $connectionProvider,
31 private readonly LogFormatterFactory $logFormatterFactory,
32 private readonly Title $title,
33 private readonly bool $allowRecreation,
34 private readonly ?string $startTime,
35 private readonly ?MessageSpecifier $submitButtonLabel,
36 ) {
37 }
38
39 public function checkConstraint(): PageEditStatus {
40 if ( $this->allowRecreation ) {
41 return PageEditStatus::newGood();
42 }
43 $deletion = $this->getDeletionSinceLastEdit();
44 if ( $deletion ) {
45 if ( $this->submitButtonLabel ) {
46 $logFormatter = $this->logFormatterFactory->newFromEntry( $deletion );
47 $username = $deletion->isDeleted( LogPage::DELETED_USER )
48 ? MessageValue::new( 'rev-deleted-user' )
49 : $deletion->getPerformerIdentity()->getName();
50 $commentHtml = $logFormatter->getComment();
51
52 return PageEditStatus::newGood( self::AS_ARTICLE_WAS_DELETED )
53 ->setOK( false )
54 ->warning(
55 $commentHtml === ''
56 ? 'edit-constraint-confirmrecreate-noreason'
57 : 'edit-constraint-confirmrecreate',
58 $username,
59 Message::rawParam( $commentHtml ),
60 $this->submitButtonLabel,
61 );
62 } else {
63 return PageEditStatus::newGood( self::AS_ARTICLE_WAS_DELETED )
64 ->setOK( false )
65 ->warning( 'deletedwhileediting' );
66 }
67 }
68 return PageEditStatus::newGood();
69 }
70
75 private function getDeletionSinceLastEdit(): ?DatabaseLogEntry {
76 if ( !$this->title->exists() && $this->title->hasDeletedEdits() ) {
77 $lastDelete = $this->getLastDelete();
78 if ( $lastDelete && $lastDelete->getTimestamp() > $this->startTime ) {
79 return $lastDelete;
80 }
81 }
82 return null;
83 }
84
89 private function getLastDelete(): ?DatabaseLogEntry {
90 $dbr = $this->connectionProvider->getReplicaDatabase();
91 $row = DatabaseLogEntry::newSelectQueryBuilder( $dbr )
92 ->where( [
93 'log_namespace' => $this->title->getNamespace(),
94 'log_title' => $this->title->getDBkey(),
95 'log_type' => 'delete',
96 'log_action' => 'delete',
97 ] )
98 ->orderBy( [ 'log_timestamp', 'log_id' ], SelectQueryBuilder::SORT_DESC )
99 ->caller( __METHOD__ )
100 ->fetchRow();
101
102 return $row ? DatabaseLogEntry::newFromRow( $row ) : null;
103 }
104
105}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
Make sure user doesn't accidentally recreate a page deleted after they started editing.
__construct(private readonly IConnectionProvider $connectionProvider, private readonly LogFormatterFactory $logFormatterFactory, private readonly Title $title, private readonly bool $allowRecreation, private readonly ?string $startTime, private readonly ?MessageSpecifier $submitButtonLabel,)
Abstract class for all constraints that can prevent edits.
A value class to process existing log entries.
Class to simplify the use of log pages.
Definition LogPage.php:34
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
Status returned by edit constraints and other page editing checks.
Represents a title within MediaWiki.
Definition Title.php:69
Value object representing a message for i18n.
Build SELECT queries with a fluent interface.
Provide primary and replica IDatabase connections.