/**
 * Aligns articles published under given publisher folder
 * @param eZContentObject $publisherObject
 */
function alignArticle( $publisherObject, $checkModified = false, $checkLocations = false, $checkLanguages = false, $checkHidden = false, $timingDelay = 0, $forceLast = 0, $sleepTime = 1 )
{
	echo "Starting treatment of publisher folder : " . $publisherObject->name() . "\n";
	echo "Fetching solr informations\n";
	$eZSolr        = eZSearch::getEngine();
	$offset        = 0;
    $forced        = 0;
	$limit         = 200;
	$publisherNode = $publisherObject->mainNode();
	$doCheck       = $checkModified || $checkLocations || $checkLanguages || $checkHidden;
	$solrInfos     = fetchSolrPublisherFolder ( $publisherNode, $checkModified, $checkLocations, $checkLanguages, $checkHidden, $sleepTime );
	
	echo "Solr count : " . count($solrInfos) . "\n";

	while ( true )
	{
		$params = array(
				'Offset'           => $offset,
				'Limit'            => $limit,
				'ClassFilterType'  => 'include',
				'ClassFilterArray' => array( 'article' ),
				'LoadDataMap'      => false,
				'AsObject'         => false,
				'MainNodeOnly'     => true,
		);
        
        if ($forceLast > 0)
            $params['SortBy'] = array ( array('published', false ) );

		$nodeList = $publisherNode->subtree( $params );
		echo "\neZ Offset : $offset\n";

		if ( count( $nodeList ) == 0 )
		{
			break;
		}
	
		foreach ( $nodeList as $mainNode )
		{
			$nodeId = $mainNode['node_id'];
			$objectId = $mainNode['contentobject_id'];
			$toUpdate = false;

            if ( $forceLast > 0 && $forced < $forceLast )
                $toUpdate = true;
			elseif ( isset($solrInfos[$nodeId]) )
			{
				if ( $doCheck )
				{
					if ( $checkLanguages )
					{
						$eZLanguages = eZContentObject::fetch($objectId)->languages();
						$toUpdate = compareLanguages( array_keys($eZLanguages), $solrInfos[$nodeId]['languages'] );
						
						showInvalidTranslations(array_keys($eZLanguages), $solrInfos[$nodeId]['languages'], $objectId);
					}

					if (!$toUpdate && $checkModified)
						$toUpdate = compareDates($mainNode['modified'], $solrInfos[$nodeId]['modified'], $timingDelay);

					if (!$toUpdate && $checkLocations)
						$toUpdate = compareLocations($objectId, $solrInfos[$nodeId]['locations']);

                    if (!$toUpdate && $checkHidden)
                        $toUpdate = compareHidden($objectId, $solrInfos[$nodeId]['hiddenCount'], $solrInfos[$nodeId]['notHiddenCount']);
				}

				unset($solrInfos[$nodeId]);
			}
			else
				$toUpdate = true;

			if ( $toUpdate )
			{
				$return = $eZSolr->addObject( eZContentObject::fetch($objectId), false );

				echo ( !$return ? '!' . $objectId . '!' : '+' );
			}
			else
				echo '-';
            
            $forced++;
		}
	
		$offset += $limit;

		eZContentObject::clearCache();
        
        if ( $sleepTime > 0 )
            sleep ($sleepTime);
	}
	
	echo "\nArticles in solr but unknown from eZPublish : " . count($solrInfos) . " articles\n";
}
 protected function getArrayRelatedObject(eZContentObject $relatedObject, $contentClassAttribute, $metaData = null)
 {
     if ($metaData === null) {
         $metaData = array();
     }
     if ($relatedObject instanceof eZContentObject && $relatedObject->attribute('main_node_id') > 0) {
         $objectName = $relatedObject->name(false, $this->ContentObjectAttribute->attribute('language_code'));
         $fieldName = parent::generateSubattributeFieldName($contentClassAttribute, 'name', self::DEFAULT_SUBATTRIBUTE_TYPE);
         if (isset($metaData[$fieldName])) {
             $metaData[$fieldName] = array_merge($metaData[$fieldName], array($objectName));
         } else {
             $metaData[$fieldName] = array($objectName);
         }
         /** @var ezfSolrDocumentFieldBase[] $baseList */
         $baseList = $this->getBaseList($relatedObject->attribute('current'));
         foreach ($baseList as $field) {
             /** @var eZContentClassAttribute $tmpClassAttribute */
             $tmpClassAttribute = $field->ContentObjectAttribute->attribute('contentclass_attribute');
             $fieldName = $field->ContentObjectAttribute->attribute('contentclass_attribute_identifier');
             $fieldNameArray = array();
             foreach (array_keys(eZSolr::$fieldTypeContexts) as $context) {
                 $fieldNameArray[] = parent::generateSubattributeFieldName($contentClassAttribute, $fieldName, ezfSolrDocumentFieldBase::getClassAttributeType($tmpClassAttribute, null, $context));
             }
             $fieldNameArray = array_unique($fieldNameArray);
             if ($tmpClassAttribute->attribute('data_type_string') == 'ezobjectrelation' or $tmpClassAttribute->attribute('data_type_string') == 'ezobjectrelationlist') {
                 /** @var self $field */
                 $finalValue = $field->getPlainTextRepresentation();
             } else {
                 $finalValue = $this->preProcessValue($field->ContentObjectAttribute->metaData(), parent::getClassAttributeType($tmpClassAttribute));
             }
             foreach ($fieldNameArray as $fieldNameValue) {
                 //eZCLI::instance()->output(var_dump($metaData));
                 if (is_array($finalValue)) {
                     $finalValue = self::recursive_implode($finalValue);
                 }
                 $finalValue = trim($finalValue, "\t\r\n ");
                 if (!empty($finalValue)) {
                     if (isset($metaData[$fieldNameValue])) {
                         $metaData[$fieldNameValue] = array_merge($metaData[$fieldNameValue], array($finalValue));
                     } else {
                         $metaData[$fieldNameValue] = array($finalValue);
                     }
                 }
             }
         }
         $metaAttributeValues = eZSolr::getMetaAttributesForObject($relatedObject);
         foreach ($metaAttributeValues as $metaInfo) {
             $value = ezfSolrDocumentFieldBase::preProcessValue($metaInfo['value'], $metaInfo['fieldType']);
             if (!is_array($value)) {
                 $value = array($value);
             }
             $metaData[ezfSolrDocumentFieldBase::generateSubmetaFieldName($metaInfo['name'], $contentClassAttribute)] = $value;
         }
     }
     return $metaData;
 }
 /**
  * Create NameList element.
  *
  * @param DOMDocument Owner DOMDocument
  * @param eZContentObject eZContentObject object.
  *
  * @return DOMElement NameList DOMDocument, example:
  *
  *     <NameList>
  *         <Name locale="eng-GB">eZ Publish rocks</Name>
  *     </NameList>
  */
 protected function createNameListDOMElementFromContentObject(DOMDocument $domDocument, eZContentObject $object)
 {
     $languageListElement = $domDocument->createElement('NameList');
     // Add language names.
     foreach ($object->attribute('current')->translationList(false, false) as $locale) {
         $languageElement = $domDocument->createElement('Name');
         $languageElement->setAttribute('locale', $locale);
         $languageElement->appendChild($domDocument->createTextNode($object->name(false, $locale)));
         $languageListElement->appendChild($languageElement);
     }
     return $languageListElement;
 }
