Wiki source code of AntiSpam Scripting API
Last modified by Vincent Massol on 2026/10/14 18:35
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
![]() |
5.1 | 1 | Most methods of the ##$services.antispam## Script Service are annotated ##@Programming## and check for programming rights before doing anything, so a page calling them must have a content author who holds that right; [[AntiSpam for developers>>doc:documentation.extensions.dev.antispam.WebHome]] describes where the Service sits in the extension. |
![]() |
2.1 | 2 | |
| 3 | Searching and cleaning: | ||
| 4 | |||
| 5 | |=Method|=Returns|=Programming right | ||
| 6 | |##getMatchingDocuments(solrQuery, nb, offset)##|The matching references, each pairing a document with its last author. ##AntiSpam## pages are always excluded.|Yes | ||
| 7 | |##getLastAuthorReferences(matchingReferences)##|The set of last authors of the passed matching references, with the guest user removed.|Yes | ||
| 8 | |##getDocumentsForAuthor(authorReference, nb, offset)##|The documents created or last modified by that account.|Yes | ||
| 9 | |##cleanDocument(documentReference, authorReferences, skipActivityStream)##|Nothing. Deletes the revisions of the document made by those authors, and the document itself when no revision is left.|Yes | ||
| 10 | |||
| 11 | Accounts: | ||
| 12 | |||
| 13 | |=Method|=Returns|=Programming right | ||
| 14 | |##getInactiveAuthors(elapsedDays, cleanAuthorsWithAvatars, count)##|At most ##count## accounts matching every inactivity condition.|Yes | ||
| 15 | |##cleanAuthors(authorReferences, skipEventStreamRecording)##|The ##Job## deleting those accounts, so that a page can display its progress.|Yes | ||
| 16 | |##getCurrentCleanAuthorJobStatus()##|The status of the running or last deletion job, or null when none ran.|No | ||
| 17 | |##getKnownUserReferences()##|Every protected account, expanding the known groups into their members.|No | ||
| 18 | |##isProtectedUser(authorReference, documentReference)##|Whether that account is protected, for that page. Pass null as the document to ask about the wiki only.|No | ||
| 19 | |||
| 20 | Checking and events: | ||
| 21 | |||
| 22 | |=Method|=Returns|=Programming right | ||
| 23 | |##isSpam(checkerHint, content, parameters)##|The result of running the named checker over a string, with the matched keywords when it is spam.|No | ||
| 24 | |##isSpam(checkerHint, document, parameters)##|The same over a document, serialised to XML first.|No | ||
| 25 | |##createEventQuery()##|A new empty event query to build on.|No | ||
| 26 | |##searchEvents(query)##|The events matching the query.|Yes | ||
| 27 | |##deleteEvent(eventId)## and ##deleteEvent(event)##|A future on the deleted event.|Yes | ||
| 28 | |||
| 29 | The hint of the checker shipped with the extension is ##simple##. The ##parameters## map is what the checker reads besides the content: the ##simple## checker takes ##ip##, ##authorReference## and ##documentReference##, and logs the match to ##AntiSpam.Logs## when the last two are present. | ||
| 30 | |||
| 31 | Testing a page against the keyword list: | ||
| 32 | |||
| 33 | {{code language="velocity"}} | ||
| 34 | {{velocity}} | ||
| 35 | #set ($result = $services.antispam.isSpam('simple', $doc, {})) | ||
| 36 | #if ($result.spam) | ||
| 37 | Matched: $result.matchedContent.keySet() | ||
| 38 | #else | ||
| 39 | No keyword matched. | ||
| 40 | #end | ||
| 41 | {{/velocity}} | ||
| 42 | {{/code}} | ||
| 43 | |||
| 44 | Listing the page creations of one account, newest first: | ||
| 45 | |||
| 46 | {{code language="velocity"}} | ||
| 47 | {{velocity}} | ||
| 48 | #set ($query = $services.antispam.createEventQuery()) | ||
| 49 | #set ($query = $query.eq('application', 'xwiki').eq('type', 'create')) | ||
| 50 | #set ($query = $query.eq('user', 'xwiki:XWiki.SomeUser')) | ||
| 51 | #set ($query = $query.addSort('date', 'DESC').setLimit(50)) | ||
| 52 | #foreach ($event in $services.antispam.searchEvents($query)) | ||
| 53 | * $event.date - $event.document | ||
| 54 | #end | ||
| 55 | {{/velocity}} | ||
| 56 | {{/code}} | ||
| 57 | |||
| 58 | Deleting the accounts that never contributed, as a job, is what the "Clean Inactive Users" screen does: | ||
| 59 | |||
| 60 | {{code language="velocity"}} | ||
| 61 | {{velocity}} | ||
| 62 | #set ($inactive = $services.antispam.getInactiveAuthors(30, true, 50)) | ||
| 63 | #set ($job = $services.antispam.cleanAuthors($inactive, true)) | ||
| 64 | Started: $job.status.state | ||
| 65 | {{/velocity}} | ||
| 66 | {{/code}} | ||
| 67 | |||
| 68 | Passing true as the last argument of ##cleanAuthors## and ##cleanDocument## wraps the work in the extension's own fold events, which is what keeps a mass deletion out of the event stream and out of everyone's notifications. |
