Wiki source code of Contributors per month

Last modified by Eleni Cojocariu on 2026/11/16 18:04

Show last authors
1 Who contributed to the new documentation in a given month, and which pages were created in it. [[New documentation pages per month>>XWikiOrg.documentation-scripts-queries.migration-progress.new-pages.WebHome]] counts the same pages for every month at once.
2
3 {{cache timeToLive="3600" id="contributors-1-{{velocity}}#set($cacheMonth = $stringtool.defaultString($request.month))#if($cacheMonth.matches('^20\d\d-(0[1-9]|1[0-2])$'))$cacheMonth#end{{/velocity}}"}}
4 {{velocity}}
5 #*
6 Why this page is wrapped in a cache macro, and what its id has to do.
7
8 The ranking needs to know who saved which page in a given month. The search index cannot answer
9 that: it holds a page's creator and its last author, not the author of each revision, so the
10 only source is the revision table, and the join below is what it costs - measured on xwiki.org,
11 about 250 ms warm and 1.6 s on a cold cache, which was most of this page's time.
12
13 The event store was measured as the alternative and does not work here. It is a Solr index on
14 date, user and page, so it would be cheap, but on xwiki.org it holds no event at all for the
15 'documentation' space before November 2025 while the tree goes back to 2024, and a page created
16 in June 2025 with 120 revisions - several of them inside the covered window - returns no event
17 under its current reference. The revision table has all of it. So the query stays, and the cost
18 is paid once an hour instead of once per view.
19
20 The cache macro's id has to carry the selected month, because the cached content is the same
21 source text whatever the month: without an id, every month would be served the first month
22 computed. Two consequences:
23
24 - The id takes $request.month only when it looks like a month, so a crawler passing
25 ?month=anything cannot mint a new cache entry - and a new computation - per request.
26 - An id means the page's own content is no longer part of the cache key, so editing anything
27 below does NOT invalidate what is already cached. Bump the number in the id when changing
28 this page, or the old output is what is served for the next hour - including a cached error
29 block, which is what a macro that fails in here leaves behind.
30 *#
31 #*
32 The notes this page used to print above the tables, kept here rather than deleted - they are what
33 the numbers mean, and they are the answer to the first question anyone asks about them.
34
35 A page counts once per person per month however many times they saved it. The two columns do not
36 overlap: a page someone created in that month is counted under Created only, so Edited is what
37 they worked on without having started it.
38
39 A documentation page is the same thing here as everywhere else: a page carrying a
40 DocApp.Code.DocumentationClass object, not hidden, original locale only. Edited is counted from
41 the revision metadata rather than from the search index, which holds only a page's creator and
42 its last author and so cannot say who edited it in a given month. XWiki.superadmin is left out -
43 those are the imports and the migration scripts, not contributions - so the Created total is
44 below the month's page count by however many pages superadmin created.
45
46 Rows are ranked by the two columns together. Both totals are sums of the rows, so a page edited
47 by three people counts three times under Edited; neither is a number of pages.
48
49 A page published through a Change Request is credited to two people, because the publish splits
50 the two fields this page reads: the creator becomes the Change Request's author, so the page
51 counts under Created for them, while the revision that published it belongs to whoever published
52 it, so the same page counts under Edited for the publisher. That is why a row can show more pages
53 created than the person saved in the month. Pages published before xwiki.org picked up Change
54 Request 1.24 (CRAPP-437, https://jira.xwiki.org/browse/CRAPP-437) do not get even the first half
55 - their creator is the publisher too - so those rows under-credit the people who went through
56 review.
57 *#
58 #set ($Q = '"')
59 ## Months are built rather than queried: a 'group by month' needs a date function whose spelling
60 ## depends on the database. January 2026 is where the sibling page starts, so the two line up.
61 #set ($firstYear = 2026)
62 #set ($firstMonth = 1)
63 #set ($thisYear = $mathtool.toInteger($datetool.get('yyyy')))
64 #set ($thisMonth = $mathtool.toInteger($datetool.get('MM')))
65 #set ($months = [])
66 #set ($y = $firstYear)
67 #set ($m = $firstMonth)
68 #foreach ($i in [1..240])
69 #if ($y < $thisYear || ($y == $thisYear && $m <= $thisMonth))
70 #if ($m < 10)
71 #set ($key = "${y}-0${m}")
72 #else
73 #set ($key = "${y}-${m}")
74 #end
75 #set ($discard = $months.add($key))
76 #if ($m == 12)
77 #set ($y = $y + 1)
78 #set ($m = 1)
79 #else
80 #set ($m = $m + 1)
81 #end
82 #end
83 #end
84 #set ($selected = "$!request.month")
85 #if (!$months.contains($selected))
86 #set ($selected = $months.get($months.size() - 1))
87 #end
88 #set ($links = [])
89 #foreach ($candidate in $months)
90 #if ($candidate == $selected)
91 #set ($discard = $links.add("**$candidate**"))
92 #else
93 #set ($url = $doc.getURL('view', "month=$candidate"))
94 #set ($discard = $links.add("[[$candidate>>path:$url]]"))
95 #end
96 #end
97 Choose a month to see who contributed and which pages were created:
98
99 $stringtool.join($links, ' | ')
100
101 = $selected =
102
103 == Contributors ==
104
105 #set ($parts = $selected.split('-'))
106 #set ($fromKey = "${selected}-01")
107 #set ($selYear = $mathtool.toInteger($parts.get(0)))
108 #set ($selMonth = $mathtool.toInteger($parts.get(1)))
109 #if ($selMonth == 12)
110 #set ($nextYear = $selYear + 1)
111 #set ($nextMonth = 1)
112 #else
113 #set ($nextYear = $selYear)
114 #set ($nextMonth = $selMonth + 1)
115 #end
116 #if ($nextMonth < 10)
117 #set ($toKey = "${nextYear}-0${nextMonth}-01")
118 #else
119 #set ($toKey = "${nextYear}-${nextMonth}-01")
120 #end
121 #set ($from = $datetool.toDate('yyyy-MM-dd', $fromKey))
122 #set ($to = $datetool.toDate('yyyy-MM-dd', $toKey))
123 ## count(distinct docId): the ranking is in pages touched, not in saves, so somebody who saved one
124 ## page twenty times does not outrank somebody who wrote twenty pages.
125 #set ($hql = 'select n.author, count(distinct n.id.docId) from XWikiRCSNodeInfo as n, XWikiDocument as doc, BaseObject as obj where doc.id = n.id.docId and obj.name = doc.fullName and obj.className = :class and doc.language = :locale and (doc.hidden = false or doc.hidden is null) and n.date >= :from and n.date < :to group by n.author')
126 #set ($query = $services.query.createQuery($hql, 'hql'))
127 #set ($discard = $query.bindValue('class', 'DocApp.Code.DocumentationClass'))
128 #set ($discard = $query.bindValue('locale', ''))
129 #set ($discard = $query.bindValue('from', $from))
130 #set ($discard = $query.bindValue('to', $to))
131 #set ($rows = $query.execute())
132 ## Pages created in the month, from the document's own creator and creation date. Every page created
133 ## by somebody in a month is also a page they touched in it, so subtracting the one from the other
134 ## leaves the pages they edited without having created them, and the two columns never double-count.
135 #set ($createdHql = 'select doc.creator, count(distinct doc.id) from XWikiDocument as doc, BaseObject as obj where obj.name = doc.fullName and obj.className = :class and doc.language = :locale and (doc.hidden = false or doc.hidden is null) and doc.creationDate >= :from and doc.creationDate < :to group by doc.creator')
136 #set ($createdQuery = $services.query.createQuery($createdHql, 'hql'))
137 #set ($discard = $createdQuery.bindValue('class', 'DocApp.Code.DocumentationClass'))
138 #set ($discard = $createdQuery.bindValue('locale', ''))
139 #set ($discard = $createdQuery.bindValue('from', $from))
140 #set ($discard = $createdQuery.bindValue('to', $to))
141 #set ($createdRows = $createdQuery.execute())
142 #set ($createdByUser = {})
143 #foreach ($row in $createdRows)
144 #set ($author = "$!row.get(0)")
145 #set ($short = $author.replaceAll('^xwiki:', ''))
146 ## Same exclusion as the revision map below. It matters here now that a creator who has no revision
147 ## in the month still gets a row: without it, an import would rank as a contributor.
148 #if ($short != 'XWiki.superadmin' && $short != 'XWiki.XWikiGuest' && $short != '')
149 #set ($previous = $createdByUser.get($short))
150 #if ($previous)
151 #set ($merged = $previous + $row.get(1))
152 #else
153 #set ($merged = $row.get(1))
154 #end
155 #set ($discard = $createdByUser.put($short, $merged))
156 #end
157 #end
158 ## The author is stored with a wiki prefix on some revisions and without one on others, so the two
159 ## spellings of the same person have to be folded together before anything is ranked.
160 #set ($byUser = {})
161 #foreach ($row in $rows)
162 #set ($author = "$!row.get(0)")
163 #set ($short = $author.replaceAll('^xwiki:', ''))
164 #if ($short != 'XWiki.superadmin' && $short != 'XWiki.XWikiGuest' && $short != '')
165 #set ($previous = $byUser.get($short))
166 #if ($previous)
167 #set ($merged = $previous + $row.get(1))
168 #else
169 #set ($merged = $row.get(1))
170 #end
171 #set ($discard = $byUser.put($short, $merged))
172 #end
173 #end
174 ## The two maps answer different questions and neither contains the other, so the rows are the
175 ## union of their keys. Ranking over the revision map alone would lose anyone who created pages in
176 ## the month without saving one - which is what a Change Request publish does to the author, whose
177 ## name goes on the creator field while the publisher's goes on the revision.
178 #set ($contributors = [])
179 #foreach ($user in $byUser.keySet())
180 #set ($discard = $contributors.add($user))
181 #end
182 #foreach ($user in $createdByUser.keySet())
183 #if (!$contributors.contains($user))
184 #set ($discard = $contributors.add($user))
185 #end
186 #end
187 #set ($ranked = [])
188 #set ($totalCreated = 0)
189 #set ($totalEdited = 0)
190 #foreach ($user in $contributors)
191 ## Read through containsKey rather than letting a null fall out of get(): a #set whose value is
192 ## null is silently skipped, which would leave the previous contributor's number in place.
193 #set ($touched = 0)
194 #if ($byUser.containsKey($user))
195 #set ($touched = $byUser.get($user))
196 #end
197 #set ($created = 0)
198 #if ($createdByUser.containsKey($user))
199 #set ($created = $createdByUser.get($user))
200 #end
201 ## Created is never clamped down to the pages the person saved: on a page published through a
202 ## Change Request the creator is the author and the revision is the publisher's, so the author
203 ## legitimately has more creations than saves. Edited is floored at zero instead, which keeps the
204 ## Created column reconciling with the listing below.
205 #if ($created >= $touched)
206 #set ($edited = 0)
207 #else
208 #set ($edited = $touched - $created)
209 #end
210 #set ($discard = $ranked.add({'user': $user, 'created': $created, 'edited': $edited, 'pages': $mathtool.add($created, $edited)}))
211 #set ($totalCreated = $totalCreated + $created)
212 #set ($totalEdited = $totalEdited + $edited)
213 #end
214 #set ($ranked = $collectiontool.sort($ranked, 'pages:desc'))
215 (% class="table" %)
216 |=#|=Contributor|=Pages created|=Pages edited
217 #foreach ($entry in $ranked)
218 #set ($displayName = "$!xwiki.getUserName($entry.user, false)")
219 #if ($displayName == '')
220 #set ($displayName = $entry.user)
221 #end
222 #set ($displayName = $displayName.replace('|', '~|'))
223 |$foreach.count|[[$displayName>>doc:$entry.user]]|$entry.created|$entry.edited
224 #end
225 #if ($ranked.isEmpty())
226 |-|//nobody//|0|0
227 #end
228 |=**$ranked.size() contributors**| |=**$totalCreated**|=**$totalEdited**
229
230 == Pages created in $selected ==
231
232 ## The pages behind the Created column, one row each: same class, same locale, same hidden test and
233 ## same window as the query above, so the two can never disagree about what a documentation page is.
234 ## Ordered by creation date, which is no longer shown but is still what puts the month in sequence.
235 ## superadmin is kept here. It is left out of the ranking above because an import is not a
236 ## contributor, but a list of what was created in the month is wrong if it hides pages - and the
237 ## difference between this table's size and the Created total above is exactly what it created.
238 #set ($pagesHql = 'select doc.fullName, doc.title, doc.creator from XWikiDocument as doc, BaseObject as obj where obj.name = doc.fullName and obj.className = :class and doc.language = :locale and (doc.hidden = false or doc.hidden is null) and doc.creationDate >= :from and doc.creationDate < :to order by doc.creationDate')
239 #set ($pagesQuery = $services.query.createQuery($pagesHql, 'hql'))
240 #set ($discard = $pagesQuery.bindValue('class', 'DocApp.Code.DocumentationClass'))
241 #set ($discard = $pagesQuery.bindValue('locale', ''))
242 #set ($discard = $pagesQuery.bindValue('from', $from))
243 #set ($discard = $pagesQuery.bindValue('to', $to))
244 #set ($createdPages = $pagesQuery.execute())
245 #if ($createdPages.isEmpty())
246 //No documentation page was created in $selected.//
247 #else
248 ## Display names are resolved once per person rather than once per row: a month of 200 pages written
249 ## by five people would otherwise load the same five user documents 200 times.
250 #set ($creatorNames = {})
251 (% class="table" %)
252 |=Page|=Created by
253 #foreach ($row in $createdPages)
254 #set ($pageRef = "$!row.get(0)")
255 ## The link label is the stored title rather than an empty label, which would make the renderer load
256 ## every one of these documents again just to read it back. A pipe in a title would end the cell.
257 #set ($label = "$!row.get(1)")
258 #if ($label == '')
259 #set ($label = $pageRef)
260 #end
261 #set ($label = $label.replace('|', '~|'))
262 #set ($creator = "$!row.get(2)")
263 #set ($creator = $creator.replaceAll('^xwiki:', ''))
264 #if (!$creatorNames.containsKey($creator))
265 #set ($creatorName = "$!xwiki.getUserName($creator, false)")
266 #if ($creatorName == '')
267 #set ($creatorName = $creator)
268 #end
269 #set ($discard = $creatorNames.put($creator, $creatorName.replace('|', '~|')))
270 #end
271 |[[$label>>doc:$pageRef]]|[[$creatorNames.get($creator)>>doc:$creator]]
272 #end
273 |=**$createdPages.size() pages**|
274 #end
275 {{/velocity}}
276 {{/cache}}

Get Connected