2013-01-04 4 views
0

WordPress 사이트에서 누군가가 게시물을 볼 때마다 게시물 ID로 업데이트되는 XML 파일이 사용되었습니다.XML 파일이 무작위로 손상되어 나타납니다.

게시물 ID가 파일에없는 경우 때마다 누군가가 새로운 노드를 추가하거나 존재하는 경우는 (1)에 의해 현재의 값을 업데이트하는 게시물을 볼

<?xml version="1.0"?> 
    <posts> 
    <refresh> 
     <datetime>2013-01-04 08:48:21</datetime> 
    </refresh> 
    <post id="21931"> 
     <postviews>5</postviews> 
    </post> 
    <post id="25420"> 
     <postviews>18</postviews> 
    </post> 
    <post id="17770"> 
     <postviews>25</postviews> 
    </post> 
    <post id="24925"> 
     <postviews>4</postviews> 
    </post> 
    </posts> 

구조는 다음과 같습니다 파일 ... .

내가 가진 문제, 때로는 파일이 제대로 기록되지 않는, 그것은 손상 되 고 (파일의 하단에 항상 오류가) 다음과 같이보고 끝나는 것입니다 ...

<?xml version="1.0"?> 
    <posts> 
    <refresh> 
     <datetime>2013-01-04 08:48:21</datetime> 
    </refresh> 
    <post id="21931"> 
     <postviews>5</postviews> 
    </post> 
    <post id="25420"> 
     <postviews>18</postviews> 
    </post> 
    <post id="17770"> 
     <postviews>25</postviews> 
    </post> 
    </posts>   
    id="24925"> 
     <postviews>4</postviews> 
    </post> 
    </posts> 

나는 호스팅 회사와 통화했는데 문제가 서버 오류로 표시되지 않습니다. 내 스크립트에서 XML 파일에 쓰는 것이 문제가 될 수 있습니까? 내가 작성한 스크립트는 포스트 ID 파일에없는 경우

<?php 

// echo get_the_ID(); 

if($post->post_status != "publish" || is_user_logged_in()) { 

    //Get the wordpress postID 
$postID = get_the_ID(); 

$postData = get_post($postID); 

// Don't do anything if the post isn't published or being viewed by logged in users - will give a false impression 
// echo $postID.'<br />'.$postData->post_title.'<br />'.$postData->post_date_gmt.'<br />'; 

} else { 

//Get the wordpress postID 
$postID = get_the_ID(); 

$postData = get_post($postID); 

// echo $postID.'<br />'.$postData->post_title.'<br />'.$postData->post_date_gmt.'<br />'; 

$xmlFile = get_stylesheet_directory().'/most-read/most-read-posts.xml'; 

// load the document 
$xml = simplexml_load_file($xmlFile); 


// Get the server time 
$serverTime = date('Y-m-d H:i:s'); 

// Is their a refresh node with an entry of datetime? 
$refreshNodeExists = $xml->xpath("//refresh"); 

// Count it if 
$countrefreshNodeExists = count($refreshNodeExists); 

// If it does exists 
if($countrefreshNodeExists != 0) { // If the ID is already in the file 

    $xmlRefresh = $xml->refresh->datetime; 

    // echo 'Refresh time is already set<br /><br />'; 

} else { 

    // echo 'Refresh time is not set<br /><br />'; 

    $refreshNode = $xml->addChild('refresh'); 

    $refreshNode->addChild('datetime', $serverTime); 

} 

// Check the time elapsed between the server time and the refreshNode 

// Get and format the refreshNode 
$to_time = strtotime($xmlRefresh); 

// Get and format the server time 
$from_time = strtotime($serverTime); 

// Calculate the time elapsed 
$refreshTimeElapsed = round(abs($to_time - $from_time)/60,2); 

// How long do we want to have between file refreshing - in minutes? 
$refreshInterval = 120; 

// Check if the time elapsed is more or less than $refreshInterval 
if ($refreshTimeElapsed > $refreshInterval) { 

    // FOR TESTING ONLY - Show the time elapsed 
    // echo 'Ooops were over the refresh limit. It\'s currently at: '.$refreshTimeElapsed.' minutes.<br /><br />'; 

    $fh = fopen($xmlFile, 'w') or die("can't open file"); 

    $stringData = '<?xml version="1.0"?><posts><refresh><datetime>'.date('Y-m-d H:i:s').'</datetime></refresh></posts>'; 

    fwrite($fh, $stringData); 

    fclose($fh); 

       /*$test = $xml->xpath("//post"); 

       if (!empty($test)) { 

        echo 'Houston, we have posts'; 

       } else { 

        echo 'That\'s a negative Houston, we have no posts here'; 

       }*/ 

    // Reset the datetime node with the current datetime to start the refresh loop again. 
    $xml->refresh->datetime = date('Y-m-d H:i:s'); 

} else { // Don't need to do anything here really 

    // FOR TESTING ONLY - show how much time has elapsed 
    // echo 'No need to change the refresh node. It\'s only at '.$refreshTimeElapsed.' minutes.<br /><br />'; 

    // Check to see if the post id is already in the xml file - has it already been set? 
    $nodeExists = $xml->xpath("//post[@id=".$postID."]"); 

    //Count the results 
    $countNodeExists = count($nodeExists); 

    if($countNodeExists > 0) { // If the ID is already in the file 

     // echo 'ID already here'; 

     // get the correct node 
     $result = $xml->xpath("//post[@id=".$postID."]/postviews"); 

     // heres the trick - the first result of xpath, and the node value (stored in [0]) 
     $result[0][0] = $result[0][0]+1; 

    } else { // If the ID isn;'t there, add a new entry in the xml file for the post 

     //echo 'ID added'; 

     $postNode = $xml->addChild('post'); // adding a new <post> to the top level node 
     $postNode->addAttribute('id', $postID); // adding a <postid> inside the new <post> 
     $postNode->addChild('postviews', 1); // adding a postviews inside the new <post> 
    } 

    // save the updated document and format it using the DOMDocument class 

    //$xml->asXML($xmlFile); 
    $dom = new DOMDocument('1.0'); 
    $dom->preserveWhiteSpace = false; 
    $dom->formatOutput = true; 
    $dom->loadXML($xml->asXML()); 
    $dom->save($xmlFile); 

} // End if ($refreshTimeElapsed > $refreshInterval) { 

} // End if logged in or not published 

?> 
+0

동시성 때문에 이것이 아닙니까? 두 명 이상의 방문자가 게시물에 액세스하여 파일에 모두 씁니까? 두 개 (또는 그 이상)의 파일 중 절반으로 끝납니다. –

답변

0

때마다 누군가는 가 새 노드를 추가하거나 존재하는 경우는 (1)에 의해 현재의 값을 업데이트 게시물을보고 ... 다음과 같습니다

나는 복수의 동시 실행으로 인한 것일 것입니다. 해당 파일에 대한 액세스를 잠그고 있습니까? 두 사람이 동시에 게시물을 보는 경우 어떻게됩니까?

나는이 정보를 데이터베이스에 저장하는 것이 더 나을 것이라고 생각한다. 필자는 공통 XML 파일에 쓰는 것이 규모가 클 것이라고 생각하지 않습니다.

+0

안녕하세요 Brian, 파일을 잠그면 문제가 단기적으로 해결됩니다. 당신이 옳다고 생각합니다. 데이터베이스 저장소가 더 좋은 방법 일 것입니다. 그러나 XML 파일은 오류를 제외하고는 괜찮습니다 .-) –

관련 문제