Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
DeleteTranslatableBundleJob.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use Job;
11use MediaWiki\MediaWikiServices;
12use Title;
13use User;
14
21class DeleteTranslatableBundleJob extends Job {
22 public static function newJob(
23 Title $target,
24 string $base,
25 string $bundleType,
26 bool $isTranslatableBundle,
27 User $performer,
28 string $reason
29 ): self {
30 $params = [
31 'translation' => $isTranslatableBundle,
32 'base' => $base,
33 'bundleType' => $bundleType,
34 'performer' => $performer->getName(),
35 'reason' => $reason
36 ];
37
38 return new self( $target, $params );
39 }
40
41 public function __construct( Title $title, array $params = [] ) {
42 parent::__construct( 'DeleteTranslatableBundleJob', $title, $params );
43 }
44
45 public function run() {
46 $title = $this->title;
47 $fuzzyBot = FuzzyBot::getUser();
48 $summary = $this->getSummary();
49 $base = $this->getBase();
50 $performer = $this->getPerformer();
51 $reason = $this->getReason();
52 $mwInstance = MediaWikiServices::getInstance();
53
54 Hooks::$allowTargetEdit = true;
55 Hooks::$jobQueueRunning = true;
56
57 $wikipage = $mwInstance->getWikiPageFactory()->newFromTitle( $title );
58 $deletePage = $mwInstance->getDeletePageFactory()->newDeletePage( $wikipage, $fuzzyBot );
59 $status = $deletePage->setSuppress( false )
60 ->forceImmediate( true )
61 ->deleteUnsafe( "{$summary}: $reason" );
62
63 $bundleFactory = Services::getInstance()->getTranslatableBundleFactory();
64 // Since the page has been removed from cache, create a bundle from the class name.
65 $bundle = $bundleFactory->getBundleFromClass( Title::newFromText( $base ), $this->getBundleType() );
66 $logger = $bundleFactory->getPageDeleteLogger( $bundle );
67
68 if ( !$status->isGood() ) {
69 if ( $this->isTranslation() ) {
70 $logger->logPageError( $performer, $reason, $status );
71 } else {
72 $logger->logBundleError( $performer, $reason, $status );
73 }
74 }
75
76 Hooks::$allowTargetEdit = false;
77
78 $cache = $mwInstance->getMainObjectStash();
79 $pageKey = $cache->makeKey( 'pt-base', $base );
80 $pages = (array)$cache->get( $pageKey );
81 $lastitem = array_pop( $pages );
82 if ( $title->getPrefixedText() === $lastitem ) {
83 $cache->delete( $pageKey );
84
85 if ( $this->isTranslation() ) {
86 $logger->logPageSuccess( $performer, $reason );
87 } else {
88 $logger->logBundleSuccess( $performer, $reason );
89 }
90
91 $title->invalidateCache();
92 Hooks::$jobQueueRunning = false;
93 }
94
95 return true;
96 }
97
98 public function getSummary(): string {
99 $base = $this->getBase();
100 if ( $this->isTranslation() ) {
101 $msg = wfMessage( 'pt-deletepage-lang-logreason', $base )->inContentLanguage()->text();
102 } else {
103 $msg = wfMessage( 'pt-deletepage-full-logreason', $base )->inContentLanguage()->text();
104 }
105
106 return $msg;
107 }
108
109 public function getReason(): string {
110 return $this->params['reason'];
111 }
112
113 private function isTranslation(): bool {
114 // Use 'full' property if 'translation' is missing. This will happen
115 // if the job is added before param 'full' was changed to 'translation'
116 // Remove after MLEB 2022.07
117 return $this->params['translation'] ?? !$this->params['full'];
118 }
119
120 public function getPerformer(): User {
121 $userFactory = MediaWikiServices::getInstance()->getUserFactory();
122 return $userFactory->newFromName( $this->params['performer'] );
123 }
124
125 public function getBase(): string {
126 return $this->params['base'];
127 }
128
129 private function getBundleType(): string {
130 // Default to TranslatablePage if param is not present
131 return $this->params['bundleType'] ?? TranslatablePage::class;
132 }
133}
Mixed bag of methods related to translatable pages.
Minimal service container.
Definition Services.php:38
FuzzyBot - the misunderstood workhorse.
Definition FuzzyBot.php:15