/**
 * Aligns location of the articles published under given publisher folder
 * @param eZContentObject $publisherObject
 */
function alignArticleLocation( $publisherObject, $offset = 0 )
{
    $limit = 200;
    $continue = true;
    $publisherNode = $publisherObject->mainNode();
    $globalVarName = 'contentServiceMainNodeID';

    if ( !isset( $GLOBALS[$globalVarName] ) )
    {
        $GLOBALS[$globalVarName] = array();
    }

    echo "Starting treatment of publisher folder : " . $publisherObject->name() . "\n";

    while ( true )
    {
        $params = array(
            'Offset' => $offset,
            'Limit' => $limit,
            'ClassFilterType' => 'include',
            'ClassFilterArray' => array(
                'article'
            ),
            'LoadDataMap' => false,
            'AsObject' => false,
            'MainNodeOnly' => true,
        );

        $nodeList = $publisherNode->subtree( $params );
        echo " // Offset : $offset // ";

        if ( count( $nodeList ) == 0 )
        {
            break;
        }

        foreach ( $nodeList as $mainNode )
        {
            $object = eZContentObject::fetch( $mainNode["contentobject_id"] );

            deploymentType::addListNodesIdToAddLocation($object);
        }

        $offset += $limit;

        eZContentObject::clearCache();
    }
}