|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the TopEditsController class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace AppBundle\Controller; |
|
7
|
|
|
|
|
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
use Xtools\Page; |
|
14
|
|
|
use Xtools\PagesRepository; |
|
15
|
|
|
use Xtools\Project; |
|
16
|
|
|
use Xtools\ProjectRepository; |
|
17
|
|
|
use Xtools\User; |
|
18
|
|
|
use Xtools\UserRepository; |
|
19
|
|
|
use Xtools\TopEdits; |
|
20
|
|
|
use Xtools\TopEditsRepository; |
|
21
|
|
|
use Xtools\Edit; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The Top Edits tool. |
|
25
|
|
|
*/ |
|
26
|
|
|
class TopEditsController extends XtoolsController |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the tool's shortname. |
|
31
|
|
|
* @return string |
|
32
|
|
|
* @codeCoverageIgnore |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getToolShortname() |
|
35
|
|
|
{ |
|
36
|
|
|
return 'topedits'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Display the form. |
|
41
|
|
|
* @Route("/topedits", name="topedits") |
|
42
|
|
|
* @Route("/topedits", name="TopEdits") |
|
43
|
|
|
* @Route("/topedits/", name="topEditsSlash") |
|
44
|
|
|
* @Route("/topedits/index.php", name="TopEditsIndex") |
|
45
|
|
|
* @Route("/topedits/{project}", name="TopEditsProject") |
|
46
|
|
|
* @param Request $request |
|
47
|
|
|
* @return Response |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
public function indexAction(Request $request) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$params = $this->parseQueryParams($request); |
|
52
|
|
|
|
|
53
|
|
|
// Redirect if at minimum project and username are provided. |
|
54
|
|
|
if (isset($params['project']) && isset($params['username'])) { |
|
55
|
|
|
return $this->redirectToRoute('TopEditsResults', $params); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Convert the given project (or default project) into a Project instance. |
|
59
|
|
|
$params['project'] = $this->getProjectFromQuery($params); |
|
60
|
|
|
|
|
61
|
|
|
return $this->render('topedits/index.html.twig', array_merge([ |
|
62
|
|
|
'xtPageTitle' => 'tool-topedits', |
|
63
|
|
|
'xtSubtitle' => 'tool-topedits-desc', |
|
64
|
|
|
'xtPage' => 'topedits', |
|
65
|
|
|
|
|
66
|
|
|
// Defaults that will get overriden if in $params. |
|
67
|
|
|
'namespace' => 0, |
|
68
|
|
|
'article' => '', |
|
69
|
|
|
], $params)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Display the results. |
|
74
|
|
|
* @Route("/topedits/{project}/{username}/{namespace}/{article}", name="TopEditsResults", |
|
75
|
|
|
* requirements={"article"=".+"}) |
|
76
|
|
|
* @param Request $request The HTTP request. |
|
77
|
|
|
* @param int $namespace |
|
78
|
|
|
* @param string $article |
|
79
|
|
|
* @return RedirectResponse|Response |
|
80
|
|
|
* @codeCoverageIgnore |
|
81
|
|
|
*/ |
|
82
|
|
|
public function resultAction(Request $request, $namespace = 0, $article = '') |
|
83
|
|
|
{ |
|
84
|
|
|
// Second parameter causes it return a Redirect to the index if the user has too many edits. |
|
85
|
|
|
$ret = $this->validateProjectAndUser($request, 'topedits'); |
|
86
|
|
|
if ($ret instanceof RedirectResponse) { |
|
87
|
|
|
return $ret; |
|
88
|
|
|
} else { |
|
89
|
|
|
list($projectData, $user) = $ret; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($article === '') { |
|
93
|
|
|
return $this->namespaceTopEdits($request, $user, $projectData, $namespace); |
|
|
|
|
|
|
94
|
|
|
} else { |
|
95
|
|
|
return $this->singlePageTopEdits($user, $projectData, $namespace, $article); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* List top edits by this user for all pages in a particular namespace. |
|
101
|
|
|
* @param Request $request The HTTP request. |
|
102
|
|
|
* @param User $user The User. |
|
103
|
|
|
* @param Project $project The project. |
|
104
|
|
|
* @param integer|string $namespace The namespace ID or 'all' |
|
105
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
106
|
|
|
* @codeCoverageIgnore |
|
107
|
|
|
*/ |
|
108
|
|
|
public function namespaceTopEdits(Request $request, User $user, Project $project, $namespace) |
|
109
|
|
|
{ |
|
110
|
|
|
$isSubRequest = $request->get('htmlonly') |
|
111
|
|
|
|| $this->container->get('request_stack')->getParentRequest() !== null; |
|
112
|
|
|
|
|
113
|
|
|
// Make sure they've opted in to see this data. |
|
114
|
|
|
if (!$project->userHasOptedIn($user)) { |
|
115
|
|
|
$optedInPage = $project |
|
|
|
|
|
|
116
|
|
|
->getRepository() |
|
117
|
|
|
->getPage($project, $project->userOptInPage($user)); |
|
118
|
|
|
|
|
119
|
|
|
return $this->render('topedits/result_namespace.html.twig', [ |
|
120
|
|
|
'xtPage' => 'topedits', |
|
121
|
|
|
'xtTitle' => $user->getUsername(), |
|
122
|
|
|
'project' => $project, |
|
123
|
|
|
'user' => $user, |
|
124
|
|
|
'namespace' => $namespace, |
|
125
|
|
|
'edits' => [], |
|
126
|
|
|
'content_title' => '', |
|
127
|
|
|
'opted_in_page' => $optedInPage, |
|
128
|
|
|
'is_sub_request' => $isSubRequest, |
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Max number of rows per namespace to show. `null` here will cause to |
|
134
|
|
|
* use the TopEdits default. |
|
135
|
|
|
* @var int |
|
136
|
|
|
*/ |
|
137
|
|
|
$limit = $isSubRequest ? 10 : null; |
|
138
|
|
|
|
|
139
|
|
|
$topEdits = new TopEdits($project, $user, $namespace, $limit); |
|
140
|
|
|
$topEditsRepo = new TopEditsRepository(); |
|
141
|
|
|
$topEditsRepo->setContainer($this->container); |
|
|
|
|
|
|
142
|
|
|
$topEdits->setRepository($topEditsRepo); |
|
143
|
|
|
|
|
144
|
|
|
return $this->render('topedits/result_namespace.html.twig', [ |
|
145
|
|
|
'xtPage' => 'topedits', |
|
146
|
|
|
'xtTitle' => $user->getUsername(), |
|
147
|
|
|
'project' => $project, |
|
148
|
|
|
'user' => $user, |
|
149
|
|
|
'namespace' => $namespace, |
|
150
|
|
|
'te' => $topEdits, |
|
151
|
|
|
'is_sub_request' => $isSubRequest, |
|
152
|
|
|
]); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* List top edits by this user for a particular page. |
|
157
|
|
|
* @param User $user The user. |
|
158
|
|
|
* @param Project $project The project. |
|
159
|
|
|
* @param int $namespaceId The ID of the namespace of the page. |
|
160
|
|
|
* @param string $pageName The title (without namespace) of the page. |
|
161
|
|
|
* @return RedirectResponse|\Symfony\Component\HttpFoundation\Response |
|
162
|
|
|
* @codeCoverageIgnore |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function singlePageTopEdits(User $user, Project $project, $namespaceId, $pageName) |
|
165
|
|
|
{ |
|
166
|
|
|
// Get the full page name (i.e. no namespace prefix if NS 0). |
|
167
|
|
|
$namespaces = $project->getNamespaces(); |
|
168
|
|
|
$fullPageName = $namespaceId ? $namespaces[$namespaceId].':'.$pageName : $pageName; |
|
169
|
|
|
|
|
170
|
|
|
$page = $this->getAndValidatePage($project, $fullPageName); |
|
|
|
|
|
|
171
|
|
|
if (is_a($page, 'Symfony\Component\HttpFoundation\RedirectResponse')) { |
|
172
|
|
|
return $page; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// Get all revisions of this page by this user. |
|
176
|
|
|
$revisionsData = $page->getRevisions($user); |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
// Loop through all revisions and format dates, find totals, etc. |
|
179
|
|
|
$totalAdded = 0; |
|
180
|
|
|
$totalRemoved = 0; |
|
181
|
|
|
$revisions = []; |
|
182
|
|
|
foreach ($revisionsData as $revision) { |
|
183
|
|
|
if ($revision['length_change'] > 0) { |
|
184
|
|
|
$totalAdded += $revision['length_change']; |
|
185
|
|
|
} else { |
|
186
|
|
|
$totalRemoved += $revision['length_change']; |
|
187
|
|
|
} |
|
188
|
|
|
$revisions[] = new Edit($page, $revision); |
|
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// Send all to the template. |
|
192
|
|
|
return $this->render('topedits/result_article.html.twig', [ |
|
193
|
|
|
'xtPage' => 'topedits', |
|
194
|
|
|
'xtTitle' => $user->getUsername() . ' - ' . $page->getTitle(), |
|
|
|
|
|
|
195
|
|
|
'project' => $project, |
|
196
|
|
|
'user' => $user, |
|
197
|
|
|
'page' => $page, |
|
198
|
|
|
'total_added' => $totalAdded, |
|
199
|
|
|
'total_removed' => $totalRemoved, |
|
200
|
|
|
'revisions' => $revisions, |
|
201
|
|
|
'revision_count' => count($revisions), |
|
202
|
|
|
]); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.