MediaWiki master
SpecialComparePages.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Specials;
10
19
26
28 private $differenceEngine;
29
30 public function __construct(
31 private readonly RevisionLookup $revisionLookup,
32 private readonly IContentHandlerFactory $contentHandlerFactory
33 ) {
34 parent::__construct( 'ComparePages' );
35 }
36
42 public function execute( $par ) {
43 $this->setHeaders();
44 $this->outputHeader();
45 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
46 $this->addHelpLink( 'Help:Diff' );
47
48 $form = HTMLForm::factory( 'ooui', [
49 'Page1' => [
50 'type' => 'title',
51 'exists' => true,
52 'name' => 'page1',
53 'label-message' => 'compare-page1',
54 'size' => '40',
55 'section' => 'page1',
56 'required' => false,
57 ],
58 'Revision1' => [
59 'type' => 'int',
60 'name' => 'rev1',
61 'label-message' => 'compare-rev1',
62 'size' => '8',
63 'section' => 'page1',
64 'validation-callback' => $this->checkExistingRevision( ... ),
65 ],
66 'Page2' => [
67 'type' => 'title',
68 'name' => 'page2',
69 'exists' => true,
70 'label-message' => 'compare-page2',
71 'size' => '40',
72 'section' => 'page2',
73 'required' => false,
74 ],
75 'Revision2' => [
76 'type' => 'int',
77 'name' => 'rev2',
78 'label-message' => 'compare-rev2',
79 'size' => '8',
80 'section' => 'page2',
81 'validation-callback' => $this->checkExistingRevision( ... ),
82 ],
83 'Action' => [
84 'type' => 'hidden',
85 'name' => 'action',
86 ],
87 'Unhide' => [
88 'type' => 'hidden',
89 'name' => 'unhide',
90 ],
91 ], $this->getContext(), 'compare' );
92
93 $form->setMethod( 'get' )
94 ->setSubmitTextMsg( 'compare-submit' )
95 ->setSubmitCallback( $this->showDiff( ... ) )
96 ->show();
97
98 if ( $this->differenceEngine ) {
99 $this->differenceEngine->showDiffPage( true );
100 }
101 }
102
107 private function showDiff( $data, HTMLForm $form ) {
108 $rev1 = $this->revOrTitle( $data['Revision1'], $data['Page1'] );
109 $rev2 = $this->revOrTitle( $data['Revision2'], $data['Page2'] );
110
111 if ( $rev1 && $rev2 ) {
112 // Revision IDs either passed the existence check or were fetched from existing titles.
113 $revisionRecord = $this->revisionLookup->getRevisionById( $rev1 );
114 $contentModel = $revisionRecord->getSlot(
115 SlotRecord::MAIN,
116 RevisionRecord::RAW
117 )->getModel();
118 $contentHandler = $this->contentHandlerFactory->getContentHandler( $contentModel );
119 $this->differenceEngine = $contentHandler->createDifferenceEngine( $form->getContext(),
120 $rev1,
121 $rev2,
122 0, // rcid
123 ( $data['Action'] == 'purge' ),
124 ( $data['Unhide'] == '1' )
125 );
126 }
127 }
128
129 private function revOrTitle( ?string $revision, ?string $title ): ?int {
130 if ( $revision ) {
131 return (int)$revision;
132 } elseif ( $title ) {
133 return Title::newFromText( $title )->getLatestRevID();
134 }
135
136 return null;
137 }
138
143 private function checkExistingRevision( $value ) {
144 if ( $value === '' || $value === null ) {
145 return true;
146 }
147 $revisionRecord = $this->revisionLookup->getRevisionById( (int)$value );
148 if ( $revisionRecord === null ) {
149 return $this->msg( 'compare-revision-not-exists' )->parseAsBlock();
150 }
151
152 return true;
153 }
154
156 protected function getGroupName() {
157 return 'pagetools';
158 }
159}
160
162class_alias( SpecialComparePages::class, 'SpecialComparePages' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
getContext()
Get the base IContextSource object.
DifferenceEngine is responsible for rendering the difference between two revisions as HTML.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:207
setMethod( $method='post')
Set the method used to submit the form.
Page revision base class.
Value object representing a content slot associated with a page revision.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getContext()
Gets the context this SpecialPage is executed in.
getOutput()
Get the OutputPage being used for this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Implements Special:ComparePages.
execute( $par)
Show a form for filtering namespace and username.
__construct(private readonly RevisionLookup $revisionLookup, private readonly IContentHandlerFactory $contentHandlerFactory)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Represents a title within MediaWiki.
Definition Title.php:69
Service for looking up page revisions.
msg( $key,... $params)