Don't look for pipes in the root node.
[lhc/web/wiklou.git] / trackback.php
index ae1e32b..f673c50 100644 (file)
@@ -1,66 +1,85 @@
 <?php
 /**
  * Provide functions to handle article trackbacks.
- * @package MediaWiki
- * @subpackage SpecialPage
+ * @file
+ * @ingroup SpecialPage
  */
+
 require_once( './includes/WebStart.php' );
 
-require_once('DatabaseFunctions.php');
+class TrackBack {
 
-/**
- *
- */
-function XMLsuccess() {
-       echo "
-<?xml version=\"1.0\" encoding=\"utf-8\"?>
+       private $r, $url, $title = null;
+
+       private function XMLsuccess() {
+               header( "Content-Type: application/xml; charset=utf-8" );
+               echo <<<XML
+<?xml version="1.0" encoding="utf-8"?>
 <response>
-<error>0</error>
+       <error>0</error>
 </response>
-       ";
-       exit;
-}
+XML;
+               exit;
+       }
 
-function XMLerror($err = "Invalid request.") {
-       header("HTTP/1.0 400 Bad Request");
-       echo "
-<?xml version=\"1.0\" encoding=\"utf-8\"?>
+       private function XMLerror( $err = "Invalid request." ) {
+               header( "HTTP/1.0 400 Bad Request" );
+               header( "Content-Type: application/xml; charset=utf-8" );
+               echo <<<XML
+<?xml version="1.0" encoding="utf-8"?>
 <response>
-<error>1</error>
-<message>Invalid request: $err</message>
+       <error>1</error>
+       <message>Invalid request: $err</message>
 </response>
-";
-               exit;
+XML;
+                       exit;
+       }
+
+       public function __construct() {
+               global $wgUseTrackbacks, $wgRequest;
+
+               if( !$wgUseTrackbacks )
+                       $this->XMLerror( "Trackbacks are disabled" );
+
+               $this->r = $wgRequest;
+
+               if( !$this->r->wasPosted() ) {
+                       $this->XMLerror( "Must be posted" );
+               }
+
+               $this->url = $wgRequest->getText( 'url' );
+               $article = $wgRequest->getText( 'article' );
+
+               if( !$this->url || !$article ) {
+                       $this->XMLerror( "Required field not specified" );
+               }
+
+               $this->title = Title::newFromText( $article );
+               if( !$this->title || !$this->title->exists() ) {
+                       $this->XMLerror( "Specified article does not exist." );
+               }
+       }
+
+       public function write() {
+               $dbw = wfGetDB( DB_MASTER );
+
+               $tbtitle = $this->r->getText( 'title' );
+               $tbex = $this->r->getText( 'excerpt' );
+               $tbname = $this->r->getText( 'blog_name' );
+
+               $dbw->insert('trackbacks', array(
+                       'tb_page'       => $this->title->getArticleID(),
+                       'tb_title'      => $tbtitle,
+                       'tb_url'        => $this->url,
+                       'tb_ex'         => $tbex,
+                       'tb_name'       => $tbname
+               ));
+
+               $dbw->commit();
+
+               $this->XMLsuccess();
+       }
 }
 
-if (!$wgUseTrackbacks)
-       XMLerror("Trackbacks are disabled.");
-
-if (   !isset($_POST['url'])
-    || !isset($_POST['blog_name'])
-    || !isset($_REQUEST['article']))
-       XMLerror("Required field not specified");
-
-$dbw =& wfGetDB(DB_MASTER);
-
-$tbtitle = $_POST['title'];
-$tbex = $_POST['excerpt'];
-$tburl = $_POST['url'];
-$tbname = $_POST['blog_name'];
-$tbarticle = $_REQUEST['article'];
-
-$title = Title::newFromText($tbarticle);
-if (!$title->exists())
-       XMLerror("Specified article does not exist.");
-
-$dbw->insert('trackbacks', array(
-       'tb_page'       => $title->getArticleID(),
-       'tb_title'      => $tbtitle,
-       'tb_url'        => $tburl,
-       'tb_ex'         => $tbex,
-       'tb_name'       => $tbname
-));
-
-XMLsuccess();
-exit;
-?>
+$tb = new TrackBack();
+$tb->write();