Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4116098
ExternalTextDiffer.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
ExternalTextDiffer.php
View Options
<?php
namespace
MediaWiki\Diff\TextDiffer
;
use
MediaWiki\Shell\Shell
;
use
RuntimeException
;
/**
* @since 1.41
*/
class
ExternalTextDiffer
extends
BaseTextDiffer
{
/** @var string */
private
$externalPath
;
/**
* @param string $externalPath The path to the executable file which provides the diff
*/
public
function
__construct
(
$externalPath
)
{
$this
->
externalPath
=
$externalPath
;
}
public
function
getName
():
string
{
return
'external'
;
}
public
function
getFormats
():
array
{
return
[
'external'
];
}
/** @inheritDoc */
public
function
getFormatContext
(
string
$format
)
{
return
self
::
CONTEXT_ROW
;
}
protected
function
doRenderBatch
(
string
$oldText
,
string
$newText
,
array
$formats
):
array
{
return
[
'external'
=>
$this
->
doRender
(
$oldText
,
$newText
)
];
}
/**
* @param string $oldText
* @param string $newText
* @return string
*/
private
function
doRender
(
$oldText
,
$newText
)
{
$tmpDir
=
wfTempDir
();
$tempName1
=
tempnam
(
$tmpDir
,
'diff_'
);
$tempName2
=
tempnam
(
$tmpDir
,
'diff_'
);
$tempFile1
=
fopen
(
$tempName1
,
"w"
);
if
(
!
$tempFile1
)
{
throw
new
RuntimeException
(
"Could not create temporary file $tempName1 for external diffing"
);
}
$tempFile2
=
fopen
(
$tempName2
,
"w"
);
if
(
!
$tempFile2
)
{
throw
new
RuntimeException
(
"Could not create temporary file $tempName2 for external diffing"
);
}
fwrite
(
$tempFile1
,
$oldText
);
fwrite
(
$tempFile2
,
$newText
);
fclose
(
$tempFile1
);
fclose
(
$tempFile2
);
$cmd
=
[
$this
->
externalPath
,
$tempName1
,
$tempName2
];
$result
=
Shell
::
command
(
$cmd
)
->
execute
();
$exitCode
=
$result
->
getExitCode
();
if
(
$exitCode
!==
0
)
{
throw
new
RuntimeException
(
"External diff command returned code {$exitCode}. Stderr: "
.
wfEscapeWikiText
(
$result
->
getStderr
()
)
);
}
$diffText
=
$result
->
getStdout
();
unlink
(
$tempName1
);
unlink
(
$tempName2
);
return
$diffText
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Aug 19, 01:14 (2 w, 1 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
71/26/794884cdaa0298784de78f56640c
Default Alt Text
ExternalTextDiffer.php (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment