Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
revision_graph
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
revision_graph
Commits
e74c51bf
Commit
e74c51bf
authored
3 years ago
by
Sabina Halkic
Committed by
Shibin Das
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3230487
by sabina.h: Refactor to use dependency injection
parent
84a44e4b
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!1
Issue #3230487: Refactor to use dependency injection
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
revision_graph.services.yml
+1
-0
1 addition, 0 deletions
revision_graph.services.yml
src/Controller/RevisionGraphNodeController.php
+45
-4
45 additions, 4 deletions
src/Controller/RevisionGraphNodeController.php
src/RevisionGraphStorage.php
+73
-57
73 additions, 57 deletions
src/RevisionGraphStorage.php
with
119 additions
and
61 deletions
revision_graph.services.yml
+
1
−
0
View file @
e74c51bf
services
:
revision_graph.storage
:
class
:
Drupal\revision_graph\RevisionGraphStorage
arguments
:
[
'
@database'
,
'
@path.current'
]
This diff is collapsed.
Click to expand it.
src/Controller/RevisionGraphNodeController.php
+
45
−
4
View file @
e74c51bf
...
...
@@ -2,16 +2,58 @@
namespace
Drupal\revision_graph\Controller
;
use
Drupal\Core\Database\Connection
;
use
Drupal\Core\Datetime\DateFormatterInterface
;
use
Drupal\Core\Entity\EntityRepositoryInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\Core\Url
;
use
Drupal\node\Controller\NodeController
;
use
Drupal\node\NodeInterface
;
use
Drupal\revision_graph
\RevisionGraphStorage
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Returns responses for Node routes.
*/
class
RevisionGraphNodeController
extends
NodeController
{
/**
* The default database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected
$connection
;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected
$entityTypeManager
;
/**
* {@inheritdoc}
*/
public
function
__construct
(
DateFormatterInterface
$date_formatter
,
RendererInterface
$renderer
,
EntityRepositoryInterface
$entity_repository
=
NULL
,
Connection
$connection
,
EntityTypeManagerInterface
$entity_type_manager
)
{
parent
::
__construct
(
$date_formatter
,
$renderer
,
$entity_repository
);
$this
->
connection
=
$connection
;
$this
->
entityTypeManager
=
$entity_type_manager
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
get
(
'date.formatter'
),
$container
->
get
(
'renderer'
),
$container
->
get
(
'entity.repository'
),
$container
->
get
(
'database'
),
$container
->
get
(
'entity_type.manager'
)
);
}
/**
* {@inheritdoc}
*/
...
...
@@ -43,8 +85,7 @@ class RevisionGraphNodeController extends NodeController {
* List of revisions.
*/
private
function
loadGraphData
(
NodeInterface
$node
)
{
$connection
=
\Drupal
::
service
(
'database'
);
$result
=
$connection
->
query
(
'SELECT * FROM {revision_graph} WHERE primary_id = :entity_id'
,
[
':entity_id'
=>
$node
->
id
()])
->
fetchAll
();
$result
=
$this
->
connection
->
query
(
'SELECT * FROM {revision_graph} WHERE primary_id = :entity_id'
,
[
':entity_id'
=>
$node
->
id
()])
->
fetchAll
();
$graph
=
[
'map'
=>
[],
'branches'
=>
[],
...
...
@@ -81,7 +122,7 @@ class RevisionGraphNodeController extends NodeController {
$commits
=
[];
$langcode
=
$node
->
language
()
->
getId
();
$node_storage
=
\Drupal
::
E
ntityTypeManager
()
->
getStorage
(
'node'
);
$node_storage
=
$this
->
e
ntityTypeManager
->
getStorage
(
'node'
);
$default_revision
=
$node
->
getRevisionId
();
foreach
(
$this
->
revisionIds
(
$node
)
as
$vid
)
{
...
...
@@ -149,7 +190,7 @@ class RevisionGraphNodeController extends NodeController {
*/
private
function
revisionIds
(
NodeInterface
$node
,
$langcode
=
NULL
)
{
$vids
=
[];
$connection
=
\Drupal
::
service
(
'database'
)
;
$connection
=
$this
->
connection
;
if
(
isset
(
$langcode
))
{
$result
=
$connection
->
query
(
'SELECT vid FROM {node_revision} WHERE nid=:nid AND langcode=:langcode ORDER BY vid DESC'
,
...
...
This diff is collapsed.
Click to expand it.
src/RevisionGraphStorage.php
+
73
−
57
View file @
e74c51bf
...
...
@@ -2,72 +2,88 @@
namespace
Drupal\revision_graph
;
use
Drupal\Core\Database\
Database
;
use
Drupal\Core\Database\
Connection
;
use
Drupal\Core\Entity\ContentEntityInterface
;
use
Drupal\
node\NodeInterf
ac
e
;
use
Drupal\
Core\Path\CurrentPathSt
ac
k
;
class
RevisionGraphStorage
{
class
RevisionGraphStorage
{
const
UNTRACKED_BANCH_NAME
=
'untracked'
;
const
UNTRACKED_BANCH_NAME
=
'untracked'
;
public
function
initGraph
(
ContentEntityInterface
$entity
)
{
$conn
=
Database
::
getConnection
();
$conn
->
insert
(
'revision_graph'
)
->
fields
(
array
(
'primary_id'
=>
$entity
->
id
(),
'version_id'
=>
$entity
->
getRevisionId
(),
'parent_version_id'
=>
$entity
->
getRevisionId
(),
'type'
=>
$entity
->
getEntityTypeId
(),
'branch'
=>
$this
->
formatBranchName
(
$entity
->
getRevisionId
(),
$entity
->
language
()
->
getId
()),
)
)
->
execute
();
}
/**
* The default database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected
$connection
;
public
function
updateGraph
(
ContentEntityInterface
$entity
)
{
$conn
=
Database
::
getConnection
();
// @TODO hunt actual parent version id.
$parent_version_id
=
$entity
->
original
->
getRevisionId
();
$branch
=
$this
->
getParentBranchName
(
$parent_version_id
)
;
/**
* The current path.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected
$currentPath
;
// For some reason we have default revision id in original.
// Hackish way to sniff the actual parent.
// @TODO Check if this is a bug in core due to some optimisation.
$current_path
=
\Drupal
::
service
(
'path.current'
)
->
getPath
();
$path_args
=
explode
(
'/'
,
$current_path
);
if
(
$path_args
[
1
]
==
'node'
&&
$path_args
[
3
]
==
'revisions'
&&
$path_args
[
5
]
==
'revert'
)
{
$parent_version_id
=
(
int
)
$path_args
[
4
];
$branch
=
$parent_version_id
;
}
/**
* Creates a RevisionGraphStorage object.
*
* @param \Drupal\Core\Database\Connection $connection
* The default database connection.
* @param \Drupal\Core\Path\CurrentPathStack $current_path
* The current path.
*/
public
function
__construct
(
Connection
$connection
,
CurrentPathStack
$current_path
)
{
$this
->
connection
=
$connection
;
$this
->
currentPath
=
$current_path
;
}
public
function
initGraph
(
ContentEntityInterface
$entity
)
{
$this
->
connection
->
insert
(
'revision_graph'
)
->
fields
([
'primary_id'
=>
$entity
->
id
(),
'version_id'
=>
$entity
->
getRevisionId
(),
'parent_version_id'
=>
$entity
->
getRevisionId
(),
'type'
=>
$entity
->
getEntityTypeId
(),
'branch'
=>
$this
->
formatBranchName
(
$entity
->
getRevisionId
(),
$entity
->
language
()
->
getId
()),
])
->
execute
();
}
$conn
->
insert
(
'revision_graph'
)
->
fields
(
array
(
'primary_id'
=>
$entity
->
id
(),
'version_id'
=>
$entity
->
getRevisionId
(),
'parent_version_id'
=>
$parent_version_id
,
'type'
=>
$entity
->
getEntityTypeId
(),
'branch'
=>
$this
->
formatBranchName
(
$branch
,
$entity
->
language
()
->
getId
()),
)
)
->
execute
();
}
private
function
formatBranchName
(
$parent_branch
,
$language_id
)
{
list
(
$branch
,
$code
)
=
explode
(
':'
,
$parent_branch
);
return
implode
(
':'
,
[
$branch
,
$language_id
]);
public
function
updateGraph
(
ContentEntityInterface
$entity
)
{
// @TODO hunt actual parent version id.
$parent_version_id
=
$entity
->
original
->
getRevisionId
();
$branch
=
$this
->
getParentBranchName
(
$parent_version_id
);
// For some reason we have default revision id in original.
// Hackish way to sniff the actual parent.
// @TODO Check if this is a bug in core due to some optimisation.
$current_path
=
$this
->
currentPath
->
getPath
();
$path_args
=
explode
(
'/'
,
$current_path
);
if
(
$path_args
[
1
]
==
'node'
&&
$path_args
[
3
]
==
'revisions'
&&
$path_args
[
5
]
==
'revert'
)
{
$parent_version_id
=
(
int
)
$path_args
[
4
];
$branch
=
$parent_version_id
;
}
private
function
getParentBranchName
(
$version_id
)
{
$connection
=
\Drupal
::
service
(
'database'
);
$result
=
$connection
->
query
(
"SELECT * FROM
{
revision_graph
}
WHERE version_id = :vid"
,
[
':vid'
=>
$version_id
])
->
fetchAll
();
$branch_name
=
self
::
UNTRACKED_BANCH_NAME
;
foreach
(
$result
as
$r
)
{
$branch_name
=
$r
->
branch
;
break
;
}
return
$branch_name
;
$this
->
connection
->
insert
(
'revision_graph'
)
->
fields
([
'primary_id'
=>
$entity
->
id
(),
'version_id'
=>
$entity
->
getRevisionId
(),
'parent_version_id'
=>
$parent_version_id
,
'type'
=>
$entity
->
getEntityTypeId
(),
'branch'
=>
$this
->
formatBranchName
(
$branch
,
$entity
->
language
()
->
getId
()),
])
->
execute
();
}
private
function
formatBranchName
(
$parent_branch
,
$language_id
)
{
list
(
$branch
,
$code
)
=
explode
(
':'
,
$parent_branch
);
return
implode
(
':'
,
[
$branch
,
$language_id
]);
}
private
function
getParentBranchName
(
$version_id
)
{
$result
=
$this
->
connection
->
query
(
"SELECT * FROM
{
revision_graph
}
WHERE version_id = :vid"
,
[
':vid'
=>
$version_id
])
->
fetchAll
();
$branch_name
=
self
::
UNTRACKED_BANCH_NAME
;
foreach
(
$result
as
$r
)
{
$branch_name
=
$r
->
branch
;
break
;
}
return
$branch_name
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment