Example #1
0
 /**
  * Clean and test 'category' parameter.
  *
  * @access	public
  * @param	string	Options passed to parameter.
  * @return	boolean	Success
  */
 public function _category($option)
 {
     $option = trim($option);
     if (empty($option)) {
         return false;
     }
     // Init array of categories to include
     $categories = [];
     $heading = false;
     $notHeading = false;
     if (substr($option, 0, 1) == '+') {
         // categories are headings
         $heading = true;
         $option = ltrim($option, '+');
     }
     if (substr($option, 0, 1) == '-') {
         // categories are NOT headings
         $notHeading = true;
         $option = ltrim($option, '-');
     }
     //We expand html entities because they contain an '& 'which would be interpreted as an AND condition
     $option = html_entity_decode($option, ENT_QUOTES);
     if (strpos($option, '|') !== false) {
         $parameters = explode('|', $option);
         $operator = 'OR';
     } else {
         $parameters = explode('&', $option);
         $operator = 'AND';
     }
     foreach ($parameters as $parameter) {
         $parameter = trim($parameter);
         if ($parameter === '_none_' || $parameter === '') {
             $this->setParameter('includeuncat', true);
             $categories[] = '';
         } elseif (!empty($parameter)) {
             if (strpos($parameter, '*') === 0 && strlen($parameter) >= 2) {
                 if (strpos($parameter, '*', 1) === 1) {
                     $parameter = substr($parameter, 2);
                     $subCategories = Query::getSubcategories($parameter, 2);
                 } else {
                     $parameter = substr($parameter, 1);
                     $subCategories = Query::getSubcategories($parameter, 1);
                 }
                 $subCategories[] = $parameter;
                 foreach ($subCategories as $subCategory) {
                     $title = \Title::newFromText($subCategory);
                     if (!is_null($title)) {
                         //The * helper is just like listing "Category1|SubCategory1".  This gets hard coded here for this purpose.
                         $categories['OR'][] = $title->getDbKey();
                     }
                 }
             } else {
                 $title = \Title::newFromText($parameter);
                 if (!is_null($title)) {
                     $categories[$operator][] = $title->getDbKey();
                 }
             }
         }
     }
     if (!empty($categories)) {
         $data = $this->getParameter('category');
         //Do a bunch of data integrity checks to avoid E_NOTICE.
         if (!is_array($data)) {
             $data = [];
         }
         if (!array_key_exists('=', $data) || !is_array($data['='])) {
             $data['='] = [];
         }
         foreach ($categories as $_operator => $_categories) {
             if (!array_key_exists($_operator, $data['=']) || !is_array($data['='][$_operator])) {
                 $data['='][$_operator] = [];
             }
             $data['='][$_operator][] = $_categories;
         }
         $this->setParameter('category', $data);
         if ($heading) {
             $this->setParameter('catheadings', array_unique(array_merge($this->getParameter('catheadings'), $categories)));
         }
         if ($notHeading) {
             $this->setParameter('catnotheadings', array_unique(array_merge($this->getParameter('catnotheadings'), $categories)));
         }
         $this->setOpenReferencesConflict(true);
         return true;
     }
     return false;
 }