coding style tweaks + removed some PHP4-isms
[lhc/web/wiklou.git] / trackback.php
1 <?php
2 /**
3 * Provide functions to handle article trackbacks.
4 * @file
5 * @ingroup SpecialPage
6 */
7
8 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
9 require ( 'phase3/includes/WebStart.php' );
10 } else {
11 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
12 }
13
14 class TrackBack {
15
16 private $r, $url, $title = null;
17
18 private function XMLsuccess() {
19 header( "Content-Type: application/xml; charset=utf-8" );
20 echo <<<XML
21 <?xml version="1.0" encoding="utf-8"?>
22 <response>
23 <error>0</error>
24 </response>
25 XML;
26 exit;
27 }
28
29 private function XMLerror( $err = "Invalid request." ) {
30 header( "HTTP/1.0 400 Bad Request" );
31 header( "Content-Type: application/xml; charset=utf-8" );
32 echo <<<XML
33 <?xml version="1.0" encoding="utf-8"?>
34 <response>
35 <error>1</error>
36 <message>Invalid request: $err</message>
37 </response>
38 XML;
39 exit;
40 }
41
42 public function __construct() {
43 global $wgUseTrackbacks, $wgRequest;
44
45 if( !$wgUseTrackbacks )
46 $this->XMLerror( "Trackbacks are disabled" );
47
48 $this->r = $wgRequest;
49
50 if( !$this->r->wasPosted() ) {
51 $this->XMLerror( "Must be posted" );
52 }
53
54 $this->url = $wgRequest->getText( 'url' );
55 $article = $wgRequest->getText( 'article' );
56
57 if( !$this->url || !$article ) {
58 $this->XMLerror( "Required field not specified" );
59 }
60
61 $this->title = Title::newFromText( $article );
62 if( !$this->title || !$this->title->exists() ) {
63 $this->XMLerror( "Specified article does not exist." );
64 }
65 }
66
67 public function write() {
68 $dbw = wfGetDB( DB_MASTER );
69
70 $tbtitle = $this->r->getText( 'title' );
71 $tbex = $this->r->getText( 'excerpt' );
72 $tbname = $this->r->getText( 'blog_name' );
73
74 $dbw->insert('trackbacks', array(
75 'tb_page' => $this->title->getArticleID(),
76 'tb_title' => $tbtitle,
77 'tb_url' => $this->url,
78 'tb_ex' => $tbex,
79 'tb_name' => $tbname
80 ));
81
82 $dbw->commit();
83
84 $this->XMLsuccess();
85 }
86 }
87
88 $tb = new TrackBack();
89 $tb->write();