dragan Posted December 8, 2013 Share Posted December 8, 2013 I'd like to write a simple module: Creating a log that lists page-edits, i.e. which user has edited which page and when. Write each new entry to a new line in a text file in site/assets/logs/edits.txt. Simple enough. Following the Helloworld.module example, I came up with this: public function init() { // add a hook after the $pages->save, to issue a notice every time a page is saved $this->pages->addHookAfter('save', $this, 'logEdits'); } public function logEdits($event) { $page = $event->arguments[0]; $logFile = '/home/foobar/public_html/site/assets/logs/edits.txt'; $fh = fopen($logFile, 'a'); $now = date("Y.m.d H:i:s"); $stringData = "{$page->path}\t{$page->url}\t{$page->title}\t$now\n"; fwrite($fh, $stringData); fclose($fh); } It works - somewhat. I'm confused though why I get 15 lines on each page save(). I get 10 empty lines, then /de/medienecho/ /de/medienecho/ Medienecho 2013.12.08 15:17:23 then another four empty lines. Should I be using something else than $this->pages->addHookAfter('save') ? Does anybody know why I get 14 empty lines instead of just the right one, once? o_O Also, if I wanted to a) list the username and b) list each edited field how would I go about that? I saw on the roadmap for 2.4 that a new $log API will be available, but I'm curious if I could write something now already without it. The var $user->name is always empty. How do I access that from inside a module function? Sorry, but I'm totally new to writing modules and using hooks... Link to comment Share on other sites More sharing options...
dragan Posted December 8, 2013 Author Share Posted December 8, 2013 OK, so I found the dev $log API already here: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/WireLog.php And I found out to use $user = $this->wire('user')->name; The 14 useless empty lines were caused by stupidly naming the function the same as the module itself, d'oh. The only remaining question now is to list fine-grained infos about the edited fields (mainly text, but file- and edits in image-fields would be useful too - at least new uploads and deletes). I don't need "old content before save" and "new content after save" - just knowing which fields were edited would be awesome. Link to comment Share on other sites More sharing options...
teppo Posted December 8, 2013 Share Posted December 8, 2013 @dragan: take a look at ProcessChangelogHooks.module and especially it's logPageEvent method. It does a bit more than this, but you could probably use parts of it pretty much directly. 2 Link to comment Share on other sites More sharing options...
dragan Posted December 9, 2013 Author Share Posted December 9, 2013 Oh, that's absolutely lovely. I searched the PW forum and Google, but that module didn't show up in results. Thanks! Link to comment Share on other sites More sharing options...
3fingers Posted December 9, 2013 Share Posted December 9, 2013 Newbie question here @teppo: could this module be used to output on fronted some informations like: " user foo has uploaded 1 image 2 minutes ago " or "user bar has updated his profile description 1 minute ago" etc etc... ? Link to comment Share on other sites More sharing options...
teppo Posted December 11, 2013 Share Posted December 11, 2013 @3fingers: sorry, I had managed to miss your question. This is exactly why I added JSON view to the latest changelog version, i.e. you could use PHP or JavaScript to request same data as JSON and parse that to whatever you need. If the module is installed to default location, you can get this data from /processwire/setup/changelog/json/. Various search parameters (GET params) also work there. Ask if you need more detailed description, I'm in a bit of a hurry right now 2 Link to comment Share on other sites More sharing options...
MadeMyDay Posted December 11, 2013 Share Posted December 11, 2013 Brilliant! Thanks! Exactly what I was looking for. So it should be easily possible to generate reports on a daily/weekly/etc. basis with a cronjob. Will try that. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now