Jump to content

A simple page-edit log module


dragan
 Share

Recommended Posts

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

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

@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 :)

  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...